Compare commits

...

10 Commits

Author SHA1 Message Date
1fc2947eb6 Merge pull request #42 from peter-evans/new-params
New parameters
2019-09-24 20:13:43 +09:00
4e47f5e151 Add branch suffix parameter 2019-09-24 20:00:49 +09:00
e543bbd98a Merge pull request #41 from ScriptAutomate/master
Add skip_ignore arg to ignore_event function
2019-09-24 19:21:12 +09:00
4fb3c76ad7 Introduced SKIP_IGNORE env variable option
If the SKIP_IGNORE env var is present, the ignore_event function will not run
2019-09-24 01:08:16 -07:00
fcc56736a6 Add skip_ignore arg to ignore_event function
During testing, I found it useful to have this function skipped so I added a simple check for SKIP_IGNORE env variable
2019-09-24 00:56:53 -07:00
014d447f0c Update README 2019-09-20 10:23:15 +09:00
9d998cbb4c Remove need for repo scoped token 2019-09-20 10:14:51 +09:00
baab1666cc Update README 2019-09-20 09:47:11 +09:00
f639d94f5e Fix ignore_event for all non-push events 2019-09-20 09:39:09 +09:00
fd4e62ed7d Update README 2019-09-12 16:18:12 +09:00
3 changed files with 60 additions and 43 deletions

View File

@ -1,4 +1,6 @@
on: push on:
repository_dispatch:
types: [test]
name: create-pull-request action testing workflow name: create-pull-request action testing workflow
jobs: jobs:
createPullRequest: createPullRequest:
@ -14,7 +16,6 @@ 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

@ -19,15 +19,11 @@ This action is experimental and may not work well for repositories that have a v
## Usage ## Usage
In addition to the default `GITHUB_TOKEN`, the action requires a `repo` scoped token in order to commit.
Create one [here](https://github.com/settings/tokens) and pass that as a secret to the `REPO_ACCESS_TOKEN` environment variable.
```yml ```yml
- name: Create Pull Request - name: Create Pull Request
uses: peter-evans/create-pull-request@v1.1.2 uses: peter-evans/create-pull-request@v1.2.0
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
``` ```
#### Environment variables #### Environment variables
@ -38,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
@ -50,20 +52,27 @@ 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
uses: peter-evans/create-pull-request@v1.1.2 uses: peter-evans/create-pull-request@v1.2.0
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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

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,28 +10,27 @@ 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(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(github_event): def ignore_event(event_name, event_data):
if 'schedule' in github_event: if event_name == "push":
print("Allow schedule event.") # Ignore push events on deleted branches
return False # The event we want to ignore occurs when a PR is created but the repository owner decides
# Ignore push events on deleted branches # not to commit the changes. They close the PR and delete the branch. This creates a
# The event we want to ignore occurs when a PR is created but the repository owner decides # "push" event that we want to ignore, otherwise it will create another branch and PR on
# not to commit the changes. They close the PR and delete the branch. This creates a # the same commit.
# "push" event that we want to ignore, otherwise it will create another branch and PR on deleted = "{deleted}".format(**event_data)
# the same commit. if deleted == "True":
deleted = "{deleted}".format(**github_event) print("Ignoring delete branch event.")
if deleted == "True": return True
print("Ignoring delete branch event.") ref = "{ref}".format(**event_data)
return True if not ref.startswith('refs/heads/'):
ref = "{ref}".format(**github_event) print("Ignoring events for tags and remotes.")
if not ref.startswith('refs/heads/'): return True
print("Ignoring events for tags and remotes.")
return True
return False return False
@ -41,13 +41,13 @@ def pr_branch_exists(repo, branch):
return False return False
def get_head_author(github_event): def get_head_author(event_name, event_data):
if 'schedule' in github_event: if event_name == "push":
email = "{head_commit[author][email]}".format(**event_data)
name = "{head_commit[author][name]}".format(**event_data)
else:
email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com' email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com'
name = os.environ['GITHUB_ACTOR'] name = os.environ['GITHUB_ACTOR']
else:
email = "{head_commit[author][email]}".format(**github_event)
name = "{head_commit[author][name]}".format(**github_event)
return email, name return email, name
@ -61,7 +61,7 @@ def set_git_config(git, email, name):
def set_git_remote_url(git, token, github_repository): 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)) git.remote('set-url', 'origin', "https://x-access-token:%s@github.com/%s" % (token, github_repository))
def commit_changes(git, branch, commit_message): def commit_changes(git, branch, commit_message):
@ -79,11 +79,10 @@ def create_pull_request(token, repo, head, base, title, body):
head=head) head=head)
def process_event(github_event, repo, branch, base): def process_event(event_name, event_data, 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',
@ -96,11 +95,11 @@ def process_event(github_event, repo, branch, base):
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action") "[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action")
# Get the HEAD committer's email and name # Get the HEAD committer's email and name
author_email, author_name = get_head_author(github_event) author_email, author_name = get_head_author(event_name, event_data)
# 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 # Update URL for the 'origin' remote
set_git_remote_url(repo.git, repo_access_token, github_repository) set_git_remote_url(repo.git, github_token, github_repository)
# Commit the repository changes # Commit the repository changes
print("Committing changes.") print("Committing changes.")
@ -121,9 +120,11 @@ def process_event(github_event, repo, branch, base):
# Get the JSON event data # Get the JSON event data
github_event = get_github_event(os.environ['GITHUB_EVENT_PATH']) event_name = os.environ['GITHUB_EVENT_NAME']
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(github_event): 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())
@ -134,15 +135,21 @@ if not ignore_event(github_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):
# Check if there are changes to pull request # Check if there are changes to pull request
if repo.is_dirty() or len(repo.untracked_files) > 0: if repo.is_dirty() or len(repo.untracked_files) > 0:
print("Repository has modified or untracked files.") print("Repository has modified or untracked files.")
process_event(github_event, repo, branch, base) process_event(event_name, event_data, repo, branch, base)
else: else:
print("Repository has no modified or untracked files. Skipping.") print("Repository has no modified or untracked files. Skipping.")
else: else: