Compare commits

..

54 Commits

Author SHA1 Message Date
Peter Evans
83c11ef20d Merge branch 'multi-platform' into multi-platform-release 2019-10-14 02:01:51 +09:00
Peter Evans
4fce601813 Merge branch 'master' into multi-platform 2019-10-14 02:01:37 +09:00
Peter Evans
f3b1bd6331 Update image version 2019-10-14 01:58:42 +09:00
Peter Evans
51ade9f54b Fix for pull refs 2019-10-14 01:39:53 +09:00
Peter Evans
3c86dbf9e6 Update README 2019-10-06 15:13:32 +09:00
Peter Evans
b2d426ea4e Update README 2019-10-06 15:06:49 +09:00
Peter Evans
fbe0cd3d1a Update README 2019-10-06 14:59:45 +09:00
Peter Evans
234d8686a5 Merge branch 'multi-platform' into multi-platform-release 2019-10-06 14:58:05 +09:00
Peter Evans
b39e1dc4cf Merge branch 'master' into multi-platform 2019-10-06 14:57:52 +09:00
Peter Evans
5f9821866a Update workflows 2019-10-06 14:52:15 +09:00
Peter Evans
ee153ec8cf Update image version 2019-10-06 14:52:01 +09:00
Peter Evans
e138d84114 Add variables to override the commit author name and email 2019-10-06 14:38:15 +09:00
Peter Evans
90843d3c39 Update README 2019-10-05 09:22:43 +09:00
Peter Evans
90ad96bd01 Update workflows 2019-10-05 09:22:35 +09:00
Peter Evans
f8cc05d28d Update README 2019-10-04 14:53:58 +09:00
Peter Evans
fe78afb6d7 Update README 2019-10-04 14:46:26 +09:00
Peter Evans
a0c4cfcf18 Merge branch 'multi-platform' into multi-platform-release 2019-10-04 14:45:05 +09:00
Peter Evans
8c223b540a Merge branch 'master' into multi-platform 2019-10-04 14:44:44 +09:00
Peter Evans
af7dbce93d Merge remote-tracking branch 'origin/master' 2019-10-04 14:36:11 +09:00
Peter Evans
2e283eddc6 Add workflow to automatically update dockerhub description 2019-10-04 14:35:58 +09:00
Peter Evans
3e0a17d097 Update action to use pre-built container 2019-10-04 14:35:22 +09:00
Peter Evans
7ebddff547 Merge pull request #52 from peter-evans/renovate/gitpython-3.x
Update dependency GitPython to v3.0.3
2019-10-04 14:30:27 +09:00
Renovate Bot
1397aded03 Update dependency GitPython to v3.0.3 2019-10-02 19:31:01 +00:00
Peter Evans
40fd4990fc Update README 2019-10-02 22:38:58 +09:00
Peter Evans
5784478398 Update README 2019-10-02 19:28:30 +09:00
Peter Evans
d30b03b696 Merge branch 'multi-platform' into multi-platform-release 2019-10-02 19:27:16 +09:00
Peter Evans
c1363c2fed Merge branch 'master' into multi-platform 2019-10-02 19:26:54 +09:00
Peter Evans
6e9abd8a8d Add action.yml 2019-10-02 19:20:07 +09:00
Peter Evans
ef76e27efb Replace fixed-name branch rebase with stash merge 2019-10-02 19:05:46 +09:00
Peter Evans
6be23859c3 Update README 2019-09-30 23:24:16 +09:00
Peter Evans
aa09d77c75 Update README
Update README


Update README


