Skip to content

Commit

Permalink
feat: add create-pr helper (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
wbautistajr authored Jan 6, 2022
1 parent 466f972 commit 44e5b47
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .github/workflows/create-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Open Pull Request

on:
push:
branches:
- create-pull-request

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

- uses: ./
with:
helper: create-pr
title: New PR
body: Implemented new feature. Added tests.
github_token: ${{ secrets.GITHUB_TOKEN }}

permissions: write-all
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Each of the following helpers are defined in a file of the same name in `src/hel
### [**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-pr**](.github/workflows/create-pr.yml)
* Opens a pull request
### [**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.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ inputs:
override_filter_globs:
description: 'A list of file paths in glob syntax (newline/comma separated) that is used to override the filter'
required: false
title:
description: 'An issue title'
required: false
outputs:
output:
description: 'The output of the helper'
Expand Down
78 changes: 78 additions & 0 deletions dist/150.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/150.index.js.map

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

3 changes: 3 additions & 0 deletions dist/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ inputs:
override_filter_globs:
description: 'A list of file paths in glob syntax (newline/comma separated) that is used to override the filter'
required: false
title:
description: 'An issue title'
required: false
outputs:
output:
description: 'The output of the helper'
Expand Down
10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18836,6 +18836,11 @@ var map = {
438,
956
],
"./create-pr": [
9150,
438,
150
],
"./create-pr-comment": [
3461,
438,
Expand All @@ -18846,6 +18851,11 @@ var map = {
438,
461
],
"./create-pr.ts": [
9150,
438,
150
],
"./create-project-card": [
6124,
438,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions src/helpers/create-pr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 { context } from '@actions/github';
import { octokit } from '../octokit';

interface CreatePR {
title: string;
body: string;
}

export const createPr = async ({ title, body }: CreatePR) => {
const {
data: { default_branch }
} = await octokit.repos.get({ ...context.repo });
return octokit.pulls.create({
title,
head: context.ref.replace('refs/heads/', ''),
base: default_branch,
body,
maintainer_can_modify: true,
...context.repo
});
};
63 changes: 63 additions & 0 deletions test/helpers/create-pr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 { context } from '@actions/github';
import { createPr } from '../../src/helpers/create-pr';
import { octokit } from '../../src/octokit';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, ref: 'refs/heads/source' },
getOctokit: jest.fn(() => ({
rest: {
repos: { get: jest.fn() },
pulls: { create: jest.fn() }
}
}))
}));

(octokit.repos.get as unknown as Mocktokit).mockImplementation(async () => ({
data: {
default_branch: 'default branch'
}
}));

describe('createPr', () => {
const title = 'title';
const body = 'body';

beforeEach(() => {
createPr({
title,
body
});
});

it('should call repos get with correct params', () => {
expect(octokit.repos.get).toHaveBeenCalledWith({ ...context.repo });
});

it('should call create with correct params', () => {
expect(octokit.pulls.create).toHaveBeenCalledWith({
title,
head: 'source',
base: 'default branch',
body,
maintainer_can_modify: true,
draft: undefined,
issue: undefined,
...context.repo
});
});
});

0 comments on commit 44e5b47

Please sign in to comment.