Merge branch 'multi-platform' into multi-platform-release

This commit is contained in:
Peter Evans 2019-09-30 19:04:26 +09:00
commit 4938fb0369
5 changed files with 164 additions and 116 deletions

View file

@ -10,23 +10,31 @@ jobs:
platform: [ubuntu-latest, macos-latest, windows-latest] platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- name: Create report file - name: Create report file
if: matrix.platform == 'ubuntu-latest' || matrix.platform == 'macos-latest' if: matrix.platform == 'ubuntu-latest' || matrix.platform == 'macos-latest'
run: touch report.txt run: date +%s > report.txt
- name: Create report file (windows) - name: Create report file (windows)
if: matrix.platform == 'windows-latest' if: matrix.platform == 'windows-latest'
run: type NUL > report.txt run: echo %DATE% %TIME% > report.txt
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@multi-platform-release uses: peter-evans/create-pull-request@multi-platform-release
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: Add report file 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_BODY: >
PULL_REQUEST_TITLE: '[Example] Add report file' This PR is auto-generated by
PULL_REQUEST_LABELS: report, automated pr [create-pull-request](https://github.com/peter-evans/create-pull-request).
PULL_REQUEST_ASSIGNEES: peter-evans PULL_REQUEST_TITLE: '[Example] Add report file'
PULL_REQUEST_REVIEWERS: peter-evans PULL_REQUEST_LABELS: report, automated pr
PULL_REQUEST_MILESTONE: 1 PULL_REQUEST_ASSIGNEES: peter-evans
PULL_REQUEST_BRANCH: example-patches PULL_REQUEST_REVIEWERS: peter-evans
BRANCH_SUFFIX: 'timestamp' 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

@ -6,19 +6,23 @@ jobs:
createPullRequest: createPullRequest:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- name: Create report file - name: Create report file
run: touch report.txt run: date +%s > report.txt
- name: Create Pull Request - name: Create Pull Request
uses: ./ uses: ./
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: Add report file 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_BODY: >
PULL_REQUEST_TITLE: '[Example] Add report file' This PR is auto-generated by
PULL_REQUEST_LABELS: report, automated pr [create-pull-request](https://github.com/peter-evans/create-pull-request).
PULL_REQUEST_ASSIGNEES: peter-evans PULL_REQUEST_TITLE: '[Example] Add report file'
PULL_REQUEST_REVIEWERS: peter-evans PULL_REQUEST_LABELS: report, automated pr
PULL_REQUEST_MILESTONE: 1 PULL_REQUEST_ASSIGNEES: peter-evans
PULL_REQUEST_BRANCH: example-patches PULL_REQUEST_REVIEWERS: peter-evans
BRANCH_SUFFIX: short-commit-hash 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"

101
README.md
View file

@ -10,29 +10,25 @@ The changes will be automatically committed to a new branch and a pull request c
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. 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. 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 Linux
```yml ```yml
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.3.1 uses: peter-evans/create-pull-request@v1.4.0
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```
Multi platform - Linux, MacOS, Windows (beta) Multi platform - Linux, MacOS, Windows (beta)
```yml ```yml
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.2.1-multi uses: peter-evans/create-pull-request@v1.3.1-multi
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
``` ```
#### Environment variables #### Environment variables
@ -48,7 +44,11 @@ These variables are all optional. If not set, a default value will be used.
- `PULL_REQUEST_TEAM_REVIEWERS` - A comma separated list of GitHub teams 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_MILESTONE` - The number of the milestone to associate this pull request with.
- `PULL_REQUEST_BRANCH` - The branch name. See **Branch naming** below for details. - `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. - `BRANCH_SUFFIX` - The branch suffix type. Valid values are `short-commit-hash` (default), `timestamp`, `random` and `none`. See **Branch naming** below for details.
Output environment variables
- `PULL_REQUEST_NUMBER` - The number of the pull request created.
The following parameters are available for debugging and troubleshooting. The following parameters are available for debugging and troubleshooting.
@ -57,22 +57,31 @@ The following parameters are available for debugging and troubleshooting.
#### 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 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 must be used 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`.
e.g. 1. `short-commit-hash` (default)
```
create-pull-request/patch-1569322532 Commits will be made to a branch suffixed with the short SHA1 commit hash.
create-pull-request/patch-1569322552 eg. `create-pull-request/patch-fcdfb59`, `create-pull-request/patch-394710b`
```
2. `timestamp`
Commits will be made to a branch suffixed by a timestamp.
eg. `create-pull-request/patch-1569322532`, `create-pull-request/patch-1569322552`
3. `random`
Commits will be made to a branch suffixed with a random alpha-numeric string. This option must be used if multiple pull requests will be created during the execution of a workflow.
eg. `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 #### Ignoring files
@ -91,22 +100,26 @@ jobs:
createPullRequest: createPullRequest:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- name: Create report file - name: Create report file
run: touch report.txt run: date +%s > report.txt
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.3.1 uses: peter-evans/create-pull-request@v1.4.0
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMIT_MESSAGE: Add report file 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_BODY: >
PULL_REQUEST_TITLE: '[Example] New report' This PR is auto-generated by
PULL_REQUEST_LABELS: report, automated pr [create-pull-request](https://github.com/peter-evans/create-pull-request).
PULL_REQUEST_ASSIGNEES: peter-evans PULL_REQUEST_TITLE: '[Example] Add report file'
PULL_REQUEST_REVIEWERS: peter-evans PULL_REQUEST_LABELS: report, automated pr
PULL_REQUEST_MILESTONE: 1 PULL_REQUEST_ASSIGNEES: peter-evans
PULL_REQUEST_BRANCH: example-patches PULL_REQUEST_REVIEWERS: peter-evans
BRANCH_SUFFIX: short-commit-hash 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:
@ -115,4 +128,4 @@ This configuration will create pull requests that look like this:
## License ## License
MIT License - see the [LICENSE](LICENSE) file for details [MIT](LICENSE)

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,7 +37,15 @@ 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
@ -51,10 +62,6 @@ 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)
@ -64,11 +71,15 @@ 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 push_changes(git, remote_exists, branch, commit_message):
git.checkout('HEAD', b=branch)
git.add('-A') git.add('-A')
git.commit(m=commit_message) git.commit(m=commit_message)
return git.push('--set-upstream', 'origin', branch) if remote_exists:
git.checkout(branch)
git.rebase('-Xtheirs', '-')
else:
git.checkout('HEAD', b=branch)
return git.push('-f', '--set-upstream', 'origin', branch)
def cs_string_to_list(str): def cs_string_to_list(str):
@ -78,7 +89,7 @@ def cs_string_to_list(str):
return list(filter(None, l)) return list(filter(None, l))
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']
@ -106,10 +117,15 @@ def process_event(event_name, event_data, repo, branch, base):
# 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, remote_exists, 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))
@ -120,6 +136,7 @@ def process_event(event_name, event_data, repo, branch, base):
base=base, base=base,
head=branch) head=branch)
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 # Set labels, assignees and milestone
if pull_request_labels is not None: if pull_request_labels is not None:
@ -157,28 +174,34 @@ if skip_ignore_event or not ignore_event(event_name, event_data):
base = os.environ['GITHUB_REF'][11:] base = os.environ['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)
# 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()
# 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: else:
print( print("Repository has no modified or untracked files. Skipping.")
"Branch '%s' was created by this action. Skipping." % base)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 201 KiB

After

Width:  |  Height:  |  Size: 201 KiB