Update README
2019-09-30 19:16:55 +09:00
Peter Evans
4938fb0369 Merge branch 'multi-platform' into multi-platform-release 2019-09-30 19:04:26 +09:00
Peter Evans
4525da514a Merge branch 'master' into multi-platform 2019-09-30 19:03:57 +09:00
Peter Evans
b86b066e8c Update README 2019-09-30 18:58:24 +09:00
Peter Evans
7556cef424 Update workflows 2019-09-30 18:57:49 +09:00
Peter Evans
ff7ec1c208 Fixed branch, random suffix and output pr number 2019-09-30 18:57:37 +09:00
Peter Evans
6f333b4ebc Update README 2019-09-30 10:08:06 +09:00
Peter Evans
65682597a6 Update workflows 2019-09-30 10:07:58 +09:00
Peter Evans
fc9f1a6380 Update image 2019-09-26 19:06:44 +09:00
Peter Evans
4bb662aed6 Update README 2019-09-26 19:03:42 +09:00
Peter Evans
a5c1d5b56d Update README 2019-09-26 18:19:51 +09:00
Peter Evans
0fe13446aa Merge branch 'multi-platform' into multi-platform-release 2019-09-26 18:17:14 +09:00
Peter Evans
f6845510f4 Merge branch 'master' into multi-platform 2019-09-26 18:15:45 +09:00
Peter Evans
dc7361b29a Update image 2019-09-26 18:14:05 +09:00
Peter Evans
3c9fba7af9 Update README 2019-09-26 18:07:43 +09:00
Peter Evans
ae3acffc2b Merge pull request #46 from peter-evans/issue-params
Issue params
2019-09-26 17:54:10 +09:00
Peter Evans
f3f5770a0e Update workflows 2019-09-26 17:49:23 +09:00
Peter Evans
64178c87e0 Add new optional parameters 2019-09-26 17:48:53 +09:00
Peter Evans
b1a0d5fc24 Update README 2019-09-26 13:35:59 +09:00
Peter Evans
7beba58e42 Merge pull request #45 from peter-evans/git-lfs
Update Docker Image to include support for git-lfs
2019-09-26 13:27:02 +09:00
Quin Outland
062968931c Update Docker Image to include support for git-lfs 2019-09-26 12:34:25 +09:00
Peter Evans
76116b7f39 Update README 2019-09-25 23:58:47 +09:00
Peter Evans
e635c55400 Update README 2019-09-25 09:42:45 +09:00
Peter Evans
9e25487be4 Update workflows 2019-09-25 09:25:52 +09:00
12 changed files with 373 additions and 134 deletions

View File

@@ -0,0 +1,42 @@
on:
repository_dispatch:
types: [create-pull-request-multi]
name: create-pull-request workflow
jobs:
createPullRequest:
name: Testing on ${{ matrix.platform }}
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v1
- name: Create report file
if: matrix.platform == 'ubuntu-latest' || matrix.platform == 'macos-latest'
run: date +%s > report.txt
- name: Create report file (windows)
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
COMMIT_AUTHOR_EMAIL: peter-evans@users.noreply.github.com
COMMIT_AUTHOR_NAME: Peter Evans
PULL_REQUEST_TITLE: '[Example] 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_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%

View File

@@ -0,0 +1,30 @@
on:
repository_dispatch:
types: [create-pull-request]
name: create-pull-request workflow
jobs:
createPullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Create report file
run: date +%s > report.txt
- name: Create Pull Request
uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: Add report file
COMMIT_AUTHOR_EMAIL: peter-evans@users.noreply.github.com
COMMIT_AUTHOR_NAME: Peter Evans
PULL_REQUEST_TITLE: '[Example] 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_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"

16
.github/workflows/push.yml vendored Normal file
View File

@@ -0,0 +1,16 @@
name: Update Docker Hub Description
on:
push:
branches:
- master
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

View File

@@ -1,22 +0,0 @@
on:
repository_dispatch:
types: [test]
name: create-pull-request action testing workflow
jobs:
createPullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Create report file
uses: finnp/create-file-action@1.0.0
env:
FILE_DATA: This is created to test create-pull-request action.
FILE_NAME: report.txt
- name: Create Pull Request
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_BRANCH: test-patches
PULL_REQUEST_TITLE: '[Test] Add report file'

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.DS_Store

View File

@@ -1,4 +1,4 @@
FROM python:3.7.3 FROM alpine:3.10.2
LABEL maintainer="Peter Evans <mail@peterevans.dev>" LABEL maintainer="Peter Evans <mail@peterevans.dev>"
LABEL repository="https://github.com/peter-evans/create-pull-request" LABEL repository="https://github.com/peter-evans/create-pull-request"
@@ -11,8 +11,10 @@ LABEL com.github.actions.color="gray-dark"
COPY LICENSE README.md / COPY LICENSE README.md /
RUN apk add python3-dev git git-lfs
COPY requirements.txt /tmp/ COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt RUN pip3 install --requirement /tmp/requirements.txt
COPY create-pull-request.py /create-pull-request.py COPY create-pull-request.py /create-pull-request.py
ENTRYPOINT [ "/create-pull-request.py" ] ENTRYPOINT [ "/create-pull-request.py" ]

