Compare commits

...

43 Commits

Author SHA1 Message Date
Peter Evans
1fc2947eb6 Merge pull request #42 from peter-evans/new-params
New parameters
2019-09-24 20:13:43 +09:00
Peter Evans
4e47f5e151 Add branch suffix parameter 2019-09-24 20:00:49 +09:00
Peter Evans
e543bbd98a Merge pull request #41 from ScriptAutomate/master
Add skip_ignore arg to ignore_event function
2019-09-24 19:21:12 +09:00
Derek Ardolf
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
Derek Ardolf
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
Peter Evans
014d447f0c Update README 2019-09-20 10:23:15 +09:00
Peter Evans
9d998cbb4c Remove need for repo scoped token 2019-09-20 10:14:51 +09:00
Peter Evans
baab1666cc Update README 2019-09-20 09:47:11 +09:00
Peter Evans
f639d94f5e Fix ignore_event for all non-push events 2019-09-20 09:39:09 +09:00
Peter Evans
fd4e62ed7d Update README 2019-09-12 16:18:12 +09:00
Peter Evans
21aff96eee Fix author email and name for scheduled jobs 2019-09-12 16:03:46 +09:00
Peter Evans
f4703cdc23 Update README 2019-09-10 18:06:54 +09:00
Peter Evans
7980880191 Fix missing colon 2019-09-10 17:58:01 +09:00
Peter Evans
4efcea62da Merge pull request #30 from stefanbuck/fix-29-scheduled-jobs
Do not break Action when using schedule jobs
2019-09-10 17:50:03 +09:00
Stefan Buck
580fc69c02 Fix scheduled events issue 2019-09-10 09:14:39 +02:00
Peter Evans
facb42d776 Merge pull request #26 from peter-evans/renovate/gitpython-3.x
Update dependency GitPython to v3.0.2
2019-08-22 13:50:04 +09:00
Renovate Bot
37fd4d3558 Update dependency GitPython to v3.0.2 2019-08-22 04:38:51 +00:00
Peter Evans
bc78d4cf02 Merge pull request #23 from peter-evans/renovate/gitpython-3.x
Update dependency GitPython to v3.0.1
2019-08-15 12:41:54 +09:00
Renovate Bot
91ff2766bf Update dependency GitPython to v3.0.1 2019-08-15 03:27:28 +00:00
Peter Evans
c216905beb Update README 2019-08-13 19:07:24 +09:00
Peter Evans
045ccaa641 Ignore events for tags and remotes 2019-08-13 19:00:56 +09:00
Peter Evans
a26e9f2362 Update README 2019-08-13 18:26:08 +09:00
Peter Evans
860dc038c0 Update for actions v2 2019-08-13 18:14:51 +09:00
Peter Evans
c0be263859 Merge pull request #16 from peter-evans/renovate/gitpython-3.x
Update dependency GitPython to v3
2019-08-12 14:43:52 +09:00
Peter Evans
0f3781327e Convert main.workflow to v2 yml 2019-08-12 14:32:59 +09:00
Renovate Bot
9b034ebd2d Update dependency GitPython to v3 2019-08-12 03:30:45 +00:00
Peter Evans
9a709f0173 Merge pull request #13 from peter-evans/renovate/gitpython-2.x
Update dependency GitPython to v2.1.13
2019-07-29 10:55:57 +09:00
Renovate Bot
cddd8048c2 Update dependency GitPython to v2.1.13 2019-07-29 01:27:39 +00:00
Peter Evans
8fe03d2026 Add explanation for ignoring the push event on deleted branches 2019-07-23 11:26:05 +09:00
Peter Evans
a5bdb09ebe Update README 2019-07-23 10:52:44 +09:00
Peter Evans
ed39fe9b45 Merge pull request #4 from peter-evans/renovate/gitpython-2.x
Update dependency GitPython to v2.1.12
2019-07-21 17:07:13 +09:00
Renovate Bot
f7b04aed2a Update dependency GitPython to v2.1.12 2019-07-21 08:04:14 +00:00
Peter Evans
d9e4270c6c Merge pull request #3 from peter-evans/renovate/pygithub-1.x
Update dependency PyGithub to v1.43.8
2019-07-21 17:01:09 +09:00
Renovate Bot
e7637f96b8 Update dependency PyGithub to v1.43.8 2019-07-21 07:59:08 +00:00
Peter Evans
dc019ec39e Update main.workflow 2019-07-21 13:36:09 +09:00
Peter Evans
abaaf77b6e Update main.workflow 2019-07-21 13:31:41 +09:00
Peter Evans
16d0603e20 Merge pull request #1 from peter-evans/renovate/configure
Configure Renovate
2019-07-18 18:47:19 +09:00
Peter Evans
5ffebf4a64 Add CODEOWNERS 2019-07-18 18:46:39 +09:00
Renovate Bot
ab526929c6 Add renovate.json 2019-07-18 09:44:25 +00:00
Peter Evans
754ec11bc0 Update entrypoint 2019-07-18 18:37:30 +09:00
Peter Evans
d73557c33a Update README 2019-07-16 22:56:36 +09:00
Peter Evans
f03950e9c3 Update README 2019-07-16 20:43:33 +09:00
Peter Evans
83e2ff790e Update README 2019-07-16 20:42:29 +09:00
8 changed files with 133 additions and 50 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@@ -0,0 +1 @@
* @peter-evans

View File

@@ -0,0 +1,22 @@
on:
repository_dispatch:
types: [test]
name: create-pull-request action testing workflow
jobs:
createPullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Create report file
uses: finnp/create-file-action@1.0.0
env:
FILE_DATA: This is created to test create-pull-request action.
FILE_NAME: report.txt
- name: Create Pull Request
uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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_BRANCH: test-patches
PULL_REQUEST_TITLE: '[Test] Add report file'

