diff --git a/.github/workflows/create-pull-request-multi.yml b/.github/workflows/create-pull-request-multi.yml new file mode 100644 index 0000000..e382fdd --- /dev/null +++ b/.github/workflows/create-pull-request-multi.yml @@ -0,0 +1,32 @@ +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: touch report.txt + - name: Create report file (windows) + if: matrix.platform == 'windows-latest' + run: type NUL > 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: 'timestamp' diff --git a/.github/workflows/create-pull-request.yml b/.github/workflows/create-pull-request.yml new file mode 100644 index 0000000..c990434 --- /dev/null +++ b/.github/workflows/create-pull-request.yml @@ -0,0 +1,24 @@ +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: touch 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_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 diff --git a/.github/workflows/repository_dispatch.yml b/.github/workflows/repository_dispatch.yml deleted file mode 100644 index ac3c7ac..0000000 --- a/.github/workflows/repository_dispatch.yml +++ /dev/null @@ -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' diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/Dockerfile b/Dockerfile index 958b3ba..420b0e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7.3 +FROM alpine:3.10.2 LABEL maintainer="Peter Evans " 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 / +RUN apk add python3-dev git git-lfs + 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 -ENTRYPOINT [ "/create-pull-request.py" ] \ No newline at end of file +ENTRYPOINT [ "/create-pull-request.py" ] diff --git a/README.md b/README.md index 44f29b3..db49bb7 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ 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. -This action is designed to be used in conjunction with other actions that modify or add files to your repository. +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 steps that modify or add files to your repository. The changes will be automatically committed to a new branch and a pull request created. 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. 3. Create a pull request to merge the new branch into the currently active branch executing the workflow. @@ -19,9 +19,18 @@ This action is experimental and may not work well for some use cases. ## Usage +Linux ```yml - name: Create Pull Request - uses: peter-evans/create-pull-request@v1.2.1 + uses: peter-evans/create-pull-request@v1.3.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +``` + +Multi platform - Linux, MacOS, Windows (beta) +```yml + - name: Create Pull Request + uses: peter-evans/create-pull-request@v1.2.1-multi env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` @@ -30,11 +39,16 @@ This action is experimental and may not work well for some use cases. These variables are all optional. If not set, a default value will be used. -- `PULL_REQUEST_BRANCH` - The branch name. See **Branch naming** below for details. -- `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. - `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) and `timestamp`. See **Branch naming** below for details. The following parameters are available for debugging and troubleshooting. @@ -52,7 +66,7 @@ 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. +Alternatively, branches can be suffixed with a timestamp by setting the environment variable `BRANCH_SUFFIX` to the value `timestamp`. This option must be used if multiple pull requests will be created during the execution of a workflow. e.g. ``` @@ -69,15 +83,30 @@ If there are files or directories you want to ignore you can simply add them to Here is an example that sets all the main environment variables. ```yml +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: touch report.txt - name: Create Pull Request - uses: peter-evans/create-pull-request@v1.2.1 + uses: peter-evans/create-pull-request@v1.3.1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PULL_REQUEST_BRANCH: my-patches + 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] New report' + 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 - COMMIT_MESSAGE: Auto-modify files by my-file-modifier-action - PULL_REQUEST_TITLE: Changes from my-file-modifier-action - PULL_REQUEST_BODY: This is an auto-generated PR with changes from my-file-modifier-action ``` This configuration will create pull requests that look like this: diff --git a/create-pull-request.py b/create-pull-request.py index f0135e2..548e667 100755 --- a/create-pull-request.py +++ b/create-pull-request.py @@ -71,19 +71,18 @@ def commit_changes(git, branch, commit_message): return git.push('--set-upstream', 'origin', branch) -def create_pull_request(token, repo, head, base, title, body): - return Github(token).get_repo(repo).create_pull( - title=title, - body=body, - base=base, - head=head) +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): # Fetch required environment variables github_token = os.environ['GITHUB_TOKEN'] github_repository = os.environ['GITHUB_REPOSITORY'] - # Fetch remaining optional environment variables + # Fetch optional environment variables with default values commit_message = os.getenv( 'COMMIT_MESSAGE', "Auto-committed changes by create-pull-request action") @@ -93,6 +92,12 @@ def process_event(event_name, event_data, repo, branch, base): 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') # Get the HEAD committer's email and name author_email, author_name = get_head_author(event_name, event_data) @@ -108,16 +113,34 @@ def process_event(event_name, event_data, repo, branch, base): # Create the pull request print("Creating a request to pull %s into %s." % (branch, base)) - pull_request = create_pull_request( - github_token, - github_repository, - branch, - base, - title, - body - ) + 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) + # 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'] diff --git a/pull-request-example.png b/pull-request-example.png index 7a79f6e..5f1ead0 100644 Binary files a/pull-request-example.png and b/pull-request-example.png differ diff --git a/requirements.txt b/requirements.txt index 4856e03..85ad43c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ GitPython==3.0.2 -PyGithub==1.43.8 \ No newline at end of file +PyGithub==1.43.8