Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
9d998cbb4c | |||
baab1666cc | |||
f639d94f5e | |||
fd4e62ed7d | |||
21aff96eee | |||
f4703cdc23 | |||
7980880191 | |||
4efcea62da | |||
580fc69c02 | |||
facb42d776 | |||
37fd4d3558 | |||
bc78d4cf02 | |||
91ff2766bf | |||
c216905beb | |||
045ccaa641 | |||
a26e9f2362 |
@ -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
|
13
README.md
13
README.md
@ -13,18 +13,20 @@ 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.
|
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.
|
3. Create a pull request to merge the new branch into the currently active branch executing the workflow.
|
||||||
|
|
||||||
Note: In general, it's not good practice to modify your repository during workflows.
|
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.
|
This action is experimental and may not work well for repositories that have a very high frequency of commits.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
The default `GITHUB_TOKEN` does not have the access neccessary for this action to work correctly.
|
In addition to the default `GITHUB_TOKEN`, the action requires a `repo` scoped token in order to commit.
|
||||||
Create a new `repo` scoped token [here](https://github.com/settings/tokens) and pass that as a secret to the `REPO_ACCESS_TOKEN` environment variable.
|
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.0.0
|
uses: peter-evans/create-pull-request@v1.1.4
|
||||||
env:
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
|
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -58,8 +60,9 @@ Here is an example that sets all the environment variables.
|
|||||||
|
|
||||||
```yml
|
```yml
|
||||||
- name: Create Pull Request
|
- name: Create Pull Request
|
||||||
uses: peter-evans/create-pull-request@v1.0.0
|
uses: peter-evans/create-pull-request@v1.1.4
|
||||||
env:
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_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
|
||||||
|
@ -10,20 +10,26 @@ 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 os.environ.get('DEBUG_EVENT') is not None:
|
||||||
|
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):
|
||||||
# Ignore push events on deleted branches
|
if event_name == "push":
|
||||||
# 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(**github_event)
|
# the same commit.
|
||||||
if deleted == "True":
|
deleted = "{deleted}".format(**event_data)
|
||||||
print("Ignoring delete branch event.")
|
if deleted == "True":
|
||||||
return 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
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -34,9 +40,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):
|
||||||
email = "{head_commit[author][email]}".format(**github_event)
|
if event_name == "push":
|
||||||
name = "{head_commit[author][name]}".format(**github_event)
|
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
|
return email, name
|
||||||
|
|
||||||
|
|
||||||
@ -50,7 +60,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):
|
||||||
@ -68,11 +78,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',
|
||||||
@ -85,11 +94,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.")
|
||||||
@ -110,9 +119,10 @@ 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):
|
if 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())
|
||||||
|
|
||||||
@ -131,7 +141,7 @@ if not ignore_event(github_event):
|
|||||||
# 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:
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
GitPython==3.0.0
|
GitPython==3.0.2
|
||||||
PyGithub==1.43.8
|
PyGithub==1.43.8
|
Reference in New Issue
Block a user