Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
21aff96eee | |||
f4703cdc23 | |||
7980880191 | |||
4efcea62da | |||
580fc69c02 | |||
facb42d776 | |||
37fd4d3558 | |||
bc78d4cf02 | |||
91ff2766bf | |||
c216905beb | |||
045ccaa641 | |||
a26e9f2362 | |||
860dc038c0 | |||
c0be263859 | |||
0f3781327e | |||
9b034ebd2d | |||
9a709f0173 | |||
cddd8048c2 | |||
8fe03d2026 | |||
a5bdb09ebe | |||
ed39fe9b45 | |||
f7b04aed2a | |||
d9e4270c6c | |||
e7637f96b8 | |||
dc019ec39e | |||
abaaf77b6e | |||
16d0603e20 | |||
5ffebf4a64 | |||
ab526929c6 | |||
754ec11bc0 | |||
d73557c33a | |||
f03950e9c3 | |||
83e2ff790e |
1
.github/CODEOWNERS
vendored
Normal file
1
.github/CODEOWNERS
vendored
Normal file
@ -0,0 +1 @@
|
||||
* @peter-evans
|
21
.github/workflows/push.yml
vendored
Normal file
21
.github/workflows/push.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
on: push
|
||||
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 }}
|
||||
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_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'
|
@ -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" ]
|
45
README.md
45
README.md
@ -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,21 @@ 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"]
|
||||
}
|
||||
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
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.1.2
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||
```
|
||||
|
||||
#### Environment variables
|
||||
@ -50,19 +58,22 @@ 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.
|
||||
|
||||
```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.1.2
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_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:
|
||||
|
||||

|
||||
|
||||
## License
|
||||
|
||||
MIT License - see the [LICENSE](LICENSE) file for details
|
||||
|
64
create-pull-request.py
Normal file → Executable file
64
create-pull-request.py
Normal file → Executable file
@ -1,4 +1,5 @@
|
||||
''' create-pull-request.py '''
|
||||
#!/usr/bin/env python3
|
||||
''' Create Pull Request '''
|
||||
import json
|
||||
import os
|
||||
from git import Repo
|
||||
@ -14,11 +15,22 @@ def get_github_event(github_event_path):
|
||||
|
||||
|
||||
def ignore_event(github_event):
|
||||
if 'schedule' in github_event:
|
||||
print("Allow schedule event.")
|
||||
return False
|
||||
# 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(**github_event)
|
||||
if deleted == "True":
|
||||
print("Ignoring delete branch event.")
|
||||
return True
|
||||
ref = "{ref}".format(**github_event)
|
||||
if not ref.startswith('refs/heads/'):
|
||||
print("Ignoring events for tags and remotes.")
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@ -30,8 +42,12 @@ def pr_branch_exists(repo, branch):
|
||||
|
||||
|
||||
def get_head_author(github_event):
|
||||
email = "{head_commit[author][email]}".format(**github_event)
|
||||
name = "{head_commit[author][name]}".format(**github_event)
|
||||
if 'schedule' in github_event:
|
||||
email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com'
|
||||
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
|
||||
|
||||
|
||||
@ -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://%s:x-oauth-basic@github.com/%s" % (token, github_repository))
|
||||
|
||||
|
||||
def commit_changes(git, branch, commit_message):
|
||||
git.checkout('HEAD', b=branch)
|
||||
git.add('-A')
|
||||
@ -59,10 +79,11 @@ def create_pull_request(token, repo, head, base, title, body):
|
||||
head=head)
|
||||
|
||||
|
||||
def process_event(github_event, repo, branch):
|
||||
def process_event(github_event, repo, branch, base):
|
||||
# Fetch required environment variables
|
||||
github_token = os.environ['GITHUB_TOKEN']
|
||||
github_repository = os.environ['GITHUB_REPOSITORY']
|
||||
repo_access_token = os.environ['REPO_ACCESS_TOKEN']
|
||||
# Fetch remaining optional environment variables
|
||||
commit_message = os.getenv(
|
||||
'COMMIT_MESSAGE',
|
||||
@ -78,9 +99,8 @@ def process_event(github_event, repo, branch):
|
||||
author_email, author_name = get_head_author(github_event)
|
||||
# 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, repo_access_token, github_repository)
|
||||
|
||||
# Commit the repository changes
|
||||
print("Committing changes.")
|
||||
@ -109,18 +129,26 @@ if not ignore_event(github_event):
|
||||
|
||||
# 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):
|
||||
# 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(github_event, repo, branch, base)
|
||||
else:
|
||||
print("Repository has no modified or untracked files. Skipping.")
|
||||
else:
|
||||
print("Repository has no modified or untracked files. Skipping.")
|
||||
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
BIN
pull-request-example.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 260 KiB |
5
renovate.json
Normal file
5
renovate.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": [
|
||||
"config:base"
|
||||
]
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
GitPython==2.1.11
|
||||
PyGithub==1.43.7
|
||||
GitPython==3.0.2
|
||||
PyGithub==1.43.8
|
Reference in New Issue
Block a user