Compare commits
95 Commits
Author | SHA1 | Date | |
---|---|---|---|
7531167f24 | |||
66a1436815 | |||
1d1fedd99c | |||
823751817d | |||
1edcf07a19 | |||
4c95214a9b | |||
b37c0a038c | |||
d9841567d1 | |||
2cce94bfb0 | |||
d968e8b11b | |||
6c73093f9b | |||
d17c882ef3 | |||
d5f4e48a66 | |||
484d8bd85d | |||
74b6a9337d | |||
2dd62d446e | |||
b6a98c049d | |||
67e8822279 | |||
956a240181 | |||
179aca65d7 | |||
e4c8b68e33 | |||
731211fd81 | |||
006313b45a | |||
3c86e9a421 | |||
e49ebca896 | |||
d9652cf06a | |||
4517bf7ce3 | |||
04aa84a2af | |||
11baa5dc07 | |||
1281ebd51a | |||
46dc4f23d5 | |||
d469aaf0ef | |||
5692e041c6 | |||
55e7b1ec28 | |||
d99b58f9d5 | |||
b8ce455e56 | |||
c9e477ec05 | |||
979d417e9d | |||
b74a77cefc | |||
2af7d9797a | |||
b72489d629 | |||
1461de7c9d | |||
3061b7bc6e | |||
98ee7d63d3 | |||
066b4398c0 | |||
028e1bbdd5 | |||
8b8594e5e4 | |||
cefb562076 | |||
9eccdd1603 | |||
f429dfef59 | |||
963e38fd7d | |||
f411b5902d | |||
97dd1f3ef4 | |||
3413a77ac7 | |||
206756cd93 | |||
263b5927d1 | |||
a0ad81c6c7 | |||
23846c7f83 | |||
96ac64ddca | |||
a2ed0052e5 | |||
6e7e889e01 | |||
7e18a60b72 | |||
0be4e396cf | |||
b86bbce924 | |||
e1e9dd3f1c | |||
8c2a43987b | |||
c5ff844b40 | |||
1f47cf3861 | |||
bcdc7ae6f9 | |||
91ca84eab5 | |||
875d189c30 | |||
7c3f5f4833 | |||
7a7e797068 | |||
a41f37fc96 | |||
10554ac6c3 | |||
2439726cfe | |||
4f04f4efd9 | |||
32d0abbc7c | |||
ed5b8b1eea | |||
ea241dfc79 | |||
0e70fec054 | |||
5077e8d6bd | |||
901e854f30 | |||
f3b1bd6331 | |||
51ade9f54b | |||
3c86dbf9e6 | |||
b2d426ea4e | |||
fbe0cd3d1a | |||
5f9821866a | |||
ee153ec8cf | |||
e138d84114 | |||
90843d3c39 | |||
90ad96bd01 | |||
f8cc05d28d | |||
fe78afb6d7 |
48
.github/workflows/create-pull-request-multi.yml
vendored
48
.github/workflows/create-pull-request-multi.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: Create Pull Request All Platforms
|
||||
on:
|
||||
repository_dispatch:
|
||||
types: [create-pull-request-multi]
|
||||
name: create-pull-request workflow
|
||||
jobs:
|
||||
createPullRequest:
|
||||
name: Testing on ${{ matrix.platform }}
|
||||
@ -18,23 +18,29 @@ jobs:
|
||||
if: matrix.platform == 'windows-latest'
|
||||
run: echo %DATE% %TIME% > report.txt
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@multi-platform-release
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
COMMIT_MESSAGE: Add report file
|
||||
PULL_REQUEST_BODY: >
|
||||
This PR is auto-generated by
|
||||
[create-pull-request](https://github.com/peter-evans/create-pull-request).
|
||||
PULL_REQUEST_TITLE: '[Example] Add report file'
|
||||
PULL_REQUEST_LABELS: report, automated pr
|
||||
PULL_REQUEST_ASSIGNEES: peter-evans
|
||||
PULL_REQUEST_REVIEWERS: peter-evans
|
||||
PULL_REQUEST_MILESTONE: 1
|
||||
PULL_REQUEST_BRANCH: example-patches
|
||||
BRANCH_SUFFIX: 'random'
|
||||
- name: Check output environment variable
|
||||
if: matrix.platform == 'ubuntu-latest' || matrix.platform == 'macos-latest'
|
||||
run: echo "Pull Request Number - $PULL_REQUEST_NUMBER"
|
||||
- name: Check output environment variable (windows)
|
||||
if: matrix.platform == 'windows-latest'
|
||||
run: echo Pull Request Number - %PULL_REQUEST_NUMBER%
|
||||
id: cpr
|
||||
uses: ./
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Add report file
|
||||
author-email: peter-evans@users.noreply.github.com
|
||||
author-name: Peter Evans
|
||||
title: '[Example] Add report file'
|
||||
body: |
|
||||
New report
|
||||
- Contains *today's* date
|
||||
- Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
labels: report, automated pr
|
||||
assignees: peter-evans
|
||||
reviewers: peter-evans
|
||||
milestone: 1
|
||||
project: Example Project
|
||||
project-column: To do
|
||||
branch: example-patches
|
||||
branch-suffix: random
|
||||
- name: Check outputs
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
|
42
.github/workflows/create-pull-request.yml
vendored
42
.github/workflows/create-pull-request.yml
vendored
@ -1,7 +1,7 @@
|
||||
name: Create Pull Request
|
||||
on:
|
||||
repository_dispatch:
|
||||
types: [create-pull-request]
|
||||
name: create-pull-request workflow
|
||||
jobs:
|
||||
createPullRequest:
|
||||
runs-on: ubuntu-latest
|
||||
@ -10,19 +10,29 @@ jobs:
|
||||
- name: Create report file
|
||||
run: date +%s > report.txt
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: ./
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
COMMIT_MESSAGE: Add report file
|
||||
PULL_REQUEST_BODY: >
|
||||
This PR is auto-generated by
|
||||
[create-pull-request](https://github.com/peter-evans/create-pull-request).
|
||||
PULL_REQUEST_TITLE: '[Example] Add report file'
|
||||
PULL_REQUEST_LABELS: report, automated pr
|
||||
PULL_REQUEST_ASSIGNEES: peter-evans
|
||||
PULL_REQUEST_REVIEWERS: peter-evans
|
||||
PULL_REQUEST_MILESTONE: 1
|
||||
PULL_REQUEST_BRANCH: example-patches
|
||||
BRANCH_SUFFIX: short-commit-hash
|
||||
- name: Check output environment variable
|
||||
run: echo "Pull Request Number - $PULL_REQUEST_NUMBER"
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Add report file
|
||||
author-email: peter-evans@users.noreply.github.com
|
||||
author-name: Peter Evans
|
||||
title: '[Example] Add report file'
|
||||
body: |
|
||||
New report
|
||||
- Contains *today's* date
|
||||
- Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
labels: report, automated pr
|
||||
assignees: peter-evans
|
||||
reviewers: peter-evans
|
||||
milestone: 1
|
||||
project: Example Project
|
||||
project-column: To do
|
||||
branch: example-patches
|
||||
branch-suffix: short-commit-hash
|
||||
- name: Check outputs
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
|
@ -2,7 +2,10 @@ name: Update Docker Hub Description
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- master
|
||||
paths:
|
||||
- README.md
|
||||
- .github/workflows/dockerhub-description.yml
|
||||
jobs:
|
||||
dockerHubDescription:
|
||||
runs-on: ubuntu-latest
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
node_modules
|
||||
.DS_Store
|
||||
|
20
Dockerfile
20
Dockerfile
@ -1,20 +0,0 @@
|
||||
FROM alpine:3.10.2
|
||||
|
||||
LABEL maintainer="Peter Evans <mail@peterevans.dev>"
|
||||
LABEL repository="https://github.com/peter-evans/create-pull-request"
|
||||
LABEL homepage="https://github.com/peter-evans/create-pull-request"
|
||||
|
||||
LABEL com.github.actions.name="Create Pull Request"
|
||||
LABEL com.github.actions.description="Creates a pull request for changes to your repository in the actions workspace"
|
||||
LABEL com.github.actions.icon="git-pull-request"
|
||||
LABEL com.github.actions.color="gray-dark"
|
||||
|
||||
COPY LICENSE README.md /
|
||||
|
||||
RUN apk add python3-dev git git-lfs
|
||||
|
||||
COPY requirements.txt /tmp/
|
||||
RUN pip3 install --requirement /tmp/requirements.txt
|
||||
|
||||
COPY create-pull-request.py /create-pull-request.py
|
||||
ENTRYPOINT [ "/create-pull-request.py" ]
|
136
README.md
136
README.md
@ -1,4 +1,4 @@
|
||||
# <img width="24" height="24" src="logo.svg"> Create Pull Request
|
||||
# <img width="24" height="24" src="assets/logo.svg"> Create Pull Request
|
||||
[](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.
|
||||
@ -10,53 +10,63 @@ The changes will be automatically committed to a new branch and a pull request c
|
||||
Create Pull Request action will:
|
||||
|
||||
1. Check for repository changes in the Actions workspace. This includes untracked (new) files as well as modified files.
|
||||
2. Commit all changes to a new branch, or update an existing pull request branch. The commit will be made using the name and email of the `HEAD` commit author.
|
||||
2. Commit all changes to a new branch, or update an existing pull request branch.
|
||||
3. Create a pull request to merge the new branch into the currently active branch executing the workflow.
|
||||
|
||||
## Usage
|
||||
|
||||
Linux
|
||||
See [examples](examples.md) for detailed use cases.
|
||||
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.4.1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
```
|
||||
|
||||
Multi platform - Linux, MacOS, Windows (beta)
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.4.1-multi
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
```
|
||||
You can also pin to a [specific release](https://github.com/peter-evans/create-pull-request/releases) version in the format `@v1.x.x`
|
||||
|
||||
**Note**: If you want pull requests created by this action to trigger an `on: pull_request` workflow then you must use a Personal Access Token instead of the default `GITHUB_TOKEN`.
|
||||
**Note**: If you want pull requests created by this action to trigger an `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`.
|
||||
See [this issue](https://github.com/peter-evans/create-pull-request/issues/48) for further details.
|
||||
|
||||
### Environment variables
|
||||
### Action inputs
|
||||
|
||||
These variables are all optional. If not set, a default value will be used.
|
||||
These inputs are *all optional*. If not set, sensible default values will be used.
|
||||
|
||||
- `COMMIT_MESSAGE` - The message to use when committing changes.
|
||||
- `PULL_REQUEST_TITLE` - The title of the pull request.
|
||||
- `PULL_REQUEST_BODY` - The body of the pull request.
|
||||
- `PULL_REQUEST_LABELS` - A comma separated list of labels.
|
||||
- `PULL_REQUEST_ASSIGNEES` - A comma separated list of assignees (GitHub usernames).
|
||||
- `PULL_REQUEST_REVIEWERS` - A comma separated list of reviewers (GitHub usernames) to request a review from.
|
||||
- `PULL_REQUEST_TEAM_REVIEWERS` - A comma separated list of GitHub teams to request a review from.
|
||||
- `PULL_REQUEST_MILESTONE` - The number of the milestone to associate this pull request with.
|
||||
- `PULL_REQUEST_BRANCH` - The branch name. See **Branch naming** below for details.
|
||||
- `BRANCH_SUFFIX` - The branch suffix type. Valid values are `short-commit-hash` (default), `timestamp`, `random` and `none`. See **Branch naming** below for details.
|
||||
| Name | Description | Default |
|
||||
| --- | --- | --- |
|
||||
| `commit-message` | The message to use when committing changes. | `Auto-committed changes by create-pull-request action` |
|
||||
| `author-email` | The email address of the commit author. | For `push` events, the HEAD commit author. Otherwise, <GITHUB_ACTOR>@users.noreply.github.com, where `GITHUB_ACTOR` is the GitHub user that initiated the event. |
|
||||
| `author-name` | The name of the commit author. | For `push` events, the HEAD commit author. Otherwise, <GITHUB_ACTOR>, the GitHub user that initiated the event. |
|
||||
| `title` | The title of the pull request. | `Auto-generated by create-pull-request action` |
|
||||
| `body` | The body of the pull request. | `Auto-generated pull request by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action` |
|
||||
| `labels` | A comma separated list of labels. | none |
|
||||
| `assignees` | A comma separated list of assignees (GitHub usernames). | none |
|
||||
| `reviewers` | A comma separated list of reviewers (GitHub usernames) to request a review from. | none |
|
||||
| `team-reviewers` | A comma separated list of GitHub teams to request a review from. | none |
|
||||
| `milestone` | The number of the milestone to associate this pull request with. | none |
|
||||
| `project` | The name of the project for which a card should be created. Requires `project-column`. | none |
|
||||
| `project-column` | The name of the project column under which a card should be created. Requires `project`. | none |
|
||||
| `branch` | The branch name. See **Branch naming** below for details. | `create-pull-request/patch` |
|
||||
| `base` | Sets the pull request base branch. | Defaults to the currently checked out branch, `GITHUB_REF`. For `pull_request` events, `GITHUB_HEAD_REF` |
|
||||
| `branch-suffix` | The branch suffix type. Valid values are `short-commit-hash`, `timestamp`, `random` and `none`. See **Branch naming** below for details. | `short-commit-hash` |
|
||||
|
||||
Output environment variables
|
||||
**Outputs**
|
||||
|
||||
- `PULL_REQUEST_NUMBER` - The number of the pull request created.
|
||||
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 following parameters are available for debugging and troubleshooting.
|
||||
|
||||
- `DEBUG_EVENT` - If present, outputs the event data that triggered the workflow.
|
||||
- `SKIP_IGNORE` - If present, the `ignore_event` function will be skipped.
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Check outputs
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
```
|
||||
|
||||
### Branch naming
|
||||
|
||||
@ -65,7 +75,7 @@ For branch naming there are two strategies. Always create a new branch each time
|
||||
#### Strategy A - Always create a new pull request branch (default)
|
||||
|
||||
For this strategy there are three options to suffix the branch name.
|
||||
The branch name is defined by the variable `PULL_REQUEST_BRANCH` and defaults to `create-pull-request/patch`. The following options are values for `BRANCH_SUFFIX`.
|
||||
The branch name is defined by the input `branch` and defaults to `create-pull-request/patch`. The following options are values for `branch-suffix`.
|
||||
|
||||
- `short-commit-hash` (default) - 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`
|
||||
|
||||
@ -75,21 +85,21 @@ The branch name is defined by the variable `PULL_REQUEST_BRANCH` and defaults to
|
||||
|
||||
#### Strategy B - Create and update a pull request branch
|
||||
|
||||
To use this strategy, set `BRANCH_SUFFIX` to the value `none`. The variable `PULL_REQUEST_BRANCH` defaults to `create-pull-request/patch`. Commits will be made to this branch and a pull request created. Any subsequent changes will be committed to the *same* branch and reflected in the existing pull request.
|
||||
To use this strategy, set `branch-suffix` to the value `none`. The input `branch` defaults to `create-pull-request/patch`. Commits will be made to this branch and a pull request created. Any subsequent changes will be committed to the *same* branch and reflected in the existing pull request.
|
||||
|
||||
### 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.
|
||||
|
||||
## Example
|
||||
## Reference Example
|
||||
|
||||
Here is an example that sets all the main environment variables.
|
||||
The following workflow is a reference example that sets all the main inputs.
|
||||
|
||||
See [examples](examples.md) for more realistic use cases.
|
||||
|
||||
```yml
|
||||
on:
|
||||
repository_dispatch:
|
||||
types: [create-pull-request]
|
||||
name: create-pull-request workflow
|
||||
name: Create Pull Request
|
||||
on: push
|
||||
jobs:
|
||||
createPullRequest:
|
||||
runs-on: ubuntu-latest
|
||||
@ -98,27 +108,37 @@ jobs:
|
||||
- name: Create report file
|
||||
run: date +%s > report.txt
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.4.1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
COMMIT_MESSAGE: Add report file
|
||||
PULL_REQUEST_BODY: >
|
||||
This PR is auto-generated by
|
||||
[create-pull-request](https://github.com/peter-evans/create-pull-request).
|
||||
PULL_REQUEST_TITLE: '[Example] Add report file'
|
||||
PULL_REQUEST_LABELS: report, automated pr
|
||||
PULL_REQUEST_ASSIGNEES: peter-evans
|
||||
PULL_REQUEST_REVIEWERS: peter-evans
|
||||
PULL_REQUEST_MILESTONE: 1
|
||||
PULL_REQUEST_BRANCH: example-patches
|
||||
BRANCH_SUFFIX: short-commit-hash
|
||||
- name: Check output environment variable
|
||||
run: echo "Pull Request Number - $PULL_REQUEST_NUMBER"
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Add report file
|
||||
author-email: peter-evans@users.noreply.github.com
|
||||
author-name: Peter Evans
|
||||
title: '[Example] Add report file'
|
||||
body: |
|
||||
New report
|
||||
- Contains *today's* date
|
||||
- Auto-generated by [create-pull-request][1]
|
||||
|
||||
[1]: https://github.com/peter-evans/create-pull-request
|
||||
labels: report, automated pr
|
||||
assignees: peter-evans
|
||||
reviewers: peter-evans
|
||||
milestone: 1
|
||||
project: Example Project
|
||||
project-column: To do
|
||||
branch: example-patches
|
||||
branch-suffix: short-commit-hash
|
||||
- name: Check outputs
|
||||
run: |
|
||||
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"
|
||||
echo "Pull Request Number - ${{ steps.cpr.outputs.pr_number }}"
|
||||
```
|
||||
|
||||
This configuration will create pull requests that look like this:
|
||||
This reference configuration will create pull requests that look like this:
|
||||
|
||||

|
||||

|
||||
|
||||
## License
|
||||
|
||||
|
42
action.yml
42
action.yml
@ -1,9 +1,45 @@
|
||||
name: 'Create Pull Request'
|
||||
author: 'Peter Evans'
|
||||
description: 'Creates a pull request for changes to your repository in the actions workspace'
|
||||
inputs:
|
||||
token:
|
||||
description: 'The GitHub authentication token'
|
||||
required: true
|
||||
commit-message:
|
||||
description: 'The message to use when committing changes.'
|
||||
author-email:
|
||||
description: 'The email address of the commit author.'
|
||||
author-name:
|
||||
description: 'The name of the commit author.'
|
||||
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.'
|
||||
branch:
|
||||
description: 'The pull request branch name.'
|
||||
base:
|
||||
description: 'Sets the pull request base branch.'
|
||||
branch-suffix:
|
||||
description: 'The branch suffix type.'
|
||||
outputs:
|
||||
pr_number:
|
||||
description: 'The pull request number'
|
||||
runs:
|
||||
using: 'docker'
|
||||
image: 'docker://peterevans/create-pull-request:1.5.0'
|
||||
using: 'node12'
|
||||
main: 'dist/index.js'
|
||||
branding:
|
||||
icon: 'git-pull-request'
|
||||
color: 'gray-dark'
|
||||
|
Before Width: | Height: | Size: 416 B After Width: | Height: | Size: 416 B |
BIN
assets/pull-request-example.png
Normal file
BIN
assets/pull-request-example.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 441 KiB |
@ -1,218 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
''' Create Pull Request '''
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import sys
|
||||
import time
|
||||
from git import Repo
|
||||
from github import Github
|
||||
|
||||
|
||||
def get_github_event(github_event_path):
|
||||
with open(github_event_path) as f:
|
||||
github_event = json.load(f)
|
||||
if bool(os.environ.get('DEBUG_EVENT')):
|
||||
print(os.environ['GITHUB_EVENT_NAME'])
|
||||
print(json.dumps(github_event, sort_keys=True, indent=2))
|
||||
return github_event
|
||||
|
||||
|
||||
def ignore_event(event_name, event_data):
|
||||
if event_name == "push":
|
||||
# Ignore push events on deleted branches
|
||||
# The event we want to ignore occurs when a PR is created but the repository owner decides
|
||||
# not to commit the changes. They close the PR and delete the branch. This creates a
|
||||
# "push" event that we want to ignore, otherwise it will create another branch and PR on
|
||||
# the same commit.
|
||||
deleted = "{deleted}".format(**event_data)
|
||||
if deleted == "True":
|
||||
print("Ignoring delete branch event.")
|
||||
return True
|
||||
ref = "{ref}".format(**event_data)
|
||||
if not ref.startswith('refs/heads/'):
|
||||
print("Ignoring events for tags and remotes.")
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_head_short_sha1(repo):
|
||||
return repo.git.rev_parse('--short', 'HEAD')
|
||||
|
||||
|
||||
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
||||
return ''.join(random.choice(chars) for _ in range(size))
|
||||
|
||||
|
||||
def remote_branch_exists(repo, branch):
|
||||
for ref in repo.remotes.origin.refs:
|
||||
if ref.name == ("origin/%s" % branch):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_head_author(event_name, event_data):
|
||||
if event_name == "push":
|
||||
email = "{head_commit[author][email]}".format(**event_data)
|
||||
name = "{head_commit[author][name]}".format(**event_data)
|
||||
else:
|
||||
email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com'
|
||||
name = os.environ['GITHUB_ACTOR']
|
||||
return email, name
|
||||
|
||||
|
||||
def set_git_config(git, email, name):
|
||||
git.config('--global', 'user.email', '"%s"' % email)
|
||||
git.config('--global', 'user.name', '"%s"' % name)
|
||||
|
||||
|
||||
def set_git_remote_url(git, token, github_repository):
|
||||
git.remote('set-url', 'origin', "https://x-access-token:%s@github.com/%s" % (token, github_repository))
|
||||
|
||||
|
||||
def checkout_branch(git, remote_exists, branch):
|
||||
if remote_exists:
|
||||
git.stash('--include-untracked')
|
||||
git.checkout(branch)
|
||||
try:
|
||||
git.stash('pop')
|
||||
except:
|
||||
git.checkout('--theirs', '.')
|
||||
git.reset()
|
||||
else:
|
||||
git.checkout('HEAD', b=branch)
|
||||
|
||||
|
||||
def push_changes(git, branch, commit_message):
|
||||
git.add('-A')
|
||||
git.commit(m=commit_message)
|
||||
return git.push('-f', '--set-upstream', 'origin', branch)
|
||||
|
||||
|
||||
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 process_event(event_name, event_data, repo, branch, base, remote_exists):
|
||||
# Fetch required environment variables
|
||||
github_token = os.environ['GITHUB_TOKEN']
|
||||
github_repository = os.environ['GITHUB_REPOSITORY']
|
||||
# Fetch optional environment variables with default values
|
||||
commit_message = os.getenv(
|
||||
'COMMIT_MESSAGE',
|
||||
"Auto-committed changes by create-pull-request action")
|
||||
title = os.getenv(
|
||||
'PULL_REQUEST_TITLE',
|
||||
"Auto-generated by create-pull-request action")
|
||||
body = os.getenv(
|
||||
'PULL_REQUEST_BODY', "Auto-generated pull request by "
|
||||
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action")
|
||||
# Fetch optional environment variables with no default values
|
||||
pull_request_labels = os.environ.get('PULL_REQUEST_LABELS')
|
||||
pull_request_assignees = os.environ.get('PULL_REQUEST_ASSIGNEES')
|
||||
pull_request_milestone = os.environ.get('PULL_REQUEST_MILESTONE')
|
||||
pull_request_reviewers = os.environ.get('PULL_REQUEST_REVIEWERS')
|
||||
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
||||
|
||||
# Update URL for the 'origin' remote
|
||||
set_git_remote_url(repo.git, github_token, github_repository)
|
||||
|
||||
# Push the local changes to the remote branch
|
||||
print("Pushing changes.")
|
||||
push_result = push_changes(repo.git, branch, commit_message)
|
||||
print(push_result)
|
||||
|
||||
# If the remote existed then a PR likely exists and we can finish here
|
||||
if remote_exists:
|
||||
print("Updated pull request branch %s." % branch)
|
||||
sys.exit()
|
||||
|
||||
# Create the pull request
|
||||
print("Creating a request to pull %s into %s." % (branch, base))
|
||||
github_repo = Github(github_token).get_repo(github_repository)
|
||||
pull_request = github_repo.create_pull(
|
||||
title=title,
|
||||
body=body,
|
||||
base=base,
|
||||
head=branch)
|
||||
print("Created pull request %d." % pull_request.number)
|
||||
os.system('echo ::set-env name=PULL_REQUEST_NUMBER::%d' % pull_request.number)
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if pull_request_labels is not None:
|
||||
print("Applying labels")
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||
if pull_request_assignees is not None:
|
||||
print("Applying assignees")
|
||||
pull_request.as_issue().edit(assignees=cs_string_to_list(pull_request_assignees))
|
||||
if pull_request_milestone is not None:
|
||||
print("Applying milestone")
|
||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers and team reviewers
|
||||
if pull_request_reviewers is not None:
|
||||
print("Requesting reviewers")
|
||||
pull_request.create_review_request(reviewers=cs_string_to_list(pull_request_reviewers))
|
||||
if pull_request_team_reviewers is not None:
|
||||
print("Requesting team reviewers")
|
||||
pull_request.create_review_request(team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
||||
|
||||
|
||||
# Get the JSON event data
|
||||
event_name = os.environ['GITHUB_EVENT_NAME']
|
||||
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
|
||||
# Check if this event should be ignored
|
||||
skip_ignore_event = bool(os.environ.get('SKIP_IGNORE'))
|
||||
if skip_ignore_event or not ignore_event(event_name, event_data):
|
||||
# Set the repo to the working directory
|
||||
repo = Repo(os.getcwd())
|
||||
|
||||
# Fetch/Set the branch name
|
||||
branch = os.getenv('PULL_REQUEST_BRANCH', 'create-pull-request/patch')
|
||||
# Set the current branch as the target base branch
|
||||
base = os.environ['GITHUB_REF'][11:]
|
||||
|
||||
# Skip if the current branch is a PR branch created by this action
|
||||
if base.startswith(branch):
|
||||
print("Branch '%s' was created by this action. Skipping." % base)
|
||||
sys.exit()
|
||||
|
||||
# Fetch an optional environment variable to determine the branch suffix
|
||||
branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# Suffix with the short SHA1 hash
|
||||
branch = "%s-%s" % (branch, get_head_short_sha1(repo))
|
||||
elif branch_suffix == "timestamp":
|
||||
# Suffix with the current timestamp
|
||||
branch = "%s-%s" % (branch, int(time.time()))
|
||||
elif branch_suffix == "random":
|
||||
# Suffix with the current timestamp
|
||||
branch = "%s-%s" % (branch, get_random_suffix())
|
||||
|
||||
# Check if the remote branch exists
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
|
||||
# If using short commit hash prefixes, check if a remote
|
||||
# branch already exists for this HEAD commit
|
||||
if branch_suffix == 'short-commit-hash' and remote_exists:
|
||||
print("Pull request branch '%s' already exists for this commit. Skipping." % branch)
|
||||
sys.exit()
|
||||
|
||||
# Get the HEAD committer's email and name
|
||||
author_email, author_name = get_head_author(event_name, event_data)
|
||||
# Set git configuration
|
||||
set_git_config(repo.git, author_email, author_name)
|
||||
# Checkout branch
|
||||
checkout_branch(repo.git, remote_exists, branch)
|
||||
|
||||
# Check if there are changes to pull request
|
||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
print("Repository has modified or untracked files.")
|
||||
process_event(event_name, event_data, repo, branch, base, remote_exists)
|
||||
else:
|
||||
print("Repository has no modified or untracked files. Skipping.")
|
4528
dist/index.js
vendored
Normal file
4528
dist/index.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
350
dist/src/create-pull-request.py
vendored
Executable file
350
dist/src/create-pull-request.py
vendored
Executable file
@ -0,0 +1,350 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create Pull Request """
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import sys
|
||||
import time
|
||||
from git import Repo
|
||||
from github import Github, GithubException
|
||||
|
||||
|
||||
def get_github_event(github_event_path):
|
||||
with open(github_event_path) as f:
|
||||
github_event = json.load(f)
|
||||
if bool(os.environ.get("DEBUG_EVENT")):
|
||||
print(os.environ["GITHUB_EVENT_NAME"])
|
||||
print(json.dumps(github_event, sort_keys=True, indent=2))
|
||||
return github_event
|
||||
|
||||
|
||||
def get_head_short_sha1(repo):
|
||||
return repo.git.rev_parse("--short", "HEAD")
|
||||
|
||||
|
||||
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
||||
return "".join(random.choice(chars) for _ in range(size))
|
||||
|
||||
|
||||
def remote_branch_exists(repo, branch):
|
||||
for ref in repo.remotes.origin.refs:
|
||||
if ref.name == ("origin/%s" % branch):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_author_default(event_name, event_data):
|
||||
if event_name == "push":
|
||||
email = "{head_commit[author][email]}".format(**event_data)
|
||||
name = "{head_commit[author][name]}".format(**event_data)
|
||||
else:
|
||||
email = os.environ["GITHUB_ACTOR"] + "@users.noreply.github.com"
|
||||
name = os.environ["GITHUB_ACTOR"]
|
||||
return email, name
|
||||
|
||||
|
||||
def set_git_config(git, email, name):
|
||||
print("Configuring git user as '%s <%s>'" % (name, email))
|
||||
git.config("--global", "user.email", '"%s"' % email)
|
||||
git.config("--global", "user.name", '"%s"' % name)
|
||||
|
||||
|
||||
def set_git_remote_url(git, token, github_repository):
|
||||
git.remote(
|
||||
"set-url",
|
||||
"origin",
|
||||
"https://x-access-token:%s@github.com/%s" % (token, github_repository),
|
||||
)
|
||||
|
||||
|
||||
def checkout_branch(git, remote_exists, branch):
|
||||
if remote_exists:
|
||||
print("Checking out branch '%s'" % branch)
|
||||
git.stash("--include-untracked")
|
||||
git.checkout(branch)
|
||||
try:
|
||||
git.stash("pop")
|
||||
except BaseException:
|
||||
git.checkout("--theirs", ".")
|
||||
git.reset()
|
||||
else:
|
||||
print("Creating new branch '%s'" % branch)
|
||||
git.checkout("HEAD", b=branch)
|
||||
|
||||
|
||||
def push_changes(git, branch, commit_message):
|
||||
git.add("-A")
|
||||
git.commit(m=commit_message)
|
||||
return git.push("-f", "--set-upstream", "origin", branch)
|
||||
|
||||
|
||||
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("::warning::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("::warning::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 process_event(github_token, github_repository, repo, branch, base):
|
||||
# Fetch optional environment variables with default values
|
||||
commit_message = os.getenv(
|
||||
"COMMIT_MESSAGE", "Auto-committed changes by create-pull-request action"
|
||||
)
|
||||
title = os.getenv(
|
||||
"PULL_REQUEST_TITLE", "Auto-generated by create-pull-request action"
|
||||
)
|
||||
body = os.getenv(
|
||||
"PULL_REQUEST_BODY",
|
||||
"Auto-generated pull request by "
|
||||
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action",
|
||||
)
|
||||
# Fetch optional environment variables with no default values
|
||||
pull_request_labels = os.environ.get("PULL_REQUEST_LABELS")
|
||||
pull_request_assignees = os.environ.get("PULL_REQUEST_ASSIGNEES")
|
||||
pull_request_milestone = os.environ.get("PULL_REQUEST_MILESTONE")
|
||||
pull_request_reviewers = os.environ.get("PULL_REQUEST_REVIEWERS")
|
||||
pull_request_team_reviewers = os.environ.get("PULL_REQUEST_TEAM_REVIEWERS")
|
||||
project_name = os.environ.get("PROJECT_NAME")
|
||||
project_column_name = os.environ.get("PROJECT_COLUMN_NAME")
|
||||
|
||||
# Push the local changes to the remote branch
|
||||
print("Pushing changes to 'origin/%s'" % branch)
|
||||
push_result = push_changes(repo.git, branch, commit_message)
|
||||
print(push_result)
|
||||
|
||||
# Create the pull request
|
||||
github_repo = Github(github_token).get_repo(github_repository)
|
||||
try:
|
||||
pull_request = github_repo.create_pull(
|
||||
title=title, body=body, base=base, head=branch
|
||||
)
|
||||
print(
|
||||
"Created pull request #%d (%s => %s)" % (pull_request.number, branch, base)
|
||||
)
|
||||
except GithubException as e:
|
||||
if e.status == 422:
|
||||
# Format the branch name
|
||||
head_branch = "%s:%s" % (github_repository.split("/")[0], branch)
|
||||
# Get the pull request
|
||||
pull_request = github_repo.get_pulls(
|
||||
state="open", base=base, head=head_branch
|
||||
)[0]
|
||||
print(
|
||||
"Updated pull request #%d (%s => %s)"
|
||||
% (pull_request.number, branch, base)
|
||||
)
|
||||
else:
|
||||
print(str(e))
|
||||
sys.exit(1)
|
||||
|
||||
# Set the output variables
|
||||
os.system("echo ::set-env name=PULL_REQUEST_NUMBER::%d" % pull_request.number)
|
||||
os.system("echo ::set-output name=pr_number::%d" % pull_request.number)
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if pull_request_labels is not None:
|
||||
print("Applying labels '%s'" % pull_request_labels)
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||
if pull_request_assignees is not None:
|
||||
print("Applying assignees '%s'" % pull_request_assignees)
|
||||
pull_request.as_issue().edit(
|
||||
assignees=cs_string_to_list(pull_request_assignees)
|
||||
)
|
||||
if pull_request_milestone is not None:
|
||||
print("Applying milestone '%s'" % pull_request_milestone)
|
||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers
|
||||
if pull_request_reviewers is not None:
|
||||
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
||||
try:
|
||||
pull_request.create_review_request(
|
||||
reviewers=cs_string_to_list(pull_request_reviewers)
|
||||
)
|
||||
except GithubException as e:
|
||||
# Likely caused by "Review cannot be requested from pull request
|
||||
# author."
|
||||
if e.status == 422:
|
||||
print("Requesting reviewers failed - %s" % e.data["message"])
|
||||
|
||||
# Set pull request team reviewers
|
||||
if pull_request_team_reviewers is not None:
|
||||
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
||||
pull_request.create_review_request(
|
||||
team_reviewers=cs_string_to_list(pull_request_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 - %s" % e.data["errors"][0]["message"]
|
||||
)
|
||||
|
||||
|
||||
# Fetch environment variables
|
||||
github_token = os.environ["GITHUB_TOKEN"]
|
||||
github_repository = os.environ["GITHUB_REPOSITORY"]
|
||||
github_ref = os.environ["GITHUB_REF"]
|
||||
event_name = os.environ["GITHUB_EVENT_NAME"]
|
||||
# Get the JSON event data
|
||||
event_data = get_github_event(os.environ["GITHUB_EVENT_PATH"])
|
||||
|
||||
# Set the repo to the working directory
|
||||
repo = Repo(os.getcwd())
|
||||
# Get the default for author email and name
|
||||
author_email, author_name = get_author_default(event_name, event_data)
|
||||
# Set commit author overrides
|
||||
author_email = os.getenv("COMMIT_AUTHOR_EMAIL", author_email)
|
||||
author_name = os.getenv("COMMIT_AUTHOR_NAME", author_name)
|
||||
# Set git configuration
|
||||
set_git_config(repo.git, author_email, author_name)
|
||||
# Update URL for the 'origin' remote
|
||||
set_git_remote_url(repo.git, github_token, github_repository)
|
||||
|
||||
# Fetch/Set the branch name
|
||||
branch_prefix = os.getenv("PULL_REQUEST_BRANCH", "create-pull-request/patch")
|
||||
# Fetch an optional base branch override
|
||||
base_override = os.environ.get("PULL_REQUEST_BASE")
|
||||
|
||||
# Set the base branch
|
||||
if base_override is not None:
|
||||
base = base_override
|
||||
print("Overriding the base with branch '%s'" % base)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith("refs/pull/"):
|
||||
# Check the PR is not raised from a fork of the repository
|
||||
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
||||
if head_repo != github_repository:
|
||||
print(
|
||||
"::warning::Pull request was raised from a fork of the repository. "
|
||||
+ "Limitations on forked repositories have been imposed by GitHub Actions. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit()
|
||||
# Switch to the merging branch instead of the merge commit
|
||||
base = os.environ["GITHUB_HEAD_REF"]
|
||||
print(
|
||||
"Removing the merge commit by switching to the pull request head branch '%s'"
|
||||
% base
|
||||
)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith("refs/heads/"):
|
||||
base = github_ref[11:]
|
||||
print("Currently checked out base assumed to be branch '%s'" % base)
|
||||
else:
|
||||
print(
|
||||
f"::warning::Currently checked out ref '{github_ref}' is not a valid base for a pull request. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit()
|
||||
|
||||
# Skip if the current branch 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 base.startswith(branch_prefix):
|
||||
print("Branch '%s' was created by this action. Skipping." % base)
|
||||
sys.exit()
|
||||
|
||||
# Fetch an optional environment variable to determine the branch suffix
|
||||
branch_suffix = os.getenv("BRANCH_SUFFIX", "short-commit-hash")
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# Suffix with the short SHA1 hash
|
||||
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
||||
elif branch_suffix == "timestamp":
|
||||
# Suffix with the current timestamp
|
||||
branch = "%s-%s" % (branch_prefix, int(time.time()))
|
||||
elif branch_suffix == "random":
|
||||
# Suffix with the current timestamp
|
||||
branch = "%s-%s" % (branch_prefix, get_random_suffix())
|
||||
elif branch_suffix == "none":
|
||||
# Fixed branch name
|
||||
branch = branch_prefix
|
||||
else:
|
||||
print("Branch suffix '%s' is not a valid value." % branch_suffix)
|
||||
sys.exit(1)
|
||||
|
||||
# Output head branch
|
||||
print("Pull request branch to create/update set to '%s'" % branch)
|
||||
|
||||
# Check if the determined head branch exists as a remote
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
if remote_exists:
|
||||
print(
|
||||
"Pull request branch '%s' already exists as remote branch 'origin/%s'"
|
||||
% (branch, branch)
|
||||
)
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# A remote branch already exists for the HEAD commit
|
||||
print(
|
||||
"Pull request branch '%s' already exists for this commit. Skipping."
|
||||
% branch
|
||||
)
|
||||
sys.exit()
|
||||
elif branch_suffix in ["timestamp", "random"]:
|
||||
# Generated branch name collision with an existing branch
|
||||
print(
|
||||
"Pull request branch '%s' collided with a branch of the same name. Please re-run."
|
||||
% branch
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Checkout branch
|
||||
checkout_branch(repo.git, remote_exists, branch)
|
||||
|
||||
# Check if there are changes to pull request
|
||||
if remote_exists:
|
||||
print(
|
||||
"Checking for local working copy changes indicating a "
|
||||
+ "diff with existing pull request branch 'origin/%s'" % branch
|
||||
)
|
||||
else:
|
||||
print(
|
||||
"Checking for local working copy changes indicating a "
|
||||
+ "diff with base 'origin/%s'" % base
|
||||
)
|
||||
|
||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
print("Modified or untracked files detected.")
|
||||
process_event(github_token, github_repository, repo, branch, base)
|
||||
else:
|
||||
print("No modified or untracked files detected. Skipping.")
|
2
dist/src/requirements.txt
vendored
Normal file
2
dist/src/requirements.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
GitPython==3.0.5
|
||||
PyGithub==1.44.1
|
52
dist/src/setup-python.js
vendored
Normal file
52
dist/src/setup-python.js
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
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;
|
390
examples.md
Normal file
390
examples.md
Normal file
@ -0,0 +1,390 @@
|
||||
# Examples
|
||||
|
||||
- [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)
|
||||
- [Keep Go up to date](#keep-go-up-to-date)
|
||||
- [Update SwaggerUI for GitHub Pages](#update-swaggerui-for-github-pages)
|
||||
- [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)
|
||||
- [Call the GitHub API from another GitHub Actions workflow](#call-the-github-api-from-another-github-actions-workflow)
|
||||
- [Use case: Create a pull request to modify/fix pull requests](#use-case-create-a-pull-request-to-modifyfix-pull-requests)
|
||||
- [autopep8](#autopep8)
|
||||
- [Misc workflow tips](#misc-workflow-tips)
|
||||
- [Filtering push events](#filtering-push-events)
|
||||
- [Dynamic configuration using variables](#dynamic-configuration-using-variables)
|
||||
- [Debugging GitHub Actions](#debugging-github-actions)
|
||||
|
||||
|
||||
## 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
|
||||
|
||||
```yml
|
||||
name: Update Dependencies
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 10 * * 1'
|
||||
jobs:
|
||||
update-deps:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: '10.x'
|
||||
- name: Update dependencies
|
||||
id: vars
|
||||
run: |
|
||||
npm install -g npm-check-updates
|
||||
ncu -u
|
||||
npm install
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
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
|
||||
branch-suffix: none
|
||||
```
|
||||
|
||||
### Keep Go up to date
|
||||
|
||||
Keep Go up to date with [ensure-latest-go](https://github.com/jmhodges/ensure-latest-go) action.
|
||||
|
||||
```yml
|
||||
name: Keeping Go up to date
|
||||
on:
|
||||
schedule:
|
||||
- cron: 47 4 * * *
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
jobs:
|
||||
fresh_go:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
with:
|
||||
ref: master
|
||||
- uses: jmhodges/ensure-latest-go@v1.0.2
|
||||
id: ensure_go
|
||||
- run: echo "##[set-output name=pr_title;]update to latest Go release ${{ steps.ensure_go.outputs.go_version}}"
|
||||
id: pr_title_maker
|
||||
- name: Create pull request
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ steps.pr_title_maker.outputs.pr_title }}
|
||||
body: Auto-generated pull request created by the GitHub Actions [create-pull-request](https://github.com/peter-evans/create-pull-request) and [ensure-latest-go](https://github.com/jmhodges/ensure-latest-go).
|
||||
commit-message: ${{ steps.pr_title_maker.outputs.pr_title }}
|
||||
branch-suffix: none
|
||||
branch: ensure-latest-go/patch-${{ steps.ensure_go.outputs.go_version }}
|
||||
```
|
||||
|
||||
### Update SwaggerUI for GitHub Pages
|
||||
|
||||
When using [GitHub Pages to host Swagger documentation](https://github.com/peter-evans/swagger-github-pages), this workflow updates the repository with the latest distribution of [SwaggerUI](https://github.com/swagger-api/swagger-ui).
|
||||
|
||||
You must create a file called `swagger-ui.version` at the root of your repository before running.
|
||||
```yml
|
||||
name: Update Swagger UI
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 10 * * *'
|
||||
jobs:
|
||||
updateSwagger:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Get Latest Swagger UI Release
|
||||
id: swagger-ui
|
||||
run: |
|
||||
echo ::set-output name=release_tag::$(curl -sL https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | jq -r ".tag_name")
|
||||
echo ::set-output name=current_tag::$(<swagger-ui.version)
|
||||
- name: Update Swagger UI
|
||||
if: steps.swagger-ui.outputs.current_tag != steps.swagger-ui.outputs.release_tag
|
||||
env:
|
||||
RELEASE_TAG: ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
SWAGGER_YAML: "swagger.yaml"
|
||||
run: |
|
||||
# Delete the dist directory and index.html
|
||||
rm -fr dist index.html
|
||||
# Download the release
|
||||
curl -sL -o $RELEASE_TAG https://api.github.com/repos/swagger-api/swagger-ui/tarball/$RELEASE_TAG
|
||||
# Extract the dist directory
|
||||
tar -xzf $RELEASE_TAG --strip-components=1 $(tar -tzf $RELEASE_TAG | head -1 | cut -f1 -d"/")/dist
|
||||
rm $RELEASE_TAG
|
||||
# Move index.html to the root
|
||||
mv dist/index.html .
|
||||
# Fix references in index.html
|
||||
sed -i "s|https://petstore.swagger.io/v2/swagger.json|$SWAGGER_YAML|g" index.html
|
||||
sed -i "s|href=\"./|href=\"dist/|g" index.html
|
||||
sed -i "s|src=\"./|src=\"dist/|g" index.html
|
||||
# Update current release
|
||||
echo ${{ steps.swagger-ui.outputs.release_tag }} > swagger-ui.version
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
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: |
|
||||
Updates [swagger-ui][1] to ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
|
||||
Auto-generated by [create-pull-request][2]
|
||||
|
||||
[1]: https://github.com/swagger-api/swagger-ui
|
||||
[2]: https://github.com/peter-evans/create-pull-request
|
||||
labels: dependencies, automated pr
|
||||
branch: swagger-ui-updates
|
||||
branch-suffix: none
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
```yml
|
||||
name: Download Website
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 10 * * *'
|
||||
jobs:
|
||||
format:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Download website
|
||||
run: |
|
||||
wget \
|
||||
--recursive \
|
||||
--level=2 \
|
||||
--wait=1 \
|
||||
--no-clobber \
|
||||
--page-requisites \
|
||||
--html-extension \
|
||||
--convert-links \
|
||||
--domains quotes.toscrape.com \
|
||||
http://quotes.toscrape.com/
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
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.
|
||||
branch: website-updates
|
||||
branch-suffix: none
|
||||
```
|
||||
|
||||
## 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.
|
||||
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.
|
||||
|
||||
Set the workflow to execute `on: repository_dispatch`.
|
||||
|
||||
```yml
|
||||
on:
|
||||
repository_dispatch:
|
||||
types: [create-pull-request]
|
||||
```
|
||||
|
||||
### Call the GitHub API from an external service
|
||||
|
||||
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)
|
||||
- `[repository]` is the name of the repository the workflow resides in.
|
||||
|
||||
```
|
||||
curl -XPOST -u "[username]:[token]" \
|
||||
-H "Accept: application/vnd.github.everest-preview+json" \
|
||||
-H "Content-Type: application/json" \
|
||||
https://api.github.com/repos/[username]/[repository]/dispatches \
|
||||
--data '{"event_type": "create-pull-request"}'
|
||||
```
|
||||
|
||||
### Call the GitHub API from another GitHub Actions workflow
|
||||
|
||||
An `on: repository_dispatch` workflow can be triggered from another workflow with [repository-dispatch](https://github.com/peter-evans/repository-dispatch) action.
|
||||
|
||||
```yml
|
||||
- name: Repository Dispatch
|
||||
uses: peter-evans/repository-dispatch@v1.0.0
|
||||
with:
|
||||
token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
repository: username/my-repo
|
||||
event-type: create-pull-request
|
||||
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
|
||||
```
|
||||
|
||||
## Use case: Create a pull request to modify/fix pull requests
|
||||
|
||||
This is a pattern that works well for any 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.
|
||||
|
||||
### autopep8
|
||||
|
||||
The following is an example workflow for a use case where [autopep8 action](https://github.com/peter-evans/autopep8) runs as both a check on pull requests and raises a further pull request to apply code fixes.
|
||||
|
||||
How it works:
|
||||
|
||||
1. When a pull request is raised the workflow executes as a check
|
||||
2. If autopep8 makes any fixes a pull request will be raised for those fixes to be merged into the current pull request branch. The workflow then deliberately causes the check to fail.
|
||||
3. When the pull request containing the fixes is merged the workflow runs again. This time autopep8 makes no changes and the check passes.
|
||||
4. The original pull request can now be merged.
|
||||
|
||||
```yml
|
||||
name: autopep8
|
||||
on: pull_request
|
||||
jobs:
|
||||
autopep8:
|
||||
# Check if the PR is not raised by this workflow and is not from a fork
|
||||
if: startsWith(github.head_ref, 'autopep8-patches') == false && github.event.pull_request.head.repo.full_name == github.repository
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: autopep8
|
||||
id: autopep8
|
||||
uses: peter-evans/autopep8@v1.1.0
|
||||
with:
|
||||
args: --exit-code --recursive --in-place --aggressive --aggressive .
|
||||
- name: Set autopep8 branch name
|
||||
id: vars
|
||||
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@v1
|
||||
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.
|
||||
labels: autopep8, automated pr
|
||||
branch: ${{ steps.vars.outputs.branch-name }}
|
||||
branch-suffix: none
|
||||
- name: Fail if autopep8 made changes
|
||||
if: steps.autopep8.outputs.exit-code == 2
|
||||
run: exit 1
|
||||
```
|
||||
|
||||
## Misc workflow tips
|
||||
|
||||
### Filtering push events
|
||||
|
||||
For workflows using `on: push` you may want to ignore push events for tags and only execute for branches. Specifying `branches` causes only events on branches to trigger the workflow. The `'**'` wildcard will match any branch name.
|
||||
|
||||
```yml
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
```
|
||||
|
||||
If you have a workflow that contains jobs to handle push events on branches as well as tags, you can make sure that the job where you use `create-pull-request` action only executes when `github.ref` is a branch by using an `if` condition as follows.
|
||||
|
||||
```yml
|
||||
on: push
|
||||
jobs:
|
||||
createPullRequest:
|
||||
if: startsWith(github.ref, 'refs/heads/')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
...
|
||||
|
||||
someOtherJob:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
...
|
||||
```
|
||||
|
||||
### Dynamic configuration using variables
|
||||
|
||||
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.
|
||||
|
||||
```yml
|
||||
- name: Set output variables
|
||||
id: vars
|
||||
run: |
|
||||
echo ::set-output name=pr_title::"[Test] Add report file $(date +%d-%m-%Y)"
|
||||
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@v1
|
||||
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.
|
||||
|
||||
```yml
|
||||
- name: Set environment variables
|
||||
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)."
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ env.PULL_REQUEST_TITLE }}
|
||||
body: ${{ env.PULL_REQUEST_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.
|
||||
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.
|
||||
To enable step debug logging set the secret `ACTIONS_STEP_DEBUG` to `true` in the repository that contains the workflow.
|
||||
|
||||
#### Output Various Contexts
|
||||
|
||||
```yml
|
||||
steps:
|
||||
- name: Dump GitHub context
|
||||
env:
|
||||
GITHUB_CONTEXT: ${{ toJson(github) }}
|
||||
run: echo "$GITHUB_CONTEXT"
|
||||
- name: Dump job context
|
||||
env:
|
||||
JOB_CONTEXT: ${{ toJson(job) }}
|
||||
run: echo "$JOB_CONTEXT"
|
||||
- name: Dump steps context
|
||||
env:
|
||||
STEPS_CONTEXT: ${{ toJson(steps) }}
|
||||
run: echo "$STEPS_CONTEXT"
|
||||
- name: Dump runner context
|
||||
env:
|
||||
RUNNER_CONTEXT: ${{ toJson(runner) }}
|
||||
run: echo "$RUNNER_CONTEXT"
|
||||
- name: Dump strategy context
|
||||
env:
|
||||
STRATEGY_CONTEXT: ${{ toJson(strategy) }}
|
||||
run: echo "$STRATEGY_CONTEXT"
|
||||
- name: Dump matrix context
|
||||
env:
|
||||
MATRIX_CONTEXT: ${{ toJson(matrix) }}
|
||||
run: echo "$MATRIX_CONTEXT"
|
||||
```
|
70
index.js
Normal file
70
index.js
Normal file
@ -0,0 +1,70 @@
|
||||
const { inspect } = require("util");
|
||||
const core = require("@actions/core");
|
||||
const exec = require("@actions/exec");
|
||||
const setupPython = require("./src/setup-python");
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
// Allows ncc to find assets to be included in the distribution
|
||||
const src = __dirname + "/src";
|
||||
core.debug(`src: ${src}`);
|
||||
|
||||
// Setup Python from the tool cache
|
||||
setupPython("3.8.0", "x64");
|
||||
|
||||
// Install requirements
|
||||
await exec.exec("pip", [
|
||||
"install",
|
||||
"--requirement",
|
||||
`${src}/requirements.txt`
|
||||
]);
|
||||
|
||||
// Fetch action inputs
|
||||
const inputs = {
|
||||
token: core.getInput("token"),
|
||||
commitMessage: core.getInput("commit-message"),
|
||||
commitAuthorEmail: core.getInput("author-email"),
|
||||
commitAuthorName: core.getInput("author-name"),
|
||||
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"),
|
||||
base: core.getInput("base"),
|
||||
branchSuffix: core.getInput("branch-suffix"),
|
||||
debugEvent: core.getInput("debug-event")
|
||||
};
|
||||
core.debug(`Inputs: ${inspect(inputs)}`);
|
||||
|
||||
// Set environment variables from inputs.
|
||||
if (inputs.token) process.env.GITHUB_TOKEN = inputs.token;
|
||||
if (inputs.commitMessage) process.env.COMMIT_MESSAGE = inputs.commitMessage;
|
||||
if (inputs.commitAuthorEmail) process.env.COMMIT_AUTHOR_EMAIL = inputs.commitAuthorEmail;
|
||||
if (inputs.commitAuthorName) process.env.COMMIT_AUTHOR_NAME = inputs.commitAuthorName;
|
||||
if (inputs.title) process.env.PULL_REQUEST_TITLE = inputs.title;
|
||||
if (inputs.body) process.env.PULL_REQUEST_BODY = inputs.body;
|
||||
if (inputs.labels) process.env.PULL_REQUEST_LABELS = inputs.labels;
|
||||
if (inputs.assignees) process.env.PULL_REQUEST_ASSIGNEES = inputs.assignees;
|
||||
if (inputs.reviewers) process.env.PULL_REQUEST_REVIEWERS = inputs.reviewers;
|
||||
if (inputs.teamReviewers) process.env.PULL_REQUEST_TEAM_REVIEWERS = inputs.teamReviewers;
|
||||
if (inputs.milestone) process.env.PULL_REQUEST_MILESTONE = inputs.milestone;
|
||||
if (inputs.project) process.env.PROJECT_NAME = inputs.project;
|
||||
if (inputs.projectColumn) process.env.PROJECT_COLUMN_NAME = inputs.projectColumn;
|
||||
if (inputs.branch) process.env.PULL_REQUEST_BRANCH = inputs.branch;
|
||||
if (inputs.base) process.env.PULL_REQUEST_BASE = inputs.base;
|
||||
if (inputs.branchSuffix) process.env.BRANCH_SUFFIX = inputs.branchSuffix;
|
||||
if (inputs.debugEvent) process.env.DEBUG_EVENT = inputs.debugEvent;
|
||||
|
||||
// Execute python script
|
||||
await exec.exec("python", [`${src}/create-pull-request.py`]);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
71
package-lock.json
generated
Normal file
71
package-lock.json
generated
Normal file
@ -0,0 +1,71 @@
|
||||
{
|
||||
"name": "create-pull-request",
|
||||
"version": "1.7.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"@actions/core": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.0.tgz",
|
||||
"integrity": "sha512-ZKdyhlSlyz38S6YFfPnyNgCDZuAF2T0Qv5eHflNWytPS8Qjvz39bZFMry9Bb/dpSnqWcNeav5yM2CTYpJeY+Dw=="
|
||||
},
|
||||
"@actions/exec": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.1.tgz",
|
||||
"integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ=="
|
||||
},
|
||||
"@actions/io": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.1.tgz",
|
||||
"integrity": "sha512-rhq+tfZukbtaus7xyUtwKfuiCRXd1hWSfmJNEpFgBQJ4woqPEpsBw04awicjwz9tyG2/MVhAEMfVn664Cri5zA=="
|
||||
},
|
||||
"@actions/tool-cache": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/@actions/tool-cache/-/tool-cache-1.1.2.tgz",
|
||||
"integrity": "sha512-IJczPaZr02ECa3Lgws/TJEVco9tjOujiQSZbO3dHuXXjhd5vrUtfOgGwhmz3/f97L910OraPZ8SknofUk6RvOQ==",
|
||||
"requires": {
|
||||
"@actions/core": "^1.1.0",
|
||||
"@actions/exec": "^1.0.1",
|
||||
"@actions/io": "^1.0.1",
|
||||
"semver": "^6.1.0",
|
||||
"typed-rest-client": "^1.4.0",
|
||||
"uuid": "^3.3.2"
|
||||
}
|
||||
},
|
||||
"@zeit/ncc": {
|
||||
"version": "0.20.5",
|
||||
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.20.5.tgz",
|
||||
"integrity": "sha512-XU6uzwvv95DqxciQx+aOLhbyBx/13ky+RK1y88Age9Du3BlA4mMPCy13BGjayOrrumOzlq1XV3SD/BWiZENXlw==",
|
||||
"dev": true
|
||||
},
|
||||
"semver": {
|
||||
"version": "6.3.0",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
|
||||
"integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw=="
|
||||
},
|
||||
"tunnel": {
|
||||
"version": "0.0.4",
|
||||
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.4.tgz",
|
||||
"integrity": "sha1-LTeFoVjBdMmhbcLARuxfxfF0IhM="
|
||||
},
|
||||
"typed-rest-client": {
|
||||
"version": "1.5.0",
|
||||
"resolved": "https://registry.npmjs.org/typed-rest-client/-/typed-rest-client-1.5.0.tgz",
|
||||
"integrity": "sha512-DVZRlmsfnTjp6ZJaatcdyvvwYwbWvR4YDNFDqb+qdTxpvaVP99YCpBkA8rxsLtAPjBVoDe4fNsnMIdZTiPuKWg==",
|
||||
"requires": {
|
||||
"tunnel": "0.0.4",
|
||||
"underscore": "1.8.3"
|
||||
}
|
||||
},
|
||||
"underscore": {
|
||||
"version": "1.8.3",
|
||||
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz",
|
||||
"integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI="
|
||||
},
|
||||
"uuid": {
|
||||
"version": "3.3.3",
|
||||
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz",
|
||||
"integrity": "sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ=="
|
||||
}
|
||||
}
|
||||
}
|
28
package.json
Normal file
28
package.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "create-pull-request",
|
||||
"version": "1.8.0",
|
||||
"description": "Creates a pull request for changes to your repository in the actions workspace",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"package": "ncc build index.js -o dist"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/peter-evans/create-pull-request.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Evans",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/peter-evans/create-pull-request/issues"
|
||||
},
|
||||
"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"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@zeit/ncc": "0.20.5"
|
||||
}
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 201 KiB |
@ -1,2 +0,0 @@
|
||||
GitPython==3.0.3
|
||||
PyGithub==1.43.8
|
350
src/create-pull-request.py
Executable file
350
src/create-pull-request.py
Executable file
@ -0,0 +1,350 @@
|
||||
#!/usr/bin/env python3
|
||||
""" Create Pull Request """
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import sys
|
||||
import time
|
||||
from git import Repo
|
||||
from github import Github, GithubException
|
||||
|
||||
|
||||
def get_github_event(github_event_path):
|
||||
with open(github_event_path) as f:
|
||||
github_event = json.load(f)
|
||||
if bool(os.environ.get("DEBUG_EVENT")):
|
||||
print(os.environ["GITHUB_EVENT_NAME"])
|
||||
print(json.dumps(github_event, sort_keys=True, indent=2))
|
||||
return github_event
|
||||
|
||||
|
||||
def get_head_short_sha1(repo):
|
||||
return repo.git.rev_parse("--short", "HEAD")
|
||||
|
||||
|
||||
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
||||
return "".join(random.choice(chars) for _ in range(size))
|
||||
|
||||
|
||||
def remote_branch_exists(repo, branch):
|
||||
for ref in repo.remotes.origin.refs:
|
||||
if ref.name == ("origin/%s" % branch):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_author_default(event_name, event_data):
|
||||
if event_name == "push":
|
||||
email = "{head_commit[author][email]}".format(**event_data)
|
||||
name = "{head_commit[author][name]}".format(**event_data)
|
||||
else:
|
||||
email = os.environ["GITHUB_ACTOR"] + "@users.noreply.github.com"
|
||||
name = os.environ["GITHUB_ACTOR"]
|
||||
return email, name
|
||||
|
||||
|
||||
def set_git_config(git, email, name):
|
||||
print("Configuring git user as '%s <%s>'" % (name, email))
|
||||
git.config("--global", "user.email", '"%s"' % email)
|
||||
git.config("--global", "user.name", '"%s"' % name)
|
||||
|
||||
|
||||
def set_git_remote_url(git, token, github_repository):
|
||||
git.remote(
|
||||
"set-url",
|
||||
"origin",
|
||||
"https://x-access-token:%s@github.com/%s" % (token, github_repository),
|
||||
)
|
||||
|
||||
|
||||
def checkout_branch(git, remote_exists, branch):
|
||||
if remote_exists:
|
||||
print("Checking out branch '%s'" % branch)
|
||||
git.stash("--include-untracked")
|
||||
git.checkout(branch)
|
||||
try:
|
||||
git.stash("pop")
|
||||
except BaseException:
|
||||
git.checkout("--theirs", ".")
|
||||
git.reset()
|
||||
else:
|
||||
print("Creating new branch '%s'" % branch)
|
||||
git.checkout("HEAD", b=branch)
|
||||
|
||||
|
||||
def push_changes(git, branch, commit_message):
|
||||
git.add("-A")
|
||||
git.commit(m=commit_message)
|
||||
return git.push("-f", "--set-upstream", "origin", branch)
|
||||
|
||||
|
||||
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("::warning::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("::warning::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 process_event(github_token, github_repository, repo, branch, base):
|
||||
# Fetch optional environment variables with default values
|
||||
commit_message = os.getenv(
|
||||
"COMMIT_MESSAGE", "Auto-committed changes by create-pull-request action"
|
||||
)
|
||||
title = os.getenv(
|
||||
"PULL_REQUEST_TITLE", "Auto-generated by create-pull-request action"
|
||||
)
|
||||
body = os.getenv(
|
||||
"PULL_REQUEST_BODY",
|
||||
"Auto-generated pull request by "
|
||||
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action",
|
||||
)
|
||||
# Fetch optional environment variables with no default values
|
||||
pull_request_labels = os.environ.get("PULL_REQUEST_LABELS")
|
||||
pull_request_assignees = os.environ.get("PULL_REQUEST_ASSIGNEES")
|
||||
pull_request_milestone = os.environ.get("PULL_REQUEST_MILESTONE")
|
||||
pull_request_reviewers = os.environ.get("PULL_REQUEST_REVIEWERS")
|
||||
pull_request_team_reviewers = os.environ.get("PULL_REQUEST_TEAM_REVIEWERS")
|
||||
project_name = os.environ.get("PROJECT_NAME")
|
||||
project_column_name = os.environ.get("PROJECT_COLUMN_NAME")
|
||||
|
||||
# Push the local changes to the remote branch
|
||||
print("Pushing changes to 'origin/%s'" % branch)
|
||||
push_result = push_changes(repo.git, branch, commit_message)
|
||||
print(push_result)
|
||||
|
||||
# Create the pull request
|
||||
github_repo = Github(github_token).get_repo(github_repository)
|
||||
try:
|
||||
pull_request = github_repo.create_pull(
|
||||
title=title, body=body, base=base, head=branch
|
||||
)
|
||||
print(
|
||||
"Created pull request #%d (%s => %s)" % (pull_request.number, branch, base)
|
||||
)
|
||||
except GithubException as e:
|
||||
if e.status == 422:
|
||||
# Format the branch name
|
||||
head_branch = "%s:%s" % (github_repository.split("/")[0], branch)
|
||||
# Get the pull request
|
||||
pull_request = github_repo.get_pulls(
|
||||
state="open", base=base, head=head_branch
|
||||
)[0]
|
||||
print(
|
||||
"Updated pull request #%d (%s => %s)"
|
||||
% (pull_request.number, branch, base)
|
||||
)
|
||||
else:
|
||||
print(str(e))
|
||||
sys.exit(1)
|
||||
|
||||
# Set the output variables
|
||||
os.system("echo ::set-env name=PULL_REQUEST_NUMBER::%d" % pull_request.number)
|
||||
os.system("echo ::set-output name=pr_number::%d" % pull_request.number)
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if pull_request_labels is not None:
|
||||
print("Applying labels '%s'" % pull_request_labels)
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||
if pull_request_assignees is not None:
|
||||
print("Applying assignees '%s'" % pull_request_assignees)
|
||||
pull_request.as_issue().edit(
|
||||
assignees=cs_string_to_list(pull_request_assignees)
|
||||
)
|
||||
if pull_request_milestone is not None:
|
||||
print("Applying milestone '%s'" % pull_request_milestone)
|
||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers
|
||||
if pull_request_reviewers is not None:
|
||||
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
||||
try:
|
||||
pull_request.create_review_request(
|
||||
reviewers=cs_string_to_list(pull_request_reviewers)
|
||||
)
|
||||
except GithubException as e:
|
||||
# Likely caused by "Review cannot be requested from pull request
|
||||
# author."
|
||||
if e.status == 422:
|
||||
print("Requesting reviewers failed - %s" % e.data["message"])
|
||||
|
||||
# Set pull request team reviewers
|
||||
if pull_request_team_reviewers is not None:
|
||||
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
||||
pull_request.create_review_request(
|
||||
team_reviewers=cs_string_to_list(pull_request_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 - %s" % e.data["errors"][0]["message"]
|
||||
)
|
||||
|
||||
|
||||
# Fetch environment variables
|
||||
github_token = os.environ["GITHUB_TOKEN"]
|
||||
github_repository = os.environ["GITHUB_REPOSITORY"]
|
||||
github_ref = os.environ["GITHUB_REF"]
|
||||
event_name = os.environ["GITHUB_EVENT_NAME"]
|
||||
# Get the JSON event data
|
||||
event_data = get_github_event(os.environ["GITHUB_EVENT_PATH"])
|
||||
|
||||
# Set the repo to the working directory
|
||||
repo = Repo(os.getcwd())
|
||||
# Get the default for author email and name
|
||||
author_email, author_name = get_author_default(event_name, event_data)
|
||||
# Set commit author overrides
|
||||
author_email = os.getenv("COMMIT_AUTHOR_EMAIL", author_email)
|
||||
author_name = os.getenv("COMMIT_AUTHOR_NAME", author_name)
|
||||
# Set git configuration
|
||||
set_git_config(repo.git, author_email, author_name)
|
||||
# Update URL for the 'origin' remote
|
||||
set_git_remote_url(repo.git, github_token, github_repository)
|
||||
|
||||
# Fetch/Set the branch name
|
||||
branch_prefix = os.getenv("PULL_REQUEST_BRANCH", "create-pull-request/patch")
|
||||
# Fetch an optional base branch override
|
||||
base_override = os.environ.get("PULL_REQUEST_BASE")
|
||||
|
||||
# Set the base branch
|
||||
if base_override is not None:
|
||||
base = base_override
|
||||
print("Overriding the base with branch '%s'" % base)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith("refs/pull/"):
|
||||
# Check the PR is not raised from a fork of the repository
|
||||
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
||||
if head_repo != github_repository:
|
||||
print(
|
||||
"::warning::Pull request was raised from a fork of the repository. "
|
||||
+ "Limitations on forked repositories have been imposed by GitHub Actions. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit()
|
||||
# Switch to the merging branch instead of the merge commit
|
||||
base = os.environ["GITHUB_HEAD_REF"]
|
||||
print(
|
||||
"Removing the merge commit by switching to the pull request head branch '%s'"
|
||||
% base
|
||||
)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith("refs/heads/"):
|
||||
base = github_ref[11:]
|
||||
print("Currently checked out base assumed to be branch '%s'" % base)
|
||||
else:
|
||||
print(
|
||||
f"::warning::Currently checked out ref '{github_ref}' is not a valid base for a pull request. "
|
||||
+ "Unable to continue. Exiting."
|
||||
)
|
||||
sys.exit()
|
||||
|
||||
# Skip if the current branch 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 base.startswith(branch_prefix):
|
||||
print("Branch '%s' was created by this action. Skipping." % base)
|
||||
sys.exit()
|
||||
|
||||
# Fetch an optional environment variable to determine the branch suffix
|
||||
branch_suffix = os.getenv("BRANCH_SUFFIX", "short-commit-hash")
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# Suffix with the short SHA1 hash
|
||||
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
||||
elif branch_suffix == "timestamp":
|
||||
# Suffix with the current timestamp
|
||||
branch = "%s-%s" % (branch_prefix, int(time.time()))
|
||||
elif branch_suffix == "random":
|
||||
# Suffix with the current timestamp
|
||||
branch = "%s-%s" % (branch_prefix, get_random_suffix())
|
||||
elif branch_suffix == "none":
|
||||
# Fixed branch name
|
||||
branch = branch_prefix
|
||||
else:
|
||||
print("Branch suffix '%s' is not a valid value." % branch_suffix)
|
||||
sys.exit(1)
|
||||
|
||||
# Output head branch
|
||||
print("Pull request branch to create/update set to '%s'" % branch)
|
||||
|
||||
# Check if the determined head branch exists as a remote
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
if remote_exists:
|
||||
print(
|
||||
"Pull request branch '%s' already exists as remote branch 'origin/%s'"
|
||||
% (branch, branch)
|
||||
)
|
||||
if branch_suffix == "short-commit-hash":
|
||||
# A remote branch already exists for the HEAD commit
|
||||
print(
|
||||
"Pull request branch '%s' already exists for this commit. Skipping."
|
||||
% branch
|
||||
)
|
||||
sys.exit()
|
||||
elif branch_suffix in ["timestamp", "random"]:
|
||||
# Generated branch name collision with an existing branch
|
||||
print(
|
||||
"Pull request branch '%s' collided with a branch of the same name. Please re-run."
|
||||
% branch
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Checkout branch
|
||||
checkout_branch(repo.git, remote_exists, branch)
|
||||
|
||||
# Check if there are changes to pull request
|
||||
if remote_exists:
|
||||
print(
|
||||
"Checking for local working copy changes indicating a "
|
||||
+ "diff with existing pull request branch 'origin/%s'" % branch
|
||||
)
|
||||
else:
|
||||
print(
|
||||
"Checking for local working copy changes indicating a "
|
||||
+ "diff with base 'origin/%s'" % base
|
||||
)
|
||||
|
||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
print("Modified or untracked files detected.")
|
||||
process_event(github_token, github_repository, repo, branch, base)
|
||||
else:
|
||||
print("No modified or untracked files detected. Skipping.")
|
2
src/requirements.txt
Normal file
2
src/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
GitPython==3.0.5
|
||||
PyGithub==1.44.1
|
52
src/setup-python.js
Normal file
52
src/setup-python.js
Normal file
@ -0,0 +1,52 @@
|
||||
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;
|
Reference in New Issue
Block a user