Compare commits

...

14 Commits

Author SHA1 Message Date
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
21aff96eee Fix author email and name for scheduled jobs 2019-09-12 16:03:46 +09:00
f4703cdc23 Update README 2019-09-10 18:06:54 +09:00
7980880191 Fix missing colon 2019-09-10 17:58:01 +09:00
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
580fc69c02 Fix scheduled events issue 2019-09-10 09:14:39 +02:00
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
37fd4d3558 Update dependency GitPython to v3.0.2 2019-08-22 04:38:51 +00:00
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
91ff2766bf Update dependency GitPython to v3.0.1 2019-08-15 03:27:28 +00:00
c216905beb Update README 2019-08-13 19:07:24 +09:00
045ccaa641 Ignore events for tags and remotes 2019-08-13 19:00:56 +09:00
a26e9f2362 Update README 2019-08-13 18:26:08 +09:00
3 changed files with 38 additions and 24 deletions

View File

@ -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.
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.
## Usage
The default `GITHUB_TOKEN` does not have the access neccessary for this action to work correctly.
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.
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.0.0
uses: peter-evans/create-pull-request@v1.1.3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
```
@ -58,8 +60,9 @@ Here is an example that sets all the environment variables.
```yml
- name: Create Pull Request
uses: peter-evans/create-pull-request@v1.0.0
uses: peter-evans/create-pull-request@v1.1.3
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

View File

@ -10,20 +10,26 @@ 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:
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
# 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
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
@ -34,9 +40,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
@ -68,7 +78,7 @@ def create_pull_request(token, repo, head, base, title, body):
head=head)
def process_event(github_event, repo, branch, base):
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']
@ -85,7 +95,7 @@ def process_event(github_event, repo, branch, base):
"[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)
# Update URL for the 'origin' remote
@ -110,9 +120,10 @@ def process_event(github_event, repo, branch, base):
# 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):
if not ignore_event(event_name, event_data):
# Set the repo to the working directory
repo = Repo(os.getcwd())
@ -131,7 +142,7 @@ if not ignore_event(github_event):
# 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)
process_event(event_name, event_data, repo, branch, base)
else:
print("Repository has no modified or untracked files. Skipping.")
else:

View File

@ -1,2 +1,2 @@
GitPython==3.0.0
GitPython==3.0.2
PyGithub==1.43.8