Skip to content

Commit 91b3782

Browse files
1 parent 7134e71 commit 91b3782

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ async function run() {
4242
});
4343
console.log(`Removed the "safe-to-test" label from pull request.`);
4444
} catch (error) {
45+
// When multiple actions are executed in a workflow, the action may try
46+
// to remove a label that was already removed by another action (race
47+
// condition).
48+
if (error.message === 'Label does not exist') {
49+
console.log('Label was removed during the execution of the action, skipping.');
50+
return;
51+
}
52+
4553
core.setFailed(error.message);
4654
}
4755
}

index.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,62 @@ describe('remove-safe-to-test-label', () => {
150150
expect(core.setFailed).toHaveBeenCalledWith(errorMessage);
151151
}
152152
});
153+
154+
test('should handle the error when the label does not exist', async () => {
155+
// Mock console.log
156+
const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
157+
158+
const payload = {
159+
pull_request: {
160+
head: {
161+
repo: {
162+
full_name: 'fork-owner/repo',
163+
},
164+
},
165+
base: {
166+
repo: {
167+
full_name: 'base-owner/repo',
168+
},
169+
},
170+
labels: [
171+
{
172+
name: 'safe-to-test',
173+
},
174+
],
175+
number: 1,
176+
},
177+
repository: {
178+
full_name: 'base-owner/repo',
179+
},
180+
};
181+
182+
github.context.eventName = 'pull_request';
183+
github.context.payload = payload;
184+
github.context.repo = { owner: 'base-owner', repo: 'repo' };
185+
186+
core.getInput.mockReturnValueOnce('safe-to-test');
187+
188+
const mockRemoveLabel = jest.fn().mockRejectedValue(new Error('Label does not exist'));
189+
github.getOctokit.mockReturnValue({
190+
rest: {
191+
issues: {
192+
removeLabel: mockRemoveLabel,
193+
},
194+
},
195+
});
196+
197+
await run();
198+
199+
expect(mockRemoveLabel).toHaveBeenCalledWith({
200+
owner: 'base-owner',
201+
repo: 'repo',
202+
issue_number: 1,
203+
name: 'safe-to-test',
204+
});
205+
expect(consoleSpy).toHaveBeenCalledWith('Label was removed during the execution of the action, skipping.');
206+
expect(core.setFailed).toHaveBeenCalledTimes(0); // Ensure setFailed was not called, indicating the action handled the error gracefully.
207+
208+
// Restore console.log to its original implementation
209+
consoleSpy.mockRestore();
210+
});
153211
});

0 commit comments

Comments
 (0)