173
README.md
View File

@@ -1,89 +1,168 @@
# Create Pull Request # <img width="24" height="24" src="logo.svg"> Create Pull Request
[![GitHub Marketplace](https://img.shields.io/badge/Marketplace-Create%20Pull%20Request-blue.svg?colorA=24292e&colorB=0366d6&style=flat&longCache=true&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAM6wAADOsB5dZE0gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAERSURBVCiRhZG/SsMxFEZPfsVJ61jbxaF0cRQRcRJ9hlYn30IHN/+9iquDCOIsblIrOjqKgy5aKoJQj4O3EEtbPwhJbr6Te28CmdSKeqzeqr0YbfVIrTBKakvtOl5dtTkK+v4HfA9PEyBFCY9AGVgCBLaBp1jPAyfAJ/AAdIEG0dNAiyP7+K1qIfMdonZic6+WJoBJvQlvuwDqcXadUuqPA1NKAlexbRTAIMvMOCjTbMwl1LtI/6KWJ5Q6rT6Ht1MA58AX8Apcqqt5r2qhrgAXQC3CZ6i1+KMd9TRu3MvA3aH/fFPnBodb6oe6HM8+lYHrGdRXW8M9bMZtPXUji69lmf5Cmamq7quNLFZXD9Rq7v0Bpc1o/tp0fisAAAAASUVORK5CYII=)](https://github.com/marketplace/actions/create-pull-request) [![GitHub Marketplace](https://img.shields.io/badge/Marketplace-Create%20Pull%20Request-blue.svg?colorA=24292e&colorB=0366d6&style=flat&longCache=true&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAM6wAADOsB5dZE0gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAERSURBVCiRhZG/SsMxFEZPfsVJ61jbxaF0cRQRcRJ9hlYn30IHN/+9iquDCOIsblIrOjqKgy5aKoJQj4O3EEtbPwhJbr6Te28CmdSKeqzeqr0YbfVIrTBKakvtOl5dtTkK+v4HfA9PEyBFCY9AGVgCBLaBp1jPAyfAJ/AAdIEG0dNAiyP7+K1qIfMdonZic6+WJoBJvQlvuwDqcXadUuqPA1NKAlexbRTAIMvMOCjTbMwl1LtI/6KWJ5Q6rT6Ht1MA58AX8Apcqqt5r2qhrgAXQC3CZ6i1+KMd9TRu3MvA3aH/fFPnBodb6oe6HM8+lYHrGdRXW8M9bMZtPXUji69lmf5Cmamq7quNLFZXD9Rq7v0Bpc1o/tp0fisAAAAASUVORK5CYII=)](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. A GitHub action to create a pull request for changes to your repository in the actions workspace.
Changes to a repository in the actions workspace persist between actions in a workflow. Changes to a repository in the Actions workspace persist between steps in a workflow.
This action is designed to be used in conjunction with other actions that modify or add files to your repository. This action is designed to be used in conjunction with other steps that modify or add files to your repository.
The changes will be automatically committed to a new branch and a pull request created. The changes will be automatically committed to a new branch and a pull request created.
Create Pull Request action will: Create Pull Request action will:
1. Check for repository changes in the actions workspace. This includes untracked (new) files as well as modified files. 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. 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. 3. Create a pull request to merge the new branch into the currently active branch executing the workflow.
Note: Modifying a repository during workflows is not good practice in general.
However, this action opens up some interesting possibilities when used carefully.
This action is experimental and may not work well for some use cases.
## Usage ## Usage
Linux
```yml ```yml
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.2.1 uses: peter-evans/create-pull-request@v1.5.1
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```
#### Environment variables Multi platform - Linux, MacOS, Windows (beta)
```yml
- name: Create Pull Request
uses: peter-evans/create-pull-request@v1.5.1-multi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
These variables are all optional. If not set, a default value will be used. **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`.
See [this issue](https://github.com/peter-evans/create-pull-request/issues/48) for further details.
- `PULL_REQUEST_BRANCH` - The branch name. See **Branch naming** below for details. ### Environment variables
- `BRANCH_SUFFIX` - Valid values are `short-commit-hash` and `timestamp`. See **Branch naming** below for details.
- `COMMIT_MESSAGE` - The message to use when committing changes. These variables are *all optional*. If not set, sensible default values will be used.
- `PULL_REQUEST_TITLE` - The title of the pull request.
- `PULL_REQUEST_BODY` - The body of the pull request. | Name | Description | Default |
| --- | --- | --- |
| `COMMIT_MESSAGE` | The message to use when committing changes. | `Auto-committed changes by create-pull-request action` |
| `COMMIT_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. |
| `COMMIT_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. |
| `PULL_REQUEST_TITLE` | The title of the pull request. | `Auto-generated by create-pull-request action` |
| `PULL_REQUEST_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` |
| `PULL_REQUEST_LABELS` | A comma separated list of labels. | none |
| `PULL_REQUEST_ASSIGNEES` | A comma separated list of assignees (GitHub usernames). | none |
| `PULL_REQUEST_REVIEWERS` | A comma separated list of reviewers (GitHub usernames) to request a review from. | none |
| `PULL_REQUEST_TEAM_REVIEWERS` | A comma separated list of GitHub teams to request a review from. | none |
| `PULL_REQUEST_MILESTONE` | The number of the milestone to associate this pull request with. | none |
| `PULL_REQUEST_BRANCH` | The branch name. See **Branch naming** below for details. | `create-pull-request/patch` |
| `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**
- `PULL_REQUEST_NUMBER` - The number of the pull request created.
**Debug environment variables**
The following parameters are available for debugging and troubleshooting. The following parameters are available for debugging and troubleshooting.
- `DEBUG_EVENT` - If present, outputs the event data that triggered the workflow. - `DEBUG_EVENT` - If present, outputs the event data that triggered the workflow.
- `SKIP_IGNORE` - If present, the `ignore_event` function will be skipped. - `SKIP_IGNORE` - If present, the `ignore_event` function will be skipped.
#### Branch naming ### Branch naming
The variable `PULL_REQUEST_BRANCH` defaults to `create-pull-request/patch`. For branch naming there are two strategies. Always create a new branch each time there are changes to be committed, OR, create a fixed-name pull request branch that will be updated with any new commits until it is merged or closed.
Commits will be made to a branch with this name and suffixed with the short SHA1 commit hash.
e.g. #### Strategy A - Always create a new pull request branch (default)
```
create-pull-request/patch-fcdfb59
create-pull-request/patch-394710b
```
Alternatively, branches can be suffixed with a timestamp by setting the environment variable `BRANCH_SUFFIX` to the value `timestamp`. This option may be necessary if multiple pull requests will be created during the execution of a workflow. 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`.
e.g. - `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`
```
create-pull-request/patch-1569322532
create-pull-request/patch-1569322552
```
#### Ignoring files - `timestamp` - Commits will be made to a branch suffixed by a timestamp. e.g. `create-pull-request/patch-1569322532`, `create-pull-request/patch-1569322552`
- `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`
#### 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.
### 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. 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 ## Examples
Here is an example that sets all the main environment variables. Here is an example that sets all the main environment variables.
```yml ```yml
- name: Create Pull Request on:
uses: peter-evans/create-pull-request@v1.2.1 repository_dispatch:
env: types: [create-pull-request]
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} name: create-pull-request workflow
PULL_REQUEST_BRANCH: my-patches jobs:
BRANCH_SUFFIX: short-commit-hash createPullRequest:
COMMIT_MESSAGE: Auto-modify files by my-file-modifier-action runs-on: ubuntu-latest
PULL_REQUEST_TITLE: Changes from my-file-modifier-action steps:
PULL_REQUEST_BODY: This is an auto-generated PR with changes from my-file-modifier-action - uses: actions/checkout@v1
- name: Create report file
run: date +%s > report.txt
- name: Create Pull Request
uses: peter-evans/create-pull-request@v1.5.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: Add report file
COMMIT_AUTHOR_EMAIL: peter-evans@users.noreply.github.com
COMMIT_AUTHOR_NAME: Peter Evans
PULL_REQUEST_TITLE: '[Example] 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_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"
``` ```
This configuration will create pull requests that look like this: This configuration will create pull requests that look like this:
![Pull Request Example](pull-request-example.png?raw=true) ![Pull Request Example](https://github.com/peter-evans/create-pull-request/blob/master/pull-request-example.png?raw=true)
### 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`. 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.5.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST_TITLE: ${{ steps.vars.outputs.pr_title }}
PULL_REQUEST_BODY: ${{ steps.vars.outputs.pr_body }}
```
Since the action reads environment variables from the system, it's technically not necessary to explicitly pass them as long as they exist in the environment. So the following method using `set-env` *also* works, but explicitly passing the configuration parameters using the previous method is perferred for its clarity.
```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.5.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
## License ## License
MIT License - see the [LICENSE](LICENSE) file for details [MIT](LICENSE)

View File

@@ -1,5 +1,9 @@
name: 'Create Pull Request' name: 'Create Pull Request'
author: 'Peter Evans'
description: 'Creates a pull request for changes to your repository in the actions workspace' description: 'Creates a pull request for changes to your repository in the actions workspace'
runs: runs:
using: 'node12' using: 'node12'
main: 'index.js' main: 'index.js'
branding:
icon: 'git-pull-request'
color: 'gray-dark'

View File

@@ -2,6 +2,9 @@
''' Create Pull Request ''' ''' Create Pull Request '''
import json import json
import os import os
import random
import string
import sys
import time import time
from git import Repo from git import Repo
from github import Github from github import Github
@@ -34,14 +37,22 @@ def ignore_event(event_name, event_data):
return False return False
def pr_branch_exists(repo, branch): 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: for ref in repo.remotes.origin.refs:
if ref.name == ("origin/%s" % branch): if ref.name == ("origin/%s" % branch):
return True return True
return False return False
def get_head_author(event_name, event_data): def get_author_default(event_name, event_data):
if event_name == "push": if event_name == "push":
email = "{head_commit[author][email]}".format(**event_data) email = "{head_commit[author][email]}".format(**event_data)
name = "{head_commit[author][name]}".format(**event_data) name = "{head_commit[author][name]}".format(**event_data)
@@ -51,39 +62,48 @@ def get_head_author(event_name, event_data):
return email, name return email, name
def get_head_short_sha1(repo):
return repo.git.rev_parse('--short', 'HEAD')
def set_git_config(git, email, name): def set_git_config(git, email, name):
git.config('--global', 'user.email', '"%s"' % email) git.config('--global', 'user.email', '"%s"' % email)
git.config('--global', 'user.name', '"%s"' % name) git.config('--global', 'user.name', '"%s"' % name)
def set_git_remote_url(git, token, github_repository): 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)) git.remote(
'set-url', 'origin', "https://x-access-token:%s@github.com/%s" %
(token, github_repository))
def commit_changes(git, branch, commit_message): def checkout_branch(git, remote_exists, branch):
git.checkout('HEAD', b=branch) if remote_exists:
git.stash('--include-untracked')
git.checkout(branch)
try:
git.stash('pop')
except BaseException:
git.checkout('--theirs', '.')
git.reset()
else:
git.checkout('HEAD', b=branch)
def push_changes(git, branch, commit_message):
git.add('-A') git.add('-A')
git.commit(m=commit_message) git.commit(m=commit_message)
return git.push('--set-upstream', 'origin', branch) return git.push('-f', '--set-upstream', 'origin', branch)
def create_pull_request(token, repo, head, base, title, body): def cs_string_to_list(str):
return Github(token).get_repo(repo).create_pull( # Split the comma separated string into a list
title=title, l = [i.strip() for i in str.split(',')]
body=body, # Remove empty strings
base=base, return list(filter(None, l))
head=head)
def process_event(event_name, event_data, repo, branch, base): def process_event(event_name, event_data, repo, branch, base, remote_exists):
# Fetch required environment variables # Fetch required environment variables
github_token = os.environ['GITHUB_TOKEN'] github_token = os.environ['GITHUB_TOKEN']
github_repository = os.environ['GITHUB_REPOSITORY'] github_repository = os.environ['GITHUB_REPOSITORY']
# Fetch remaining optional environment variables # Fetch optional environment variables with default values
commit_message = os.getenv( commit_message = os.getenv(
'COMMIT_MESSAGE', 'COMMIT_MESSAGE',
"Auto-committed changes by create-pull-request action") "Auto-committed changes by create-pull-request action")
@@ -93,30 +113,60 @@ def process_event(event_name, event_data, repo, branch, base):
body = os.getenv( body = os.getenv(
'PULL_REQUEST_BODY', "Auto-generated pull request by " 'PULL_REQUEST_BODY', "Auto-generated pull request by "
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action") "[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')
# 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)
# Update URL for the 'origin' remote # Update URL for the 'origin' remote
set_git_remote_url(repo.git, github_token, github_repository) set_git_remote_url(repo.git, github_token, github_repository)
# Commit the repository changes # Push the local changes to the remote branch
print("Committing changes.") print("Pushing changes.")
commit_result = commit_changes(repo.git, branch, commit_message) push_result = push_changes(repo.git, branch, commit_message)
print(commit_result) 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 # Create the pull request
print("Creating a request to pull %s into %s." % (branch, base)) print("Creating a request to pull %s into %s." % (branch, base))
pull_request = create_pull_request( github_repo = Github(github_token).get_repo(github_repository)
github_token, pull_request = github_repo.create_pull(
github_repository, title=title,
branch, body=body,
base, base=base,
title, head=branch)
body
)
print("Created pull request %d." % pull_request.number) 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 # Get the JSON event data
@@ -130,32 +180,63 @@ if skip_ignore_event or not ignore_event(event_name, event_data):
# Fetch/Set the branch name # Fetch/Set the branch name
branch = os.getenv('PULL_REQUEST_BRANCH', 'create-pull-request/patch') 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:] # Set the base branch
github_ref = os.environ['GITHUB_REF']
if github_ref.startswith('refs/pull/'):
base = os.environ['GITHUB_HEAD_REF']
# Reset to the merging branch instead of the merge commit
repo.git.checkout(base)
else:
base = github_ref[11:]
# Skip if the current branch is a PR branch created by this action # Skip if the current branch is a PR branch created by this action
if not base.startswith(branch): if base.startswith(branch):
# Fetch an optional environment variable to determine the branch suffix print("Branch '%s' was created by this action. Skipping." % base)
branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash') sys.exit()
if branch_suffix == "timestamp":
# Suffix with the current timestamp
branch = "%s-%s" % (branch, int(time.time()))
else:
# Suffix with the short SHA1 hash
branch = "%s-%s" % (branch, get_head_short_sha1(repo))
# Check if a PR branch already exists for this HEAD commit # Fetch an optional environment variable to determine the branch suffix
if not pr_branch_exists(repo, branch): branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
# Check if there are changes to pull request if branch_suffix == "short-commit-hash":
if repo.is_dirty() or len(repo.untracked_files) > 0: # Suffix with the short SHA1 hash
print("Repository has modified or untracked files.") branch = "%s-%s" % (branch, get_head_short_sha1(repo))
process_event(event_name, event_data, repo, branch, base) elif branch_suffix == "timestamp":
else: # Suffix with the current timestamp
print("Repository has no modified or untracked files. Skipping.") branch = "%s-%s" % (branch, int(time.time()))
else: elif branch_suffix == "random":
print( # Suffix with the current timestamp
"Pull request branch '%s' already exists for this commit. Skipping." % branch = "%s-%s" % (branch, get_random_suffix())
branch)
else: # 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( print(
"Branch '%s' was created by this action. Skipping." % base) "Pull request branch '%s' already exists for this commit. Skipping." %
branch)
sys.exit()
# 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)
# 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.")

6
logo.svg Normal file
View File

@@ -0,0 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg" width="43%" height="43%" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="color: #000000;"><title>git-pull-request</title>
<circle cx="18" cy="18" r="3"></circle>
<circle cx="6" cy="6" r="3"></circle>
<path d="M13 6h3a2 2 0 0 1 2 2v7"></path>
<line x1="6" y1="9" x2="6" y2="21"></line>
</svg>

After

Width:  |  Height:  |  Size: 416 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 260 KiB

After

Width:  |  Height:  |  Size: 201 KiB

View File

@@ -1,2 +1,2 @@
GitPython==3.0.2 GitPython==3.0.3
PyGithub==1.43.8 PyGithub==1.43.8