Skip to content

Commit

Permalink
Exit with exit code 0 when event is not pull_request or `pull_reque…
Browse files Browse the repository at this point in the history
…st_target` (#9)
  • Loading branch information
nilsreichardt authored Apr 6, 2023
1 parent cd6d4f2 commit 228977e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ GitHub Action.
## Usage

```yaml
on:
# This action only works with pull_request and pull_request_target events.
#
# For other events, it succeeds with exit code 0.
pull_request: # or pull_request_target

jobs:
# It's important that you run this job first, because you need to remove the
# "safe to test" label when the PR comes from a fork in order to ensure that
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ async function run() {
try {
const context = github.context;

const allowedEvents = ['pull_request', 'pull_request_target', 'merge_group'];
if (allowedEvents.indexOf(context.eventName) === -1) {
core.setFailed(`This action only works with the following events: ${allowedEvents.join(', ')}.`);
const allowedEvents = ['pull_request', 'pull_request_target'];
const isNotAllowedEvent = allowedEvents.indexOf(context.eventName) === -1;
if (isNotAllowedEvent) {
console.log(`Event "${context.eventName}", skipping. This action only works with the following events: ${allowedEvents.join(', ')}.`);
return;
}

Expand Down
7 changes: 3 additions & 4 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('remove-safe-to-test-label', () => {
expect(github.getOctokit().rest.issues.removeLabel).toHaveBeenCalledTimes(0);
});

test('should fail when eventName is not allowed', async () => {
test('should skip when eventName is not allowed', async () => {
const payload = {
pull_request: {
head: {
Expand All @@ -133,9 +133,8 @@ describe('remove-safe-to-test-label', () => {
core.getInput.mockReturnValue('safe-to-test');
await run();

expect(core.setFailed).toHaveBeenCalledWith(
'This action only works with the following events: pull_request, pull_request_target, merge_group.'
);
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(github.getOctokit).toHaveBeenCalledTimes(0);
});

test('should fail when there is an error', async () => {
Expand Down

0 comments on commit 228977e

Please sign in to comment.