Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/stop-if-merged.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Stop if Merged

on:
pull_request:
branches: [main]
paths:
- 'src/helpers/stop-if-merged.ts'

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./
with:
helper: stop-if-merged
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ with:

Each of the following helpers are defined in a file of the same name in `src/helpers`:

### [stop-if-merged](.github/workflows/stop-if-merged.yml)

- Action will stop the workflow if the PR has been merged.

| Parameter | Description | Default value | Required |
| :-----------------: | :---------------------------------------------------------------------------: | :-----------: | :------: |
| pull_request_number | Number of the PR, visible in PR url and by `github.event.pull_request.number` | N/A | ✔️ |

### [get-email-on-user-profile](.github/workflows/get-email-on-user-profile.yml)

- Retrieves a user's github email with optional regex verification
Expand Down
46 changes: 23 additions & 23 deletions dist/573.index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/573.index.js.map

Large diffs are not rendered by default.

115 changes: 115 additions & 0 deletions dist/846.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/846.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43167,6 +43167,16 @@ var map = {
461,
83
],
"./stop-if-merged": [
9846,
461,
846
],
"./stop-if-merged.ts": [
9846,
461,
846
],
"./update-check-result": [
1843,
461,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions src/helpers/stop-if-merged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { HelperInputs } from '../types/generated';
import { context } from '@actions/github';
import * as core from '@actions/core';
import { octokit } from '../octokit';

export class StopIfMerged extends HelperInputs {
pull_request_number = '';
}

export const stopIfMerged = async ({ pull_request_number }: StopIfMerged) => {
const { data } = await octokit.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: parseInt(pull_request_number)
});

if (data.merged) {
throw new Error(`Pull request #${pull_request_number} is already merged.`);
} else {
core.info(`Pull request #${pull_request_number} is not merged. Workflow will continue...`);
}
};
44 changes: 44 additions & 0 deletions test/helpers/stop-if-merged.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Mocktokit } from '../types';
import { stopIfMerged } from '../../src/helpers/stop-if-merged';
import { octokit } from '../../src/octokit';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn(() => ({
rest: {
pulls: { get: jest.fn() }
}
}))
}));

const setPrStatus = (merged: boolean) => {
(octokit.pulls.get as unknown as Mocktokit).mockResolvedValueOnce({
data: { merged }
});
};

describe('stopIfMerged', () => {
it('should throw an error if it is merged', async () => {
setPrStatus(true);
await expect(stopIfMerged({ pull_request_number: '123' })).rejects.toThrow('Pull request #123 is already merged.');
});

it('should not throw an error if it is not merged', async () => {
setPrStatus(false);
await expect(stopIfMerged({ pull_request_number: '123' })).resolves.not.toThrow();
});
});
Loading