Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

cherry-picked PR #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sync_issues_to_jira/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ ADD sync_issue.py /sync_issue.py
ADD sync_pr.py /sync_pr.py
ADD sync_to_jira.py /sync_to_jira.py
ADD test_sync_to_jira.py /test_sync_to_jira.py
ADD push_event.py /push_event.py

ENTRYPOINT ["/usr/bin/python3", "/sync_to_jira.py"]
65 changes: 65 additions & 0 deletions sync_issues_to_jira/push_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
#
# Copyright 2019 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from github import Github
import os
import re

def handle_push_event(event):
issue_numbers = []
for commit in event['commits']:
commit_message = commit['message']
issue_numbers += parse_commit_message(commit_message)

github = Github(os.environ['GITHUB_TOKEN'])
repo = github.get_repo(os.environ['GITHUB_REPOSITORY'])
for issue in issue_numbers:
gh_issue = repo.get_issue(int(issue))
if gh_issue.pull_request:
update_pull_request(gh_issue.as_pull_request())

def update_pull_request(pull_request):
print("Updating %s" % pull_request)
if pull_request.state == 'open':
print('Pull request is open, nothing to update.')
igrr marked this conversation as resolved.
Show resolved Hide resolved
return
original_title = pull_request.title
# Prepend [Merged] to the pull request title
igrr marked this conversation as resolved.
Show resolved Hide resolved
new_title = '[Merged] ' + original_title
pull_request.edit(title=new_title)
# Thank contributor for opening pull request. Let them know we didn't throw it away
pull_request.create_issue_comment('The pull request has been cherry-picked, the commit is linked above.\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes we actually do "close" a PR by doing something different to what the author originally intended, and with a totally different commit. We try to avoid this, but it's necessary sometimes.

Ideally this action would check if the commit author is the same as the author of any of the commits in the PR branch, and only continue if it is. However this is probably a rare enough occasion that you can consider it a "nice to have". :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for suggestion. I have noted this feature request and will implement it in the future.

Thank you for your contribution!')


def parse_commit_message(commit_message):
# Regex matches numbers that come after Fix, fix, Fixed, fixed, Fixes, fixes keyword followed by any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our internal guide suggests writing "Merges ..." as well (which isn't a GitHub keyword) and then manually telling the author their PR has been cherry-picked. This is something I originally did a lot as it seemed more polite. However, in recenttimes most people (including me) don't do this any more and they will write "Closes" or "Fixes".

I will send you a link to the page, up to you if you prefer to update the script to make the original workflow convenient or update the workflow to match the current behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update workflow after this functionality is live in github-actions.

# combination of spaces and colons, followed by exactly one hashtag. The same applies for Close and Resolve
# keywords and their combinations. Note: fixing, closing and resolving don't work.
# Only first number is picked. To match multiple numbers you have to add fix or close or resolve or implement keyword
# for each of them.
# Example:
# fixed: #1 #2 #3
# resolved ::: ::: :: #4
# closes: ##5
# fix: # 6
# asdfresolves #7
# closeasdf: #8
# closes #9 <any sting in between> fixed #10
# fixing #11
igrr marked this conversation as resolved.
Show resolved Hide resolved
# Matches: [1, 4, 7, 9, 10]
pattern = re.compile('(?:[Ff]ix(?:e[sd]?)(?:\ |:)+|(?:[Cc]los(?:e[sd]?)(?:\ |:)+)|(?:[Rr]esolv(?:e[sd]?)(?:\ |:)+))#(\d+)')
igrr marked this conversation as resolved.
Show resolved Hide resolved
return pattern.findall(commit_message)
6 changes: 6 additions & 0 deletions sync_issues_to_jira/sync_to_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json
from sync_pr import sync_remain_prs
from sync_issue import *
from push_event import handle_push_event


class _JIRA(JIRA):
Expand Down Expand Up @@ -52,6 +53,11 @@ def main():
with open(os.environ['GITHUB_EVENT_PATH'], 'r') as f:
event = json.load(f)
print(json.dumps(event, indent=4))

# Check if it's a push event
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest this would be easier to test and maintain as a standalone action. There isn't any conceptual relationship between this functionality and JIRA, and the script doesn't use any functions from the jira sync code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to refactor the github-actions so it is standalone action. Unfortunately this requires much more effort than I anticipated initially and I suggest we address that in new pull request.

if os.environ['GITHUB_EVENT_NAME'] == 'push':
handle_push_event(event)
return

event_name = os.environ['GITHUB_EVENT_NAME'] # The name of the webhook event that triggered the workflow.
action = event["action"]
Expand Down