Skip to content

Commit f639d94

Browse files
committed
Fix ignore_event for all non-push events
1 parent fd4e62e commit f639d94

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

create-pull-request.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,26 @@ def get_github_event(github_event_path):
1010
with open(github_event_path) as f:
1111
github_event = json.load(f)
1212
if os.environ.get('DEBUG_EVENT') is not None:
13+
print(os.environ['GITHUB_EVENT_NAME'])
1314
print(json.dumps(github_event, sort_keys=True, indent=2))
1415
return github_event
1516

1617

17-
def ignore_event(github_event):
18-
if 'schedule' in github_event:
19-
print("Allow schedule event.")
20-
return False
21-
# Ignore push events on deleted branches
22-
# The event we want to ignore occurs when a PR is created but the repository owner decides
23-
# not to commit the changes. They close the PR and delete the branch. This creates a
24-
# "push" event that we want to ignore, otherwise it will create another branch and PR on
25-
# the same commit.
26-
deleted = "{deleted}".format(**github_event)
27-
if deleted == "True":
28-
print("Ignoring delete branch event.")
29-
return True
30-
ref = "{ref}".format(**github_event)
31-
if not ref.startswith('refs/heads/'):
32-
print("Ignoring events for tags and remotes.")
33-
return True
18+
def ignore_event(event_name, event_data):
19+
if event_name == "push":
20+
# Ignore push events on deleted branches
21+
# The event we want to ignore occurs when a PR is created but the repository owner decides
22+
# not to commit the changes. They close the PR and delete the branch. This creates a
23+
# "push" event that we want to ignore, otherwise it will create another branch and PR on
24+
# the same commit.
25+
deleted = "{deleted}".format(**event_data)
26+
if deleted == "True":
27+
print("Ignoring delete branch event.")
28+
return True
29+
ref = "{ref}".format(**event_data)
30+
if not ref.startswith('refs/heads/'):
31+
print("Ignoring events for tags and remotes.")
32+
return True
3433
return False
3534

3635

@@ -41,13 +40,13 @@ def pr_branch_exists(repo, branch):
4140
return False
4241

4342

44-
def get_head_author(github_event):
45-
if 'schedule' in github_event:
43+
def get_head_author(event_name, event_data):
44+
if event_name == "push":
45+
email = "{head_commit[author][email]}".format(**event_data)
46+
name = "{head_commit[author][name]}".format(**event_data)
47+
else:
4648
email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com'
4749
name = os.environ['GITHUB_ACTOR']
48-
else:
49-
email = "{head_commit[author][email]}".format(**github_event)
50-
name = "{head_commit[author][name]}".format(**github_event)
5150
return email, name
5251

5352

@@ -79,7 +78,7 @@ def create_pull_request(token, repo, head, base, title, body):
7978
head=head)
8079

8180

82-
def process_event(github_event, repo, branch, base):
81+
def process_event(event_name, event_data, repo, branch, base):
8382
# Fetch required environment variables
8483
github_token = os.environ['GITHUB_TOKEN']
8584
github_repository = os.environ['GITHUB_REPOSITORY']
@@ -96,7 +95,7 @@ def process_event(github_event, repo, branch, base):
9695
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action")
9796

9897
# Get the HEAD committer's email and name
99-
author_email, author_name = get_head_author(github_event)
98+
author_email, author_name = get_head_author(event_name, event_data)
10099
# Set git configuration
101100
set_git_config(repo.git, author_email, author_name)
102101
# Update URL for the 'origin' remote
@@ -121,9 +120,10 @@ def process_event(github_event, repo, branch, base):
121120

122121

123122
# Get the JSON event data
124-
github_event = get_github_event(os.environ['GITHUB_EVENT_PATH'])
123+
event_name = os.environ['GITHUB_EVENT_NAME']
124+
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
125125
# Check if this event should be ignored
126-
if not ignore_event(github_event):
126+
if not ignore_event(event_name, event_data):
127127
# Set the repo to the working directory
128128
repo = Repo(os.getcwd())
129129

@@ -142,7 +142,7 @@ def process_event(github_event, repo, branch, base):
142142
# Check if there are changes to pull request
143143
if repo.is_dirty() or len(repo.untracked_files) > 0:
144144
print("Repository has modified or untracked files.")
145-
process_event(github_event, repo, branch, base)
145+
process_event(event_name, event_data, repo, branch, base)
146146
else:
147147
print("Repository has no modified or untracked files. Skipping.")
148148
else:

0 commit comments

Comments
 (0)