Update for actions v2

This commit is contained in:
Peter Evans 2019-08-13 18:14:43 +09:00
parent c0be263859
commit 860dc038c0
3 changed files with 48 additions and 31 deletions

View file

@ -14,6 +14,7 @@ jobs:
uses: ./ uses: ./
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_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: This PR is auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request).
PULL_REQUEST_BRANCH: test-patches PULL_REQUEST_BRANCH: test-patches

View file

@ -13,13 +13,19 @@ Create Pull Request action will:
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. 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: In general, it's not good practice to modify your repository during workflows.
This action is experimental and may not work well for repositories that have a very high frequency of commits.
## Usage ## Usage
```hcl The default `GITHUB_TOKEN` does not have the access neccessary for this action to work correctly.
action "Create Pull Request" { Create a new `repo` scoped token [here](https://github.com/settings/tokens) and pass that as a secret to the `REPO_ACCESS_TOKEN` environment variable.
uses = "peter-evans/create-pull-request@v1.0.0"
secrets = ["GITHUB_TOKEN"] ```yml
} - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.0.0
env:
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
``` ```
#### Environment variables #### Environment variables
@ -50,17 +56,15 @@ If there are files or directories you want to ignore you can simply add them to
Here is an example that sets all the environment variables. Here is an example that sets all the environment variables.
```hcl ```yml
action "Create Pull Request" { - name: Create Pull Request
uses = "peter-evans/create-pull-request@v1.0.0" uses: peter-evans/create-pull-request@v1.0.0
secrets = ["GITHUB_TOKEN"] env:
env = { REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
PULL_REQUEST_BRANCH = "my-patches" PULL_REQUEST_BRANCH: my-patches
COMMIT_MESSAGE = "Auto-modify files by my-file-modifier-action" COMMIT_MESSAGE: Auto-modify files by my-file-modifier-action
PULL_REQUEST_TITLE = "Changes from 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" 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: This configuration will create pull requests that look like this:

View file

@ -49,6 +49,10 @@ def set_git_config(git, email, name):
git.config('--global', 'user.name', '"%s"' % name) git.config('--global', 'user.name', '"%s"' % name)
def set_git_remote_url(git, token, github_repository):
git.remote('set-url', 'origin', "https://%s:x-oauth-basic@github.com/%s" % (token, github_repository))
def commit_changes(git, branch, commit_message): def commit_changes(git, branch, commit_message):
git.checkout('HEAD', b=branch) git.checkout('HEAD', b=branch)
git.add('-A') git.add('-A')
@ -64,10 +68,11 @@ def create_pull_request(token, repo, head, base, title, body):
head=head) head=head)
def process_event(github_event, repo, branch): def process_event(github_event, repo, branch, base):
# 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']
repo_access_token = os.environ['REPO_ACCESS_TOKEN']
# Fetch remaining optional environment variables # Fetch remaining optional environment variables
commit_message = os.getenv( commit_message = os.getenv(
'COMMIT_MESSAGE', 'COMMIT_MESSAGE',
@ -83,9 +88,8 @@ def process_event(github_event, repo, branch):
author_email, author_name = get_head_author(github_event) author_email, author_name = get_head_author(github_event)
# Set git configuration # Set git configuration
set_git_config(repo.git, author_email, author_name) set_git_config(repo.git, author_email, author_name)
# Update URL for the 'origin' remote
# Set the target base branch of the pull request set_git_remote_url(repo.git, repo_access_token, github_repository)
base = repo.active_branch.name
# Commit the repository changes # Commit the repository changes
print("Committing changes.") print("Committing changes.")
@ -114,18 +118,26 @@ if not ignore_event(github_event):
# 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')
# Suffix with the short SHA1 hash # Set the current branch as the target base branch
branch = "%s-%s" % (branch, get_head_short_sha1(repo)) base = os.environ['GITHUB_REF'][11:]
# Check if a PR branch already exists for this HEAD commit # Skip if the current branch is a PR branch created by this action
if not pr_branch_exists(repo, branch): if not base.startswith(branch):
# Check if there are changes to pull request # Suffix with the short SHA1 hash
if repo.is_dirty() or len(repo.untracked_files) > 0: branch = "%s-%s" % (branch, get_head_short_sha1(repo))
print("Repository has modified or untracked files.")
process_event(github_event, repo, branch) # Check if a PR branch already exists for this HEAD commit
if not pr_branch_exists(repo, 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(github_event, repo, branch, base)
else:
print("Repository has no modified or untracked files. Skipping.")
else: else:
print("Repository has no modified or untracked files. Skipping.") print(
"Pull request branch '%s' already exists for this commit. Skipping." %
branch)
else: else:
print( print(
"Pull request branch '%s' already exists for this commit. Skipping." % "Branch '%s' was created by this action. Skipping." % base)
branch)