Compare commits
245 Commits
Author | SHA1 | Date | |
---|---|---|---|
45c510e1f6 | |||
249b80db6b | |||
6c2b44c6ac | |||
76c58cf6a9 | |||
8c603dbb04 | |||
d01e0807ef | |||
ce699aa2d1 | |||
9984f611a7 | |||
ff0beed1b2 | |||
ddeca94037 | |||
0fd77ba8cc | |||
c7f493a800 | |||
91664dfb28 | |||
13ec5274b1 | |||
bcf9790963 | |||
88ea447de7 | |||
da928d5fcc | |||
2465e435b9 | |||
37b2bd1eca | |||
eb13e17e17 | |||
a1ecc20658 | |||
ffcad23634 | |||
f4b52b768a | |||
af682c8fcb | |||
7378b23cb0 | |||
370ae6d537 | |||
ae0797ee12 | |||
e05457394a | |||
44f76dd5b3 | |||
279e66ed27 | |||
ce9dd3641e | |||
1a00b34382 | |||
e17bb55cb7 | |||
1890e1ec35 | |||
a49ee3308e | |||
16fa12ee5f | |||
5ea31358e9 | |||
105f0d3816 | |||
8fb2374109 | |||
a68328a1ee | |||
9bf4b302a5 | |||
da6a868b86 | |||
cbd5e97fef | |||
095c53659b | |||
fc687f898b | |||
3b32bfa4f7 | |||
9c3bf74d10 | |||
b9ed02107c | |||
2f31350598 | |||
d14206169d | |||
82c423c73b | |||
7cf22579c6 | |||
5893420513 | |||
8bdec9b230 | |||
2570a753e0 | |||
1c466bedd5 | |||
a755ad5ffb | |||
52cabd40b9 | |||
12b7e7a141 | |||
8f96fd0252 | |||
5f45e30ff9 | |||
348abed1de | |||
06ab0ad94e | |||
de6e791e81 | |||
184bc2eccd | |||
4dab765cf5 | |||
329417f094 | |||
7311df63a5 | |||
47a0e6b298 | |||
9b02473bb0 | |||
40b1b245eb | |||
64c8d492d5 | |||
28fddffcf8 | |||
ba8309ff9c | |||
ee482f51bd | |||
bf7dd38b26 | |||
71a37c5c33 | |||
f9af0fbfc0 | |||
0c57887fe8 | |||
7ad9f6e012 | |||
dd364f2835 | |||
ddd52205b6 | |||
a81d0faef6 | |||
fc03a820b2 | |||
b2cdf099d0 | |||
f3d08d3f0b | |||
3a21b8d668 | |||
132b02c8e1 | |||
9507cdc7ac | |||
f4bf0d40c3 | |||
cf325ed84b | |||
ebc5e02585 | |||
c01f8fadc7 | |||
c0c9424b03 | |||
7649c29e89 | |||
125f154b50 | |||
8c01dce3ac | |||
3c6aade49b | |||
76c09b178c | |||
2f62e00e1b | |||
16354377ad | |||
809468fcd7 | |||
216b3681c3 | |||
6fa44e144d | |||
b5b1bc17a4 | |||
794518a553 | |||
f4ee4a8333 | |||
053b501145 | |||
1a182d0679 | |||
24012f5c84 | |||
a6a1a418bf | |||
5429e57002 | |||
3c32fac1fe | |||
803cc5ea8a | |||
4ba9ca3d10 | |||
40e70b8f7b | |||
b7eabdce59 | |||
43dc723813 | |||
4d3b0a48ef | |||
e9a825aacd | |||
3509fd45ae | |||
14ee9d1df2 | |||
5d969a55c1 | |||
44130f6fc9 | |||
86ccd8cdef | |||
7e7fa32a5f | |||
5f7beeb2ff | |||
926d56fcba | |||
8d744a2cd3 | |||
c2d829c681 | |||
d77392faf0 | |||
ccd2b64012 | |||
58fb221778 | |||
7a856e8b5d | |||
65d7a66451 | |||
44a7f59b6f | |||
8acaf6bb4c | |||
498d78cb23 | |||
172ec762f8 | |||
5cb0d674f3 | |||
eb892d7803 | |||
f8274253bd | |||
0f423da02c | |||
9573f479a0 | |||
eb4cde120d | |||
25902ccdd1 | |||
39b337e8bb | |||
7e70d8e63c | |||
1a640f5b01 | |||
9faa8dc1d9 | |||
afcf57957d | |||
8cc3564bf3 | |||
6910c5cd03 | |||
14836c6ff3 | |||
8fbfcfbcbb | |||
326f260418 | |||
75104b7d7e | |||
fbb7e0e650 | |||
0f1e60a1f8 | |||
caa116d991 | |||
d2f72f0799 | |||
ded05960f3 | |||
eb605db8a3 | |||
b11e4c665b | |||
65327d17a5 | |||
0837238e66 | |||
17bd947e89 | |||
24b42ba7f4 | |||
c0aaf5bab9 | |||
968cb0f4d9 | |||
9d6f73d546 | |||
70d240d0c4 | |||
7bb7d96c96 | |||
66fcd19e8d | |||
d93f2b46fd | |||
246328e3d8 | |||
61cff7c673 | |||
46ba7bdfe8 | |||
d650be7389 | |||
2f7173349f | |||
4ca95026d7 | |||
64c4efd526 | |||
16e35685ce | |||
3b12cf0165 | |||
2a283f5fc3 | |||
8ed207bcca | |||
bd1f6727cd | |||
9a3acf8f32 | |||
b38fd9eb87 | |||
4a9e76e377 | |||
8cb4c8b741 | |||
b9eb5dd95e | |||
5502904068 | |||
56ad1fed7b | |||
2132f428f6 | |||
c558d39395 | |||
db640fa8db | |||
95d6677567 | |||
1f4e24248b | |||
08595270b5 | |||
237244614a | |||
6295d61f0c | |||
d4024e2876 | |||
ee96ad03d9 | |||
755b39d2ff | |||
9f95ac6c53 | |||
cedbe4ad47 | |||
0e48ed8743 | |||
e7291b422e | |||
eb824681a8 | |||
4cc13107a9 | |||
c71b8e4206 | |||
e2bf7f9b75 | |||
e1f4cfdcd4 | |||
b3f0552507 | |||
f4be118b21 | |||
c9f22f86fb | |||
35d5f3c8ae | |||
000a0fc06a | |||
2a59f517a7 | |||
48ce89bc7d | |||
6570353abb | |||
8f6cecd6c4 | |||
e14ef3b543 | |||
c5778e5181 | |||
374fc61fef | |||
6fa547cc6f | |||
4db3619128 | |||
989a8308ec | |||
6249109e58 | |||
c9b850c450 | |||
340e629d2f | |||
abc19caa82 | |||
3474dda921 | |||
ad11b10aa4 | |||
86aa5be8bf | |||
6867319cf3 | |||
cc84a2389e | |||
7e7150d0e8 | |||
eb99d45ce6 | |||
115b7391e1 | |||
8305970523 | |||
32f5c5dd5f | |||
628c2d7d35 | |||
37582e8764 |
3
.eslintignore
Normal file
3
.eslintignore
Normal file
@ -0,0 +1,3 @@
|
||||
dist/
|
||||
lib/
|
||||
node_modules/
|
@ -1,17 +1,19 @@
|
||||
{
|
||||
"env": {
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"rules": {
|
||||
}
|
||||
}
|
||||
"env": { "node": true, "jest": true },
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": { "ecmaVersion": 9, "sourceType": "module" },
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:import/errors",
|
||||
"plugin:import/warnings",
|
||||
"plugin:import/typescript",
|
||||
"plugin:prettier/recommended",
|
||||
"prettier/@typescript-eslint"
|
||||
],
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"rules": {
|
||||
"@typescript-eslint/camelcase": "off"
|
||||
}
|
||||
}
|
||||
|
1
.github/CODEOWNERS
vendored
1
.github/CODEOWNERS
vendored
@ -1 +0,0 @@
|
||||
* @peter-evans
|
131
.github/workflows/ci.yml
vendored
Normal file
131
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,131 @@
|
||||
name: CI
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
paths-ignore:
|
||||
- 'README.md'
|
||||
- 'docs/**'
|
||||
pull_request:
|
||||
branches: [master]
|
||||
paths-ignore:
|
||||
- 'README.md'
|
||||
- 'docs/**'
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 12.x
|
||||
- uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
- run: npm run format-check
|
||||
- run: npm run lint
|
||||
- run: npm run test
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: dist
|
||||
path: dist
|
||||
- uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: action.yml
|
||||
path: action.yml
|
||||
|
||||
test:
|
||||
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
|
||||
needs: [build]
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
target: [built, committed]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ref: master
|
||||
- if: matrix.target == 'built' || github.event_name == 'pull_request'
|
||||
uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: dist
|
||||
path: dist
|
||||
- if: matrix.target == 'built' || github.event_name == 'pull_request'
|
||||
uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: action.yml
|
||||
path: .
|
||||
|
||||
- name: Create change
|
||||
run: date +%s > report.txt
|
||||
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: ./
|
||||
with:
|
||||
commit-message: '[CI] test ${{ matrix.target }}'
|
||||
committer: GitHub <noreply@github.com>
|
||||
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
|
||||
title: '[CI] test ${{ matrix.target }}'
|
||||
body: |
|
||||
- CI test case for target '${{ matrix.target }}'
|
||||
|
||||
Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
branch: ci-test-${{ matrix.target }}
|
||||
|
||||
- name: Close Pull
|
||||
uses: peter-evans/close-pull@v1
|
||||
with:
|
||||
pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
|
||||
comment: '[CI] test ${{ matrix.target }}'
|
||||
delete-branch: true
|
||||
|
||||
commentTestSuiteHelp:
|
||||
if: github.event_name == 'pull_request'
|
||||
needs: [test]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Find Comment
|
||||
uses: peter-evans/find-comment@v1
|
||||
id: fc
|
||||
with:
|
||||
issue-number: ${{ github.event.number }}
|
||||
comment-author: 'github-actions[bot]'
|
||||
body-includes: Full test suite slash command
|
||||
|
||||
- if: steps.fc.outputs.comment-id == ''
|
||||
name: Create comment
|
||||
uses: peter-evans/create-or-update-comment@v1
|
||||
with:
|
||||
issue-number: ${{ github.event.number }}
|
||||
body: |
|
||||
Full test suite slash command (repository admin only)
|
||||
```
|
||||
/test repository=${{ github.event.pull_request.head.repo.full_name }} ref=${{ github.event.pull_request.head.ref }} build=true
|
||||
```
|
||||
|
||||
package:
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||
needs: [test]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/download-artifact@v2
|
||||
with:
|
||||
name: dist
|
||||
path: dist
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
commit-message: 'build: update distribution'
|
||||
title: Update distribution
|
||||
body: |
|
||||
- Updates the distribution for changes on `master`
|
||||
|
||||
Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
branch: update-distribution
|
35
.github/workflows/cpr-example-command.yml
vendored
35
.github/workflows/cpr-example-command.yml
vendored
@ -7,38 +7,43 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Create report file
|
||||
|
||||
- name: Make changes to pull request
|
||||
run: date +%s > report.txt
|
||||
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: ./
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Add report file
|
||||
committer: Peter Evans <peter-evans@users.noreply.github.com>
|
||||
title: '[Example] Add report file'
|
||||
commit-message: Update report
|
||||
committer: GitHub <noreply@github.com>
|
||||
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
|
||||
signoff: false
|
||||
title: '[Example] Update report'
|
||||
body: |
|
||||
New report
|
||||
- Contains *today's* date
|
||||
Update report
|
||||
- Updated with *today's* date
|
||||
- Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
labels: report, automated pr
|
||||
labels: |
|
||||
report
|
||||
automated pr
|
||||
assignees: peter-evans
|
||||
reviewers: peter-evans
|
||||
milestone: 1
|
||||
project: Example Project
|
||||
project-column: To do
|
||||
draft: false
|
||||
branch: example-patches
|
||||
request-to-parent: false
|
||||
- name: Check outputs
|
||||
delete-branch: true
|
||||
|
||||
- name: Check output
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
|
||||
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
|
||||
|
||||
- name: Add reaction
|
||||
uses: peter-evans/create-or-update-comment@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
|
||||
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
|
||||
reaction-type: hooray
|
||||
|
19
.github/workflows/dockerhub-description.yml
vendored
19
.github/workflows/dockerhub-description.yml
vendored
@ -1,19 +0,0 @@
|
||||
name: Update Docker Hub Description
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
paths:
|
||||
- README.md
|
||||
- .github/workflows/dockerhub-description.yml
|
||||
jobs:
|
||||
dockerHubDescription:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Docker Hub Description
|
||||
uses: peter-evans/dockerhub-description@v2.1.0
|
||||
env:
|
||||
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
DOCKERHUB_REPOSITORY: peterevans/create-pull-request
|
17
.github/workflows/slash-command-dispatch.yml
vendored
17
.github/workflows/slash-command-dispatch.yml
vendored
@ -7,10 +7,9 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Slash Command Dispatch
|
||||
uses: peter-evans/slash-command-dispatch@v1
|
||||
uses: peter-evans/slash-command-dispatch@v2
|
||||
with:
|
||||
token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
reaction-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
|
||||
config: >
|
||||
[
|
||||
{
|
||||
@ -19,12 +18,6 @@ jobs:
|
||||
"repository": "peter-evans/create-pull-request-tests",
|
||||
"named_args": true
|
||||
},
|
||||
{
|
||||
"command": "pytest",
|
||||
"permission": "admin",
|
||||
"repository": "peter-evans/create-pull-request-tests",
|
||||
"named_args": true
|
||||
},
|
||||
{
|
||||
"command": "clean",
|
||||
"permission": "admin",
|
||||
@ -34,5 +27,11 @@ jobs:
|
||||
"command": "cpr-example",
|
||||
"permission": "admin",
|
||||
"issue_type": "issue"
|
||||
},
|
||||
{
|
||||
"command": "rebase",
|
||||
"permission": "admin",
|
||||
"repository": "peter-evans/slash-command-dispatch-processor",
|
||||
"issue_type": "pull-request"
|
||||
}
|
||||
]
|
||||
|
31
.github/workflows/update-dep.yml
vendored
Normal file
31
.github/workflows/update-dep.yml
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
name: Update Dependencies
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 1 * * 4'
|
||||
jobs:
|
||||
update-dep:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: '12.x'
|
||||
- name: Update dependencies
|
||||
run: |
|
||||
npx -p npm-check-updates ncu -u
|
||||
npm install
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
|
||||
commit-message: 'chore: update dependencies'
|
||||
committer: GitHub <noreply@github.com>
|
||||
author: actions-bot <actions-bot@users.noreply.github.com>
|
||||
title: Update dependencies
|
||||
body: |
|
||||
- Dependency updates
|
||||
|
||||
Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
branch: update-dependencies
|
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,6 +1,4 @@
|
||||
__pycache__
|
||||
.python-version
|
||||
|
||||
node_modules
|
||||
lib/
|
||||
node_modules/
|
||||
|
||||
.DS_Store
|
||||
|
3
.prettierignore
Normal file
3
.prettierignore
Normal file
@ -0,0 +1,3 @@
|
||||
dist/
|
||||
lib/
|
||||
node_modules/
|
11
.prettierrc.json
Normal file
11
.prettierrc.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"printWidth": 80,
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "none",
|
||||
"bracketSpacing": false,
|
||||
"arrowParens": "avoid",
|
||||
"parser": "typescript"
|
||||
}
|
181
README.md
181
README.md
@ -1,4 +1,5 @@
|
||||
# <img width="24" height="24" src="docs/assets/logo.svg"> Create Pull Request
|
||||
[](https://github.com/peter-evans/create-pull-request/actions?query=workflow%3ACI)
|
||||
[](https://github.com/marketplace/actions/create-pull-request)
|
||||
|
||||
A GitHub action to create a pull request for changes to your repository in the actions workspace.
|
||||
@ -20,113 +21,96 @@ Create Pull Request action will:
|
||||
|
||||
- [Concepts, guidelines and advanced usage](docs/concepts-guidelines.md)
|
||||
- [Examples](docs/examples.md)
|
||||
- [Updating from v1](docs/updating.md)
|
||||
- [Updating to v3](docs/updating.md)
|
||||
|
||||
## Usage
|
||||
|
||||
```yml
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
```
|
||||
|
||||
You can also pin to a [specific release](https://github.com/peter-evans/create-pull-request/releases) version in the format `@v2.x.x`
|
||||
You can also pin to a [specific release](https://github.com/peter-evans/create-pull-request/releases) version in the format `@v3.x.x`
|
||||
|
||||
### Action inputs
|
||||
|
||||
With the exception of `token`, all inputs are **optional**. If not set, sensible default values will be used.
|
||||
All inputs are **optional**. If not set, sensible defaults will be used.
|
||||
|
||||
**Note**: If you want pull requests created by this action to trigger an `on: push` or `on: pull_request` workflow then you must use a [Personal Access Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) instead of the default `GITHUB_TOKEN`. Alternatively, allow the action to [push using SSH](https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#push-using-ssh-deploy-keys) by configuring a deploy key.
|
||||
**Note**: If you want pull requests created by this action to trigger an `on: push` or `on: pull_request` workflow then you cannot use the default `GITHUB_TOKEN`. See the [documentation here](docs/concepts-guidelines.md#triggering-further-workflow-runs) for workarounds.
|
||||
|
||||
| Name | Description | Default |
|
||||
| --- | --- | --- |
|
||||
| `token` | `GITHUB_TOKEN` or a `repo` scoped [PAT](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). | |
|
||||
| `path` | Relative path under `$GITHUB_WORKSPACE` to the repository. | `$GITHUB_WORKSPACE` |
|
||||
| `token` | `GITHUB_TOKEN` or a `repo` scoped [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token). | `GITHUB_TOKEN` |
|
||||
| `path` | Relative path under `GITHUB_WORKSPACE` to the repository. | `GITHUB_WORKSPACE` |
|
||||
| `commit-message` | The message to use when committing changes. | `[create-pull-request] automated change` |
|
||||
| `committer` | The committer name and email address in the format `Display Name <email@address.com>`. | Defaults to the GitHub Actions bot user. See [Committer and author](#committer-and-author) for details. |
|
||||
| `author` | The author name and email address in the format `Display Name <email@address.com>`. | Defaults to the GitHub Actions bot user. See [Committer and author](#committer-and-author) for details. |
|
||||
| `committer` | The committer name and email address in the format `Display Name <email@address.com>`. Defaults to the GitHub Actions bot user. | `GitHub <noreply@github.com>` |
|
||||
| `author` | The author name and email address in the format `Display Name <email@address.com>`. Defaults to the user who triggered the workflow run. | `${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>` |
|
||||
| `signoff` | Add [`Signed-off-by`](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---signoff) line by the committer at the end of the commit log message. | `false` |
|
||||
| `branch` | The pull request branch name. | `create-pull-request/patch` |
|
||||
| `delete-branch` | Delete the `branch` when closing pull requests, and when undeleted after merging. Recommend `true`. | `false` |
|
||||
| `branch-suffix` | The branch suffix type when using the alternative branching strategy. Valid values are `random`, `timestamp` and `short-commit-hash`. See [Alternative strategy](#alternative-strategy---always-create-a-new-pull-request-branch) for details. | |
|
||||
| `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. |
|
||||
| `push-to-fork` | A fork of the checked-out parent repository to which the pull request branch will be pushed. e.g. `owner/repo-fork`. The pull request will be created to merge the fork's branch into the parent's base. See [push pull request branches to a fork](docs/concepts-guidelines.md#push-pull-request-branches-to-a-fork) for details. | |
|
||||
| `title` | The title of the pull request. | `Changes by create-pull-request action` |
|
||||
| `body` | The body of the pull request. | `Automated changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action` |
|
||||
| `labels` | A comma separated list of labels. | |
|
||||
| `assignees` | A comma separated list of assignees (GitHub usernames). | |
|
||||
| `reviewers` | A comma separated list of reviewers (GitHub usernames) to request a review from. | |
|
||||
| `team-reviewers` | A comma separated list of GitHub teams to request a review from. | |
|
||||
| `labels` | A comma or newline-separated list of labels. | |
|
||||
| `assignees` | A comma or newline-separated list of assignees (GitHub usernames). | |
|
||||
| `reviewers` | A comma or newline-separated list of reviewers (GitHub usernames) to request a review from. | |
|
||||
| `team-reviewers` | A comma or newline-separated list of GitHub teams to request a review from. Note that a `repo` scoped [PAT](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) may be required. See [this issue](https://github.com/peter-evans/create-pull-request/issues/155). | |
|
||||
| `milestone` | The number of the milestone to associate this pull request with. | |
|
||||
| `project` | The name of the project for which a card should be created. Requires `project-column`. | |
|
||||
| `project-column` | The name of the project column under which a card should be created. Requires `project`. | |
|
||||
| `branch` | The branch name. See [Branch naming](#branch-naming) for details. | `create-pull-request/patch` |
|
||||
| `request-to-parent` | Create the pull request in the parent repository of the checked out fork. | `false` |
|
||||
| `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. |
|
||||
| `branch-suffix` | The branch suffix type. Valid values are `random`, `timestamp` and `short-commit-hash`. See [Branch naming](#branch-naming) for details. | |
|
||||
| `draft` | Create a [draft pull request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#draft-pull-requests). | `false` |
|
||||
|
||||
**Outputs**
|
||||
### Action outputs
|
||||
|
||||
The pull request number is output as both an environment variable and a step output.
|
||||
Note that in order to read the step output the action step must have an id.
|
||||
The pull request number and URL are available as step outputs.
|
||||
Note that in order to read the step outputs the action step must have an id.
|
||||
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
- name: Check outputs
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
|
||||
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
|
||||
```
|
||||
|
||||
### Checkout
|
||||
### Action behaviour
|
||||
|
||||
This action expects repositories to be checked out with `actions/checkout@v2`.
|
||||
The default behaviour of the action is to create a pull request that will be continually updated with new changes until it is merged or closed.
|
||||
Changes are committed and pushed to a fixed-name branch, the name of which can be configured with the `branch` input.
|
||||
Any subsequent changes will be committed to the *same* branch and reflected in the open pull request.
|
||||
|
||||
If there is some reason you need to use `actions/checkout@v1` the following step can be added to checkout the branch.
|
||||
How the action behaves:
|
||||
|
||||
```yml
|
||||
- uses: actions/checkout@v1
|
||||
- run: git checkout "${GITHUB_REF:11}"
|
||||
```
|
||||
- If there are changes (i.e. a diff exists with the checked-out base branch), the changes will be pushed to a new `branch` and a pull request created.
|
||||
- If there are no changes (i.e. no diff exists with the checked-out base branch), no pull request will be created and the action exits silently.
|
||||
- If a pull request already exists and there are no further changes (i.e. no diff with the current pull request branch) then the action exits silently.
|
||||
- If a pull request exists and new changes on the base branch make the pull request unnecessary (i.e. there is no longer a diff between the pull request branch and the base), the pull request is automatically closed. Additionally, if `delete-branch` is set to `true` the `branch` will be deleted.
|
||||
|
||||
### Branch naming
|
||||
For further details about how the action works and usage guidelines, see [Concepts, guidelines and advanced usage](docs/concepts-guidelines.md).
|
||||
|
||||
For branch naming there are two strategies. Create a fixed-name pull request branch that will be updated with new changes until it is merged or closed, OR, always create a new unique branch each time there are changes to be committed.
|
||||
#### Alternative strategy - Always create a new pull request branch
|
||||
|
||||
#### Strategy A - Create and update a pull request branch (default)
|
||||
For some use cases it may be desirable to always create a new unique branch each time there are changes to be committed.
|
||||
This strategy is *not recommended* because if not used carefully it could result in multiple pull requests being created unnecessarily. If in doubt, use the [default strategy](#action-behaviour) of creating an updating a fixed-name branch.
|
||||
|
||||
This strategy is the default behaviour of the action. The input `branch` defaults to `create-pull-request/patch`. Changes will be committed to this branch and a pull request created. Any subsequent changes will be committed to the *same* branch and reflected in the open pull request. If the pull request is merged or closed a new one will be created. If subsequent changes cause the branch to no longer differ from the base the pull request will be automatically closed and the branch deleted.
|
||||
To use this strategy, set input `branch-suffix` with one of the following options.
|
||||
|
||||
#### Strategy B - Always create a new pull request branch
|
||||
|
||||
For this strategy there are three options to suffix the branch name.
|
||||
The branch name is defined by the input `branch` and defaults to `create-pull-request/patch`. The following options are values for `branch-suffix`.
|
||||
|
||||
- `random` - Commits will be made to a branch suffixed with a random alpha-numeric string. This option should be used if multiple pull requests will be created during the execution of a workflow. e.g. `create-pull-request/patch-6qj97jr`, `create-pull-request/patch-5jrjhvd`
|
||||
- `random` - Commits will be made to a branch suffixed with a random alpha-numeric string. e.g. `create-pull-request/patch-6qj97jr`, `create-pull-request/patch-5jrjhvd`
|
||||
|
||||
- `timestamp` - Commits will be made to a branch suffixed by a timestamp. e.g. `create-pull-request/patch-1569322532`, `create-pull-request/patch-1569322552`
|
||||
|
||||
- `short-commit-hash` - Commits will be made to a branch suffixed with the short SHA1 commit hash. e.g. `create-pull-request/patch-fcdfb59`, `create-pull-request/patch-394710b`
|
||||
|
||||
### Ignoring files
|
||||
|
||||
If there are files or directories you want to ignore you can simply add them to a `.gitignore` file at the root of your repository. The action will respect this file.
|
||||
|
||||
### Committer and author
|
||||
|
||||
If neither `committer` or `author` inputs are supplied the action will default to making commits that appear to be made by the GitHub Actions bot user.
|
||||
|
||||
In most cases, where the committer and author are the same, just the committer can be set.
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
committer: Peter Evans <peter-evans@users.noreply.github.com>
|
||||
```
|
||||
|
||||
### Controlling commits
|
||||
|
||||
As well as relying on the action to handle uncommitted changes, you can additionally make your own commits before the action runs.
|
||||
Note that the repository must be checked out on a branch with a remote, it won't work for [events which checkout a commit](docs/concepts-guidelines.md#events-which-checkout-a-commit).
|
||||
|
||||
```yml
|
||||
steps:
|
||||
@ -143,58 +127,83 @@ As well as relying on the action to handle uncommitted changes, you can addition
|
||||
- name: Uncommitted change
|
||||
run: date +%s > report.txt
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
```
|
||||
|
||||
### Ignoring files
|
||||
|
||||
If there are files or directories you want to ignore you can simply add them to a `.gitignore` file at the root of your repository. The action will respect this file.
|
||||
|
||||
### Create a project card
|
||||
|
||||
To create a project card for the pull request, pass the `pull-request-number` step output to [create-or-update-project-card](https://github.com/peter-evans/create-or-update-project-card) action.
|
||||
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
|
||||
- name: Create or Update Project Card
|
||||
uses: peter-evans/create-or-update-project-card@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
project-name: My project
|
||||
column-name: My column
|
||||
issue-number: ${{ steps.cpr.outputs.pull-request-number }}
|
||||
```
|
||||
|
||||
## Reference Example
|
||||
|
||||
The following workflow is a reference example that sets all the main inputs.
|
||||
The following workflow sets many of the action's inputs for reference purposes.
|
||||
Check the [defaults](#action-inputs) to avoid setting inputs unnecessarily.
|
||||
|
||||
See [examples](docs/examples.md) for more realistic use cases.
|
||||
|
||||
```yml
|
||||
name: Create Pull Request
|
||||
on: push
|
||||
jobs:
|
||||
createPullRequest:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Create report file
|
||||
|
||||
- name: Make changes to pull request
|
||||
run: date +%s > report.txt
|
||||
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Add report file
|
||||
committer: Peter Evans <peter-evans@users.noreply.github.com>
|
||||
author: Peter Evans <peter-evans@users.noreply.github.com>
|
||||
title: '[Example] Add report file'
|
||||
token: ${{ secrets.PAT }}
|
||||
commit-message: Update report
|
||||
committer: GitHub <noreply@github.com>
|
||||
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
|
||||
signoff: false
|
||||
branch: example-patches
|
||||
delete-branch: true
|
||||
title: '[Example] Update report'
|
||||
body: |
|
||||
New report
|
||||
- Contains *today's* date
|
||||
Update report
|
||||
- Updated with *today's* date
|
||||
- Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
labels: report, automated pr
|
||||
labels: |
|
||||
report
|
||||
automated pr
|
||||
assignees: peter-evans
|
||||
reviewers: peter-evans
|
||||
team-reviewers: owners, maintainers
|
||||
team-reviewers: |
|
||||
owners
|
||||
maintainers
|
||||
milestone: 1
|
||||
project: Example Project
|
||||
project-column: To do
|
||||
branch: example-patches
|
||||
request-to-parent: false
|
||||
draft: false
|
||||
|
||||
- name: Check outputs
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
|
||||
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
|
||||
```
|
||||
|
||||
This reference configuration will create pull requests that look like this:
|
||||
An example based on the above reference configuration creates pull requests that look like this:
|
||||
|
||||

|
||||
|
||||
|
1743
__test__/create-or-update-branch.int.test.ts
Normal file
1743
__test__/create-or-update-branch.int.test.ts
Normal file
File diff suppressed because it is too large
Load Diff
40
__test__/entrypoint.sh
Executable file
40
__test__/entrypoint.sh
Executable file
@ -0,0 +1,40 @@
|
||||
#!/bin/sh -l
|
||||
set -euo pipefail
|
||||
|
||||
# Save the working directory
|
||||
WORKINGDIR=$PWD
|
||||
|
||||
# Create and serve a remote repo
|
||||
mkdir -p /git/remote
|
||||
git init --bare /git/remote/test-base.git
|
||||
git daemon --verbose --enable=receive-pack --base-path=/git/remote --export-all /git/remote &>/dev/null &
|
||||
|
||||
# Give the daemon time to start
|
||||
sleep 2
|
||||
|
||||
# Create a local clone and make an initial commit
|
||||
mkdir -p /git/local
|
||||
git clone git://127.0.0.1/test-base.git /git/local/test-base
|
||||
cd /git/local/test-base
|
||||
git config --global user.email "you@example.com"
|
||||
git config --global user.name "Your Name"
|
||||
echo "#test-base" > README.md
|
||||
git add .
|
||||
git commit -m "initial commit"
|
||||
git push -u
|
||||
git log -1 --pretty=oneline
|
||||
git config --global --unset user.email
|
||||
git config --global --unset user.name
|
||||
git config -l
|
||||
|
||||
# Clone a server-side fork of the base repo
|
||||
cd $WORKINGDIR
|
||||
git clone --mirror git://127.0.0.1/test-base.git /git/remote/test-fork.git
|
||||
cd /git/remote/test-fork.git
|
||||
git log -1 --pretty=oneline
|
||||
|
||||
# Restore the working directory
|
||||
cd $WORKINGDIR
|
||||
|
||||
# Execute integration tests
|
||||
jest int --runInBand
|
49
__test__/git-auth-helper.int.test.ts
Normal file
49
__test__/git-auth-helper.int.test.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import {GitCommandManager} from '../lib/git-command-manager'
|
||||
import {GitAuthHelper} from '../lib/git-auth-helper'
|
||||
|
||||
const REPO_PATH = '/git/local/test-base'
|
||||
|
||||
const extraheaderConfigKey = 'http.https://github.com/.extraheader'
|
||||
|
||||
describe('git-auth-helper tests', () => {
|
||||
let git: GitCommandManager
|
||||
let gitAuthHelper: GitAuthHelper
|
||||
|
||||
beforeAll(async () => {
|
||||
git = await GitCommandManager.create(REPO_PATH)
|
||||
gitAuthHelper = new GitAuthHelper(git)
|
||||
})
|
||||
|
||||
it('tests save and restore with no persisted auth', async () => {
|
||||
await gitAuthHelper.savePersistedAuth()
|
||||
await gitAuthHelper.restorePersistedAuth()
|
||||
})
|
||||
|
||||
it('tests configure and removal of auth', async () => {
|
||||
await gitAuthHelper.configureToken('github-token')
|
||||
expect(await git.configExists(extraheaderConfigKey)).toBeTruthy()
|
||||
expect(await git.getConfigValue(extraheaderConfigKey)).toEqual(
|
||||
'AUTHORIZATION: basic eC1hY2Nlc3MtdG9rZW46Z2l0aHViLXRva2Vu'
|
||||
)
|
||||
|
||||
await gitAuthHelper.removeAuth()
|
||||
expect(await git.configExists(extraheaderConfigKey)).toBeFalsy()
|
||||
})
|
||||
|
||||
it('tests save and restore of persisted auth', async () => {
|
||||
const extraheaderConfigValue = 'AUTHORIZATION: basic ***persisted-auth***'
|
||||
await git.config(extraheaderConfigKey, extraheaderConfigValue)
|
||||
|
||||
await gitAuthHelper.savePersistedAuth()
|
||||
|
||||
const exists = await git.configExists(extraheaderConfigKey)
|
||||
expect(exists).toBeFalsy()
|
||||
|
||||
await gitAuthHelper.restorePersistedAuth()
|
||||
|
||||
const configValue = await git.getConfigValue(extraheaderConfigKey)
|
||||
expect(configValue).toEqual(extraheaderConfigValue)
|
||||
|
||||
await gitAuthHelper.removeAuth()
|
||||
})
|
||||
})
|
23
__test__/integration-tests.sh
Executable file
23
__test__/integration-tests.sh
Executable file
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
IMAGE="cpr-integration-tests:latest"
|
||||
ARG1=${1:-}
|
||||
|
||||
if [[ "$(docker images -q $IMAGE 2> /dev/null)" == "" || $ARG1 == "build" ]]; then
|
||||
echo "Building Docker image $IMAGE ..."
|
||||
|
||||
cat > Dockerfile << EOF
|
||||
FROM node:12-alpine
|
||||
RUN apk --no-cache add git git-daemon
|
||||
RUN npm install jest --global
|
||||
WORKDIR /cpr
|
||||
COPY __test__/entrypoint.sh /entrypoint.sh
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
EOF
|
||||
|
||||
docker build -t $IMAGE .
|
||||
rm Dockerfile
|
||||
fi
|
||||
|
||||
docker run -v $PWD:/cpr $IMAGE
|
124
__test__/utils.unit.test.ts
Normal file
124
__test__/utils.unit.test.ts
Normal file
@ -0,0 +1,124 @@
|
||||
import * as path from 'path'
|
||||
import * as utils from '../lib/utils'
|
||||
|
||||
const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
|
||||
|
||||
describe('utils tests', () => {
|
||||
beforeAll(() => {
|
||||
// GitHub workspace
|
||||
process.env['GITHUB_WORKSPACE'] = __dirname
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
// Restore GitHub workspace
|
||||
delete process.env['GITHUB_WORKSPACE']
|
||||
if (originalGitHubWorkspace) {
|
||||
process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
|
||||
}
|
||||
})
|
||||
|
||||
test('getStringAsArray splits string input by newlines and commas', async () => {
|
||||
const array = utils.getStringAsArray('1, 2, 3\n4, 5, 6')
|
||||
expect(array.length).toEqual(6)
|
||||
|
||||
const array2 = utils.getStringAsArray('')
|
||||
expect(array2.length).toEqual(0)
|
||||
})
|
||||
|
||||
test('getRepoPath successfully returns the path to the repository', async () => {
|
||||
expect(utils.getRepoPath()).toEqual(process.env['GITHUB_WORKSPACE'])
|
||||
expect(utils.getRepoPath('foo')).toEqual(
|
||||
path.resolve(process.env['GITHUB_WORKSPACE'] || '', 'foo')
|
||||
)
|
||||
})
|
||||
|
||||
test('getRemoteDetail successfully parses remote URLs', async () => {
|
||||
const remote1 = utils.getRemoteDetail(
|
||||
'https://github.com/peter-evans/create-pull-request'
|
||||
)
|
||||
expect(remote1.protocol).toEqual('HTTPS')
|
||||
expect(remote1.repository).toEqual('peter-evans/create-pull-request')
|
||||
|
||||
const remote2 = utils.getRemoteDetail(
|
||||
'https://xxx:x-oauth-basic@github.com/peter-evans/create-pull-request'
|
||||
)
|
||||
expect(remote2.protocol).toEqual('HTTPS')
|
||||
expect(remote2.repository).toEqual('peter-evans/create-pull-request')
|
||||
|
||||
const remote3 = utils.getRemoteDetail(
|
||||
'git@github.com:peter-evans/create-pull-request.git'
|
||||
)
|
||||
expect(remote3.protocol).toEqual('SSH')
|
||||
expect(remote3.repository).toEqual('peter-evans/create-pull-request')
|
||||
})
|
||||
|
||||
test('getRemoteDetail fails to parse a remote URL', async () => {
|
||||
const remoteUrl = 'https://github.com/peter-evans'
|
||||
try {
|
||||
utils.getRemoteDetail(remoteUrl)
|
||||
// Fail the test if an error wasn't thrown
|
||||
expect(true).toEqual(false)
|
||||
} catch (e) {
|
||||
expect(e.message).toEqual(
|
||||
`The format of '${remoteUrl}' is not a valid GitHub repository URL`
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
test('getRemoteUrl successfully returns remote URLs', async () => {
|
||||
const url1 = utils.getRemoteUrl('HTTPS', 'peter-evans/create-pull-request')
|
||||
expect(url1).toEqual('https://github.com/peter-evans/create-pull-request')
|
||||
|
||||
const url2 = utils.getRemoteUrl('SSH', 'peter-evans/create-pull-request')
|
||||
expect(url2).toEqual('git@github.com:peter-evans/create-pull-request.git')
|
||||
})
|
||||
|
||||
test('secondsSinceEpoch returns the number of seconds since the Epoch', async () => {
|
||||
const seconds = `${utils.secondsSinceEpoch()}`
|
||||
expect(seconds.length).toEqual(10)
|
||||
})
|
||||
|
||||
test('randomString returns strings of length 7', async () => {
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
expect(utils.randomString().length).toEqual(7)
|
||||
}
|
||||
})
|
||||
|
||||
test('parseDisplayNameEmail successfully parses display name email formats', async () => {
|
||||
const parsed1 = utils.parseDisplayNameEmail('abc def <abc@def.com>')
|
||||
expect(parsed1.name).toEqual('abc def')
|
||||
expect(parsed1.email).toEqual('abc@def.com')
|
||||
|
||||
const parsed2 = utils.parseDisplayNameEmail(
|
||||
'github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>'
|
||||
)
|
||||
expect(parsed2.name).toEqual('github-actions[bot]')
|
||||
expect(parsed2.email).toEqual(
|
||||
'41898282+github-actions[bot]@users.noreply.github.com'
|
||||
)
|
||||
})
|
||||
|
||||
test('parseDisplayNameEmail fails to parse display name email formats', async () => {
|
||||
const displayNameEmail1 = 'abc@def.com'
|
||||
try {
|
||||
utils.parseDisplayNameEmail(displayNameEmail1)
|
||||
// Fail the test if an error wasn't thrown
|
||||
expect(true).toEqual(false)
|
||||
} catch (e) {
|
||||
expect(e.message).toEqual(
|
||||
`The format of '${displayNameEmail1}' is not a valid email address with display name`
|
||||
)
|
||||
}
|
||||
|
||||
const displayNameEmail2 = ' < >'
|
||||
try {
|
||||
utils.parseDisplayNameEmail(displayNameEmail2)
|
||||
// Fail the test if an error wasn't thrown
|
||||
expect(true).toEqual(false)
|
||||
} catch (e) {
|
||||
expect(e.message).toEqual(
|
||||
`The format of '${displayNameEmail2}' is not a valid email address with display name`
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
83
action.yml
83
action.yml
@ -2,45 +2,70 @@ name: 'Create Pull Request'
|
||||
description: 'Creates a pull request for changes to your repository in the actions workspace'
|
||||
inputs:
|
||||
token:
|
||||
description: 'GITHUB_TOKEN or a repo scoped PAT'
|
||||
required: true
|
||||
description: 'GITHUB_TOKEN or a `repo` scoped Personal Access Token (PAT)'
|
||||
default: ${{ github.token }}
|
||||
path:
|
||||
description: 'Relative path under $GITHUB_WORKSPACE to the repository.'
|
||||
description: >
|
||||
Relative path under $GITHUB_WORKSPACE to the repository.
|
||||
Defaults to $GITHUB_WORKSPACE.
|
||||
commit-message:
|
||||
description: 'The message to use when committing changes.'
|
||||
default: '[create-pull-request] automated change'
|
||||
committer:
|
||||
description: 'The committer name and email address.'
|
||||
description: >
|
||||
The committer name and email address in the format `Display Name <email@address.com>`.
|
||||
Defaults to the GitHub Actions bot user.
|
||||
default: 'GitHub <noreply@github.com>'
|
||||
author:
|
||||
description: 'The author name and email address.'
|
||||
title:
|
||||
description: 'The title of the pull request.'
|
||||
body:
|
||||
description: 'The body of the pull request.'
|
||||
labels:
|
||||
description: 'A comma separated list of labels.'
|
||||
assignees:
|
||||
description: 'A comma separated list of assignees (GitHub usernames).'
|
||||
reviewers:
|
||||
description: 'A comma separated list of reviewers (GitHub usernames) to request a review from.'
|
||||
team-reviewers:
|
||||
description: 'A comma separated list of GitHub teams to request a review from.'
|
||||
milestone:
|
||||
description: 'The number of the milestone to associate this pull request with.'
|
||||
project:
|
||||
description: 'The name of the project for which a card should be created.'
|
||||
project-column:
|
||||
description: 'The name of the project column under which a card should be created.'
|
||||
description: >
|
||||
The author name and email address in the format `Display Name <email@address.com>`.
|
||||
Defaults to the user who triggered the workflow run.
|
||||
default: '${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>'
|
||||
signoff:
|
||||
description: 'Add `Signed-off-by` line by the committer at the end of the commit log message.'
|
||||
default: false
|
||||
branch:
|
||||
description: 'The pull request branch name.'
|
||||
request-to-parent:
|
||||
description: 'Create the pull request in the parent repository of the checked out fork.'
|
||||
default: 'create-pull-request/patch'
|
||||
delete-branch:
|
||||
description: >
|
||||
Delete the `branch` when closing pull requests, and when undeleted after merging.
|
||||
Recommend `true`.
|
||||
default: false
|
||||
base:
|
||||
description: 'The pull request base branch.'
|
||||
branch-suffix:
|
||||
description: 'The branch suffix type.'
|
||||
description: 'The branch suffix type when using the alternative branching strategy.'
|
||||
base:
|
||||
description: >
|
||||
The pull request base branch.
|
||||
Defaults to the branch checked out in the workflow.
|
||||
push-to-fork:
|
||||
description: >
|
||||
A fork of the checked out parent repository to which the pull request branch will be pushed.
|
||||
e.g. `owner/repo-fork`.
|
||||
The pull request will be created to merge the fork's branch into the parent's base.
|
||||
title:
|
||||
description: 'The title of the pull request.'
|
||||
default: 'Changes by create-pull-request action'
|
||||
body:
|
||||
description: 'The body of the pull request.'
|
||||
default: 'Automated changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action'
|
||||
labels:
|
||||
description: 'A comma or newline separated list of labels.'
|
||||
assignees:
|
||||
description: 'A comma or newline separated list of assignees (GitHub usernames).'
|
||||
reviewers:
|
||||
description: 'A comma or newline separated list of reviewers (GitHub usernames) to request a review from.'
|
||||
team-reviewers:
|
||||
description: >
|
||||
A comma or newline separated list of GitHub teams to request a review from.
|
||||
Note that a `repo` scoped Personal Access Token (PAT) may be required.
|
||||
milestone:
|
||||
description: 'The number of the milestone to associate the pull request with.'
|
||||
draft:
|
||||
description: 'Create a draft pull request'
|
||||
default: false
|
||||
outputs:
|
||||
pr_number:
|
||||
pull-request-number:
|
||||
description: 'The pull request number'
|
||||
runs:
|
||||
using: 'node12'
|
||||
|
48
dist/cpr/common.py
vendored
48
dist/cpr/common.py
vendored
@ -1,48 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
|
||||
|
||||
def get_random_string(length=7, chars=string.ascii_lowercase + string.digits):
|
||||
return "".join(random.choice(chars) for _ in range(length))
|
||||
|
||||
|
||||
def parse_github_repository(url):
|
||||
# Parse the protocol and github repository from a URL
|
||||
# e.g. HTTPS, peter-evans/create-pull-request
|
||||
https_pattern = re.compile(r"^https://github.com/(.+/.+)$")
|
||||
ssh_pattern = re.compile(r"^git@github.com:(.+/.+).git$")
|
||||
|
||||
match = https_pattern.match(url)
|
||||
if match is not None:
|
||||
return "HTTPS", match.group(1)
|
||||
|
||||
match = ssh_pattern.match(url)
|
||||
if match is not None:
|
||||
return "SSH", match.group(1)
|
||||
|
||||
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")
|
||||
|
||||
|
||||
def parse_display_name_email(display_name_email):
|
||||
# Parse the name and email address from a string in the following format
|
||||
# Display Name <email@address.com>
|
||||
pattern = re.compile(r"^([^<]+)\s*<([^>]+)>$")
|
||||
|
||||
# Check we have a match
|
||||
match = pattern.match(display_name_email)
|
||||
if match is None:
|
||||
raise ValueError(
|
||||
f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
||||
|
||||
# Check that name and email are not just whitespace
|
||||
name = match.group(1).strip()
|
||||
email = match.group(2).strip()
|
||||
if len(name) == 0 or len(email) == 0:
|
||||
raise ValueError(
|
||||
f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
||||
|
||||
return name, email
|
145
dist/cpr/create_or_update_branch.py
vendored
145
dist/cpr/create_or_update_branch.py
vendored
@ -1,145 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create or Update Branch """
|
||||
import common as cmn
|
||||
from git import Repo, GitCommandError
|
||||
import os
|
||||
|
||||
|
||||
CHERRYPICK_EMPTY = (
|
||||
"The previous cherry-pick is now empty, possibly due to conflict resolution."
|
||||
)
|
||||
|
||||
|
||||
def fetch_successful(repo, repo_url, branch):
|
||||
try:
|
||||
repo.git.fetch(repo_url, f"{branch}:refs/remotes/origin/{branch}")
|
||||
except GitCommandError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_ahead(repo, branch_1, branch_2):
|
||||
# Return true if branch_2 is ahead of branch_1
|
||||
return (
|
||||
int(repo.git.rev_list("--right-only", "--count", f"{branch_1}...{branch_2}"))
|
||||
> 0
|
||||
)
|
||||
|
||||
|
||||
def is_behind(repo, branch_1, branch_2):
|
||||
# Return true if branch_2 is behind branch_1
|
||||
return (
|
||||
int(repo.git.rev_list("--left-only", "--count", f"{branch_1}...{branch_2}")) > 0
|
||||
)
|
||||
|
||||
|
||||
def is_even(repo, branch_1, branch_2):
|
||||
# Return true if branch_2 is even with branch_1
|
||||
return not is_ahead(repo, branch_1, branch_2) and not is_behind(
|
||||
repo, branch_1, branch_2
|
||||
)
|
||||
|
||||
|
||||
def has_diff(repo, branch_1, branch_2):
|
||||
diff = repo.git.diff(f"{branch_1}..{branch_2}")
|
||||
return len(diff) > 0
|
||||
|
||||
|
||||
def create_or_update_branch(repo, repo_url, commit_message, base, branch):
|
||||
# Set the default return values
|
||||
action = "none"
|
||||
diff = False
|
||||
|
||||
# Get the working base. This may or may not be the actual base.
|
||||
working_base = repo.git.symbolic_ref("HEAD", "--short")
|
||||
# If the base is not specified it is assumed to be the working base
|
||||
if base is None:
|
||||
base = working_base
|
||||
|
||||
# Save the working base changes to a temporary branch
|
||||
temp_branch = cmn.get_random_string(length=20)
|
||||
repo.git.checkout("HEAD", b=temp_branch)
|
||||
# Commit any uncomitted changes
|
||||
if repo.is_dirty(untracked_files=True):
|
||||
print(f"Uncommitted changes found. Adding a commit.")
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m=commit_message)
|
||||
|
||||
# Perform fetch and reset the working base
|
||||
# Commits made during the workflow will be removed
|
||||
repo.git.fetch("--force", repo_url, f"{working_base}:{working_base}")
|
||||
|
||||
# If the working base is not the base, rebase the temp branch commits
|
||||
if working_base != base:
|
||||
print(
|
||||
f"Rebasing commits made to branch '{working_base}' on to base branch '{base}'"
|
||||
)
|
||||
# Checkout the actual base
|
||||
repo.git.fetch("--force", repo_url, f"{base}:{base}")
|
||||
repo.git.checkout(base)
|
||||
# Cherrypick commits from the temporary branch starting from the working base
|
||||
commits = repo.git.rev_list("--reverse", f"{working_base}..{temp_branch}", ".")
|
||||
for commit in commits.splitlines():
|
||||
try:
|
||||
repo.git.cherry_pick(
|
||||
"--strategy",
|
||||
"recursive",
|
||||
"--strategy-option",
|
||||
"theirs",
|
||||
f"{commit}",
|
||||
)
|
||||
except GitCommandError as e:
|
||||
if CHERRYPICK_EMPTY not in e.stderr:
|
||||
print("Unexpected error: ", e)
|
||||
raise
|
||||
# Reset the temp branch to the working index
|
||||
repo.git.checkout("-B", temp_branch, "HEAD")
|
||||
# Reset the base
|
||||
repo.git.fetch("--force", repo_url, f"{base}:{base}")
|
||||
|
||||
# Try to fetch the pull request branch
|
||||
if not fetch_successful(repo, repo_url, branch):
|
||||
# The pull request branch does not exist
|
||||
print(f"Pull request branch '{branch}' does not exist yet")
|
||||
# Create the pull request branch
|
||||
repo.git.checkout("HEAD", b=branch)
|
||||
# Check if the pull request branch is ahead of the base
|
||||
diff = is_ahead(repo, base, branch)
|
||||
if diff:
|
||||
action = "created"
|
||||
print(f"Created branch '{branch}'")
|
||||
else:
|
||||
print(
|
||||
f"Branch '{branch}' is not ahead of base '{base}' and will not be created"
|
||||
)
|
||||
else:
|
||||
# The pull request branch exists
|
||||
print(
|
||||
f"Pull request branch '{branch}' already exists as remote branch 'origin/{branch}'"
|
||||
)
|
||||
# Checkout the pull request branch
|
||||
repo.git.checkout(branch)
|
||||
|
||||
if has_diff(repo, branch, temp_branch):
|
||||
# If the branch differs from the recreated temp version then the branch is reset
|
||||
# For changes on base this action is similar to a rebase of the pull request branch
|
||||
print(f"Resetting '{branch}'")
|
||||
repo.git.checkout("-B", branch, temp_branch)
|
||||
# repo.git.switch("-C", branch, temp_branch)
|
||||
|
||||
# Check if the pull request branch has been updated
|
||||
# If the branch was reset or updated it will be ahead
|
||||
# It may be behind if a reset now results in no diff with the base
|
||||
if not is_even(repo, f"origin/{branch}", branch):
|
||||
action = "updated"
|
||||
print(f"Updated branch '{branch}'")
|
||||
else:
|
||||
print(f"Branch '{branch}' is even with its remote and will not be updated")
|
||||
|
||||
# Check if the pull request branch is ahead of the base
|
||||
diff = is_ahead(repo, base, branch)
|
||||
|
||||
# Delete the temporary branch
|
||||
repo.git.branch("--delete", "--force", temp_branch)
|
||||
|
||||
return {"action": action, "diff": diff, "base": base}
|
140
dist/cpr/create_or_update_pull_request.py
vendored
140
dist/cpr/create_or_update_pull_request.py
vendored
@ -1,140 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create or Update Pull Request """
|
||||
from github import Github, GithubException
|
||||
import os
|
||||
|
||||
|
||||
def cs_string_to_list(str):
|
||||
# Split the comma separated string into a list
|
||||
l = [i.strip() for i in str.split(",")]
|
||||
# Remove empty strings
|
||||
return list(filter(None, l))
|
||||
|
||||
|
||||
def create_project_card(github_repo, project_name, project_column_name, pull_request):
|
||||
# Locate the project by name
|
||||
project = None
|
||||
for project_item in github_repo.get_projects("all"):
|
||||
if project_item.name == project_name:
|
||||
project = project_item
|
||||
break
|
||||
|
||||
if not project:
|
||||
print("::error::Project not found. Unable to create project card.")
|
||||
return
|
||||
|
||||
# Locate the column by name
|
||||
column = None
|
||||
for column_item in project.get_columns():
|
||||
if column_item.name == project_column_name:
|
||||
column = column_item
|
||||
break
|
||||
|
||||
if not column:
|
||||
print("::error::Project column not found. Unable to create project card.")
|
||||
return
|
||||
|
||||
# Create a project card for the pull request
|
||||
column.create_card(content_id=pull_request.id, content_type="PullRequest")
|
||||
print(
|
||||
"Added pull request #%d to project '%s' under column '%s'"
|
||||
% (pull_request.number, project.name, column.name)
|
||||
)
|
||||
|
||||
|
||||
def create_or_update_pull_request(
|
||||
github_token,
|
||||
github_repository,
|
||||
branch,
|
||||
base,
|
||||
title,
|
||||
body,
|
||||
labels,
|
||||
assignees,
|
||||
milestone,
|
||||
reviewers,
|
||||
team_reviewers,
|
||||
project_name,
|
||||
project_column_name,
|
||||
request_to_parent,
|
||||
):
|
||||
if request_to_parent is None:
|
||||
request_to_parent = False
|
||||
else:
|
||||
request_to_parent = request_to_parent.lower() in ['true', '1', 't', 'y', 'yes', 'on']
|
||||
|
||||
github_repo = head_repo = Github(github_token).get_repo(github_repository)
|
||||
if request_to_parent:
|
||||
github_repo = github_repo.parent
|
||||
if github_repo is None:
|
||||
raise ValueError("The checked out repository is not a fork. Input 'request-to-parent' should be set to false.")
|
||||
|
||||
head_branch = f"{head_repo.owner.login}:{branch}"
|
||||
|
||||
# Create the pull request
|
||||
try:
|
||||
pull_request = github_repo.create_pull(
|
||||
title=title, body=body, base=base, head=head_branch
|
||||
)
|
||||
print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
|
||||
except GithubException as e:
|
||||
if e.status == 422:
|
||||
# A pull request exists for this branch and base
|
||||
# Get the pull request
|
||||
pull_request = github_repo.get_pulls(
|
||||
state="open", base=base, head=head_branch
|
||||
)[0]
|
||||
# Update title and body
|
||||
pull_request.as_issue().edit(title=title, body=body)
|
||||
print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
|
||||
else:
|
||||
print(str(e))
|
||||
raise
|
||||
|
||||
# Set the output variables
|
||||
os.system(f"echo ::set-env name=PULL_REQUEST_NUMBER::{pull_request.number}")
|
||||
os.system(f"echo ::set-output name=pr_number::{pull_request.number}")
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if labels is not None:
|
||||
print(f"Applying labels '{labels}'")
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(labels))
|
||||
if assignees is not None:
|
||||
print(f"Applying assignees '{assignees}'")
|
||||
pull_request.as_issue().edit(assignees=cs_string_to_list(assignees))
|
||||
if milestone is not None:
|
||||
print(f"Applying milestone '{milestone}'")
|
||||
milestone = github_repo.get_milestone(int(milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers
|
||||
if reviewers is not None:
|
||||
print(f"Requesting reviewers '{reviewers}'")
|
||||
try:
|
||||
pull_request.create_review_request(reviewers=cs_string_to_list(reviewers))
|
||||
except GithubException as e:
|
||||
# Likely caused by "Review cannot be requested from pull request author."
|
||||
if e.status == 422:
|
||||
print("Request reviewers failed - {}".format(e.data["message"]))
|
||||
|
||||
# Set pull request team reviewers
|
||||
if team_reviewers is not None:
|
||||
print(f"Requesting team reviewers '{team_reviewers}'")
|
||||
pull_request.create_review_request(
|
||||
team_reviewers=cs_string_to_list(team_reviewers)
|
||||
)
|
||||
|
||||
# Create a project card for the pull request
|
||||
if project_name is not None and project_column_name is not None:
|
||||
try:
|
||||
create_project_card(
|
||||
github_repo, project_name, project_column_name, pull_request
|
||||
)
|
||||
except GithubException as e:
|
||||
# Likely caused by "Project already has the associated issue."
|
||||
if e.status == 422:
|
||||
print(
|
||||
"Create project card failed - {}".format(
|
||||
e.data["errors"][0]["message"]
|
||||
)
|
||||
)
|
228
dist/cpr/create_pull_request.py
vendored
228
dist/cpr/create_pull_request.py
vendored
@ -1,228 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create Pull Request """
|
||||
import base64
|
||||
import common as cmn
|
||||
import create_or_update_branch as coub
|
||||
import create_or_update_pull_request as coupr
|
||||
from git import Repo, GitCommandError
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# Default the committer and author to the GitHub Actions bot
|
||||
DEFAULT_COMMITTER = "GitHub <noreply@github.com>"
|
||||
DEFAULT_AUTHOR = (
|
||||
"github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
)
|
||||
DEFAULT_COMMIT_MESSAGE = "[create-pull-request] automated change"
|
||||
DEFAULT_TITLE = "Changes by create-pull-request action"
|
||||
DEFAULT_BODY = (
|
||||
"Automated changes by "
|
||||
+ "[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action"
|
||||
)
|
||||
DEFAULT_BRANCH = "create-pull-request/patch"
|
||||
|
||||
|
||||
def get_git_config_value(repo, name):
|
||||
try:
|
||||
return repo.git.config("--get", name)
|
||||
except GitCommandError:
|
||||
return None
|
||||
|
||||
|
||||
def get_repository_detail(repo):
|
||||
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
|
||||
if remote_origin_url is None:
|
||||
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
|
||||
protocol, github_repository = cmn.parse_github_repository(remote_origin_url)
|
||||
return remote_origin_url, protocol, github_repository
|
||||
|
||||
|
||||
def git_user_config_is_set(repo):
|
||||
name = get_git_config_value(repo, "user.name")
|
||||
email = get_git_config_value(repo, "user.email")
|
||||
|
||||
if name is not None and email is not None:
|
||||
print(f"Git user already configured as '{name} <{email}>'")
|
||||
return True
|
||||
|
||||
committer_name = get_git_config_value(repo, "committer.name")
|
||||
committer_email = get_git_config_value(repo, "committer.email")
|
||||
author_name = get_git_config_value(repo, "author.name")
|
||||
author_email = get_git_config_value(repo, "author.email")
|
||||
|
||||
if (
|
||||
committer_name is not None
|
||||
and committer_email is not None
|
||||
and author_name is not None
|
||||
and author_email is not None
|
||||
):
|
||||
print(
|
||||
f"Git committer already configured as '{committer_name} <{committer_email}>'"
|
||||
)
|
||||
print(f"Git author already configured as '{author_name} <{author_email}>'")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def set_committer_author(repo, committer, author):
|
||||
# If either committer or author is supplied they will be cross used
|
||||
if committer is None and author is not None:
|
||||
print("Supplied author will also be used as the committer.")
|
||||
committer = author
|
||||
if author is None and committer is not None:
|
||||
print("Supplied committer will also be used as the author.")
|
||||
author = committer
|
||||
|
||||
# If no committer/author has been supplied but user configuration already
|
||||
# exists in git config we can exit and use the existing config as-is.
|
||||
if committer is None and author is None:
|
||||
if git_user_config_is_set(repo):
|
||||
return
|
||||
|
||||
# Set defaults if no committer/author has been supplied
|
||||
if committer is None and author is None:
|
||||
committer = DEFAULT_COMMITTER
|
||||
author = DEFAULT_AUTHOR
|
||||
|
||||
# Set git environment. This will not persist after the action completes.
|
||||
committer_name, committer_email = cmn.parse_display_name_email(committer)
|
||||
author_name, author_email = cmn.parse_display_name_email(author)
|
||||
repo.git.update_environment(
|
||||
GIT_COMMITTER_NAME=committer_name,
|
||||
GIT_COMMITTER_EMAIL=committer_email,
|
||||
GIT_AUTHOR_NAME=author_name,
|
||||
GIT_AUTHOR_EMAIL=author_email,
|
||||
)
|
||||
print(f"Configured git committer as '{committer_name} <{committer_email}>'")
|
||||
print(f"Configured git author as '{author_name} <{author_email}>'")
|
||||
|
||||
|
||||
# Get required environment variables
|
||||
github_token = os.environ["GITHUB_TOKEN"]
|
||||
# Get environment variables with defaults
|
||||
path = os.getenv("CPR_PATH", os.getcwd())
|
||||
branch = os.getenv("CPR_BRANCH", DEFAULT_BRANCH)
|
||||
commit_message = os.getenv("CPR_COMMIT_MESSAGE", DEFAULT_COMMIT_MESSAGE)
|
||||
# Get environment variables with a default of 'None'
|
||||
committer = os.environ.get("CPR_COMMITTER")
|
||||
author = os.environ.get("CPR_AUTHOR")
|
||||
base = os.environ.get("CPR_BASE")
|
||||
|
||||
# Set the repo path
|
||||
repo = Repo(path)
|
||||
|
||||
# Determine the GitHub repository from git config
|
||||
# This will be the target repository for the pull request
|
||||
repo_url, protocol, github_repository = get_repository_detail(repo)
|
||||
print(f"Target repository set to {github_repository}")
|
||||
|
||||
if protocol == "HTTPS":
|
||||
print(f"::debug::Using HTTPS protocol")
|
||||
# Encode and configure the basic credential for HTTPS access
|
||||
basic_credential = base64.b64encode(
|
||||
f"x-access-token:{github_token}".encode("utf-8")
|
||||
).decode("utf-8")
|
||||
# Mask the basic credential in logs and debug output
|
||||
print(f"::add-mask::{basic_credential}")
|
||||
repo.git.set_persistent_git_options(
|
||||
c=f"http.https://github.com/.extraheader=AUTHORIZATION: basic {basic_credential}"
|
||||
)
|
||||
|
||||
# Determine if the checked out ref is a valid base for a pull request
|
||||
# The action needs the checked out HEAD ref to be a branch
|
||||
# This check will fail in the following cases:
|
||||
# - HEAD is detached
|
||||
# - HEAD is a merge commit (pull_request events)
|
||||
# - HEAD is a tag
|
||||
try:
|
||||
working_base = repo.git.symbolic_ref("HEAD", "--short")
|
||||
except GitCommandError as e:
|
||||
print(f"::debug::{e.stderr}")
|
||||
print(
|
||||
f"::error::The checked out ref is not a valid base for a pull request. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Exit if the working base is a PR branch created by this action.
|
||||
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
||||
# a PAT allows workflow actions to trigger further events.
|
||||
if working_base.startswith(branch):
|
||||
print(
|
||||
f"::error::Working base branch '{working_base}' was created by this action. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Fetch an optional environment variable to determine the branch suffix
|
||||
branch_suffix = os.environ.get("CPR_BRANCH_SUFFIX")
|
||||
if branch_suffix is not None:
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# Suffix with the short SHA1 hash
|
||||
branch = "{}-{}".format(branch, repo.git.rev_parse("--short", "HEAD"))
|
||||
elif branch_suffix == "timestamp":
|
||||
# Suffix with the current timestamp
|
||||
branch = "{}-{}".format(branch, int(time.time()))
|
||||
elif branch_suffix == "random":
|
||||
# Suffix with a 7 character random string
|
||||
branch = "{}-{}".format(branch, cmn.get_random_string())
|
||||
else:
|
||||
print(
|
||||
f"::error::Branch suffix '{branch_suffix}' is not a valid value. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Output head branch
|
||||
print(f"Pull request branch to create or update set to '{branch}'")
|
||||
|
||||
# Set the committer and author
|
||||
try:
|
||||
set_committer_author(repo, committer, author)
|
||||
except ValueError as e:
|
||||
print(f"::error::{e} " + "Unable to continue. Exiting.")
|
||||
sys.exit(1)
|
||||
|
||||
# Create or update the pull request branch
|
||||
result = coub.create_or_update_branch(repo, repo_url, commit_message, base, branch)
|
||||
|
||||
if result["action"] in ["created", "updated"]:
|
||||
# The branch was created or updated
|
||||
print(f"Pushing pull request branch to 'origin/{branch}'")
|
||||
repo.git.push("--force", repo_url, f"HEAD:refs/heads/{branch}")
|
||||
|
||||
# Set the base. It would have been 'None' if not specified as an input
|
||||
base = result["base"]
|
||||
|
||||
# If there is no longer a diff with the base delete the branch and exit
|
||||
if not result["diff"]:
|
||||
print(f"Branch '{branch}' no longer differs from base branch '{base}'")
|
||||
print(f"Closing pull request and deleting branch '{branch}'")
|
||||
repo.git.push("--delete", "--force", repo_url, f"refs/heads/{branch}")
|
||||
sys.exit()
|
||||
|
||||
# Fetch optional environment variables with default values
|
||||
title = os.getenv("CPR_TITLE", DEFAULT_TITLE)
|
||||
body = os.getenv("CPR_BODY", DEFAULT_BODY)
|
||||
|
||||
# Create or update the pull request
|
||||
coupr.create_or_update_pull_request(
|
||||
github_token,
|
||||
github_repository,
|
||||
branch,
|
||||
base,
|
||||
title,
|
||||
body,
|
||||
os.environ.get("CPR_LABELS"),
|
||||
os.environ.get("CPR_ASSIGNEES"),
|
||||
os.environ.get("CPR_MILESTONE"),
|
||||
os.environ.get("CPR_REVIEWERS"),
|
||||
os.environ.get("CPR_TEAM_REVIEWERS"),
|
||||
os.environ.get("CPR_PROJECT_NAME"),
|
||||
os.environ.get("CPR_PROJECT_COLUMN_NAME"),
|
||||
os.environ.get("CPR_REQUEST_TO_PARENT"),
|
||||
)
|
2
dist/cpr/requirements.txt
vendored
2
dist/cpr/requirements.txt
vendored
@ -1,2 +0,0 @@
|
||||
GitPython==3.1.0
|
||||
PyGithub==1.47
|
63
dist/cpr/test_common.py
vendored
63
dist/cpr/test_common.py
vendored
@ -1,63 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Test Common """
|
||||
import common as cmn
|
||||
import pytest
|
||||
|
||||
|
||||
def test_get_random_string():
|
||||
assert len(cmn.get_random_string()) == 7
|
||||
assert len(cmn.get_random_string(length=20)) == 20
|
||||
|
||||
|
||||
def test_parse_github_repository_success():
|
||||
protocol, repository = cmn.parse_github_repository(
|
||||
"https://github.com/peter-evans/create-pull-request"
|
||||
)
|
||||
assert protocol == "HTTPS"
|
||||
assert repository == "peter-evans/create-pull-request"
|
||||
|
||||
protocol, repository = cmn.parse_github_repository(
|
||||
"git@github.com:peter-evans/create-pull-request.git"
|
||||
)
|
||||
assert protocol == "SSH"
|
||||
assert repository == "peter-evans/create-pull-request"
|
||||
|
||||
|
||||
def test_parse_github_repository_failure():
|
||||
url = "https://github.com/peter-evans"
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
cmn.parse_github_repository(url)
|
||||
assert (
|
||||
e_info.value.args[0]
|
||||
== f"The format of '{url}' is not a valid GitHub repository URL"
|
||||
)
|
||||
|
||||
|
||||
def test_parse_display_name_email_success():
|
||||
name, email = cmn.parse_display_name_email("abc def <abc@def.com>")
|
||||
assert name == "abc def"
|
||||
assert email == "abc@def.com"
|
||||
|
||||
name, email = cmn.parse_display_name_email(
|
||||
"github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
)
|
||||
assert name == "github-actions[bot]"
|
||||
assert email == "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
|
||||
|
||||
def test_parse_display_name_email_failure():
|
||||
display_name_email = "abc@def.com"
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
cmn.parse_display_name_email(display_name_email)
|
||||
assert (
|
||||
e_info.value.args[0]
|
||||
== f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
||||
|
||||
display_name_email = " < >"
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
cmn.parse_display_name_email(display_name_email)
|
||||
assert (
|
||||
e_info.value.args[0]
|
||||
== f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
757
dist/cpr/test_create_or_update_branch.py
vendored
757
dist/cpr/test_create_or_update_branch.py
vendored
@ -1,757 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Test Create or Update Branch """
|
||||
import create_or_update_branch as coub
|
||||
from git import Repo
|
||||
import os
|
||||
import pytest
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# Set git repo
|
||||
REPO_PATH = os.getenv("COUB_REPO_PATH", os.getcwd())
|
||||
repo = Repo(REPO_PATH)
|
||||
|
||||
# Set git environment
|
||||
author_name = "github-actions[bot]"
|
||||
author_email = "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
committer_name = "GitHub"
|
||||
committer_email = "noreply@github.com"
|
||||
repo.git.update_environment(
|
||||
GIT_AUTHOR_NAME=author_name,
|
||||
GIT_AUTHOR_EMAIL=author_email,
|
||||
GIT_COMMITTER_NAME=committer_name,
|
||||
GIT_COMMITTER_EMAIL=committer_email,
|
||||
)
|
||||
|
||||
REPO_URL = repo.git.config("--get", "remote.origin.url")
|
||||
|
||||
TRACKED_FILE = "tracked-file.txt"
|
||||
UNTRACKED_FILE = "untracked-file.txt"
|
||||
|
||||
DEFAULT_BRANCH = "tests/master"
|
||||
NOT_BASE_BRANCH = "tests/branch-that-is-not-the-base"
|
||||
NOT_EXIST_BRANCH = "tests/branch-that-does-not-exist"
|
||||
|
||||
COMMIT_MESSAGE = "[create-pull-request] automated change"
|
||||
BRANCH = "tests/create-pull-request/patch"
|
||||
BASE = DEFAULT_BRANCH
|
||||
|
||||
|
||||
def create_tracked_change(content=None):
|
||||
if content is None:
|
||||
content = str(time.time())
|
||||
# Create a tracked file change
|
||||
with open(os.path.join(REPO_PATH, TRACKED_FILE), "w") as f:
|
||||
f.write(content)
|
||||
return content
|
||||
|
||||
|
||||
def create_untracked_change(content=None):
|
||||
if content is None:
|
||||
content = str(time.time())
|
||||
# Create an untracked file change
|
||||
with open(os.path.join(REPO_PATH, UNTRACKED_FILE), "w") as f:
|
||||
f.write(content)
|
||||
return content
|
||||
|
||||
|
||||
def get_tracked_content():
|
||||
# Read the content of the tracked file
|
||||
with open(os.path.join(REPO_PATH, TRACKED_FILE), "r") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def get_untracked_content():
|
||||
# Read the content of the untracked file
|
||||
with open(os.path.join(REPO_PATH, UNTRACKED_FILE), "r") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def create_changes(tracked_content=None, untracked_content=None):
|
||||
tracked_content = create_tracked_change(tracked_content)
|
||||
untracked_content = create_untracked_change(untracked_content)
|
||||
return tracked_content, untracked_content
|
||||
|
||||
|
||||
def create_commits(number=2, final_tracked_content=None, final_untracked_content=None):
|
||||
for i in range(number):
|
||||
commit_number = i + 1
|
||||
if commit_number == number:
|
||||
tracked_content, untracked_content = create_changes(
|
||||
final_tracked_content, final_untracked_content
|
||||
)
|
||||
else:
|
||||
tracked_content, untracked_content = create_changes()
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m=f"Commit {commit_number}")
|
||||
return tracked_content, untracked_content
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def before_after_all():
|
||||
print("Before all tests")
|
||||
# Check there are no local changes that might be
|
||||
# destroyed by running these tests
|
||||
assert not repo.is_dirty(untracked_files=True)
|
||||
|
||||
# Create a new default branch for the test run
|
||||
repo.remotes.origin.fetch()
|
||||
repo.git.checkout("master")
|
||||
repo.git.checkout("HEAD", b=NOT_BASE_BRANCH)
|
||||
create_tracked_change()
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m="This commit should not appear in pr branches")
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{NOT_BASE_BRANCH}")
|
||||
# Create a new default branch for the test run
|
||||
repo.git.checkout("master")
|
||||
repo.git.checkout("HEAD", b=DEFAULT_BRANCH)
|
||||
create_tracked_change()
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m="Add file to be a tracked file for tests")
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
|
||||
yield
|
||||
|
||||
print("After all tests")
|
||||
repo.git.checkout("master")
|
||||
# Delete the "not base branch" created for the test run
|
||||
repo.git.branch("--delete", "--force", NOT_BASE_BRANCH)
|
||||
repo.git.push("--delete", "--force", REPO_URL, f"refs/heads/{NOT_BASE_BRANCH}")
|
||||
# Delete the default branch created for the test run
|
||||
repo.git.branch("--delete", "--force", DEFAULT_BRANCH)
|
||||
repo.git.push("--delete", "--force", REPO_URL, f"refs/heads/{DEFAULT_BRANCH}")
|
||||
|
||||
|
||||
def before_test():
|
||||
print("Before test")
|
||||
# Checkout the default branch
|
||||
repo.git.checkout(DEFAULT_BRANCH)
|
||||
|
||||
|
||||
def after_test(delete_remote=True):
|
||||
print("After test")
|
||||
# Output git log
|
||||
print(repo.git.log("-5", pretty="oneline"))
|
||||
# Delete the pull request branch if it exists
|
||||
repo.git.checkout(DEFAULT_BRANCH)
|
||||
print(f"Deleting {BRANCH}")
|
||||
for branch in repo.branches:
|
||||
if branch.name == BRANCH:
|
||||
repo.git.branch("--delete", "--force", BRANCH)
|
||||
break
|
||||
if delete_remote:
|
||||
print(f"Deleting origin/{BRANCH}")
|
||||
for ref in repo.remotes.origin.refs:
|
||||
if ref.name == f"origin/{BRANCH}":
|
||||
repo.git.push("--delete", "--force", REPO_URL, f"refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch("--prune")
|
||||
break
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def before_after_tests():
|
||||
before_test()
|
||||
yield
|
||||
after_test()
|
||||
|
||||
|
||||
# Tests if a branch exists and can be fetched
|
||||
def coub_fetch_successful():
|
||||
assert coub.fetch_successful(repo, REPO_URL, NOT_BASE_BRANCH)
|
||||
assert not coub.fetch_successful(repo, REPO_URL, NOT_EXIST_BRANCH)
|
||||
|
||||
|
||||
# Tests no changes resulting in no new branch being created
|
||||
def coub_no_changes_on_create():
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
|
||||
|
||||
# Tests create and update with a tracked file change
|
||||
def coub_tracked_changes():
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
|
||||
# Tests create and update with an untracked file change
|
||||
def coub_untracked_changes():
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with identical changes
|
||||
# The pull request branch will not be updated
|
||||
def coub_identical_changes():
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create identical tracked and untracked file changes
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with commits on the base inbetween
|
||||
def coub_commits_on_base():
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and then an update with no changes
|
||||
# This effectively reverts the branch back to match the base and results in no diff
|
||||
def coub_changes_no_diff():
|
||||
# Save the default branch tracked content
|
||||
default_tracked_content = get_tracked_content()
|
||||
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Running with no update effectively reverts the branch back to match the base
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == default_tracked_content
|
||||
|
||||
|
||||
# Tests create and update with commits on the base inbetween
|
||||
# The changes on base effectively revert the branch back to match the base and results in no diff
|
||||
def coub_commits_on_base_no_diff():
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Create the same tracked and untracked file changes that were made to the base
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with commits on the working base (during the workflow)
|
||||
def coub_commits_on_working_base():
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
def coub_changes_and_commits_on_working_base():
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
# with commits on the base inbetween
|
||||
def coub_changes_and_commits_on_base_and_working_base():
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests no changes resulting in no new branch being created
|
||||
def coub_wbnb_no_changes_on_create():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with a tracked file change
|
||||
def coub_wbnb_tracked_changes():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with an untracked file change
|
||||
def coub_wbnb_untracked_changes():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with identical changes
|
||||
# The pull request branch will not be updated
|
||||
def coub_wbnb_identical_changes():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create identical tracked and untracked file changes
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with commits on the base inbetween
|
||||
def coub_wbnb_commits_on_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and then an update with no changes
|
||||
# This effectively reverts the branch back to match the base and results in no diff
|
||||
def coub_wbnb_changes_no_diff():
|
||||
# Save the default branch tracked content
|
||||
default_tracked_content = get_tracked_content()
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Running with no update effectively reverts the branch back to match the base
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == default_tracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with commits on the base inbetween
|
||||
# The changes on base effectively revert the branch back to match the base and results in no diff
|
||||
# This scenario will cause cherrypick to fail due to an empty commit.
|
||||
# The commit is empty because the changes now exist on the base.
|
||||
def coub_wbnb_commits_on_base_no_diff():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create the same tracked and untracked file changes that were made to the base
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with commits on the working base (during the workflow)
|
||||
def coub_wbnb_commits_on_working_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
def coub_wbnb_changes_and_commits_on_working_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
# with commits on the base inbetween
|
||||
def coub_wbnb_changes_and_commits_on_base_and_working_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# pytest -v -s ~/git/create-pull-request/src
|
||||
|
||||
test_coub_fetch_successful = coub_fetch_successful
|
||||
|
||||
test_coub_no_changes_on_create = coub_no_changes_on_create
|
||||
test_coub_tracked_changes = coub_tracked_changes
|
||||
test_coub_untracked_changes = coub_untracked_changes
|
||||
test_coub_identical_changes = coub_identical_changes
|
||||
test_coub_commits_on_base = coub_commits_on_base
|
||||
|
||||
test_coub_changes_no_diff = coub_changes_no_diff
|
||||
test_coub_commits_on_base_no_diff = coub_commits_on_base_no_diff
|
||||
|
||||
test_coub_commits_on_working_base = coub_commits_on_working_base
|
||||
test_coub_changes_and_commits_on_working_base = coub_changes_and_commits_on_working_base
|
||||
test_coub_changes_and_commits_on_base_and_working_base = (
|
||||
coub_changes_and_commits_on_base_and_working_base
|
||||
)
|
||||
|
||||
# WBNB
|
||||
test_coub_wbnb_no_changes_on_create = coub_wbnb_no_changes_on_create
|
||||
test_coub_wbnb_tracked_changes = coub_wbnb_tracked_changes
|
||||
test_coub_wbnb_untracked_changes = coub_wbnb_untracked_changes
|
||||
test_coub_wbnb_identical_changes = coub_wbnb_identical_changes
|
||||
test_coub_wbnb_commits_on_base = coub_wbnb_commits_on_base
|
||||
|
||||
test_coub_wbnb_changes_no_diff = coub_wbnb_changes_no_diff
|
||||
test_coub_wbnb_commits_on_base_no_diff = coub_wbnb_commits_on_base_no_diff
|
||||
|
||||
test_coub_wbnb_commits_on_working_base = coub_wbnb_commits_on_working_base
|
||||
test_coub_wbnb_changes_and_commits_on_working_base = (
|
||||
coub_wbnb_changes_and_commits_on_working_base
|
||||
)
|
||||
test_coub_wbnb_changes_and_commits_on_base_and_working_base = (
|
||||
coub_wbnb_changes_and_commits_on_base_and_working_base
|
||||
)
|
11697
dist/index.js
vendored
11697
dist/index.js
vendored
File diff suppressed because it is too large
Load Diff
BIN
dist/vendor/Deprecated-1.2.7.tar.gz
vendored
BIN
dist/vendor/Deprecated-1.2.7.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/GitPython-3.1.0.tar.gz
vendored
BIN
dist/vendor/GitPython-3.1.0.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/PyGithub-1.47.tar.gz
vendored
BIN
dist/vendor/PyGithub-1.47.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/PyJWT-1.7.1.tar.gz
vendored
BIN
dist/vendor/PyJWT-1.7.1.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/certifi-2019.11.28.tar.gz
vendored
BIN
dist/vendor/certifi-2019.11.28.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/chardet-3.0.4.tar.gz
vendored
BIN
dist/vendor/chardet-3.0.4.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/gitdb-4.0.2.tar.gz
vendored
BIN
dist/vendor/gitdb-4.0.2.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/idna-2.9.tar.gz
vendored
BIN
dist/vendor/idna-2.9.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/requests-2.23.0.tar.gz
vendored
BIN
dist/vendor/requests-2.23.0.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/smmap-3.0.1.tar.gz
vendored
BIN
dist/vendor/smmap-3.0.1.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/urllib3-1.25.8.tar.gz
vendored
BIN
dist/vendor/urllib3-1.25.8.tar.gz
vendored
Binary file not shown.
BIN
dist/vendor/wrapt-1.12.1.tar.gz
vendored
BIN
dist/vendor/wrapt-1.12.1.tar.gz
vendored
Binary file not shown.
@ -33,8 +33,8 @@
|
||||
master.commit("Last commit on base");
|
||||
const localMaster = gitgraph.branch("<#1> master (local)");
|
||||
localMaster.commit({
|
||||
subject: "<uncommited changes>",
|
||||
body: "Changes made to the local base during the workflow",
|
||||
subject: "<uncommitted changes>",
|
||||
body: "Changes to the local base during the workflow",
|
||||
})
|
||||
const remotePatch = gitgraph.branch("create-pull-request/patch");
|
||||
remotePatch.merge({
|
||||
@ -48,8 +48,8 @@
|
||||
|
||||
const localMaster2 = gitgraph.branch("<#2> master (local)");
|
||||
localMaster2.commit({
|
||||
subject: "<uncommited changes>",
|
||||
body: "Changes made to the updated local base during the workflow",
|
||||
subject: "<uncommitted changes>",
|
||||
body: "Changes to the updated local base during the workflow",
|
||||
})
|
||||
remotePatch.merge({
|
||||
branch: localMaster2,
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 207 KiB After Width: | Height: | Size: 109 KiB |
Binary file not shown.
Before Width: | Height: | Size: 414 KiB After Width: | Height: | Size: 327 KiB |
@ -7,19 +7,20 @@ This document covers terminology, how the action works, general usage guidelines
|
||||
- [How the action works](#how-the-action-works)
|
||||
- [Guidelines](#guidelines)
|
||||
- [Providing a consistent base](#providing-a-consistent-base)
|
||||
- [Pull request events](#pull-request-events)
|
||||
- [Restrictions on forked repositories](#restrictions-on-forked-repositories)
|
||||
- [Events which checkout a commit](#events-which-checkout-a-commit)
|
||||
- [Restrictions on repository forks](#restrictions-on-repository-forks)
|
||||
- [Triggering further workflow runs](#triggering-further-workflow-runs)
|
||||
- [Security](#security)
|
||||
- [Advanced usage](#advanced-usage)
|
||||
- [Creating pull requests in a remote repository](#creating-pull-requests-in-a-remote-repository)
|
||||
- [Push using SSH (deploy keys)](#push-using-ssh-deploy-keys)
|
||||
- [Push pull request branches to a fork](#push-pull-request-branches-to-a-fork)
|
||||
- [Running in a container](#running-in-a-container)
|
||||
- [Creating pull requests on tag push](#creating-pull-requests-on-tag-push)
|
||||
- [Authenticating with GitHub App generated tokens](#authenticating-with-github-app-generated-tokens)
|
||||
- [Running in a container or on self-hosted runners](#running-in-a-container-or-on-self-hosted-runners)
|
||||
|
||||
## Terminology
|
||||
|
||||
[Pull requests](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#about-pull-requests) are proposed changes to a repository branch that can be reviewed by a repository's collaborators before being accepted or rejected.
|
||||
[Pull requests](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#about-pull-requests) are proposed changes to a repository branch that can be reviewed by a repository's collaborators before being accepted or rejected.
|
||||
|
||||
A pull request references two branches:
|
||||
|
||||
@ -28,9 +29,8 @@ A pull request references two branches:
|
||||
|
||||
## Events and checkout
|
||||
|
||||
For each [event type](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows) there is a default `GITHUB_SHA` that will be checked out by the GitHub Actions [checkout](https://github.com/actions/checkout) action.
|
||||
|
||||
The majority of events will default to checking out the "last commit on default branch," which in most cases will be the latest commit on `master`.
|
||||
This action expects repositories to be checked out with the official GitHub Actions [checkout](https://github.com/actions/checkout) action.
|
||||
For each [event type](https://docs.github.com/en/actions/reference/events-that-trigger-workflows) there is a default `GITHUB_SHA` that will be checked out.
|
||||
|
||||
The default can be overridden by specifying a `ref` on checkout.
|
||||
|
||||
@ -42,7 +42,7 @@ The default can be overridden by specifying a `ref` on checkout.
|
||||
|
||||
## How the action works
|
||||
|
||||
By default, the action expects to be executed on the pull request `base`—the branch you intend to modify with the proposed changes.
|
||||
Unless the `base` input is supplied, the action expects the target repository to be checked out on the pull request `base`—the branch you intend to modify with the proposed changes.
|
||||
|
||||
Workflow steps:
|
||||
|
||||
@ -58,11 +58,11 @@ The following git diagram shows how the action creates and updates a pull reques
|
||||
|
||||
### Providing a consistent base
|
||||
|
||||
For the action to work correctly it should be executed in a workflow that checks out a *consistent base* branch. This will be the base of the pull request unless overridden with the `base` input.
|
||||
For the action to work correctly it should be executed in a workflow that checks out a *consistent* base branch. This will be the base of the pull request unless overridden with the `base` input.
|
||||
|
||||
This means your workflow should be consistently checking out the branch that you intend to modify once the PR is merged.
|
||||
|
||||
In the following example, the [`push`](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#push-event-push) and [`create`](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#create-event-create) events both trigger the same workflow. This will cause the checkout action to checkout commits from inconsistent branches. Do *not* do this. It will cause multiple pull requests to be created for each additional `base` the action is executed against.
|
||||
In the following example, the [`push`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push) and [`create`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#create) events both trigger the same workflow. This will cause the checkout action to checkout inconsistent branches and commits. Do *not* do this. It will cause multiple pull requests to be created for each additional `base` the action is executed against.
|
||||
|
||||
```yml
|
||||
on:
|
||||
@ -75,32 +75,48 @@ jobs:
|
||||
- uses: actions/checkout@v2
|
||||
```
|
||||
|
||||
Although rare, there may be use cases where it makes sense to execute the workflow on a branch that is not the base of the pull request. In these cases, the base branch can be specified with the `base` action input. The action will attempt to rebase changes made during the workflow on to the actual base.
|
||||
There may be use cases where it makes sense to execute the workflow on a branch that is not the base of the pull request. In these cases, the base branch can be specified with the `base` action input. The action will attempt to rebase changes made during the workflow on to the actual base.
|
||||
|
||||
### Pull request events
|
||||
### Events which checkout a commit
|
||||
|
||||
Workflows triggered by `pull_request` events will by default check out a [merge commit](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request). To prevent the merge commit being included in created pull requests it is necessary to checkout the `head_ref`.
|
||||
The [default checkout](#events-and-checkout) for the majority of events will leave the repository checked out on a branch.
|
||||
However, some events such as `release` and `pull_request` will leave the repository in a "detached HEAD" state.
|
||||
This is because they checkout a commit, not a branch.
|
||||
In these cases, you *must supply* the `base` input so the action can rebase changes made during the workflow for the pull request.
|
||||
|
||||
Workflows triggered by [`pull_request`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request) events will by default check out a merge commit. Set the `base` input as follows to base the new pull request on the current pull request's branch.
|
||||
|
||||
```yml
|
||||
- uses: actions/checkout@v2
|
||||
- uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
ref: ${{ github.head_ref }}
|
||||
base: ${{ github.head_ref }}
|
||||
```
|
||||
|
||||
### Restrictions on forked repositories
|
||||
Workflows triggered by [`release`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#release) events will by default check out a tag. For most use cases, you will need to set the `base` input to the branch name of the tagged commit.
|
||||
|
||||
GitHub Actions have imposed restrictions on events triggered by a forked repository. For example, the `pull_request` event triggered by a fork opening a pull request in the upstream repository.
|
||||
```yml
|
||||
- uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
base: master
|
||||
```
|
||||
|
||||
- Events from forks cannot access secrets, except for for the default `GITHUB_TOKEN`.
|
||||
### Restrictions on repository forks
|
||||
|
||||
GitHub Actions have imposed restrictions on workflow runs triggered by public repository forks.
|
||||
Private repositories can be configured to [enable workflows](https://docs.github.com/en/github/administering-a-repository/disabling-or-limiting-github-actions-for-a-repository#enabling-workflows-for-private-repository-forks) from forks to run without restriction.
|
||||
|
||||
The restrictions apply to the `pull_request` event triggered by a fork opening a pull request in the upstream repository.
|
||||
|
||||
- Events from forks cannot access secrets, except for the default `GITHUB_TOKEN`.
|
||||
> With the exception of GITHUB_TOKEN, secrets are not passed to the runner when a workflow is triggered from a forked repository.
|
||||
|
||||
[GitHub Actions: Using encrypted secrets in a workflow](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets#using-encrypted-secrets-in-a-workflow)
|
||||
[GitHub Actions: Using encrypted secrets in a workflow](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#using-encrypted-secrets-in-a-workflow)
|
||||
|
||||
- The `GITHUB_TOKEN` has read-only access when an event is triggered by a forked repository.
|
||||
|
||||
[GitHub Actions: Permissions for the GITHUB_TOKEN](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#permissions-for-the-github_token)
|
||||
[GitHub Actions: Permissions for the GITHUB_TOKEN](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#permissions-for-the-github_token)
|
||||
|
||||
These restrictions mean that during a `pull_request` event triggered by a forked repository the action will be unable to commit changes to a branch.
|
||||
These restrictions mean that during a `pull_request` event triggered by a forked repository, actions have no write access to GitHub resources and will fail on any attempt.
|
||||
|
||||
A job condition can be added to prevent workflows from executing when triggered by a repository fork.
|
||||
|
||||
@ -113,31 +129,45 @@ jobs:
|
||||
if: github.event.pull_request.head.repo.full_name == github.repository
|
||||
```
|
||||
|
||||
### Triggering further workflow runs
|
||||
|
||||
Pull requests created by the action using the default `GITHUB_TOKEN` cannot trigger other workflows. If you have `on: pull_request` or `on: push` workflows acting as checks on pull requests, they will not run.
|
||||
|
||||
> When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run.
|
||||
|
||||
[GitHub Actions: Events that trigger workflows](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token)
|
||||
|
||||
#### Workarounds to trigger further workflow runs
|
||||
|
||||
There are a number of workarounds with different pros and cons.
|
||||
|
||||
- Use the default `GITHUB_TOKEN` and allow the action to create pull requests that have no checks enabled. Manually close pull requests and immediately reopen them. This will enable `on: pull_request` workflows to run and be added as checks.
|
||||
|
||||
- Use a `repo` scoped [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) created on an account that has write access to the repository that pull requests are being created in. This is the standard workaround and [recommended by GitHub](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token). However, the PAT cannot be scoped to a specific repository so the token becomes a very sensitive secret. If this is a concern, the PAT can instead be created for a dedicated [machine account](https://docs.github.com/en/github/site-policy/github-terms-of-service#3-account-requirements) that has collaborator access to the repository. Also note that because the account that owns the PAT will be the creator of pull requests, that user account will be unable to perform actions such as request changes or approve the pull request.
|
||||
|
||||
- Use [SSH (deploy keys)](#push-using-ssh-deploy-keys) to push the pull request branch. This is arguably more secure than using a PAT because deploy keys can be set per repository. However, this method will only trigger `on: push` workflows.
|
||||
|
||||
- Use a [machine account that creates pull requests from its own fork](#push-pull-request-branches-to-a-fork). This is the most secure because the PAT created only grants access to the machine account's fork, not the main repository. This method will trigger `on: pull_request` workflows to run. Workflows triggered `on: push` will not run because the push event is in the fork.
|
||||
|
||||
- Use a [GitHub App to generate a token](#authenticating-with-github-app-generated-tokens) that can be used with this action. GitHub App generated tokens are more secure than using a PAT because GitHub App access permissions can be set with finer granularity and are scoped to only repositories where the App is installed. This method will trigger both `on: push` and `on: pull_request` workflows.
|
||||
|
||||
### Security
|
||||
|
||||
From a security perspective it's good practice to fork third-party actions, review the code, and use your fork of the action in workflows.
|
||||
By using third-party actions directly the risk exists that it could be modified to do something malicious, such as capturing secrets.
|
||||
|
||||
This action uses [ncc](https://github.com/zeit/ncc) to compile the Node.js code and dependencies into a single file.
|
||||
Python dependencies are vendored and committed to the repository [here](https://github.com/peter-evans/create-pull-request/tree/master/dist/vendor).
|
||||
No dependencies are downloaded during the action execution.
|
||||
|
||||
Vendored Python dependencies can be reviewed by rebuilding the [dist](https://github.com/peter-evans/create-pull-request/tree/master/dist) directory and redownloading dependencies.
|
||||
The following commands require Node and Python 3.
|
||||
|
||||
```
|
||||
npm install
|
||||
npm run clean
|
||||
npm run package
|
||||
Alternatively, use the action directly and reference the commit hash for the version you want to target.
|
||||
```yml
|
||||
- uses: thirdparty/foo-action@172ec762f2ac8e050062398456fccd30444f8f30
|
||||
```
|
||||
|
||||
The `dist` directory should be rebuilt leaving no git diff.
|
||||
This action uses [ncc](https://github.com/vercel/ncc) to compile the Node.js code and dependencies into a single JavaScript file under the [dist](https://github.com/peter-evans/create-pull-request/tree/master/dist) directory.
|
||||
|
||||
## Advanced usage
|
||||
|
||||
### Creating pull requests in a remote repository
|
||||
|
||||
Checking out a branch from a different repository from where the workflow is executing will make *that repository* the target for the created pull request. In this case, a `repo` scoped [Personal Access Token (PAT)](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) is required.
|
||||
Checking out a branch from a different repository from where the workflow is executing will make *that repository* the target for the created pull request. In this case, a `repo` scoped [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) is required.
|
||||
|
||||
```yml
|
||||
- uses: actions/checkout@v2
|
||||
@ -147,75 +177,108 @@ Checking out a branch from a different repository from where the workflow is exe
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- uses: peter-evans/create-pull-request@v2
|
||||
- uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.PAT }}
|
||||
```
|
||||
|
||||
### Push using SSH (deploy keys)
|
||||
|
||||
[Deploy keys](https://developer.github.com/v3/guides/managing-deploy-keys/#deploy-keys) can be set per repository and so are arguably more secure than using a `repo` scoped [Personal Access Token (PAT)](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line).
|
||||
[Deploy keys](https://developer.github.com/v3/guides/managing-deploy-keys/#deploy-keys) can be set per repository and so are arguably more secure than using a `repo` scoped [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token).
|
||||
Allowing the action to push with a configured deploy key will trigger `on: push` workflows. This makes it an alternative to using a PAT to trigger checks for pull requests.
|
||||
Note that you cannot use deploy keys alone to [create a pull request in a remote repository](#creating-pull-requests-in-a-remote-repository) because then using a PAT would become a requirement. This method only makes sense if creating a pull request in the repository where the workflow is running.
|
||||
|
||||
How to use SSH (deploy keys) with create-pull-request action:
|
||||
|
||||
1. [Create a new SSH key pair](https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key) for your repository. Do not set a passphrase.
|
||||
1. [Create a new SSH key pair](https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key) for your repository. Do not set a passphrase.
|
||||
2. Copy the contents of the public key (.pub file) to a new repository [deploy key](https://developer.github.com/v3/guides/managing-deploy-keys/#deploy-keys) and check the box to "Allow write access."
|
||||
3. Add a secret to the repository containing the entire contents of the private key.
|
||||
4. As shown in the example steps below, use the [`webfactory/ssh-agent`](https://github.com/webfactory/ssh-agent) action to install the private key and clone your repository. Remember to checkout the `base` of your pull request if it's not the default branch, e.g. `git checkout my-branch`.
|
||||
4. As shown in the example below, configure `actions/checkout` to use the deploy key you have created.
|
||||
|
||||
```yml
|
||||
steps:
|
||||
- uses: webfactory/ssh-agent@v0.2.0
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
|
||||
- name: Checkout via SSH
|
||||
run: git clone git@github.com:peter-evans/create-pull-request.git .
|
||||
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
```
|
||||
|
||||
### Push pull request branches to a fork
|
||||
|
||||
To enforce security, you can use a dedicated user using [machine account](https://help.github.com/en/github/site-policy/github-terms-of-service#3-account-requirements).
|
||||
This user has no access to the main repository, it will use their own fork to push code and create the pull request.
|
||||
Instead of pushing pull request branches to the repository you want to update, you can push them to a fork of that repository.
|
||||
This allows you to employ the [principle of least privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege) by using a dedicated user acting as a [machine account](https://docs.github.com/en/github/site-policy/github-terms-of-service#3-account-requirements).
|
||||
This user has no access to the main repository.
|
||||
It will use their own fork to push code and create the pull request.
|
||||
|
||||
1. Create a new github user, then login with this user.
|
||||
2. fork the repository.
|
||||
3. create a [Personal Access Token (PAT)](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line).
|
||||
4. logout and go back to your main user.
|
||||
5. Add a secret to the repository containing the above PAT.
|
||||
6. As shown in the example below, switch the git remote to the fork's url after checkout and set the action input `request-on-parent` to `true`.
|
||||
1. Create a new GitHub user and login.
|
||||
2. Fork the repository that you will be creating pull requests in.
|
||||
3. Create a [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token).
|
||||
4. Logout and log back into your main user account.
|
||||
5. Add a secret to your repository containing the above PAT.
|
||||
6. As shown in the following example workflow, set the `push-to-fork` input to the full repository name of the fork.
|
||||
|
||||
```yaml
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- run: |
|
||||
git config user.password ${{ secrets.PAT }}
|
||||
git remote set-url origin https://github.com/bot-user/fork-project
|
||||
git fetch --unshallow -p origin
|
||||
# Make changes to pull request here
|
||||
|
||||
- uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.MACHINE_USER_PAT }}
|
||||
push-to-fork: machine-user/fork-of-repository
|
||||
```
|
||||
|
||||
### Authenticating with GitHub App generated tokens
|
||||
|
||||
A GitHub App can be created for the sole purpose of generating tokens for use with GitHub actions.
|
||||
These tokens can be used in place of `GITHUB_TOKEN` or a [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token).
|
||||
GitHub App generated tokens are more secure than using a PAT because GitHub App access permissions can be set with finer granularity and are scoped to only repositories where the App is installed.
|
||||
|
||||
1. Create a minimal [GitHub App](https://docs.github.com/en/developers/apps/creating-a-github-app), setting the following fields:
|
||||
|
||||
- Set `GitHub App name`.
|
||||
- Set `Homepage URL` to anything you like, such as your GitHub profile page.
|
||||
- Uncheck `Active` under `Webhook`. You do not need to enter a `Webhook URL`.
|
||||
- Under `Repository permissions: Contents` select `Access: Read & write`.
|
||||
- Under `Repository permissions: Pull requests` select `Access: Read & write`.
|
||||
|
||||
2. Create a Private key from the App settings page and store it securely.
|
||||
|
||||
3. Install the App on any repository where workflows will run requiring tokens.
|
||||
|
||||
4. Set secrets on your repository containing the GitHub App ID, and the private key you created in step 2. e.g. `APP_ID`, `APP_PRIVATE_KEY`.
|
||||
|
||||
5. The following example workflow shows how to use [tibdex/github-app-token](https://github.com/tibdex/github-app-token) to generate a token for use with this action.
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- uses: tibdex/github-app-token@v1
|
||||
id: generate-token
|
||||
with:
|
||||
app_id: ${{ secrets.APP_ID }}
|
||||
private_key: ${{ secrets.APP_PRIVATE_KEY }}
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- uses: peter-evans/create-pull-request@v2
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.PAT }}
|
||||
request-on-parent: true
|
||||
token: ${{ steps.generate-token.outputs.token }}
|
||||
```
|
||||
|
||||
### Running in a container
|
||||
### Running in a container or on self-hosted runners
|
||||
|
||||
This action can be run inside a container by installing the action's dependencies either in the Docker image itself, or during the workflow.
|
||||
This action can be run inside a container, or on [self-hosted runners](https://docs.github.com/en/actions/hosting-your-own-runners), by installing the necessary dependencies.
|
||||
|
||||
The action requires `python3`, `pip3` and `git` to be installed and on the `PATH`.
|
||||
This action requires `git` to be installed and on the `PATH`. Note that `actions/checkout` requires Git 2.18 or higher to be installed, otherwise it will just download the source of the repository instead of cloning it.
|
||||
|
||||
Note that `actions/checkout` requires Git 2.18 or higher to be installed, otherwise it will just download the source of the repository instead of cloning it.
|
||||
The following examples of running in a container show the dependencies being installed during the workflow, but they could also be pre-installed in a custom image.
|
||||
|
||||
**Alpine container example:**
|
||||
```yml
|
||||
@ -226,16 +289,14 @@ jobs:
|
||||
image: alpine
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
run: apk --no-cache add git python3
|
||||
run: apk --no-cache add git
|
||||
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
```
|
||||
|
||||
**Ubuntu container example:**
|
||||
@ -251,77 +312,12 @@ jobs:
|
||||
apt-get update
|
||||
apt-get install -y software-properties-common
|
||||
add-apt-repository -y ppa:git-core/ppa
|
||||
apt-get install -y python3 python3-pip git
|
||||
apt-get install -y git
|
||||
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
```
|
||||
|
||||
### Creating pull requests on tag push
|
||||
|
||||
An `on: push` workflow will also trigger when tags are pushed.
|
||||
During these events, the `actions/checkout` action will check out the `ref/tags/<tag>` git ref by default.
|
||||
This means the repository will *not* be checked out on an active branch.
|
||||
|
||||
If you would like to run `create-pull-request` action on the tagged commit you can achieve this by creating a temporary branch as follows.
|
||||
|
||||
```yml
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
jobs:
|
||||
example:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Create a temporary tag branch
|
||||
run: |
|
||||
git config --global user.name 'GitHub'
|
||||
git config --global user.email 'noreply@github.com'
|
||||
git checkout -b temp-${GITHUB_REF:10}
|
||||
git push --set-upstream origin temp-${GITHUB_REF:10}
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
base: master
|
||||
|
||||
- name: Delete tag branch
|
||||
run: |
|
||||
git push --delete origin temp-${GITHUB_REF:10}
|
||||
```
|
||||
|
||||
This is an alternative, simpler workflow to the one above. However, this is not guaranteed to checkout the tagged commit.
|
||||
There is a chance that in between the tag being pushed and checking out the `master` branch in the workflow, another commit is made to `master`. If that possibility is not a concern, this workflow will work fine.
|
||||
|
||||
```yml
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
jobs:
|
||||
example:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ref: master
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
```
|
||||
|
259
docs/examples.md
259
docs/examples.md
@ -3,9 +3,14 @@
|
||||
- [Use case: Create a pull request to update X on push](#use-case-create-a-pull-request-to-update-x-on-push)
|
||||
- [Update project authors](#update-project-authors)
|
||||
- [Keep a branch up-to-date with another](#keep-a-branch-up-to-date-with-another)
|
||||
- [Use case: Create a pull request to update X on release](#use-case-create-a-pull-request-to-update-x-on-release)
|
||||
- [Update changelog](#update-changelog)
|
||||
- [Use case: Create a pull request to update X periodically](#use-case-create-a-pull-request-to-update-x-periodically)
|
||||
- [Update NPM dependencies](#update-npm-dependencies)
|
||||
- [Update Gradle dependencies](#update-gradle-dependencies)
|
||||
- [Update Cargo dependencies](#update-cargo-dependencies)
|
||||
- [Update SwaggerUI for GitHub Pages](#update-swaggerui-for-github-pages)
|
||||
- [Keep a fork up-to-date with its upstream](#keep-a-fork-up-to-date-with-its-upstream)
|
||||
- [Spider and download a website](#spider-and-download-a-website)
|
||||
- [Use case: Create a pull request to update X by calling the GitHub API](#use-case-create-a-pull-request-to-update-x-by-calling-the-github-api)
|
||||
- [Call the GitHub API from an external service](#call-the-github-api-from-an-external-service)
|
||||
@ -15,6 +20,7 @@
|
||||
- [Misc workflow tips](#misc-workflow-tips)
|
||||
- [Filtering push events](#filtering-push-events)
|
||||
- [Dynamic configuration using variables](#dynamic-configuration-using-variables)
|
||||
- [Setting the pull request body from a file](#setting-the-pull-request-body-from-a-file)
|
||||
- [Debugging GitHub Actions](#debugging-github-actions)
|
||||
|
||||
|
||||
@ -43,9 +49,8 @@ jobs:
|
||||
run: |
|
||||
git log --format='%aN <%aE>%n%cN <%cE>' | sort -u > AUTHORS
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: update authors
|
||||
title: Update AUTHORS
|
||||
body: Credit new contributors by updating AUTHORS
|
||||
@ -76,45 +81,185 @@ jobs:
|
||||
git fetch origin master:master
|
||||
git reset --hard master
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
branch: production-promotion
|
||||
```
|
||||
|
||||
## Use case: Create a pull request to update X on release
|
||||
|
||||
This pattern will work well for updating any kind of static content based on the tagged commit of a release. Note that because `release` is one of the [events which checkout a commit](concepts-guidelines.md#events-which-checkout-a-commit) it is necessary to supply the `base` input to the action.
|
||||
|
||||
### Update changelog
|
||||
|
||||
Raises a pull request to update the `CHANGELOG.md` file based on the tagged commit of the release.
|
||||
Note that [git-chglog](https://github.com/git-chglog/git-chglog/) requires some configuration files to exist in the repository before this workflow will work.
|
||||
|
||||
This workflow assumes the tagged release was made on a default branch called `master`.
|
||||
|
||||
```yml
|
||||
name: Update Changelog
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
jobs:
|
||||
updateChangelog:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Update Changelog
|
||||
run: |
|
||||
curl -o git-chglog -L https://github.com/git-chglog/git-chglog/releases/download/0.9.1/git-chglog_linux_amd64
|
||||
chmod u+x git-chglog
|
||||
./git-chglog -o CHANGELOG.md
|
||||
rm git-chglog
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
commit-message: update changelog
|
||||
title: Update Changelog
|
||||
body: Update changelog to reflect release changes
|
||||
branch: update-changelog
|
||||
base: master
|
||||
```
|
||||
|
||||
## Use case: Create a pull request to update X periodically
|
||||
|
||||
This pattern will work well for updating any kind of static content from an external source. The workflow executes on a schedule and raises a pull request when there are changes.
|
||||
|
||||
### Update NPM dependencies
|
||||
|
||||
This workflow will create a pull request for npm dependencies.
|
||||
It works best in combination with a build workflow triggered on `push` and `pull_request`.
|
||||
A [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) can be used in order for the creation of the pull request to trigger further workflows. See the [documentation here](concepts-guidelines.md#triggering-further-workflow-runs) for further details.
|
||||
|
||||
```yml
|
||||
name: Update Dependencies
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 10 * * 1'
|
||||
jobs:
|
||||
update-deps:
|
||||
update-dep:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: '10.x'
|
||||
node-version: '12.x'
|
||||
- name: Update dependencies
|
||||
id: vars
|
||||
run: |
|
||||
npm install -g npm-check-updates
|
||||
ncu -u
|
||||
npx -p npm-check-updates ncu -u
|
||||
npm install
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: update dependencies
|
||||
title: Automated Dependency Updates
|
||||
body: This is an auto-generated PR with dependency updates.
|
||||
branch: dep-updates
|
||||
token: ${{ secrets.PAT }}
|
||||
commit-message: Update dependencies
|
||||
title: Update dependencies
|
||||
body: |
|
||||
- Dependency updates
|
||||
|
||||
Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
branch: update-dependencies
|
||||
```
|
||||
|
||||
The above workflow works best in combination with a build workflow triggered on `push` and `pull_request`.
|
||||
|
||||
```yml
|
||||
name: CI
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
branches: [master]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: 12.x
|
||||
- run: npm ci
|
||||
- run: npm run test
|
||||
- run: npm run build
|
||||
```
|
||||
|
||||
### Update Gradle dependencies
|
||||
|
||||
The following workflow will create a pull request for Gradle dependencies.
|
||||
It requires first configuring your project to use Gradle lockfiles.
|
||||
See [here](https://github.com/peter-evans/gradle-auto-dependency-updates) for how to configure your project and use the following workflow.
|
||||
|
||||
```yml
|
||||
name: Update Dependencies
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 1 * * 1'
|
||||
jobs:
|
||||
update-dep:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
- name: Grant execute permission for gradlew
|
||||
run: chmod +x gradlew
|
||||
- name: Perform dependency resolution and write new lockfiles
|
||||
run: ./gradlew dependencies --write-locks
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.PAT }}
|
||||
commit-message: Update dependencies
|
||||
title: Update dependencies
|
||||
body: |
|
||||
- Dependency updates
|
||||
|
||||
Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
branch: update-dependencies
|
||||
```
|
||||
|
||||
### Update Cargo dependencies
|
||||
|
||||
The following workflow will create a pull request for Cargo dependencies.
|
||||
It optionally uses [`cargo-edit`](https://github.com/killercup/cargo-edit) to update `Cargo.toml` and keep it in sync with `Cargo.lock`.
|
||||
|
||||
```yml
|
||||
name: Update Dependencies
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 1 * * 1'
|
||||
jobs:
|
||||
update-dep:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Update dependencies
|
||||
run: |
|
||||
cargo install cargo-edit
|
||||
cargo update
|
||||
cargo upgrade --to-lockfile
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.PAT }}
|
||||
commit-message: Update dependencies
|
||||
title: Update dependencies
|
||||
body: |
|
||||
- Dependency updates
|
||||
|
||||
Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
branch: update-dependencies
|
||||
```
|
||||
|
||||
### Update SwaggerUI for GitHub Pages
|
||||
@ -159,9 +304,8 @@ jobs:
|
||||
# Update current release
|
||||
echo ${{ steps.swagger-ui.outputs.release_tag }} > swagger-ui.version
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Update swagger-ui to ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
title: Update SwaggerUI to ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
body: |
|
||||
@ -175,6 +319,41 @@ jobs:
|
||||
branch: swagger-ui-updates
|
||||
```
|
||||
|
||||
### Keep a fork up-to-date with its upstream
|
||||
|
||||
This example is designed to be run in a seperate repository from the fork repository itself.
|
||||
The aim of this is to prevent committing anything to the fork's default branch would cause it to differ from the upstream.
|
||||
|
||||
In the following example workflow, `owner/repo` is the upstream repository and `fork-owner/repo` is the fork. It assumes the default branch of the upstream repository is called `master`.
|
||||
|
||||
The [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) should have `repo` scope. Additionally, if the upstream makes changes to the `.github/workflows` directory, the action will be unable to push the changes to a branch and throw the error "_(refusing to allow a GitHub App to create or update workflow `.github/workflows/xxx.yml` without `workflows` permission)_". To allow these changes to be pushed to the fork, add the `workflow` scope to the PAT. Of course, allowing this comes with the risk that the workflow changes from the upstream could run and do something unexpected. Disabling GitHub Actions in the fork is highly recommended to prevent this.
|
||||
|
||||
When you merge the pull request make sure to choose the [`Rebase and merge`](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-request-merges#rebase-and-merge-your-pull-request-commits) option. This will make the fork's commits match the commits on the upstream.
|
||||
|
||||
```yml
|
||||
name: Update fork
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 0 * * 0'
|
||||
jobs:
|
||||
updateFork:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
repository: fork-owner/repo
|
||||
- name: Reset the default branch with upstream changes
|
||||
run: |
|
||||
git remote add upstream https://github.com/owner/repo.git
|
||||
git fetch upstream master:upstream-master
|
||||
git reset --hard upstream-master
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.PAT }}
|
||||
branch: upstream-changes
|
||||
```
|
||||
|
||||
### Spider and download a website
|
||||
|
||||
This workflow spiders a website and downloads the content. Any changes to the website will be raised in a pull request.
|
||||
@ -202,9 +381,8 @@ jobs:
|
||||
--domains quotes.toscrape.com \
|
||||
http://quotes.toscrape.com/
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: update local website copy
|
||||
title: Automated Updates to Local Website Copy
|
||||
body: This is an auto-generated PR with website updates.
|
||||
@ -213,7 +391,7 @@ jobs:
|
||||
|
||||
## Use case: Create a pull request to update X by calling the GitHub API
|
||||
|
||||
You can use the GitHub API to trigger a webhook event called [`repository_dispatch`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#external-events-repository_dispatch) when you want to trigger a workflow for activity that happens outside of GitHub.
|
||||
You can use the GitHub API to trigger a webhook event called [`repository_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#repository_dispatch) when you want to trigger a workflow for any activity that happens outside of GitHub.
|
||||
This pattern will work well for updating any kind of static content from an external source.
|
||||
|
||||
You can modify any of the examples in the previous section to work in this fashion.
|
||||
@ -231,7 +409,7 @@ on:
|
||||
An `on: repository_dispatch` workflow can be triggered by a call to the GitHub API as follows.
|
||||
|
||||
- `[username]` is a GitHub username
|
||||
- `[token]` is a `repo` scoped [Personal Access Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)
|
||||
- `[token]` is a `repo` scoped [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)
|
||||
- `[repository]` is the name of the repository the workflow resides in.
|
||||
|
||||
```
|
||||
@ -248,7 +426,7 @@ An `on: repository_dispatch` workflow can be triggered from another workflow wit
|
||||
|
||||
```yml
|
||||
- name: Repository Dispatch
|
||||
uses: peter-evans/repository-dispatch@v1.0.0
|
||||
uses: peter-evans/repository-dispatch@v1
|
||||
with:
|
||||
token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
repository: username/my-repo
|
||||
@ -262,7 +440,8 @@ An `on: repository_dispatch` workflow can be triggered from another workflow wit
|
||||
|
||||
This is a pattern that lends itself to automated code linting and fixing. A pull request can be created to fix or modify something during an `on: pull_request` workflow. The pull request containing the fix will be raised with the original pull request as the base. This can be then be merged to update the original pull request and pass any required tests.
|
||||
|
||||
Note that due to [limitations on forked repositories](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#permissions-for-the-github_token) workflows for this use case do not work for pull requests raised from forks.
|
||||
Note that due to [token restrictions on public repository forks](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#permissions-for-the-github_token), workflows for this use case do not work for pull requests raised from forks.
|
||||
Private repositories can be configured to [enable workflows](https://docs.github.com/en/github/administering-a-repository/disabling-or-limiting-github-actions-for-a-repository#enabling-workflows-for-private-repository-forks) from forks to run without restriction.
|
||||
|
||||
### autopep8
|
||||
|
||||
@ -297,9 +476,8 @@ jobs:
|
||||
run: echo ::set-output name=branch-name::"autopep8-patches/${{ github.head_ref }}"
|
||||
- name: Create Pull Request
|
||||
if: steps.autopep8.outputs.exit-code == 2
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: autopep8 action fixes
|
||||
title: Fixes by autopep8 action
|
||||
body: This is an auto-generated PR with fixes by autopep8.
|
||||
@ -346,7 +524,7 @@ jobs:
|
||||
|
||||
The following examples show how configuration for the action can be dynamically defined in a previous workflow step.
|
||||
|
||||
The recommended method is to use [`set-output`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-output-parameter-set-output). Note that the step where output variables are defined must have an id.
|
||||
The recommended method is to use [`set-output`](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-output-parameter). Note that the step where output variables are defined must have an id.
|
||||
|
||||
```yml
|
||||
- name: Set output variables
|
||||
@ -356,39 +534,42 @@ The recommended method is to use [`set-output`](https://help.github.com/en/githu
|
||||
echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \
|
||||
by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ steps.vars.outputs.pr_title }}
|
||||
body: ${{ steps.vars.outputs.pr_body }}
|
||||
```
|
||||
|
||||
Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env) can be used to create environment variables.
|
||||
### Setting the pull request body from a file
|
||||
|
||||
This example shows how file content can be read into a variable and passed to the action.
|
||||
The content must be [escaped to preserve newlines](https://github.community/t/set-output-truncates-multiline-strings/16852/3).
|
||||
|
||||
```yml
|
||||
- name: Set environment variables
|
||||
- id: get-pr-body
|
||||
run: |
|
||||
echo ::set-env name=PULL_REQUEST_TITLE::"[Test] Add report file $(date +%d-%m-%Y)"
|
||||
echo ::set-env name=PULL_REQUEST_BODY::"This PR was auto-generated on $(date +%d-%m-%Y) \
|
||||
by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
|
||||
body=$(cat pr-body.txt)
|
||||
body="${body//'%'/'%25'}"
|
||||
body="${body//$'\n'/'%0A'}"
|
||||
body="${body//$'\r'/'%0D'}"
|
||||
echo ::set-output name=body::$body
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v2
|
||||
uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ env.PULL_REQUEST_TITLE }}
|
||||
body: ${{ env.PULL_REQUEST_BODY }}
|
||||
body: ${{ steps.get-pr-body.outputs.body }}
|
||||
```
|
||||
|
||||
### Debugging GitHub Actions
|
||||
|
||||
#### Runner Diagnostic Logging
|
||||
|
||||
[Runner diagnostic logging](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/managing-a-workflow-run#enabling-runner-diagnostic-logging) provides additional log files that contain information about how a runner is executing an action.
|
||||
[Runner diagnostic logging](https://docs.github.com/en/actions/configuring-and-managing-workflows/managing-a-workflow-run#enabling-runner-diagnostic-logging) provides additional log files that contain information about how a runner is executing an action.
|
||||
To enable runner diagnostic logging, set the secret `ACTIONS_RUNNER_DEBUG` to `true` in the repository that contains the workflow.
|
||||
|
||||
#### Step Debug Logging
|
||||
|
||||
[Step debug logging](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/managing-a-workflow-run#enabling-step-debug-logging) increases the verbosity of a job's logs during and after a job's execution.
|
||||
[Step debug logging](https://docs.github.com/en/actions/configuring-and-managing-workflows/managing-a-workflow-run#enabling-step-debug-logging) increases the verbosity of a job's logs during and after a job's execution.
|
||||
To enable step debug logging set the secret `ACTIONS_STEP_DEBUG` to `true` in the repository that contains the workflow.
|
||||
|
||||
#### Output Various Contexts
|
||||
|
@ -1,24 +1,72 @@
|
||||
# Updating from `v1` to `v2`
|
||||
## Updating from `v2` to `v3`
|
||||
|
||||
## Breaking changes
|
||||
### Breaking changes
|
||||
|
||||
- The `author` input now defaults to the user who triggered the workflow run. This default is set via [action.yml](../action.yml) as `${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>`, where `github.actor` is the GitHub user account associated with the run. For example, `peter-evans <peter-evans@users.noreply.github.com>`.
|
||||
|
||||
To continue to use the `v2` default, set the `author` input as follows.
|
||||
```yaml
|
||||
- uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
||||
```
|
||||
|
||||
- The `author` and `committer` inputs are no longer cross-used if only one is supplied. Additionally, when neither input is set, the `author` and `committer` are no longer determined from an existing identity set in git config. In both cases, the inputs will fall back to their default set in [action.yml](../action.yml).
|
||||
|
||||
- Deprecated inputs `project` and `project-column` have been removed in favour of an additional action step. See [Create a project card](https://github.com/peter-evans/create-pull-request#create-a-project-card) for details.
|
||||
|
||||
- Deprecated output `pr_number` has been removed in favour of `pull-request-number`.
|
||||
|
||||
- Input `request-to-parent` has been removed in favour of `push-to-fork`. This greatly simplifies pushing the pull request branch to a fork of the parent repository. See [Push pull request branches to a fork](concepts-guidelines.md#push-pull-request-branches-to-a-fork) for details.
|
||||
|
||||
e.g.
|
||||
```yaml
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
# Make changes to pull request here
|
||||
|
||||
- uses: peter-evans/create-pull-request@v3
|
||||
with:
|
||||
token: ${{ secrets.MACHINE_USER_PAT }}
|
||||
push-to-fork: machine-user/fork-of-repository
|
||||
```
|
||||
|
||||
### New features
|
||||
|
||||
- The action has been converted to Typescript giving it a significant performance improvement.
|
||||
|
||||
- If you run this action in a container, or on [self-hosted runners](https://docs.github.com/en/actions/hosting-your-own-runners), `python` and `pip` are no longer required dependencies. See [Running in a container or on self-hosted runners](concepts-guidelines.md#running-in-a-container-or-on-self-hosted-runners) for details.
|
||||
|
||||
- Inputs `labels`, `assignees`, `reviewers` and `team-reviewers` can now be newline separated, or comma separated.
|
||||
e.g.
|
||||
```yml
|
||||
labels: |
|
||||
chore
|
||||
dependencies
|
||||
automated
|
||||
```
|
||||
|
||||
## Updating from `v1` to `v2`
|
||||
|
||||
### Breaking changes
|
||||
|
||||
- `v2` now expects repositories to be checked out with `actions/checkout@v2`
|
||||
|
||||
To use `actions/checkout@v1` the following step to checkout the branch is necessary.
|
||||
```
|
||||
```yml
|
||||
- uses: actions/checkout@v1
|
||||
- name: Checkout branch
|
||||
run: git checkout "${GITHUB_REF:11}"
|
||||
```
|
||||
|
||||
- The two branch naming strategies have been swapped. Fixed branch naming strategy is now the default. i.e. `branch-suffix: none` is now the default and should be removed from configuration if set.
|
||||
- The two branch naming strategies have been swapped. Fixed-branch naming strategy is now the default. i.e. `branch-suffix: none` is now the default and should be removed from configuration if set.
|
||||
|
||||
- `author-name`, `author-email`, `committer-name`, `committer-email` have been removed in favour of `author` and `committer`.
|
||||
They can both be set in the format `Display Name <email@address.com>`
|
||||
|
||||
If neither `author` or `committer` are set the action will default to making commits as the GitHub Actions bot user.
|
||||
|
||||
## New features
|
||||
### New features
|
||||
|
||||
- Unpushed commits made during the workflow before the action runs will now be considered as changes to be raised in the pull request. See [Controlling commits](https://github.com/peter-evans/create-pull-request#controlling-commits) for details.
|
||||
- New commits made to the pull request base will now be taken into account when pull requests are updated.
|
||||
|
@ -1,3 +1,11 @@
|
||||
process.env = Object.assign(process.env, {
|
||||
GITHUB_WORKSPACE: __dirname
|
||||
});
|
||||
module.exports = {
|
||||
clearMocks: true,
|
||||
moduleFileExtensions: ['js', 'ts'],
|
||||
testEnvironment: 'node',
|
||||
testMatch: ['**/*.test.ts'],
|
||||
testRunner: 'jest-circus/runner',
|
||||
transform: {
|
||||
'^.+\\.ts$': 'ts-jest'
|
||||
},
|
||||
verbose: true
|
||||
}
|
||||
|
5928
package-lock.json
generated
5928
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
50
package.json
50
package.json
@ -1,21 +1,27 @@
|
||||
{
|
||||
"name": "create-pull-request",
|
||||
"version": "2.0.0",
|
||||
"version": "3.0.0",
|
||||
"private": true,
|
||||
"description": "Creates a pull request for changes to your repository in the actions workspace",
|
||||
"main": "index.js",
|
||||
"main": "lib/main.js",
|
||||
"scripts": {
|
||||
"clean": "rm -rf dist",
|
||||
"lint": "eslint src/index.js",
|
||||
"test": "eslint src/index.js && jest",
|
||||
"build": "ncc build src/index.js -o dist",
|
||||
"vendor-deps": "pip download -r src/cpr/requirements.txt --no-binary=:all: -d dist/vendor",
|
||||
"package": "npm run build && npm run vendor-deps"
|
||||
"build": "tsc && ncc build",
|
||||
"format": "prettier --write '**/*.ts'",
|
||||
"format-check": "prettier --check '**/*.ts'",
|
||||
"lint": "eslint src/**/*.ts",
|
||||
"test:unit": "jest unit",
|
||||
"test:int": "__test__/integration-tests.sh",
|
||||
"test": "npm run test:unit && npm run test:int"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/peter-evans/create-pull-request.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"keywords": [
|
||||
"actions",
|
||||
"pull",
|
||||
"request"
|
||||
],
|
||||
"author": "Peter Evans",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
@ -23,14 +29,26 @@
|
||||
},
|
||||
"homepage": "https://github.com/peter-evans/create-pull-request",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.1.1",
|
||||
"@actions/exec": "^1.0.1",
|
||||
"@actions/tool-cache": "^1.1.2",
|
||||
"is-docker": "^2.0.0"
|
||||
"@actions/core": "1.2.6",
|
||||
"@actions/exec": "1.0.4",
|
||||
"@octokit/core": "3.1.2",
|
||||
"@octokit/plugin-paginate-rest": "2.4.0",
|
||||
"@octokit/plugin-rest-endpoint-methods": "4.2.0",
|
||||
"uuid": "8.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@zeit/ncc": "0.22.0",
|
||||
"eslint": "6.8.0",
|
||||
"jest": "25.2.3"
|
||||
"@types/jest": "26.0.14",
|
||||
"@types/node": "14.10.3",
|
||||
"@typescript-eslint/parser": "4.1.1",
|
||||
"@vercel/ncc": "0.24.1",
|
||||
"eslint": "7.9.0",
|
||||
"eslint-plugin-github": "4.1.1",
|
||||
"eslint-plugin-jest": "24.0.1",
|
||||
"jest": "26.4.2",
|
||||
"jest-circus": "26.4.2",
|
||||
"js-yaml": "3.14.0",
|
||||
"prettier": "2.1.2",
|
||||
"ts-jest": "26.3.0",
|
||||
"typescript": "4.0.2"
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
]
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
|
||||
|
||||
def get_random_string(length=7, chars=string.ascii_lowercase + string.digits):
|
||||
return "".join(random.choice(chars) for _ in range(length))
|
||||
|
||||
|
||||
def parse_github_repository(url):
|
||||
# Parse the protocol and github repository from a URL
|
||||
# e.g. HTTPS, peter-evans/create-pull-request
|
||||
https_pattern = re.compile(r"^https://github.com/(.+/.+)$")
|
||||
ssh_pattern = re.compile(r"^git@github.com:(.+/.+).git$")
|
||||
|
||||
match = https_pattern.match(url)
|
||||
if match is not None:
|
||||
return "HTTPS", match.group(1)
|
||||
|
||||
match = ssh_pattern.match(url)
|
||||
if match is not None:
|
||||
return "SSH", match.group(1)
|
||||
|
||||
raise ValueError(f"The format of '{url}' is not a valid GitHub repository URL")
|
||||
|
||||
|
||||
def parse_display_name_email(display_name_email):
|
||||
# Parse the name and email address from a string in the following format
|
||||
# Display Name <email@address.com>
|
||||
pattern = re.compile(r"^([^<]+)\s*<([^>]+)>$")
|
||||
|
||||
# Check we have a match
|
||||
match = pattern.match(display_name_email)
|
||||
if match is None:
|
||||
raise ValueError(
|
||||
f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
||||
|
||||
# Check that name and email are not just whitespace
|
||||
name = match.group(1).strip()
|
||||
email = match.group(2).strip()
|
||||
if len(name) == 0 or len(email) == 0:
|
||||
raise ValueError(
|
||||
f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
||||
|
||||
return name, email
|
@ -1,145 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create or Update Branch """
|
||||
import common as cmn
|
||||
from git import Repo, GitCommandError
|
||||
import os
|
||||
|
||||
|
||||
CHERRYPICK_EMPTY = (
|
||||
"The previous cherry-pick is now empty, possibly due to conflict resolution."
|
||||
)
|
||||
|
||||
|
||||
def fetch_successful(repo, repo_url, branch):
|
||||
try:
|
||||
repo.git.fetch(repo_url, f"{branch}:refs/remotes/origin/{branch}")
|
||||
except GitCommandError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_ahead(repo, branch_1, branch_2):
|
||||
# Return true if branch_2 is ahead of branch_1
|
||||
return (
|
||||
int(repo.git.rev_list("--right-only", "--count", f"{branch_1}...{branch_2}"))
|
||||
> 0
|
||||
)
|
||||
|
||||
|
||||
def is_behind(repo, branch_1, branch_2):
|
||||
# Return true if branch_2 is behind branch_1
|
||||
return (
|
||||
int(repo.git.rev_list("--left-only", "--count", f"{branch_1}...{branch_2}")) > 0
|
||||
)
|
||||
|
||||
|
||||
def is_even(repo, branch_1, branch_2):
|
||||
# Return true if branch_2 is even with branch_1
|
||||
return not is_ahead(repo, branch_1, branch_2) and not is_behind(
|
||||
repo, branch_1, branch_2
|
||||
)
|
||||
|
||||
|
||||
def has_diff(repo, branch_1, branch_2):
|
||||
diff = repo.git.diff(f"{branch_1}..{branch_2}")
|
||||
return len(diff) > 0
|
||||
|
||||
|
||||
def create_or_update_branch(repo, repo_url, commit_message, base, branch):
|
||||
# Set the default return values
|
||||
action = "none"
|
||||
diff = False
|
||||
|
||||
# Get the working base. This may or may not be the actual base.
|
||||
working_base = repo.git.symbolic_ref("HEAD", "--short")
|
||||
# If the base is not specified it is assumed to be the working base
|
||||
if base is None:
|
||||
base = working_base
|
||||
|
||||
# Save the working base changes to a temporary branch
|
||||
temp_branch = cmn.get_random_string(length=20)
|
||||
repo.git.checkout("HEAD", b=temp_branch)
|
||||
# Commit any uncomitted changes
|
||||
if repo.is_dirty(untracked_files=True):
|
||||
print(f"Uncommitted changes found. Adding a commit.")
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m=commit_message)
|
||||
|
||||
# Perform fetch and reset the working base
|
||||
# Commits made during the workflow will be removed
|
||||
repo.git.fetch("--force", repo_url, f"{working_base}:{working_base}")
|
||||
|
||||
# If the working base is not the base, rebase the temp branch commits
|
||||
if working_base != base:
|
||||
print(
|
||||
f"Rebasing commits made to branch '{working_base}' on to base branch '{base}'"
|
||||
)
|
||||
# Checkout the actual base
|
||||
repo.git.fetch("--force", repo_url, f"{base}:{base}")
|
||||
repo.git.checkout(base)
|
||||
# Cherrypick commits from the temporary branch starting from the working base
|
||||
commits = repo.git.rev_list("--reverse", f"{working_base}..{temp_branch}", ".")
|
||||
for commit in commits.splitlines():
|
||||
try:
|
||||
repo.git.cherry_pick(
|
||||
"--strategy",
|
||||
"recursive",
|
||||
"--strategy-option",
|
||||
"theirs",
|
||||
f"{commit}",
|
||||
)
|
||||
except GitCommandError as e:
|
||||
if CHERRYPICK_EMPTY not in e.stderr:
|
||||
print("Unexpected error: ", e)
|
||||
raise
|
||||
# Reset the temp branch to the working index
|
||||
repo.git.checkout("-B", temp_branch, "HEAD")
|
||||
# Reset the base
|
||||
repo.git.fetch("--force", repo_url, f"{base}:{base}")
|
||||
|
||||
# Try to fetch the pull request branch
|
||||
if not fetch_successful(repo, repo_url, branch):
|
||||
# The pull request branch does not exist
|
||||
print(f"Pull request branch '{branch}' does not exist yet")
|
||||
# Create the pull request branch
|
||||
repo.git.checkout("HEAD", b=branch)
|
||||
# Check if the pull request branch is ahead of the base
|
||||
diff = is_ahead(repo, base, branch)
|
||||
if diff:
|
||||
action = "created"
|
||||
print(f"Created branch '{branch}'")
|
||||
else:
|
||||
print(
|
||||
f"Branch '{branch}' is not ahead of base '{base}' and will not be created"
|
||||
)
|
||||
else:
|
||||
# The pull request branch exists
|
||||
print(
|
||||
f"Pull request branch '{branch}' already exists as remote branch 'origin/{branch}'"
|
||||
)
|
||||
# Checkout the pull request branch
|
||||
repo.git.checkout(branch)
|
||||
|
||||
if has_diff(repo, branch, temp_branch):
|
||||
# If the branch differs from the recreated temp version then the branch is reset
|
||||
# For changes on base this action is similar to a rebase of the pull request branch
|
||||
print(f"Resetting '{branch}'")
|
||||
repo.git.checkout("-B", branch, temp_branch)
|
||||
# repo.git.switch("-C", branch, temp_branch)
|
||||
|
||||
# Check if the pull request branch has been updated
|
||||
# If the branch was reset or updated it will be ahead
|
||||
# It may be behind if a reset now results in no diff with the base
|
||||
if not is_even(repo, f"origin/{branch}", branch):
|
||||
action = "updated"
|
||||
print(f"Updated branch '{branch}'")
|
||||
else:
|
||||
print(f"Branch '{branch}' is even with its remote and will not be updated")
|
||||
|
||||
# Check if the pull request branch is ahead of the base
|
||||
diff = is_ahead(repo, base, branch)
|
||||
|
||||
# Delete the temporary branch
|
||||
repo.git.branch("--delete", "--force", temp_branch)
|
||||
|
||||
return {"action": action, "diff": diff, "base": base}
|
@ -1,140 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create or Update Pull Request """
|
||||
from github import Github, GithubException
|
||||
import os
|
||||
|
||||
|
||||
def cs_string_to_list(str):
|
||||
# Split the comma separated string into a list
|
||||
l = [i.strip() for i in str.split(",")]
|
||||
# Remove empty strings
|
||||
return list(filter(None, l))
|
||||
|
||||
|
||||
def create_project_card(github_repo, project_name, project_column_name, pull_request):
|
||||
# Locate the project by name
|
||||
project = None
|
||||
for project_item in github_repo.get_projects("all"):
|
||||
if project_item.name == project_name:
|
||||
project = project_item
|
||||
break
|
||||
|
||||
if not project:
|
||||
print("::error::Project not found. Unable to create project card.")
|
||||
return
|
||||
|
||||
# Locate the column by name
|
||||
column = None
|
||||
for column_item in project.get_columns():
|
||||
if column_item.name == project_column_name:
|
||||
column = column_item
|
||||
break
|
||||
|
||||
if not column:
|
||||
print("::error::Project column not found. Unable to create project card.")
|
||||
return
|
||||
|
||||
# Create a project card for the pull request
|
||||
column.create_card(content_id=pull_request.id, content_type="PullRequest")
|
||||
print(
|
||||
"Added pull request #%d to project '%s' under column '%s'"
|
||||
% (pull_request.number, project.name, column.name)
|
||||
)
|
||||
|
||||
|
||||
def create_or_update_pull_request(
|
||||
github_token,
|
||||
github_repository,
|
||||
branch,
|
||||
base,
|
||||
title,
|
||||
body,
|
||||
labels,
|
||||
assignees,
|
||||
milestone,
|
||||
reviewers,
|
||||
team_reviewers,
|
||||
project_name,
|
||||
project_column_name,
|
||||
request_to_parent,
|
||||
):
|
||||
if request_to_parent is None:
|
||||
request_to_parent = False
|
||||
else:
|
||||
request_to_parent = request_to_parent.lower() in ['true', '1', 't', 'y', 'yes', 'on']
|
||||
|
||||
github_repo = head_repo = Github(github_token).get_repo(github_repository)
|
||||
if request_to_parent:
|
||||
github_repo = github_repo.parent
|
||||
if github_repo is None:
|
||||
raise ValueError("The checked out repository is not a fork. Input 'request-to-parent' should be set to false.")
|
||||
|
||||
head_branch = f"{head_repo.owner.login}:{branch}"
|
||||
|
||||
# Create the pull request
|
||||
try:
|
||||
pull_request = github_repo.create_pull(
|
||||
title=title, body=body, base=base, head=head_branch
|
||||
)
|
||||
print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
|
||||
except GithubException as e:
|
||||
if e.status == 422:
|
||||
# A pull request exists for this branch and base
|
||||
# Get the pull request
|
||||
pull_request = github_repo.get_pulls(
|
||||
state="open", base=base, head=head_branch
|
||||
)[0]
|
||||
# Update title and body
|
||||
pull_request.as_issue().edit(title=title, body=body)
|
||||
print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
|
||||
else:
|
||||
print(str(e))
|
||||
raise
|
||||
|
||||
# Set the output variables
|
||||
os.system(f"echo ::set-env name=PULL_REQUEST_NUMBER::{pull_request.number}")
|
||||
os.system(f"echo ::set-output name=pr_number::{pull_request.number}")
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if labels is not None:
|
||||
print(f"Applying labels '{labels}'")
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(labels))
|
||||
if assignees is not None:
|
||||
print(f"Applying assignees '{assignees}'")
|
||||
pull_request.as_issue().edit(assignees=cs_string_to_list(assignees))
|
||||
if milestone is not None:
|
||||
print(f"Applying milestone '{milestone}'")
|
||||
milestone = github_repo.get_milestone(int(milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers
|
||||
if reviewers is not None:
|
||||
print(f"Requesting reviewers '{reviewers}'")
|
||||
try:
|
||||
pull_request.create_review_request(reviewers=cs_string_to_list(reviewers))
|
||||
except GithubException as e:
|
||||
# Likely caused by "Review cannot be requested from pull request author."
|
||||
if e.status == 422:
|
||||
print("Request reviewers failed - {}".format(e.data["message"]))
|
||||
|
||||
# Set pull request team reviewers
|
||||
if team_reviewers is not None:
|
||||
print(f"Requesting team reviewers '{team_reviewers}'")
|
||||
pull_request.create_review_request(
|
||||
team_reviewers=cs_string_to_list(team_reviewers)
|
||||
)
|
||||
|
||||
# Create a project card for the pull request
|
||||
if project_name is not None and project_column_name is not None:
|
||||
try:
|
||||
create_project_card(
|
||||
github_repo, project_name, project_column_name, pull_request
|
||||
)
|
||||
except GithubException as e:
|
||||
# Likely caused by "Project already has the associated issue."
|
||||
if e.status == 422:
|
||||
print(
|
||||
"Create project card failed - {}".format(
|
||||
e.data["errors"][0]["message"]
|
||||
)
|
||||
)
|
@ -1,228 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create Pull Request """
|
||||
import base64
|
||||
import common as cmn
|
||||
import create_or_update_branch as coub
|
||||
import create_or_update_pull_request as coupr
|
||||
from git import Repo, GitCommandError
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# Default the committer and author to the GitHub Actions bot
|
||||
DEFAULT_COMMITTER = "GitHub <noreply@github.com>"
|
||||
DEFAULT_AUTHOR = (
|
||||
"github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
)
|
||||
DEFAULT_COMMIT_MESSAGE = "[create-pull-request] automated change"
|
||||
DEFAULT_TITLE = "Changes by create-pull-request action"
|
||||
DEFAULT_BODY = (
|
||||
"Automated changes by "
|
||||
+ "[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action"
|
||||
)
|
||||
DEFAULT_BRANCH = "create-pull-request/patch"
|
||||
|
||||
|
||||
def get_git_config_value(repo, name):
|
||||
try:
|
||||
return repo.git.config("--get", name)
|
||||
except GitCommandError:
|
||||
return None
|
||||
|
||||
|
||||
def get_repository_detail(repo):
|
||||
remote_origin_url = get_git_config_value(repo, "remote.origin.url")
|
||||
if remote_origin_url is None:
|
||||
raise ValueError("Failed to fetch 'remote.origin.url' from git config")
|
||||
protocol, github_repository = cmn.parse_github_repository(remote_origin_url)
|
||||
return remote_origin_url, protocol, github_repository
|
||||
|
||||
|
||||
def git_user_config_is_set(repo):
|
||||
name = get_git_config_value(repo, "user.name")
|
||||
email = get_git_config_value(repo, "user.email")
|
||||
|
||||
if name is not None and email is not None:
|
||||
print(f"Git user already configured as '{name} <{email}>'")
|
||||
return True
|
||||
|
||||
committer_name = get_git_config_value(repo, "committer.name")
|
||||
committer_email = get_git_config_value(repo, "committer.email")
|
||||
author_name = get_git_config_value(repo, "author.name")
|
||||
author_email = get_git_config_value(repo, "author.email")
|
||||
|
||||
if (
|
||||
committer_name is not None
|
||||
and committer_email is not None
|
||||
and author_name is not None
|
||||
and author_email is not None
|
||||
):
|
||||
print(
|
||||
f"Git committer already configured as '{committer_name} <{committer_email}>'"
|
||||
)
|
||||
print(f"Git author already configured as '{author_name} <{author_email}>'")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def set_committer_author(repo, committer, author):
|
||||
# If either committer or author is supplied they will be cross used
|
||||
if committer is None and author is not None:
|
||||
print("Supplied author will also be used as the committer.")
|
||||
committer = author
|
||||
if author is None and committer is not None:
|
||||
print("Supplied committer will also be used as the author.")
|
||||
author = committer
|
||||
|
||||
# If no committer/author has been supplied but user configuration already
|
||||
# exists in git config we can exit and use the existing config as-is.
|
||||
if committer is None and author is None:
|
||||
if git_user_config_is_set(repo):
|
||||
return
|
||||
|
||||
# Set defaults if no committer/author has been supplied
|
||||
if committer is None and author is None:
|
||||
committer = DEFAULT_COMMITTER
|
||||
author = DEFAULT_AUTHOR
|
||||
|
||||
# Set git environment. This will not persist after the action completes.
|
||||
committer_name, committer_email = cmn.parse_display_name_email(committer)
|
||||
author_name, author_email = cmn.parse_display_name_email(author)
|
||||
repo.git.update_environment(
|
||||
GIT_COMMITTER_NAME=committer_name,
|
||||
GIT_COMMITTER_EMAIL=committer_email,
|
||||
GIT_AUTHOR_NAME=author_name,
|
||||
GIT_AUTHOR_EMAIL=author_email,
|
||||
)
|
||||
print(f"Configured git committer as '{committer_name} <{committer_email}>'")
|
||||
print(f"Configured git author as '{author_name} <{author_email}>'")
|
||||
|
||||
|
||||
# Get required environment variables
|
||||
github_token = os.environ["GITHUB_TOKEN"]
|
||||
# Get environment variables with defaults
|
||||
path = os.getenv("CPR_PATH", os.getcwd())
|
||||
branch = os.getenv("CPR_BRANCH", DEFAULT_BRANCH)
|
||||
commit_message = os.getenv("CPR_COMMIT_MESSAGE", DEFAULT_COMMIT_MESSAGE)
|
||||
# Get environment variables with a default of 'None'
|
||||
committer = os.environ.get("CPR_COMMITTER")
|
||||
author = os.environ.get("CPR_AUTHOR")
|
||||
base = os.environ.get("CPR_BASE")
|
||||
|
||||
# Set the repo path
|
||||
repo = Repo(path)
|
||||
|
||||
# Determine the GitHub repository from git config
|
||||
# This will be the target repository for the pull request
|
||||
repo_url, protocol, github_repository = get_repository_detail(repo)
|
||||
print(f"Target repository set to {github_repository}")
|
||||
|
||||
if protocol == "HTTPS":
|
||||
print(f"::debug::Using HTTPS protocol")
|
||||
# Encode and configure the basic credential for HTTPS access
|
||||
basic_credential = base64.b64encode(
|
||||
f"x-access-token:{github_token}".encode("utf-8")
|
||||
).decode("utf-8")
|
||||
# Mask the basic credential in logs and debug output
|
||||
print(f"::add-mask::{basic_credential}")
|
||||
repo.git.set_persistent_git_options(
|
||||
c=f"http.https://github.com/.extraheader=AUTHORIZATION: basic {basic_credential}"
|
||||
)
|
||||
|
||||
# Determine if the checked out ref is a valid base for a pull request
|
||||
# The action needs the checked out HEAD ref to be a branch
|
||||
# This check will fail in the following cases:
|
||||
# - HEAD is detached
|
||||
# - HEAD is a merge commit (pull_request events)
|
||||
# - HEAD is a tag
|
||||
try:
|
||||
working_base = repo.git.symbolic_ref("HEAD", "--short")
|
||||
except GitCommandError as e:
|
||||
print(f"::debug::{e.stderr}")
|
||||
print(
|
||||
f"::error::The checked out ref is not a valid base for a pull request. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Exit if the working base is a PR branch created by this action.
|
||||
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
||||
# a PAT allows workflow actions to trigger further events.
|
||||
if working_base.startswith(branch):
|
||||
print(
|
||||
f"::error::Working base branch '{working_base}' was created by this action. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Fetch an optional environment variable to determine the branch suffix
|
||||
branch_suffix = os.environ.get("CPR_BRANCH_SUFFIX")
|
||||
if branch_suffix is not None:
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# Suffix with the short SHA1 hash
|
||||
branch = "{}-{}".format(branch, repo.git.rev_parse("--short", "HEAD"))
|
||||
elif branch_suffix == "timestamp":
|
||||
# Suffix with the current timestamp
|
||||
branch = "{}-{}".format(branch, int(time.time()))
|
||||
elif branch_suffix == "random":
|
||||
# Suffix with a 7 character random string
|
||||
branch = "{}-{}".format(branch, cmn.get_random_string())
|
||||
else:
|
||||
print(
|
||||
f"::error::Branch suffix '{branch_suffix}' is not a valid value. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Output head branch
|
||||
print(f"Pull request branch to create or update set to '{branch}'")
|
||||
|
||||
# Set the committer and author
|
||||
try:
|
||||
set_committer_author(repo, committer, author)
|
||||
except ValueError as e:
|
||||
print(f"::error::{e} " + "Unable to continue. Exiting.")
|
||||
sys.exit(1)
|
||||
|
||||
# Create or update the pull request branch
|
||||
result = coub.create_or_update_branch(repo, repo_url, commit_message, base, branch)
|
||||
|
||||
if result["action"] in ["created", "updated"]:
|
||||
# The branch was created or updated
|
||||
print(f"Pushing pull request branch to '{repo.full_name}/{branch}'")
|
||||
repo.git.push("--force", repo_url, f"HEAD:refs/heads/{branch}")
|
||||
|
||||
# Set the base. It would have been 'None' if not specified as an input
|
||||
base = result["base"]
|
||||
|
||||
# If there is no longer a diff with the base delete the branch and exit
|
||||
if not result["diff"]:
|
||||
print(f"Branch '{branch}' no longer differs from base branch '{base}'")
|
||||
print(f"Closing pull request and deleting branch '{branch}'")
|
||||
repo.git.push("--delete", "--force", repo_url, f"refs/heads/{branch}")
|
||||
sys.exit()
|
||||
|
||||
# Fetch optional environment variables with default values
|
||||
title = os.getenv("CPR_TITLE", DEFAULT_TITLE)
|
||||
body = os.getenv("CPR_BODY", DEFAULT_BODY)
|
||||
|
||||
# Create or update the pull request
|
||||
coupr.create_or_update_pull_request(
|
||||
github_token,
|
||||
github_repository,
|
||||
branch,
|
||||
base,
|
||||
title,
|
||||
body,
|
||||
os.environ.get("CPR_LABELS"),
|
||||
os.environ.get("CPR_ASSIGNEES"),
|
||||
os.environ.get("CPR_MILESTONE"),
|
||||
os.environ.get("CPR_REVIEWERS"),
|
||||
os.environ.get("CPR_TEAM_REVIEWERS"),
|
||||
os.environ.get("CPR_PROJECT_NAME"),
|
||||
os.environ.get("CPR_PROJECT_COLUMN_NAME"),
|
||||
os.environ.get("CPR_REQUEST_TO_PARENT"),
|
||||
)
|
@ -1,2 +0,0 @@
|
||||
GitPython==3.1.0
|
||||
PyGithub==1.47
|
@ -1,63 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Test Common """
|
||||
import common as cmn
|
||||
import pytest
|
||||
|
||||
|
||||
def test_get_random_string():
|
||||
assert len(cmn.get_random_string()) == 7
|
||||
assert len(cmn.get_random_string(length=20)) == 20
|
||||
|
||||
|
||||
def test_parse_github_repository_success():
|
||||
protocol, repository = cmn.parse_github_repository(
|
||||
"https://github.com/peter-evans/create-pull-request"
|
||||
)
|
||||
assert protocol == "HTTPS"
|
||||
assert repository == "peter-evans/create-pull-request"
|
||||
|
||||
protocol, repository = cmn.parse_github_repository(
|
||||
"git@github.com:peter-evans/create-pull-request.git"
|
||||
)
|
||||
assert protocol == "SSH"
|
||||
assert repository == "peter-evans/create-pull-request"
|
||||
|
||||
|
||||
def test_parse_github_repository_failure():
|
||||
url = "https://github.com/peter-evans"
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
cmn.parse_github_repository(url)
|
||||
assert (
|
||||
e_info.value.args[0]
|
||||
== f"The format of '{url}' is not a valid GitHub repository URL"
|
||||
)
|
||||
|
||||
|
||||
def test_parse_display_name_email_success():
|
||||
name, email = cmn.parse_display_name_email("abc def <abc@def.com>")
|
||||
assert name == "abc def"
|
||||
assert email == "abc@def.com"
|
||||
|
||||
name, email = cmn.parse_display_name_email(
|
||||
"github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
)
|
||||
assert name == "github-actions[bot]"
|
||||
assert email == "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
|
||||
|
||||
def test_parse_display_name_email_failure():
|
||||
display_name_email = "abc@def.com"
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
cmn.parse_display_name_email(display_name_email)
|
||||
assert (
|
||||
e_info.value.args[0]
|
||||
== f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
||||
|
||||
display_name_email = " < >"
|
||||
with pytest.raises(ValueError) as e_info:
|
||||
cmn.parse_display_name_email(display_name_email)
|
||||
assert (
|
||||
e_info.value.args[0]
|
||||
== f"The format of '{display_name_email}' is not a valid email address with display name"
|
||||
)
|
@ -1,757 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Test Create or Update Branch """
|
||||
import create_or_update_branch as coub
|
||||
from git import Repo
|
||||
import os
|
||||
import pytest
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
# Set git repo
|
||||
REPO_PATH = os.getenv("COUB_REPO_PATH", os.getcwd())
|
||||
repo = Repo(REPO_PATH)
|
||||
|
||||
# Set git environment
|
||||
author_name = "github-actions[bot]"
|
||||
author_email = "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
committer_name = "GitHub"
|
||||
committer_email = "noreply@github.com"
|
||||
repo.git.update_environment(
|
||||
GIT_AUTHOR_NAME=author_name,
|
||||
GIT_AUTHOR_EMAIL=author_email,
|
||||
GIT_COMMITTER_NAME=committer_name,
|
||||
GIT_COMMITTER_EMAIL=committer_email,
|
||||
)
|
||||
|
||||
REPO_URL = repo.git.config("--get", "remote.origin.url")
|
||||
|
||||
TRACKED_FILE = "tracked-file.txt"
|
||||
UNTRACKED_FILE = "untracked-file.txt"
|
||||
|
||||
DEFAULT_BRANCH = "tests/master"
|
||||
NOT_BASE_BRANCH = "tests/branch-that-is-not-the-base"
|
||||
NOT_EXIST_BRANCH = "tests/branch-that-does-not-exist"
|
||||
|
||||
COMMIT_MESSAGE = "[create-pull-request] automated change"
|
||||
BRANCH = "tests/create-pull-request/patch"
|
||||
BASE = DEFAULT_BRANCH
|
||||
|
||||
|
||||
def create_tracked_change(content=None):
|
||||
if content is None:
|
||||
content = str(time.time())
|
||||
# Create a tracked file change
|
||||
with open(os.path.join(REPO_PATH, TRACKED_FILE), "w") as f:
|
||||
f.write(content)
|
||||
return content
|
||||
|
||||
|
||||
def create_untracked_change(content=None):
|
||||
if content is None:
|
||||
content = str(time.time())
|
||||
# Create an untracked file change
|
||||
with open(os.path.join(REPO_PATH, UNTRACKED_FILE), "w") as f:
|
||||
f.write(content)
|
||||
return content
|
||||
|
||||
|
||||
def get_tracked_content():
|
||||
# Read the content of the tracked file
|
||||
with open(os.path.join(REPO_PATH, TRACKED_FILE), "r") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def get_untracked_content():
|
||||
# Read the content of the untracked file
|
||||
with open(os.path.join(REPO_PATH, UNTRACKED_FILE), "r") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def create_changes(tracked_content=None, untracked_content=None):
|
||||
tracked_content = create_tracked_change(tracked_content)
|
||||
untracked_content = create_untracked_change(untracked_content)
|
||||
return tracked_content, untracked_content
|
||||
|
||||
|
||||
def create_commits(number=2, final_tracked_content=None, final_untracked_content=None):
|
||||
for i in range(number):
|
||||
commit_number = i + 1
|
||||
if commit_number == number:
|
||||
tracked_content, untracked_content = create_changes(
|
||||
final_tracked_content, final_untracked_content
|
||||
)
|
||||
else:
|
||||
tracked_content, untracked_content = create_changes()
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m=f"Commit {commit_number}")
|
||||
return tracked_content, untracked_content
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def before_after_all():
|
||||
print("Before all tests")
|
||||
# Check there are no local changes that might be
|
||||
# destroyed by running these tests
|
||||
assert not repo.is_dirty(untracked_files=True)
|
||||
|
||||
# Create a new default branch for the test run
|
||||
repo.remotes.origin.fetch()
|
||||
repo.git.checkout("master")
|
||||
repo.git.checkout("HEAD", b=NOT_BASE_BRANCH)
|
||||
create_tracked_change()
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m="This commit should not appear in pr branches")
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{NOT_BASE_BRANCH}")
|
||||
# Create a new default branch for the test run
|
||||
repo.git.checkout("master")
|
||||
repo.git.checkout("HEAD", b=DEFAULT_BRANCH)
|
||||
create_tracked_change()
|
||||
repo.git.add("-A")
|
||||
repo.git.commit(m="Add file to be a tracked file for tests")
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
|
||||
yield
|
||||
|
||||
print("After all tests")
|
||||
repo.git.checkout("master")
|
||||
# Delete the "not base branch" created for the test run
|
||||
repo.git.branch("--delete", "--force", NOT_BASE_BRANCH)
|
||||
repo.git.push("--delete", "--force", REPO_URL, f"refs/heads/{NOT_BASE_BRANCH}")
|
||||
# Delete the default branch created for the test run
|
||||
repo.git.branch("--delete", "--force", DEFAULT_BRANCH)
|
||||
repo.git.push("--delete", "--force", REPO_URL, f"refs/heads/{DEFAULT_BRANCH}")
|
||||
|
||||
|
||||
def before_test():
|
||||
print("Before test")
|
||||
# Checkout the default branch
|
||||
repo.git.checkout(DEFAULT_BRANCH)
|
||||
|
||||
|
||||
def after_test(delete_remote=True):
|
||||
print("After test")
|
||||
# Output git log
|
||||
print(repo.git.log("-5", pretty="oneline"))
|
||||
# Delete the pull request branch if it exists
|
||||
repo.git.checkout(DEFAULT_BRANCH)
|
||||
print(f"Deleting {BRANCH}")
|
||||
for branch in repo.branches:
|
||||
if branch.name == BRANCH:
|
||||
repo.git.branch("--delete", "--force", BRANCH)
|
||||
break
|
||||
if delete_remote:
|
||||
print(f"Deleting origin/{BRANCH}")
|
||||
for ref in repo.remotes.origin.refs:
|
||||
if ref.name == f"origin/{BRANCH}":
|
||||
repo.git.push("--delete", "--force", REPO_URL, f"refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch("--prune")
|
||||
break
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def before_after_tests():
|
||||
before_test()
|
||||
yield
|
||||
after_test()
|
||||
|
||||
|
||||
# Tests if a branch exists and can be fetched
|
||||
def coub_fetch_successful():
|
||||
assert coub.fetch_successful(repo, REPO_URL, NOT_BASE_BRANCH)
|
||||
assert not coub.fetch_successful(repo, REPO_URL, NOT_EXIST_BRANCH)
|
||||
|
||||
|
||||
# Tests no changes resulting in no new branch being created
|
||||
def coub_no_changes_on_create():
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
|
||||
|
||||
# Tests create and update with a tracked file change
|
||||
def coub_tracked_changes():
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
|
||||
# Tests create and update with an untracked file change
|
||||
def coub_untracked_changes():
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with identical changes
|
||||
# The pull request branch will not be updated
|
||||
def coub_identical_changes():
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create identical tracked and untracked file changes
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with commits on the base inbetween
|
||||
def coub_commits_on_base():
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and then an update with no changes
|
||||
# This effectively reverts the branch back to match the base and results in no diff
|
||||
def coub_changes_no_diff():
|
||||
# Save the default branch tracked content
|
||||
default_tracked_content = get_tracked_content()
|
||||
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Running with no update effectively reverts the branch back to match the base
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == default_tracked_content
|
||||
|
||||
|
||||
# Tests create and update with commits on the base inbetween
|
||||
# The changes on base effectively revert the branch back to match the base and results in no diff
|
||||
def coub_commits_on_base_no_diff():
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Create the same tracked and untracked file changes that were made to the base
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with commits on the working base (during the workflow)
|
||||
def coub_commits_on_working_base():
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
def coub_changes_and_commits_on_working_base():
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
# with commits on the base inbetween
|
||||
def coub_changes_and_commits_on_base_and_working_base():
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, None, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests no changes resulting in no new branch being created
|
||||
def coub_wbnb_no_changes_on_create():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with a tracked file change
|
||||
def coub_wbnb_tracked_changes():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create a tracked file change
|
||||
tracked_content = create_tracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with an untracked file change
|
||||
def coub_wbnb_untracked_changes():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create an untracked file change
|
||||
untracked_content = create_untracked_change()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with identical changes
|
||||
# The pull request branch will not be updated
|
||||
def coub_wbnb_identical_changes():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create identical tracked and untracked file changes
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "none"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with commits on the base inbetween
|
||||
def coub_wbnb_commits_on_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and then an update with no changes
|
||||
# This effectively reverts the branch back to match the base and results in no diff
|
||||
def coub_wbnb_changes_no_diff():
|
||||
# Save the default branch tracked content
|
||||
default_tracked_content = get_tracked_content()
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Running with no update effectively reverts the branch back to match the base
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == default_tracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with commits on the base inbetween
|
||||
# The changes on base effectively revert the branch back to match the base and results in no diff
|
||||
# This scenario will cause cherrypick to fail due to an empty commit.
|
||||
# The commit is empty because the changes now exist on the base.
|
||||
def coub_wbnb_commits_on_base_no_diff():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create the same tracked and untracked file changes that were made to the base
|
||||
create_changes(tracked_content, untracked_content)
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"] == False
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with commits on the working base (during the workflow)
|
||||
def coub_wbnb_commits_on_working_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
tracked_content, untracked_content = create_commits()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
def coub_wbnb_changes_and_commits_on_working_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# Working Base is Not Base (WBNB)
|
||||
# Tests create and update with changes and commits on the working base (during the workflow)
|
||||
# with commits on the base inbetween
|
||||
def coub_wbnb_changes_and_commits_on_base_and_working_base():
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "created"
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
# Push pull request branch to remote
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
after_test(delete_remote=False)
|
||||
before_test()
|
||||
|
||||
# Create commits on the base
|
||||
create_commits()
|
||||
repo.git.push("--force", REPO_URL, f"HEAD:refs/heads/{DEFAULT_BRANCH}")
|
||||
repo.remotes.origin.fetch()
|
||||
|
||||
# Set the working base to a branch that is not the pull request base
|
||||
repo.git.checkout(NOT_BASE_BRANCH)
|
||||
# Create commits on the working base
|
||||
create_commits()
|
||||
# Create tracked and untracked file changes
|
||||
tracked_content, untracked_content = create_changes()
|
||||
result = coub.create_or_update_branch(repo, REPO_URL, COMMIT_MESSAGE, BASE, BRANCH)
|
||||
assert result["action"] == "updated"
|
||||
assert result["diff"]
|
||||
assert get_tracked_content() == tracked_content
|
||||
assert get_untracked_content() == untracked_content
|
||||
|
||||
|
||||
# pytest -v -s ~/git/create-pull-request/src
|
||||
|
||||
test_coub_fetch_successful = coub_fetch_successful
|
||||
|
||||
test_coub_no_changes_on_create = coub_no_changes_on_create
|
||||
test_coub_tracked_changes = coub_tracked_changes
|
||||
test_coub_untracked_changes = coub_untracked_changes
|
||||
test_coub_identical_changes = coub_identical_changes
|
||||
test_coub_commits_on_base = coub_commits_on_base
|
||||
|
||||
test_coub_changes_no_diff = coub_changes_no_diff
|
||||
test_coub_commits_on_base_no_diff = coub_commits_on_base_no_diff
|
||||
|
||||
test_coub_commits_on_working_base = coub_commits_on_working_base
|
||||
test_coub_changes_and_commits_on_working_base = coub_changes_and_commits_on_working_base
|
||||
test_coub_changes_and_commits_on_base_and_working_base = (
|
||||
coub_changes_and_commits_on_base_and_working_base
|
||||
)
|
||||
|
||||
# WBNB
|
||||
test_coub_wbnb_no_changes_on_create = coub_wbnb_no_changes_on_create
|
||||
test_coub_wbnb_tracked_changes = coub_wbnb_tracked_changes
|
||||
test_coub_wbnb_untracked_changes = coub_wbnb_untracked_changes
|
||||
test_coub_wbnb_identical_changes = coub_wbnb_identical_changes
|
||||
test_coub_wbnb_commits_on_base = coub_wbnb_commits_on_base
|
||||
|
||||
test_coub_wbnb_changes_no_diff = coub_wbnb_changes_no_diff
|
||||
test_coub_wbnb_commits_on_base_no_diff = coub_wbnb_commits_on_base_no_diff
|
||||
|
||||
test_coub_wbnb_commits_on_working_base = coub_wbnb_commits_on_working_base
|
||||
test_coub_wbnb_changes_and_commits_on_working_base = (
|
||||
coub_wbnb_changes_and_commits_on_working_base
|
||||
)
|
||||
test_coub_wbnb_changes_and_commits_on_base_and_working_base = (
|
||||
coub_wbnb_changes_and_commits_on_base_and_working_base
|
||||
)
|
244
src/create-or-update-branch.ts
Normal file
244
src/create-or-update-branch.ts
Normal file
@ -0,0 +1,244 @@
|
||||
import * as core from '@actions/core'
|
||||
import {GitCommandManager} from './git-command-manager'
|
||||
import {v4 as uuidv4} from 'uuid'
|
||||
|
||||
const CHERRYPICK_EMPTY =
|
||||
'The previous cherry-pick is now empty, possibly due to conflict resolution.'
|
||||
|
||||
export enum WorkingBaseType {
|
||||
Branch = 'branch',
|
||||
Commit = 'commit'
|
||||
}
|
||||
|
||||
export async function getWorkingBaseAndType(
|
||||
git: GitCommandManager
|
||||
): Promise<[string, WorkingBaseType]> {
|
||||
const symbolicRefResult = await git.exec(
|
||||
['symbolic-ref', 'HEAD', '--short'],
|
||||
true
|
||||
)
|
||||
if (symbolicRefResult.exitCode == 0) {
|
||||
// A ref is checked out
|
||||
return [symbolicRefResult.stdout.trim(), WorkingBaseType.Branch]
|
||||
} else {
|
||||
// A commit is checked out (detached HEAD)
|
||||
const headSha = await git.revParse('HEAD')
|
||||
return [headSha, WorkingBaseType.Commit]
|
||||
}
|
||||
}
|
||||
|
||||
export async function tryFetch(
|
||||
git: GitCommandManager,
|
||||
remote: string,
|
||||
branch: string
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
await git.fetch([`${branch}:refs/remotes/${remote}/${branch}`], remote)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Return true if branch2 is ahead of branch1
|
||||
async function isAhead(
|
||||
git: GitCommandManager,
|
||||
branch1: string,
|
||||
branch2: string
|
||||
): Promise<boolean> {
|
||||
const result = await git.revList(
|
||||
[`${branch1}...${branch2}`],
|
||||
['--right-only', '--count']
|
||||
)
|
||||
return Number(result) > 0
|
||||
}
|
||||
|
||||
// Return true if branch2 is behind branch1
|
||||
async function isBehind(
|
||||
git: GitCommandManager,
|
||||
branch1: string,
|
||||
branch2: string
|
||||
): Promise<boolean> {
|
||||
const result = await git.revList(
|
||||
[`${branch1}...${branch2}`],
|
||||
['--left-only', '--count']
|
||||
)
|
||||
return Number(result) > 0
|
||||
}
|
||||
|
||||
// Return true if branch2 is even with branch1
|
||||
async function isEven(
|
||||
git: GitCommandManager,
|
||||
branch1: string,
|
||||
branch2: string
|
||||
): Promise<boolean> {
|
||||
return (
|
||||
!(await isAhead(git, branch1, branch2)) &&
|
||||
!(await isBehind(git, branch1, branch2))
|
||||
)
|
||||
}
|
||||
|
||||
function splitLines(multilineString: string): string[] {
|
||||
return multilineString
|
||||
.split('\n')
|
||||
.map(s => s.trim())
|
||||
.filter(x => x !== '')
|
||||
}
|
||||
|
||||
export async function createOrUpdateBranch(
|
||||
git: GitCommandManager,
|
||||
commitMessage: string,
|
||||
base: string,
|
||||
branch: string,
|
||||
branchRemoteName: string,
|
||||
signoff: boolean
|
||||
): Promise<CreateOrUpdateBranchResult> {
|
||||
// Get the working base.
|
||||
// When a ref, it may or may not be the actual base.
|
||||
// When a commit, we must rebase onto the actual base.
|
||||
const [workingBase, workingBaseType] = await getWorkingBaseAndType(git)
|
||||
core.info(`Working base is ${workingBaseType} '${workingBase}'`)
|
||||
if (workingBaseType == WorkingBaseType.Commit && !base) {
|
||||
throw new Error(`When in 'detached HEAD' state, 'base' must be supplied.`)
|
||||
}
|
||||
|
||||
// If the base is not specified it is assumed to be the working base.
|
||||
base = base ? base : workingBase
|
||||
const baseRemote = 'origin'
|
||||
|
||||
// Set the default return values
|
||||
const result: CreateOrUpdateBranchResult = {
|
||||
action: 'none',
|
||||
base: base,
|
||||
hasDiffWithBase: false
|
||||
}
|
||||
|
||||
// Save the working base changes to a temporary branch
|
||||
const tempBranch = uuidv4()
|
||||
await git.checkout(tempBranch, 'HEAD')
|
||||
// Commit any uncommitted changes
|
||||
if (await git.isDirty(true)) {
|
||||
core.info('Uncommitted changes found. Adding a commit.')
|
||||
await git.exec(['add', '-A'])
|
||||
const params = ['-m', commitMessage]
|
||||
if (signoff) {
|
||||
params.push('--signoff')
|
||||
}
|
||||
await git.commit(params)
|
||||
}
|
||||
|
||||
// Perform fetch and reset the working base
|
||||
// Commits made during the workflow will be removed
|
||||
if (workingBaseType == WorkingBaseType.Branch) {
|
||||
core.info(`Resetting working base branch '${workingBase}'`)
|
||||
if (branchRemoteName == 'fork') {
|
||||
// If pushing to a fork we must fetch with 'unshallow' to avoid the following error on git push
|
||||
// ! [remote rejected] HEAD -> tests/push-branch-to-fork (shallow update not allowed)
|
||||
await git.fetch([`${workingBase}:${workingBase}`], baseRemote, [
|
||||
'--force'
|
||||
])
|
||||
} else {
|
||||
// If the remote is 'origin' we can git reset
|
||||
await git.checkout(workingBase)
|
||||
await git.exec(['reset', '--hard', `${baseRemote}/${workingBase}`])
|
||||
}
|
||||
}
|
||||
|
||||
// If the working base is not the base, rebase the temp branch commits
|
||||
// This will also be true if the working base type is a commit
|
||||
if (workingBase != base) {
|
||||
core.info(
|
||||
`Rebasing commits made to ${workingBaseType} '${workingBase}' on to base branch '${base}'`
|
||||
)
|
||||
// Checkout the actual base
|
||||
await git.fetch([`${base}:${base}`], baseRemote, ['--force'])
|
||||
await git.checkout(base)
|
||||
// Cherrypick commits from the temporary branch starting from the working base
|
||||
const commits = await git.revList(
|
||||
[`${workingBase}..${tempBranch}`, '.'],
|
||||
['--reverse']
|
||||
)
|
||||
for (const commit of splitLines(commits)) {
|
||||
const result = await git.cherryPick(
|
||||
['--strategy=recursive', '--strategy-option=theirs', commit],
|
||||
true
|
||||
)
|
||||
if (result.exitCode != 0 && !result.stderr.includes(CHERRYPICK_EMPTY)) {
|
||||
throw new Error(`Unexpected error: ${result.stderr}`)
|
||||
}
|
||||
}
|
||||
// Reset the temp branch to the working index
|
||||
await git.checkout(tempBranch, 'HEAD')
|
||||
// Reset the base
|
||||
await git.fetch([`${base}:${base}`], baseRemote, ['--force'])
|
||||
}
|
||||
|
||||
// Try to fetch the pull request branch
|
||||
if (!(await tryFetch(git, branchRemoteName, branch))) {
|
||||
// The pull request branch does not exist
|
||||
core.info(`Pull request branch '${branch}' does not exist yet.`)
|
||||
// Create the pull request branch
|
||||
await git.checkout(branch, tempBranch)
|
||||
// Check if the pull request branch is ahead of the base
|
||||
result.hasDiffWithBase = await isAhead(git, base, branch)
|
||||
if (result.hasDiffWithBase) {
|
||||
result.action = 'created'
|
||||
core.info(`Created branch '${branch}'`)
|
||||
} else {
|
||||
core.info(
|
||||
`Branch '${branch}' is not ahead of base '${base}' and will not be created`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// The pull request branch exists
|
||||
core.info(
|
||||
`Pull request branch '${branch}' already exists as remote branch '${branchRemoteName}/${branch}'`
|
||||
)
|
||||
// Checkout the pull request branch
|
||||
await git.checkout(branch)
|
||||
|
||||
// Reset the branch if one of the following conditions is true.
|
||||
// - If the branch differs from the recreated temp branch.
|
||||
// - If the recreated temp branch is not ahead of the base. This means there will be
|
||||
// no pull request diff after the branch is reset. This will reset any undeleted
|
||||
// branches after merging. In particular, it catches a case where the branch was
|
||||
// squash merged but not deleted. We need to reset to make sure it doesn't appear
|
||||
// to have a diff with the base due to different commits for the same changes.
|
||||
// For changes on base this reset is equivalent to a rebase of the pull request branch.
|
||||
if (
|
||||
(await git.hasDiff([`${branch}..${tempBranch}`])) ||
|
||||
!(await isAhead(git, base, tempBranch))
|
||||
) {
|
||||
core.info(`Resetting '${branch}'`)
|
||||
// Alternatively, git switch -C branch tempBranch
|
||||
await git.checkout(branch, tempBranch)
|
||||
}
|
||||
|
||||
// Check if the pull request branch has been updated
|
||||
// If the branch was reset or updated it will be ahead
|
||||
// It may be behind if a reset now results in no diff with the base
|
||||
if (!(await isEven(git, `${branchRemoteName}/${branch}`, branch))) {
|
||||
result.action = 'updated'
|
||||
core.info(`Updated branch '${branch}'`)
|
||||
} else {
|
||||
result.action = 'not-updated'
|
||||
core.info(
|
||||
`Branch '${branch}' is even with its remote and will not be updated`
|
||||
)
|
||||
}
|
||||
|
||||
// Check if the pull request branch is ahead of the base
|
||||
result.hasDiffWithBase = await isAhead(git, base, branch)
|
||||
}
|
||||
|
||||
// Delete the temporary branch
|
||||
await git.exec(['branch', '--delete', '--force', tempBranch])
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
interface CreateOrUpdateBranchResult {
|
||||
action: string
|
||||
base: string
|
||||
hasDiffWithBase: boolean
|
||||
}
|
230
src/create-pull-request.ts
Normal file
230
src/create-pull-request.ts
Normal file
@ -0,0 +1,230 @@
|
||||
import * as core from '@actions/core'
|
||||
import {
|
||||
createOrUpdateBranch,
|
||||
getWorkingBaseAndType,
|
||||
WorkingBaseType
|
||||
} from './create-or-update-branch'
|
||||
import {GitHubHelper} from './github-helper'
|
||||
import {GitCommandManager} from './git-command-manager'
|
||||
import {GitAuthHelper} from './git-auth-helper'
|
||||
import * as utils from './utils'
|
||||
|
||||
export interface Inputs {
|
||||
token: string
|
||||
path: string
|
||||
commitMessage: string
|
||||
committer: string
|
||||
author: string
|
||||
signoff: boolean
|
||||
branch: string
|
||||
deleteBranch: boolean
|
||||
branchSuffix: string
|
||||
base: string
|
||||
pushToFork: string
|
||||
title: string
|
||||
body: string
|
||||
labels: string[]
|
||||
assignees: string[]
|
||||
reviewers: string[]
|
||||
teamReviewers: string[]
|
||||
milestone: number
|
||||
draft: boolean
|
||||
}
|
||||
|
||||
export async function createPullRequest(inputs: Inputs): Promise<void> {
|
||||
let gitAuthHelper
|
||||
try {
|
||||
// Get the repository path
|
||||
const repoPath = utils.getRepoPath(inputs.path)
|
||||
// Create a git command manager
|
||||
const git = await GitCommandManager.create(repoPath)
|
||||
|
||||
// Save and unset the extraheader auth config if it exists
|
||||
core.startGroup('Save persisted git credentials')
|
||||
gitAuthHelper = new GitAuthHelper(git)
|
||||
await gitAuthHelper.savePersistedAuth()
|
||||
core.endGroup()
|
||||
|
||||
// Init the GitHub client
|
||||
const githubHelper = new GitHubHelper(inputs.token)
|
||||
|
||||
core.startGroup('Determining the base and head repositories')
|
||||
// Determine the base repository from git config
|
||||
const remoteUrl = await git.tryGetRemoteUrl()
|
||||
const baseRemote = utils.getRemoteDetail(remoteUrl)
|
||||
// Determine the head repository; the target for the pull request branch
|
||||
const branchRemoteName = inputs.pushToFork ? 'fork' : 'origin'
|
||||
const branchRepository = inputs.pushToFork
|
||||
? inputs.pushToFork
|
||||
: baseRemote.repository
|
||||
if (inputs.pushToFork) {
|
||||
// Check if the supplied fork is really a fork of the base
|
||||
const parentRepository = await githubHelper.getRepositoryParent(
|
||||
branchRepository
|
||||
)
|
||||
if (parentRepository != baseRemote.repository) {
|
||||
throw new Error(
|
||||
`Repository '${branchRepository}' is not a fork of '${baseRemote.repository}'. Unable to continue.`
|
||||
)
|
||||
}
|
||||
// Add a remote for the fork
|
||||
const remoteUrl = utils.getRemoteUrl(
|
||||
baseRemote.protocol,
|
||||
branchRepository
|
||||
)
|
||||
await git.exec(['remote', 'add', 'fork', remoteUrl])
|
||||
}
|
||||
core.endGroup()
|
||||
core.info(
|
||||
`Pull request branch target repository set to ${branchRepository}`
|
||||
)
|
||||
|
||||
// Configure auth
|
||||
if (baseRemote.protocol == 'HTTPS') {
|
||||
core.startGroup('Configuring credential for HTTPS authentication')
|
||||
await gitAuthHelper.configureToken(inputs.token)
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
core.startGroup('Checking the base repository state')
|
||||
const [workingBase, workingBaseType] = await getWorkingBaseAndType(git)
|
||||
core.info(`Working base is ${workingBaseType} '${workingBase}'`)
|
||||
// When in detached HEAD state (checked out on a commit), we need to
|
||||
// know the 'base' branch in order to rebase changes.
|
||||
if (workingBaseType == WorkingBaseType.Commit && !inputs.base) {
|
||||
throw new Error(
|
||||
`When the repository is checked out on a commit instead of a branch, the 'base' input must be supplied.`
|
||||
)
|
||||
}
|
||||
// If the base is not specified it is assumed to be the working base.
|
||||
const base = inputs.base ? inputs.base : workingBase
|
||||
// Throw an error if the base and branch are not different branches
|
||||
// of the 'origin' remote. An identically named branch in the `fork`
|
||||
// remote is perfectly fine.
|
||||
if (branchRemoteName == 'origin' && base == inputs.branch) {
|
||||
throw new Error(
|
||||
`The 'base' and 'branch' for a pull request must be different branches. Unable to continue.`
|
||||
)
|
||||
}
|
||||
// For self-hosted runners the repository state persists between runs.
|
||||
// This command prunes the stale remote ref when the pull request branch was
|
||||
// deleted after being merged or closed. Without this the push using
|
||||
// '--force-with-lease' fails due to "stale info."
|
||||
// https://github.com/peter-evans/create-pull-request/issues/633
|
||||
await git.exec(['remote', 'prune', branchRemoteName])
|
||||
core.endGroup()
|
||||
|
||||
// Apply the branch suffix if set
|
||||
if (inputs.branchSuffix) {
|
||||
switch (inputs.branchSuffix) {
|
||||
case 'short-commit-hash':
|
||||
// Suffix with the short SHA1 hash
|
||||
inputs.branch = `${inputs.branch}-${await git.revParse('HEAD', [
|
||||
'--short'
|
||||
])}`
|
||||
break
|
||||
case 'timestamp':
|
||||
// Suffix with the current timestamp
|
||||
inputs.branch = `${inputs.branch}-${utils.secondsSinceEpoch()}`
|
||||
break
|
||||
case 'random':
|
||||
// Suffix with a 7 character random string
|
||||
inputs.branch = `${inputs.branch}-${utils.randomString()}`
|
||||
break
|
||||
default:
|
||||
throw new Error(
|
||||
`Branch suffix '${inputs.branchSuffix}' is not a valid value. Unable to continue.`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Output head branch
|
||||
core.info(
|
||||
`Pull request branch to create or update set to '${inputs.branch}'`
|
||||
)
|
||||
|
||||
// Configure the committer and author
|
||||
core.startGroup('Configuring the committer and author')
|
||||
const parsedAuthor = utils.parseDisplayNameEmail(inputs.author)
|
||||
const parsedCommitter = utils.parseDisplayNameEmail(inputs.committer)
|
||||
git.setIdentityGitOptions([
|
||||
'-c',
|
||||
`author.name=${parsedAuthor.name}`,
|
||||
'-c',
|
||||
`author.email=${parsedAuthor.email}`,
|
||||
'-c',
|
||||
`committer.name=${parsedCommitter.name}`,
|
||||
'-c',
|
||||
`committer.email=${parsedCommitter.email}`
|
||||
])
|
||||
core.info(
|
||||
`Configured git committer as '${parsedCommitter.name} <${parsedCommitter.email}>'`
|
||||
)
|
||||
core.info(
|
||||
`Configured git author as '${parsedAuthor.name} <${parsedAuthor.email}>'`
|
||||
)
|
||||
core.endGroup()
|
||||
|
||||
// Create or update the pull request branch
|
||||
core.startGroup('Create or update the pull request branch')
|
||||
const result = await createOrUpdateBranch(
|
||||
git,
|
||||
inputs.commitMessage,
|
||||
inputs.base,
|
||||
inputs.branch,
|
||||
branchRemoteName,
|
||||
inputs.signoff
|
||||
)
|
||||
core.endGroup()
|
||||
|
||||
if (['created', 'updated'].includes(result.action)) {
|
||||
// The branch was created or updated
|
||||
core.startGroup(
|
||||
`Pushing pull request branch to '${branchRemoteName}/${inputs.branch}'`
|
||||
)
|
||||
await git.push([
|
||||
'--force-with-lease',
|
||||
branchRemoteName,
|
||||
`HEAD:refs/heads/${inputs.branch}`
|
||||
])
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
// Set the base. It would have been '' if not specified as an input
|
||||
inputs.base = result.base
|
||||
|
||||
if (result.hasDiffWithBase) {
|
||||
// Create or update the pull request
|
||||
await githubHelper.createOrUpdatePullRequest(
|
||||
inputs,
|
||||
baseRemote.repository,
|
||||
branchRepository
|
||||
)
|
||||
} else {
|
||||
// There is no longer a diff with the base
|
||||
// Check we are in a state where a branch exists
|
||||
if (['updated', 'not-updated'].includes(result.action)) {
|
||||
core.info(
|
||||
`Branch '${inputs.branch}' no longer differs from base branch '${inputs.base}'`
|
||||
)
|
||||
if (inputs.deleteBranch) {
|
||||
core.info(`Deleting branch '${inputs.branch}'`)
|
||||
await git.push([
|
||||
'--delete',
|
||||
'--force',
|
||||
branchRemoteName,
|
||||
`refs/heads/${inputs.branch}`
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
} finally {
|
||||
// Remove auth and restore persisted auth config if it existed
|
||||
core.startGroup('Restore persisted git credentials')
|
||||
await gitAuthHelper.removeAuth()
|
||||
await gitAuthHelper.restorePersistedAuth()
|
||||
core.endGroup()
|
||||
}
|
||||
}
|
126
src/git-auth-helper.ts
Normal file
126
src/git-auth-helper.ts
Normal file
@ -0,0 +1,126 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as fs from 'fs'
|
||||
import {GitCommandManager} from './git-command-manager'
|
||||
import * as path from 'path'
|
||||
import {URL} from 'url'
|
||||
|
||||
export class GitAuthHelper {
|
||||
private git: GitCommandManager
|
||||
private gitConfigPath: string
|
||||
private extraheaderConfigKey: string
|
||||
private extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***'
|
||||
private extraheaderConfigValueRegex = '^AUTHORIZATION:'
|
||||
private persistedExtraheaderConfigValue = ''
|
||||
|
||||
constructor(git: GitCommandManager) {
|
||||
this.git = git
|
||||
this.gitConfigPath = path.join(
|
||||
this.git.getWorkingDirectory(),
|
||||
'.git',
|
||||
'config'
|
||||
)
|
||||
const serverUrl = this.getServerUrl()
|
||||
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`
|
||||
}
|
||||
|
||||
async savePersistedAuth(): Promise<void> {
|
||||
// Save and unset persisted extraheader credential in git config if it exists
|
||||
this.persistedExtraheaderConfigValue = await this.getAndUnset()
|
||||
}
|
||||
|
||||
async restorePersistedAuth(): Promise<void> {
|
||||
if (this.persistedExtraheaderConfigValue) {
|
||||
try {
|
||||
await this.setExtraheaderConfig(this.persistedExtraheaderConfigValue)
|
||||
core.info('Persisted git credentials restored')
|
||||
} catch (e) {
|
||||
core.warning(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async configureToken(token: string): Promise<void> {
|
||||
// Encode and configure the basic credential for HTTPS access
|
||||
const basicCredential = Buffer.from(
|
||||
`x-access-token:${token}`,
|
||||
'utf8'
|
||||
).toString('base64')
|
||||
core.setSecret(basicCredential)
|
||||
const extraheaderConfigValue = `AUTHORIZATION: basic ${basicCredential}`
|
||||
await this.setExtraheaderConfig(extraheaderConfigValue)
|
||||
}
|
||||
|
||||
async removeAuth(): Promise<void> {
|
||||
await this.getAndUnset()
|
||||
}
|
||||
|
||||
private async setExtraheaderConfig(
|
||||
extraheaderConfigValue: string
|
||||
): Promise<void> {
|
||||
// Configure a placeholder value. This approach avoids the credential being captured
|
||||
// by process creation audit events, which are commonly logged. For more information,
|
||||
// refer to https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/manage/component-updates/command-line-process-auditing
|
||||
// See https://github.com/actions/checkout/blob/main/src/git-auth-helper.ts#L267-L274
|
||||
await this.git.config(
|
||||
this.extraheaderConfigKey,
|
||||
this.extraheaderConfigPlaceholderValue
|
||||
)
|
||||
// Replace the placeholder
|
||||
await this.gitConfigStringReplace(
|
||||
this.extraheaderConfigPlaceholderValue,
|
||||
extraheaderConfigValue
|
||||
)
|
||||
}
|
||||
|
||||
private async getAndUnset(): Promise<string> {
|
||||
let configValue = ''
|
||||
// Save and unset persisted extraheader credential in git config if it exists
|
||||
if (
|
||||
await this.git.configExists(
|
||||
this.extraheaderConfigKey,
|
||||
this.extraheaderConfigValueRegex
|
||||
)
|
||||
) {
|
||||
configValue = await this.git.getConfigValue(
|
||||
this.extraheaderConfigKey,
|
||||
this.extraheaderConfigValueRegex
|
||||
)
|
||||
if (
|
||||
await this.git.tryConfigUnset(
|
||||
this.extraheaderConfigKey,
|
||||
this.extraheaderConfigValueRegex
|
||||
)
|
||||
) {
|
||||
core.info(`Unset config key '${this.extraheaderConfigKey}'`)
|
||||
} else {
|
||||
core.warning(
|
||||
`Failed to unset config key '${this.extraheaderConfigKey}'`
|
||||
)
|
||||
}
|
||||
}
|
||||
return configValue
|
||||
}
|
||||
|
||||
private async gitConfigStringReplace(
|
||||
find: string,
|
||||
replace: string
|
||||
): Promise<void> {
|
||||
let content = (await fs.promises.readFile(this.gitConfigPath)).toString()
|
||||
const index = content.indexOf(find)
|
||||
if (index < 0 || index != content.lastIndexOf(find)) {
|
||||
throw new Error(`Unable to replace '${find}' in ${this.gitConfigPath}`)
|
||||
}
|
||||
content = content.replace(find, replace)
|
||||
await fs.promises.writeFile(this.gitConfigPath, content)
|
||||
}
|
||||
|
||||
private getServerUrl(): URL {
|
||||
// todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
|
||||
// See https://github.com/actions/checkout/blob/main/src/url-helper.ts#L22-L29
|
||||
return new URL(
|
||||
process.env['GITHUB_SERVER_URL'] ||
|
||||
process.env['GITHUB_URL'] ||
|
||||
'https://github.com'
|
||||
)
|
||||
}
|
||||
}
|
293
src/git-command-manager.ts
Normal file
293
src/git-command-manager.ts
Normal file
@ -0,0 +1,293 @@
|
||||
import * as exec from '@actions/exec'
|
||||
import * as io from '@actions/io'
|
||||
import * as utils from './utils'
|
||||
import * as path from 'path'
|
||||
|
||||
const tagsRefSpec = '+refs/tags/*:refs/tags/*'
|
||||
|
||||
export class GitCommandManager {
|
||||
private gitPath: string
|
||||
private workingDirectory: string
|
||||
// Git options used when commands require an identity
|
||||
private identityGitOptions?: string[]
|
||||
|
||||
private constructor(workingDirectory: string, gitPath: string) {
|
||||
this.workingDirectory = workingDirectory
|
||||
this.gitPath = gitPath
|
||||
}
|
||||
|
||||
static async create(workingDirectory: string): Promise<GitCommandManager> {
|
||||
const gitPath = await io.which('git', true)
|
||||
return new GitCommandManager(workingDirectory, gitPath)
|
||||
}
|
||||
|
||||
setIdentityGitOptions(identityGitOptions: string[]): void {
|
||||
this.identityGitOptions = identityGitOptions
|
||||
}
|
||||
|
||||
async checkout(ref: string, startPoint?: string): Promise<void> {
|
||||
const args = ['checkout', '--progress']
|
||||
if (startPoint) {
|
||||
args.push('-B', ref, startPoint)
|
||||
} else {
|
||||
args.push(ref)
|
||||
}
|
||||
await this.exec(args)
|
||||
}
|
||||
|
||||
async cherryPick(
|
||||
options?: string[],
|
||||
allowAllExitCodes = false
|
||||
): Promise<GitOutput> {
|
||||
const args = ['cherry-pick']
|
||||
if (this.identityGitOptions) {
|
||||
args.unshift(...this.identityGitOptions)
|
||||
}
|
||||
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
|
||||
return await this.exec(args, allowAllExitCodes)
|
||||
}
|
||||
|
||||
async commit(options?: string[]): Promise<void> {
|
||||
const args = ['commit']
|
||||
if (this.identityGitOptions) {
|
||||
args.unshift(...this.identityGitOptions)
|
||||
}
|
||||
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
|
||||
await this.exec(args)
|
||||
}
|
||||
|
||||
async config(
|
||||
configKey: string,
|
||||
configValue: string,
|
||||
globalConfig?: boolean
|
||||
): Promise<void> {
|
||||
await this.exec([
|
||||
'config',
|
||||
globalConfig ? '--global' : '--local',
|
||||
configKey,
|
||||
configValue
|
||||
])
|
||||
}
|
||||
|
||||
async configExists(
|
||||
configKey: string,
|
||||
configValue = '.',
|
||||
globalConfig?: boolean
|
||||
): Promise<boolean> {
|
||||
const output = await this.exec(
|
||||
[
|
||||
'config',
|
||||
globalConfig ? '--global' : '--local',
|
||||
'--name-only',
|
||||
'--get-regexp',
|
||||
configKey,
|
||||
configValue
|
||||
],
|
||||
true
|
||||
)
|
||||
return output.exitCode === 0
|
||||
}
|
||||
|
||||
async fetch(
|
||||
refSpec: string[],
|
||||
remoteName?: string,
|
||||
options?: string[]
|
||||
): Promise<void> {
|
||||
const args = ['-c', 'protocol.version=2', 'fetch']
|
||||
if (!refSpec.some(x => x === tagsRefSpec)) {
|
||||
args.push('--no-tags')
|
||||
}
|
||||
|
||||
args.push('--progress', '--no-recurse-submodules')
|
||||
if (
|
||||
utils.fileExistsSync(path.join(this.workingDirectory, '.git', 'shallow'))
|
||||
) {
|
||||
args.push('--unshallow')
|
||||
}
|
||||
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
|
||||
if (remoteName) {
|
||||
args.push(remoteName)
|
||||
} else {
|
||||
args.push('origin')
|
||||
}
|
||||
for (const arg of refSpec) {
|
||||
args.push(arg)
|
||||
}
|
||||
|
||||
await this.exec(args)
|
||||
}
|
||||
|
||||
async getConfigValue(configKey: string, configValue = '.'): Promise<string> {
|
||||
const output = await this.exec([
|
||||
'config',
|
||||
'--local',
|
||||
'--get-regexp',
|
||||
configKey,
|
||||
configValue
|
||||
])
|
||||
return output.stdout.trim().split(`${configKey} `)[1]
|
||||
}
|
||||
|
||||
getWorkingDirectory(): string {
|
||||
return this.workingDirectory
|
||||
}
|
||||
|
||||
async hasDiff(options?: string[]): Promise<boolean> {
|
||||
const args = ['diff', '--quiet']
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
const output = await this.exec(args, true)
|
||||
return output.exitCode === 1
|
||||
}
|
||||
|
||||
async isDirty(untracked: boolean): Promise<boolean> {
|
||||
// Check untracked changes
|
||||
if (untracked && (await this.status(['--porcelain', '-unormal']))) {
|
||||
return true
|
||||
}
|
||||
// Check working index changes
|
||||
if (await this.hasDiff()) {
|
||||
return true
|
||||
}
|
||||
// Check staged changes
|
||||
if (await this.hasDiff(['--staged'])) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
async push(options?: string[]): Promise<void> {
|
||||
const args = ['push']
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
await this.exec(args)
|
||||
}
|
||||
|
||||
async revList(
|
||||
commitExpression: string[],
|
||||
options?: string[]
|
||||
): Promise<string> {
|
||||
const args = ['rev-list']
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
args.push(...commitExpression)
|
||||
const output = await this.exec(args)
|
||||
return output.stdout.trim()
|
||||
}
|
||||
|
||||
async revParse(ref: string, options?: string[]): Promise<string> {
|
||||
const args = ['rev-parse']
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
args.push(ref)
|
||||
const output = await this.exec(args)
|
||||
return output.stdout.trim()
|
||||
}
|
||||
|
||||
async status(options?: string[]): Promise<string> {
|
||||
const args = ['status']
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
const output = await this.exec(args)
|
||||
return output.stdout.trim()
|
||||
}
|
||||
|
||||
async symbolicRef(ref: string, options?: string[]): Promise<string> {
|
||||
const args = ['symbolic-ref', ref]
|
||||
if (options) {
|
||||
args.push(...options)
|
||||
}
|
||||
const output = await this.exec(args)
|
||||
return output.stdout.trim()
|
||||
}
|
||||
|
||||
async tryConfigUnset(
|
||||
configKey: string,
|
||||
configValue = '.',
|
||||
globalConfig?: boolean
|
||||
): Promise<boolean> {
|
||||
const output = await this.exec(
|
||||
[
|
||||
'config',
|
||||
globalConfig ? '--global' : '--local',
|
||||
'--unset',
|
||||
configKey,
|
||||
configValue
|
||||
],
|
||||
true
|
||||
)
|
||||
return output.exitCode === 0
|
||||
}
|
||||
|
||||
async tryGetRemoteUrl(): Promise<string> {
|
||||
const output = await this.exec(
|
||||
['config', '--local', '--get', 'remote.origin.url'],
|
||||
true
|
||||
)
|
||||
|
||||
if (output.exitCode !== 0) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const stdout = output.stdout.trim()
|
||||
if (stdout.includes('\n')) {
|
||||
return ''
|
||||
}
|
||||
|
||||
return stdout
|
||||
}
|
||||
|
||||
async exec(args: string[], allowAllExitCodes = false): Promise<GitOutput> {
|
||||
const result = new GitOutput()
|
||||
|
||||
const env = {}
|
||||
for (const key of Object.keys(process.env)) {
|
||||
env[key] = process.env[key]
|
||||
}
|
||||
|
||||
const stdout: string[] = []
|
||||
const stderr: string[] = []
|
||||
|
||||
const options = {
|
||||
cwd: this.workingDirectory,
|
||||
env,
|
||||
ignoreReturnCode: allowAllExitCodes,
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => {
|
||||
stdout.push(data.toString())
|
||||
},
|
||||
stderr: (data: Buffer) => {
|
||||
stderr.push(data.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.exitCode = await exec.exec(`"${this.gitPath}"`, args, options)
|
||||
result.stdout = stdout.join('')
|
||||
result.stderr = stderr.join('')
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
class GitOutput {
|
||||
stdout = ''
|
||||
stderr = ''
|
||||
exitCode = 0
|
||||
}
|
97
src/git.js
97
src/git.js
@ -1,97 +0,0 @@
|
||||
const core = require("@actions/core");
|
||||
const exec = require("@actions/exec");
|
||||
const path = require("path");
|
||||
|
||||
function getRepoPath(relativePath) {
|
||||
let githubWorkspacePath = process.env["GITHUB_WORKSPACE"];
|
||||
if (!githubWorkspacePath) {
|
||||
throw new Error("GITHUB_WORKSPACE not defined");
|
||||
}
|
||||
githubWorkspacePath = path.resolve(githubWorkspacePath);
|
||||
core.debug(`githubWorkspacePath: ${githubWorkspacePath}`);
|
||||
|
||||
repoPath = githubWorkspacePath;
|
||||
if (relativePath) repoPath = path.resolve(repoPath, relativePath);
|
||||
|
||||
core.debug(`repoPath: ${repoPath}`);
|
||||
return repoPath;
|
||||
}
|
||||
|
||||
async function execGit(repoPath, args, ignoreReturnCode = false) {
|
||||
const stdout = [];
|
||||
const options = {
|
||||
cwd: repoPath,
|
||||
ignoreReturnCode: ignoreReturnCode,
|
||||
listeners: {
|
||||
stdout: data => {
|
||||
stdout.push(data.toString());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var result = {};
|
||||
result.exitCode = await exec.exec("git", args, options);
|
||||
result.stdout = stdout.join("");
|
||||
return result;
|
||||
}
|
||||
|
||||
async function addConfigOption(repoPath, name, value) {
|
||||
const result = await execGit(
|
||||
repoPath,
|
||||
["config", "--local", "--add", name, value],
|
||||
true
|
||||
);
|
||||
return result.exitCode === 0;
|
||||
}
|
||||
|
||||
async function unsetConfigOption(repoPath, name, valueRegex=".") {
|
||||
const result = await execGit(
|
||||
repoPath,
|
||||
["config", "--local", "--unset", name, valueRegex],
|
||||
true
|
||||
);
|
||||
return result.exitCode === 0;
|
||||
}
|
||||
|
||||
async function configOptionExists(repoPath, name, valueRegex=".") {
|
||||
const result = await execGit(
|
||||
repoPath,
|
||||
["config", "--local", "--name-only", "--get-regexp", name, valueRegex],
|
||||
true
|
||||
);
|
||||
return result.exitCode === 0;
|
||||
}
|
||||
|
||||
async function getConfigOption(repoPath, name, valueRegex=".") {
|
||||
const result = await execGit(
|
||||
repoPath,
|
||||
["config", "--local", "--get-regexp", name, valueRegex],
|
||||
true
|
||||
);
|
||||
const option = result.stdout.trim().split(`${name} `);
|
||||
return {
|
||||
name: name,
|
||||
value: option[1]
|
||||
}
|
||||
}
|
||||
|
||||
async function getAndUnsetConfigOption(repoPath, name, valueRegex=".") {
|
||||
if (await configOptionExists(repoPath, name, valueRegex)) {
|
||||
const option = await getConfigOption(repoPath, name, valueRegex);
|
||||
if (await unsetConfigOption(repoPath, name, valueRegex)) {
|
||||
core.debug(`Unset config option '${name}'`);
|
||||
return option;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getRepoPath,
|
||||
execGit,
|
||||
addConfigOption,
|
||||
unsetConfigOption,
|
||||
configOptionExists,
|
||||
getConfigOption,
|
||||
getAndUnsetConfigOption
|
||||
};
|
@ -1,98 +0,0 @@
|
||||
const path = require("path");
|
||||
const {
|
||||
getRepoPath,
|
||||
execGit,
|
||||
addConfigOption,
|
||||
unsetConfigOption,
|
||||
configOptionExists,
|
||||
getConfigOption,
|
||||
getAndUnsetConfigOption
|
||||
} = require("./git");
|
||||
|
||||
test("getRepoPath", async () => {
|
||||
expect(getRepoPath()).toEqual(process.env["GITHUB_WORKSPACE"]);
|
||||
expect(getRepoPath("foo")).toEqual(
|
||||
path.resolve(process.env["GITHUB_WORKSPACE"], "foo")
|
||||
);
|
||||
});
|
||||
|
||||
test("execGit", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const result = await execGit(
|
||||
repoPath,
|
||||
["config", "--local", "--name-only", "--get-regexp", "remote.origin.url"],
|
||||
true
|
||||
);
|
||||
expect(result.exitCode).toEqual(0);
|
||||
expect(result.stdout.trim()).toEqual("remote.origin.url");
|
||||
});
|
||||
|
||||
test("add and unset config option", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const add = await addConfigOption(repoPath, "test.add.and.unset.config.option", "foo");
|
||||
expect(add).toBeTruthy();
|
||||
const unset = await unsetConfigOption(repoPath, "test.add.and.unset.config.option");
|
||||
expect(unset).toBeTruthy();
|
||||
});
|
||||
|
||||
test("add and unset config option with value regex", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const add = await addConfigOption(repoPath, "test.add.and.unset.config.option", "foo bar");
|
||||
expect(add).toBeTruthy();
|
||||
const unset = await unsetConfigOption(repoPath, "test.add.and.unset.config.option", "^foo");
|
||||
expect(unset).toBeTruthy();
|
||||
});
|
||||
|
||||
test("configOptionExists returns true", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const result = await configOptionExists(repoPath, "remote.origin.url");
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
test("configOptionExists returns false", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const result = await configOptionExists(repoPath, "this.key.does.not.exist");
|
||||
expect(result).toBeFalsy();
|
||||
});
|
||||
|
||||
test("get config option", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const add = await addConfigOption(repoPath, "test.get.config.option", "foo");
|
||||
expect(add).toBeTruthy();
|
||||
const option = await getConfigOption(repoPath, "test.get.config.option");
|
||||
expect(option.value).toEqual("foo");
|
||||
const unset = await unsetConfigOption(repoPath, "test.get.config.option");
|
||||
expect(unset).toBeTruthy();
|
||||
});
|
||||
|
||||
test("get config option with value regex", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const add = await addConfigOption(repoPath, "test.get.config.option", "foo bar");
|
||||
expect(add).toBeTruthy();
|
||||
const option = await getConfigOption(repoPath, "test.get.config.option", "^foo");
|
||||
expect(option.value).toEqual("foo bar");
|
||||
const unset = await unsetConfigOption(repoPath, "test.get.config.option", "^foo");
|
||||
expect(unset).toBeTruthy();
|
||||
});
|
||||
|
||||
test("get and unset config option is successful", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const add = await addConfigOption(repoPath, "test.get.and.unset.config.option", "foo");
|
||||
expect(add).toBeTruthy();
|
||||
const getAndUnset = await getAndUnsetConfigOption(repoPath, "test.get.and.unset.config.option");
|
||||
expect(getAndUnset.value).toEqual("foo");
|
||||
});
|
||||
|
||||
test("get and unset config option is successful with value regex", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const add = await addConfigOption(repoPath, "test.get.and.unset.config.option", "foo bar");
|
||||
expect(add).toBeTruthy();
|
||||
const getAndUnset = await getAndUnsetConfigOption(repoPath, "test.get.and.unset.config.option", "^foo");
|
||||
expect(getAndUnset.value).toEqual("foo bar");
|
||||
});
|
||||
|
||||
test("get and unset config option is unsuccessful", async () => {
|
||||
const repoPath = getRepoPath();
|
||||
const getAndUnset = await getAndUnsetConfigOption(repoPath, "this.key.does.not.exist");
|
||||
expect(getAndUnset).toBeNull();
|
||||
});
|
172
src/github-helper.ts
Normal file
172
src/github-helper.ts
Normal file
@ -0,0 +1,172 @@
|
||||
import * as core from '@actions/core'
|
||||
import {Inputs} from './create-pull-request'
|
||||
import {Octokit, OctokitOptions} from './octokit-client'
|
||||
|
||||
const ERROR_PR_REVIEW_FROM_AUTHOR =
|
||||
'Review cannot be requested from pull request author'
|
||||
|
||||
interface Repository {
|
||||
owner: string
|
||||
repo: string
|
||||
}
|
||||
|
||||
interface Pull {
|
||||
number: number
|
||||
html_url: string
|
||||
}
|
||||
|
||||
export class GitHubHelper {
|
||||
private octokit: InstanceType<typeof Octokit>
|
||||
|
||||
constructor(token: string) {
|
||||
const options: OctokitOptions = {}
|
||||
if (token) {
|
||||
options.auth = `${token}`
|
||||
}
|
||||
this.octokit = new Octokit(options)
|
||||
}
|
||||
|
||||
private parseRepository(repository: string): Repository {
|
||||
const [owner, repo] = repository.split('/')
|
||||
return {
|
||||
owner: owner,
|
||||
repo: repo
|
||||
}
|
||||
}
|
||||
|
||||
private async createOrUpdate(
|
||||
inputs: Inputs,
|
||||
baseRepository: string,
|
||||
headBranch: string
|
||||
): Promise<Pull> {
|
||||
// Try to create the pull request
|
||||
try {
|
||||
const {data: pull} = await this.octokit.pulls.create({
|
||||
...this.parseRepository(baseRepository),
|
||||
title: inputs.title,
|
||||
head: headBranch,
|
||||
base: inputs.base,
|
||||
body: inputs.body,
|
||||
draft: inputs.draft
|
||||
})
|
||||
core.info(
|
||||
`Created pull request #${pull.number} (${headBranch} => ${inputs.base})`
|
||||
)
|
||||
return {
|
||||
number: pull.number,
|
||||
html_url: pull.html_url
|
||||
}
|
||||
} catch (e) {
|
||||
if (
|
||||
e.message &&
|
||||
e.message.includes(`A pull request already exists for ${headBranch}`)
|
||||
) {
|
||||
core.info(`A pull request already exists for ${headBranch}`)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
// Update the pull request that exists for this branch and base
|
||||
const {data: pulls} = await this.octokit.pulls.list({
|
||||
...this.parseRepository(baseRepository),
|
||||
state: 'open',
|
||||
head: headBranch,
|
||||
base: inputs.base
|
||||
})
|
||||
const {data: pull} = await this.octokit.pulls.update({
|
||||
...this.parseRepository(baseRepository),
|
||||
pull_number: pulls[0].number,
|
||||
title: inputs.title,
|
||||
body: inputs.body,
|
||||
draft: inputs.draft
|
||||
})
|
||||
core.info(
|
||||
`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`
|
||||
)
|
||||
return {
|
||||
number: pull.number,
|
||||
html_url: pull.html_url
|
||||
}
|
||||
}
|
||||
|
||||
async getRepositoryParent(headRepository: string): Promise<string> {
|
||||
const {data: headRepo} = await this.octokit.repos.get({
|
||||
...this.parseRepository(headRepository)
|
||||
})
|
||||
if (!headRepo.parent) {
|
||||
throw new Error(
|
||||
`Repository '${headRepository}' is not a fork. Unable to continue.`
|
||||
)
|
||||
}
|
||||
return headRepo.parent.full_name
|
||||
}
|
||||
|
||||
async createOrUpdatePullRequest(
|
||||
inputs: Inputs,
|
||||
baseRepository: string,
|
||||
headRepository: string
|
||||
): Promise<void> {
|
||||
const [headOwner] = headRepository.split('/')
|
||||
const headBranch = `${headOwner}:${inputs.branch}`
|
||||
|
||||
// Create or update the pull request
|
||||
const pull = await this.createOrUpdate(inputs, baseRepository, headBranch)
|
||||
|
||||
// Set outputs
|
||||
core.startGroup('Setting outputs')
|
||||
core.setOutput('pull-request-number', pull.number)
|
||||
core.setOutput('pull-request-url', pull.html_url)
|
||||
// Deprecated
|
||||
core.exportVariable('PULL_REQUEST_NUMBER', pull.number)
|
||||
core.endGroup()
|
||||
|
||||
// Set milestone, labels and assignees
|
||||
const updateIssueParams = {}
|
||||
if (inputs.milestone) {
|
||||
updateIssueParams['milestone'] = inputs.milestone
|
||||
core.info(`Applying milestone '${inputs.milestone}'`)
|
||||
}
|
||||
if (inputs.labels.length > 0) {
|
||||
updateIssueParams['labels'] = inputs.labels
|
||||
core.info(`Applying labels '${inputs.labels}'`)
|
||||
}
|
||||
if (inputs.assignees.length > 0) {
|
||||
updateIssueParams['assignees'] = inputs.assignees
|
||||
core.info(`Applying assignees '${inputs.assignees}'`)
|
||||
}
|
||||
if (Object.keys(updateIssueParams).length > 0) {
|
||||
await this.octokit.issues.update({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
...updateIssueParams
|
||||
})
|
||||
}
|
||||
|
||||
// Request reviewers and team reviewers
|
||||
const requestReviewersParams = {}
|
||||
if (inputs.reviewers.length > 0) {
|
||||
requestReviewersParams['reviewers'] = inputs.reviewers
|
||||
core.info(`Requesting reviewers '${inputs.reviewers}'`)
|
||||
}
|
||||
if (inputs.teamReviewers.length > 0) {
|
||||
requestReviewersParams['team_reviewers'] = inputs.teamReviewers
|
||||
core.info(`Requesting team reviewers '${inputs.teamReviewers}'`)
|
||||
}
|
||||
if (Object.keys(requestReviewersParams).length > 0) {
|
||||
try {
|
||||
await this.octokit.pulls.requestReviewers({
|
||||
...this.parseRepository(baseRepository),
|
||||
pull_number: pull.number,
|
||||
...requestReviewersParams
|
||||
})
|
||||
} catch (e) {
|
||||
if (e.message && e.message.includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
|
||||
core.warning(ERROR_PR_REVIEW_FROM_AUTHOR)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
120
src/index.js
120
src/index.js
@ -1,120 +0,0 @@
|
||||
const { inspect } = require("util");
|
||||
const isDocker = require("is-docker");
|
||||
const core = require("@actions/core");
|
||||
const exec = require("@actions/exec");
|
||||
const setupPython = require("./setup-python");
|
||||
const {
|
||||
getRepoPath,
|
||||
getAndUnsetConfigOption,
|
||||
addConfigOption
|
||||
} = require("./git");
|
||||
|
||||
const EXTRAHEADER_OPTION = "http.https://github.com/.extraheader";
|
||||
const EXTRAHEADER_VALUE_REGEX = "^AUTHORIZATION:";
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
// Allows ncc to find assets to be included in the distribution
|
||||
const cpr = __dirname + "/cpr";
|
||||
core.debug(`cpr: ${cpr}`);
|
||||
|
||||
// Determine how to access python and pip
|
||||
const { pip, python } = (function() {
|
||||
if (isDocker()) {
|
||||
core.info("Running inside a Docker container");
|
||||
// Python 3 assumed to be installed and on the PATH
|
||||
return {
|
||||
pip: "pip3",
|
||||
python: "python3"
|
||||
};
|
||||
} else {
|
||||
// Setup Python from the tool cache
|
||||
setupPython("3.x", "x64");
|
||||
return {
|
||||
pip: "pip",
|
||||
python: "python"
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
// Install requirements
|
||||
await exec.exec(pip, [
|
||||
"install",
|
||||
"--requirement",
|
||||
`${cpr}/requirements.txt`,
|
||||
"--no-index",
|
||||
`--find-links=${__dirname}/vendor`
|
||||
]);
|
||||
|
||||
// Fetch action inputs
|
||||
const inputs = {
|
||||
token: core.getInput("token"),
|
||||
path: core.getInput("path"),
|
||||
commitMessage: core.getInput("commit-message"),
|
||||
committer: core.getInput("committer"),
|
||||
author: core.getInput("author"),
|
||||
title: core.getInput("title"),
|
||||
body: core.getInput("body"),
|
||||
labels: core.getInput("labels"),
|
||||
assignees: core.getInput("assignees"),
|
||||
reviewers: core.getInput("reviewers"),
|
||||
teamReviewers: core.getInput("team-reviewers"),
|
||||
milestone: core.getInput("milestone"),
|
||||
project: core.getInput("project"),
|
||||
projectColumn: core.getInput("project-column"),
|
||||
branch: core.getInput("branch"),
|
||||
request_to_parent: core.getInput("request-to-parent"),
|
||||
base: core.getInput("base"),
|
||||
branchSuffix: core.getInput("branch-suffix")
|
||||
};
|
||||
core.debug(`Inputs: ${inspect(inputs)}`);
|
||||
|
||||
// Set environment variables from inputs.
|
||||
if (inputs.token) process.env.GITHUB_TOKEN = inputs.token;
|
||||
if (inputs.path) process.env.CPR_PATH = inputs.path;
|
||||
if (inputs.commitMessage) process.env.CPR_COMMIT_MESSAGE = inputs.commitMessage;
|
||||
if (inputs.committer) process.env.CPR_COMMITTER = inputs.committer;
|
||||
if (inputs.author) process.env.CPR_AUTHOR = inputs.author;
|
||||
if (inputs.title) process.env.CPR_TITLE = inputs.title;
|
||||
if (inputs.body) process.env.CPR_BODY = inputs.body;
|
||||
if (inputs.labels) process.env.CPR_LABELS = inputs.labels;
|
||||
if (inputs.assignees) process.env.CPR_ASSIGNEES = inputs.assignees;
|
||||
if (inputs.reviewers) process.env.CPR_REVIEWERS = inputs.reviewers;
|
||||
if (inputs.teamReviewers) process.env.CPR_TEAM_REVIEWERS = inputs.teamReviewers;
|
||||
if (inputs.milestone) process.env.CPR_MILESTONE = inputs.milestone;
|
||||
if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project;
|
||||
if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn;
|
||||
if (inputs.branch) process.env.CPR_BRANCH = inputs.branch;
|
||||
if (inputs.request_to_parent) process.env.CPR_REQUEST_TO_PARENT = inputs.request_to_parent;
|
||||
if (inputs.base) process.env.CPR_BASE = inputs.base;
|
||||
if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix;
|
||||
|
||||
// Get the repository path
|
||||
var repoPath = getRepoPath(inputs.path);
|
||||
// Get the extraheader config option if it exists
|
||||
var extraHeaderOption = await getAndUnsetConfigOption(
|
||||
repoPath,
|
||||
EXTRAHEADER_OPTION,
|
||||
EXTRAHEADER_VALUE_REGEX
|
||||
);
|
||||
|
||||
// Execute create pull request
|
||||
await exec.exec(python, [`${cpr}/create_pull_request.py`]);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
} finally {
|
||||
// Restore the extraheader config option
|
||||
if (extraHeaderOption) {
|
||||
if (
|
||||
await addConfigOption(
|
||||
repoPath,
|
||||
EXTRAHEADER_OPTION,
|
||||
extraHeaderOption.value
|
||||
)
|
||||
)
|
||||
core.debug(`Restored config option '${EXTRAHEADER_OPTION}'`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
37
src/main.ts
Normal file
37
src/main.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import * as core from '@actions/core'
|
||||
import {Inputs, createPullRequest} from './create-pull-request'
|
||||
import {inspect} from 'util'
|
||||
import * as utils from './utils'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const inputs: Inputs = {
|
||||
token: core.getInput('token'),
|
||||
path: core.getInput('path'),
|
||||
commitMessage: core.getInput('commit-message'),
|
||||
committer: core.getInput('committer'),
|
||||
author: core.getInput('author'),
|
||||
signoff: core.getInput('signoff') === 'true',
|
||||
branch: core.getInput('branch'),
|
||||
deleteBranch: core.getInput('delete-branch') === 'true',
|
||||
branchSuffix: core.getInput('branch-suffix'),
|
||||
base: core.getInput('base'),
|
||||
pushToFork: core.getInput('push-to-fork'),
|
||||
title: core.getInput('title'),
|
||||
body: core.getInput('body'),
|
||||
labels: utils.getInputAsArray('labels'),
|
||||
assignees: utils.getInputAsArray('assignees'),
|
||||
reviewers: utils.getInputAsArray('reviewers'),
|
||||
teamReviewers: utils.getInputAsArray('team-reviewers'),
|
||||
milestone: Number(core.getInput('milestone')),
|
||||
draft: core.getInput('draft') === 'true'
|
||||
}
|
||||
core.debug(`Inputs: ${inspect(inputs)}`)
|
||||
|
||||
await createPullRequest(inputs)
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
7
src/octokit-client.ts
Normal file
7
src/octokit-client.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import {Octokit as Core} from '@octokit/core'
|
||||
import {paginateRest} from '@octokit/plugin-paginate-rest'
|
||||
import {restEndpointMethods} from '@octokit/plugin-rest-endpoint-methods'
|
||||
export {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods'
|
||||
export {OctokitOptions} from '@octokit/core/dist-types/types'
|
||||
|
||||
export const Octokit = Core.plugin(paginateRest, restEndpointMethods)
|
@ -1,52 +0,0 @@
|
||||
const core = require("@actions/core");
|
||||
const tc = require("@actions/tool-cache");
|
||||
const path = require("path");
|
||||
const semver = require("semver");
|
||||
|
||||
/**
|
||||
* Setup for Python from the GitHub Actions tool cache
|
||||
* Converted from https://github.com/actions/setup-python
|
||||
*
|
||||
* @param {string} versionSpec version of Python
|
||||
* @param {string} arch architecture (x64|x32)
|
||||
*/
|
||||
let setupPython = function(versionSpec, arch) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const IS_WINDOWS = process.platform === "win32";
|
||||
|
||||
// Find the version of Python we want in the tool cache
|
||||
const installDir = tc.find("Python", versionSpec, arch);
|
||||
core.debug(`installDir: ${installDir}`);
|
||||
|
||||
// Set paths
|
||||
core.exportVariable("pythonLocation", installDir);
|
||||
core.addPath(installDir);
|
||||
if (IS_WINDOWS) {
|
||||
core.addPath(path.join(installDir, "Scripts"));
|
||||
} else {
|
||||
core.addPath(path.join(installDir, "bin"));
|
||||
}
|
||||
|
||||
if (IS_WINDOWS) {
|
||||
// Add --user directory
|
||||
// `installDir` from tool cache should look like $AGENT_TOOLSDIRECTORY/Python/<semantic version>/x64/
|
||||
// So if `findLocalTool` succeeded above, we must have a conformant `installDir`
|
||||
const version = path.basename(path.dirname(installDir));
|
||||
const major = semver.major(version);
|
||||
const minor = semver.minor(version);
|
||||
|
||||
const userScriptsDir = path.join(
|
||||
process.env["APPDATA"] || "",
|
||||
"Python",
|
||||
`Python${major}${minor}`,
|
||||
"Scripts"
|
||||
);
|
||||
core.addPath(userScriptsDir);
|
||||
}
|
||||
// On Linux and macOS, pip will create the --user directory and add it to PATH as needed.
|
||||
|
||||
resolve();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = setupPython;
|
139
src/utils.ts
Normal file
139
src/utils.ts
Normal file
@ -0,0 +1,139 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
export function getInputAsArray(
|
||||
name: string,
|
||||
options?: core.InputOptions
|
||||
): string[] {
|
||||
return getStringAsArray(core.getInput(name, options))
|
||||
}
|
||||
|
||||
export function getStringAsArray(str: string): string[] {
|
||||
return str
|
||||
.split(/[\n,]+/)
|
||||
.map(s => s.trim())
|
||||
.filter(x => x !== '')
|
||||
}
|
||||
|
||||
export function getRepoPath(relativePath?: string): string {
|
||||
let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
|
||||
if (!githubWorkspacePath) {
|
||||
throw new Error('GITHUB_WORKSPACE not defined')
|
||||
}
|
||||
githubWorkspacePath = path.resolve(githubWorkspacePath)
|
||||
core.debug(`githubWorkspacePath: ${githubWorkspacePath}`)
|
||||
|
||||
let repoPath = githubWorkspacePath
|
||||
if (relativePath) repoPath = path.resolve(repoPath, relativePath)
|
||||
|
||||
core.debug(`repoPath: ${repoPath}`)
|
||||
return repoPath
|
||||
}
|
||||
|
||||
interface RemoteDetail {
|
||||
protocol: string
|
||||
repository: string
|
||||
}
|
||||
|
||||
export function getRemoteDetail(remoteUrl: string): RemoteDetail {
|
||||
// Parse the protocol and github repository from a URL
|
||||
// e.g. HTTPS, peter-evans/create-pull-request
|
||||
const httpsUrlPattern = /^https:\/\/.*@?github.com\/(.+\/.+)$/i
|
||||
const sshUrlPattern = /^git@github.com:(.+\/.+).git$/i
|
||||
|
||||
const httpsMatch = remoteUrl.match(httpsUrlPattern)
|
||||
if (httpsMatch) {
|
||||
return {
|
||||
protocol: 'HTTPS',
|
||||
repository: httpsMatch[1]
|
||||
}
|
||||
}
|
||||
|
||||
const sshMatch = remoteUrl.match(sshUrlPattern)
|
||||
if (sshMatch) {
|
||||
return {
|
||||
protocol: 'SSH',
|
||||
repository: sshMatch[1]
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`The format of '${remoteUrl}' is not a valid GitHub repository URL`
|
||||
)
|
||||
}
|
||||
|
||||
export function getRemoteUrl(protocol: string, repository: string): string {
|
||||
return protocol == 'HTTPS'
|
||||
? `https://github.com/${repository}`
|
||||
: `git@github.com:${repository}.git`
|
||||
}
|
||||
|
||||
export function secondsSinceEpoch(): number {
|
||||
const now = new Date()
|
||||
return Math.round(now.getTime() / 1000)
|
||||
}
|
||||
|
||||
export function randomString(): string {
|
||||
return Math.random().toString(36).substr(2, 7)
|
||||
}
|
||||
|
||||
interface DisplayNameEmail {
|
||||
name: string
|
||||
email: string
|
||||
}
|
||||
|
||||
export function parseDisplayNameEmail(
|
||||
displayNameEmail: string
|
||||
): DisplayNameEmail {
|
||||
// Parse the name and email address from a string in the following format
|
||||
// Display Name <email@address.com>
|
||||
const pattern = /^([^<]+)\s*<([^>]+)>$/i
|
||||
|
||||
// Check we have a match
|
||||
const match = displayNameEmail.match(pattern)
|
||||
if (!match) {
|
||||
throw new Error(
|
||||
`The format of '${displayNameEmail}' is not a valid email address with display name`
|
||||
)
|
||||
}
|
||||
|
||||
// Check that name and email are not just whitespace
|
||||
const name = match[1].trim()
|
||||
const email = match[2].trim()
|
||||
if (!name || !email) {
|
||||
throw new Error(
|
||||
`The format of '${displayNameEmail}' is not a valid email address with display name`
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
name: name,
|
||||
email: email
|
||||
}
|
||||
}
|
||||
|
||||
export function fileExistsSync(path: string): boolean {
|
||||
if (!path) {
|
||||
throw new Error("Arg 'path' must not be empty")
|
||||
}
|
||||
|
||||
let stats: fs.Stats
|
||||
try {
|
||||
stats = fs.statSync(path)
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
return false
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Encountered an error when checking whether path '${path}' exists: ${error.message}`
|
||||
)
|
||||
}
|
||||
|
||||
if (!stats.isDirectory()) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
16
tsconfig.json
Normal file
16
tsconfig.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"outDir": "./lib",
|
||||
"rootDir": "./src",
|
||||
"declaration": true,
|
||||
"strict": true,
|
||||
"noImplicitAny": false,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"exclude": ["__test__", "lib", "node_modules"]
|
||||
}
|
Reference in New Issue
Block a user