Skip to content

Commit

Permalink
refactor: approve pr helper (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Dec 31, 2021
1 parent 547390b commit d0eab83
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ name: Auto Approve PR
on:
pull_request:
branches: [ main ]
paths:
- 'src/helpers/auto-approve-pr.ts'
types: [ opened ]

jobs:
test:
if: github.event.pull_request.user.login == 'dependabot'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- uses: ./
with:
helper: auto-approve-pr
auto_approved_user: 'renovate[bot]'
login: ${{ github.event.pull_request.user.login }}
helper: approve-pr
github_token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }} # must have write:repo permission
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ Each of the following helpers are defined in a file of the same name in `src/hel
* Adds one or more labels to a PR
### [**add-pr-approval-label**](.github/workflows/add-pr-approval-label.yml)
* Upon PR review, adds a `CORE APPROVED` label if the reviewer is a part of the provided Github team, otherwise adds the `PEER APPROVED` label
### [**approve-pr**](.github/workflows/auto-approve-pr.yml)
* Approves a PR
### [**assign-pr-reviewers**](.github/workflows/assign-pr-reviewers.yml)
* Randomly assigns members of a github team to review a PR. If `login` is provided, it does nothing if that user is already part of the team
* You can also pass a `slack_webhook_url` to notify the assignees that they are assigned to the PR!
### [**create-project-card**](.github/workflows/create-project-card.yml)
* Creates a Project card into your GitHub Project repository by providing a `project_name` and `project_destination_column_name` in which the card should be created.
* If `note` is provided, it will add that information into the card. If it is not provided, it will use the PR information details to populate it.
* Useful when opening a pull request and want to track its information details into a GitHub Project.
### [**move-project-card**](.github/workflows/move-project-card.yml)
* Moves a GitHub Project card to a new column, using the `project_origin_column_name` and`project_destination_column_name` you provide.
* In order to move a card from one place to another, it must already exist.
### [**auto-approve-pr**](.github/workflows/auto-approve-pr.yml)
* Automatically approves a PR if the reviewer's login matches the provided login
### [**check-pr-title**](.github/workflows/check-pr-title.yml)
* Checks whether PR title matches a certain regular expression
### [**create-pr-comment**](.github/workflows/create-pr-comment.yml)
Expand Down Expand Up @@ -54,6 +51,9 @@ Additionally, the following parameters can be used for additional control over t
* Creates a new in-progress Github "deployment" for a commit. More information on Github deployment events can be found [here](https://docs.github.com/en/rest/reference/repos#deployments)
### [**manage-merge-queue**](.github/workflows/manage-merge-queue.yml)
* Manages a queue for PRs. Can be used to build a fully automated merge queue when used in conjunction with `prepare-queued-pr-for-merge`.
### [**move-project-card**](.github/workflows/move-project-card.yml)
* Moves a GitHub Project card to a new column, using the `project_origin_column_name` and`project_destination_column_name` you provide.
* In order to move a card from one place to another, it must already exist.
### [**notify-pipeline-complete**](.github/workflows/notify-pipeline-complete.yml)
* Sets a "pipeline" commit status to green for all open PRs
### [**prepare-queued-pr-for-merge**](.github/workflows/prepare-queued-pr-for-merge.yml)
Expand Down
3 changes: 0 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ inputs:
paths_no_filter:
description: 'A list of file paths to be included regardless of the filter (newline/comma separated)'
required: false
auto_approved_user:
description: 'User login whose request should be auto approved'
required: false
slack_webhook_url:
description: 'A Slack webhook URL'
required: false
Expand Down
67 changes: 67 additions & 0 deletions dist/522.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/522.index.js.map

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

3 changes: 0 additions & 3 deletions dist/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ inputs:
paths_no_filter:
description: 'A list of file paths to be included regardless of the filter (newline/comma separated)'
required: false
auto_approved_user:
description: 'User login whose request should be auto approved'
required: false
slack_webhook_url:
description: 'A Slack webhook URL'
required: false
Expand Down
20 changes: 10 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18800,6 +18800,16 @@ var map = {
445,
905
],
"./approve-pr": [
6522,
438,
522
],
"./approve-pr.ts": [
6522,
438,
522
],
"./assign-pr-reviewers": [
3154,
438,
Expand All @@ -18816,16 +18826,6 @@ var map = {
545,
154
],
"./auto-approve-pr": [
185,
438,
185
],
"./auto-approve-pr.ts": [
185,
438,
185
],
"./check-pr-title": [
2956,
438,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 7 additions & 15 deletions src/helpers/auto-approve-pr.ts → src/helpers/approve-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@ limitations under the License.
import { context } from '@actions/github';
import { octokit } from '../octokit';

interface AutoApprovePr {
login: string;
auto_approved_user: string;
}

export const autoApprovePr = ({ login, auto_approved_user }: AutoApprovePr) => {
if (login === auto_approved_user) {
octokit.pulls.createReview({
...context.repo,
pull_number: context.issue.number,
body: 'Approved by bot',
event: 'APPROVE'
});
}
};
export const approvePr = () =>
octokit.pulls.createReview({
pull_number: context.issue.number,
body: 'Approved by bot',
event: 'APPROVE',
...context.repo
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { autoApprovePr } from '../../src/helpers/auto-approve-pr';
import { approvePr } from '../../src/helpers/approve-pr';
import { context } from '@actions/github';
import { octokit } from '../../src/octokit';

Expand All @@ -21,40 +21,17 @@ jest.mock('@actions/github', () => ({
getOctokit: jest.fn(() => ({ rest: { pulls: { createReview: jest.fn() } } }))
}));

describe('autoApprovePr', () => {
const auto_approved_user = 'renovate';

afterEach(() => {
jest.clearAllMocks();
});

describe('non renovate user', () => {
const login = 'user';

beforeEach(() => {
autoApprovePr({ login, auto_approved_user });
});

it('should not approve', () => {
expect(octokit.pulls.createReview).toBeCalledTimes(0);
});
describe('approvePr', () => {
beforeEach(() => {
approvePr();
});

describe('renovate pr', () => {
const login = 'renovate';
const pull_number = 123;

beforeEach(() => {
autoApprovePr({ login, auto_approved_user });
});

it('should approve', () => {
expect(octokit.pulls.createReview).toHaveBeenCalledWith({
pull_number,
body: 'Approved by bot',
event: 'APPROVE',
...context.repo
});
it('should call createReview with correct params', () => {
expect(octokit.pulls.createReview).toHaveBeenCalledWith({
pull_number: 123,
body: 'Approved by bot',
event: 'APPROVE',
...context.repo
});
});
});

0 comments on commit d0eab83

Please sign in to comment.