Add branch suffix parameter

This commit is contained in:
Peter Evans 2019-09-24 20:00:49 +09:00
parent e543bbd98a
commit 4e47f5e151
2 changed files with 28 additions and 8 deletions

View File

@ -34,7 +34,12 @@ These variables are all optional. If not set, a default value will be used.
- `COMMIT_MESSAGE` - The message to use when committing changes. - `COMMIT_MESSAGE` - The message to use when committing changes.
- `PULL_REQUEST_TITLE` - The title of the pull request. - `PULL_REQUEST_TITLE` - The title of the pull request.
- `PULL_REQUEST_BODY` - The body of the pull request. - `PULL_REQUEST_BODY` - The body of the pull request.
- `SKIP_IGNORE` - If present, the `ignore_event` function will not run - `BRANCH_SUFFIX` - Valid values are `short-commit-hash` and `timestamp`. See **Branch naming** below for details.
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.
#### Branch naming #### Branch naming
@ -47,13 +52,21 @@ create-pull-request/patch-fcdfb59
create-pull-request/patch-394710b 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.
e.g.
```
create-pull-request/patch-1569322532
create-pull-request/patch-1569322552
```
#### Ignoring files #### 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 ## Example
Here is an example that sets all the environment variables (except `SKIP_IGNORE`). Here is an example that sets all the main environment variables.
```yml ```yml
- name: Create Pull Request - name: Create Pull Request

View File

@ -2,6 +2,7 @@
''' Create Pull Request ''' ''' Create Pull Request '''
import json import json
import os import os
import time
from git import Repo from git import Repo
from github import Github from github import Github
@ -9,14 +10,14 @@ from github import Github
def get_github_event(github_event_path): def get_github_event(github_event_path):
with open(github_event_path) as f: with open(github_event_path) as f:
github_event = json.load(f) github_event = json.load(f)
if os.environ.get('DEBUG_EVENT') is not None: if bool(os.environ.get('DEBUG_EVENT')):
print(os.environ['GITHUB_EVENT_NAME']) print(os.environ['GITHUB_EVENT_NAME'])
print(json.dumps(github_event, sort_keys=True, indent=2)) print(json.dumps(github_event, sort_keys=True, indent=2))
return github_event return github_event
def ignore_event(event_name, event_data, skip_ignore): def ignore_event(event_name, event_data):
if event_name == "push" and not skip_ignore: if event_name == "push":
# Ignore push events on deleted branches # Ignore push events on deleted branches
# The event we want to ignore occurs when a PR is created but the repository owner decides # 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 # not to commit the changes. They close the PR and delete the branch. This creates a
@ -123,7 +124,7 @@ event_name = os.environ['GITHUB_EVENT_NAME']
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH']) event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
# Check if this event should be ignored # Check if this event should be ignored
skip_ignore_event = bool(os.environ.get('SKIP_IGNORE')) skip_ignore_event = bool(os.environ.get('SKIP_IGNORE'))
if not ignore_event(event_name, event_data, skip_ignore_event): if skip_ignore_event or not ignore_event(event_name, event_data):
# Set the repo to the working directory # Set the repo to the working directory
repo = Repo(os.getcwd()) repo = Repo(os.getcwd())
@ -134,8 +135,14 @@ if not ignore_event(event_name, event_data, skip_ignore_event):
# 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 not base.startswith(branch):
# Suffix with the short SHA1 hash # Fetch an optional environment variable to determine the branch suffix
branch = "%s-%s" % (branch, get_head_short_sha1(repo)) branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
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 # Check if a PR branch already exists for this HEAD commit
if not pr_branch_exists(repo, branch): if not pr_branch_exists(repo, branch):