Merge pull request #42 from peter-evans/new-params

New parameters
This commit is contained in:
Peter Evans 2019-09-24 20:13:43 +09:00 committed by GitHub
commit 1fc2947eb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View file

@ -34,6 +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.
- `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
@ -46,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. 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,7 +10,7 @@ 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
@ -122,7 +123,8 @@ def process_event(event_name, event_data, repo, branch, base):
event_name = os.environ['GITHUB_EVENT_NAME'] 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
if not ignore_event(event_name, event_data): skip_ignore_event = bool(os.environ.get('SKIP_IGNORE'))
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())
@ -133,8 +135,14 @@ if not ignore_event(event_name, event_data):
# 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):