View File

@@ -15,4 +15,4 @@ COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt
COPY create-pull-request.py /create-pull-request.py
ENTRYPOINT [ "python", "/create-pull-request.py" ]
ENTRYPOINT [ "/create-pull-request.py" ]

View File

@@ -4,7 +4,7 @@
A GitHub action to create a pull request for changes to your repository in the actions workspace.
Changes to a repository in the actions workspace persist between actions in a workflow.
This action is useful to pair with other actions that modify or add files to your repository.
This action is designed to be used in conjunction with other actions that modify or add files to your repository.
The changes will be automatically committed to a new branch and a pull request created.
Create Pull Request action will:
@@ -13,13 +13,17 @@ 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.
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 repositories that have a very high frequency of commits.
## Usage
```hcl
action "Create Pull Request" {
uses = "peter-evans/create-pull-request@v1.0.0"
secrets = ["GITHUB_TOKEN"]
}
```yml
- name: Create Pull Request
uses: peter-evans/create-pull-request@v1.2.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
#### Environment variables
@@ -30,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.
- `PULL_REQUEST_TITLE` - The title 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
@@ -42,27 +52,37 @@ 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 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
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
Here is an example that sets all the environment variables.
Here is an example that sets all the main environment variables.
```hcl
action "Create Pull Request" {
uses = "peter-evans/create-pull-request@v1.0.0"
secrets = ["GITHUB_TOKEN"]
env = {
PULL_REQUEST_BRANCH = "auto-branch"
COMMIT_MESSAGE = "Auto-modify files by 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"
}
}
```yml
- name: Create Pull Request
uses: peter-evans/create-pull-request@v1.2.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST_BRANCH: my-patches
COMMIT_MESSAGE: Auto-modify files by 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
```
This configuration will create pull requests that look like this:
![Pull Request Example](pull-request-example.png?raw=true)
## License
MIT License - see the [LICENSE](LICENSE) file for details

93
create-pull-request.py Normal file → Executable file
View File

@@ -1,6 +1,8 @@
''' create-pull-request.py '''
#!/usr/bin/env python3
''' Create Pull Request '''
import json
import os
import time
from git import Repo
from github import Github
@@ -8,17 +10,27 @@ from github import Github
def get_github_event(github_event_path):
with open(github_event_path) as 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))
return github_event
def ignore_event(github_event):
# Ignore push events on deleted branches
deleted = "{deleted}".format(**github_event)
if deleted == "True":
print("Ignoring delete branch event.")
return True
def ignore_event(event_name, event_data):
if event_name == "push":
# Ignore push events on deleted branches
# 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
# "push" event that we want to ignore, otherwise it will create another branch and PR on
# the same commit.
deleted = "{deleted}".format(**event_data)
if deleted == "True":
print("Ignoring delete branch event.")
return True
ref = "{ref}".format(**event_data)
if not ref.startswith('refs/heads/'):
print("Ignoring events for tags and remotes.")
return True
return False
@@ -29,9 +41,13 @@ def pr_branch_exists(repo, branch):
return False
def get_head_author(github_event):
email = "{head_commit[author][email]}".format(**github_event)
name = "{head_commit[author][name]}".format(**github_event)
def get_head_author(event_name, event_data):
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'
name = os.environ['GITHUB_ACTOR']
return email, name
@@ -44,6 +60,10 @@ def set_git_config(git, email, name):
git.config('--global', 'user.name', '"%s"' % name)
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))
def commit_changes(git, branch, commit_message):
git.checkout('HEAD', b=branch)
git.add('-A')
@@ -59,7 +79,7 @@ def create_pull_request(token, repo, head, base, title, body):
head=head)
def process_event(github_event, repo, branch):
def process_event(event_name, event_data, repo, branch, base):
# Fetch required environment variables
github_token = os.environ['GITHUB_TOKEN']
github_repository = os.environ['GITHUB_REPOSITORY']
@@ -75,12 +95,11 @@ def process_event(github_event, repo, branch):
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action")
# 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_config(repo.git, author_email, author_name)
# Set the target base branch of the pull request
base = repo.active_branch.name
# Update URL for the 'origin' remote
set_git_remote_url(repo.git, github_token, github_repository)
# Commit the repository changes
print("Committing changes.")
@@ -101,26 +120,42 @@ def process_event(github_event, repo, branch):
# 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
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
repo = Repo(os.getcwd())
# Fetch/Set the branch name
branch = os.getenv('PULL_REQUEST_BRANCH', 'create-pull-request/patch')
# Suffix with the short SHA1 hash
branch = "%s-%s" % (branch, get_head_short_sha1(repo))
# Set the current branch as the target base branch
base = os.environ['GITHUB_REF'][11:]
# 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)
# Skip if the current branch is a PR branch created by this action
if not base.startswith(branch):
# Fetch an optional environment variable to determine the branch suffix
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:
print("Repository has no modified or untracked files. Skipping.")
# 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
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(event_name, event_data, repo, branch, base)
else:
print("Repository has no modified or untracked files. Skipping.")
else:
print(
"Pull request branch '%s' already exists for this commit. Skipping." %
branch)
else:
print(
"Pull request branch '%s' already exists for this commit. Skipping." %
branch)
"Branch '%s' was created by this action. Skipping." % base)

BIN
pull-request-example.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

5
renovate.json Normal file
View File

@@ -0,0 +1,5 @@
{
"extends": [
"config:base"
]
}

View File

@@ -1,2 +1,2 @@
GitPython==2.1.11
PyGithub==1.43.7
GitPython==3.0.2
PyGithub==1.43.8