From d112a7ed234b0db1e6341b1bba36d143717cbfb5 Mon Sep 17 00:00:00 2001 From: Tomas Borcin Date: Wed, 9 Jun 2021 12:59:34 +0200 Subject: [PATCH 1/4] cherry-picked PR --- sync_issues_to_jira/Dockerfile | 1 + sync_issues_to_jira/push_event.py | 49 +++++++++++++++++++++++++++++ sync_issues_to_jira/sync_to_jira.py | 6 ++++ 3 files changed, 56 insertions(+) create mode 100644 sync_issues_to_jira/push_event.py diff --git a/sync_issues_to_jira/Dockerfile b/sync_issues_to_jira/Dockerfile index 986db78..ed1bf74 100644 --- a/sync_issues_to_jira/Dockerfile +++ b/sync_issues_to_jira/Dockerfile @@ -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"] diff --git a/sync_issues_to_jira/push_event.py b/sync_issues_to_jira/push_event.py new file mode 100644 index 0000000..4dc9bf6 --- /dev/null +++ b/sync_issues_to_jira/push_event.py @@ -0,0 +1,49 @@ +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.') + return + original_title = pull_request.title + # Prepend [Merged] to the pull request title + 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.\ + 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 + # 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 fixed #10 + # fixing #11 + # Matches: [1, 4, 7, 9, 10] + pattern = re.compile('(?:[Ff]ix(?:e[sd]?)(?:\ |:)+|(?:[Cc]los(?:e[sd]?)(?:\ |:)+)|(?:[Rr]esolv(?:e[sd]?)(?:\ |:)+))#(\d+)') + return pattern.findall(commit_message) \ No newline at end of file diff --git a/sync_issues_to_jira/sync_to_jira.py b/sync_issues_to_jira/sync_to_jira.py index f1a8be2..6f488fc 100755 --- a/sync_issues_to_jira/sync_to_jira.py +++ b/sync_issues_to_jira/sync_to_jira.py @@ -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): @@ -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 + 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"] From ecae9f0da8e6f93fe9c29798b7719a2763fa7be2 Mon Sep 17 00:00:00 2001 From: Tomas Borcin Date: Wed, 9 Jun 2021 13:07:29 +0200 Subject: [PATCH 2/4] Added license header --- sync_issues_to_jira/push_event.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sync_issues_to_jira/push_event.py b/sync_issues_to_jira/push_event.py index 4dc9bf6..2668652 100644 --- a/sync_issues_to_jira/push_event.py +++ b/sync_issues_to_jira/push_event.py @@ -1,3 +1,19 @@ +#!/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 From 513c1d25204a8f5d6530e4cbc901f79b388373b8 Mon Sep 17 00:00:00 2001 From: Tomas Borcin Date: Tue, 15 Jun 2021 08:08:40 +0200 Subject: [PATCH 3/4] change regex pattern to accept full url and make it more readable --- sync_issues_to_jira/push_event.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/sync_issues_to_jira/push_event.py b/sync_issues_to_jira/push_event.py index 2668652..db9ea9a 100644 --- a/sync_issues_to_jira/push_event.py +++ b/sync_issues_to_jira/push_event.py @@ -46,12 +46,15 @@ def update_pull_request(pull_request): def parse_commit_message(commit_message): - # Regex matches numbers that come after Fix, fix, Fixed, fixed, Fixes, fixes keyword followed by any + # First regex matches numbers that come after Fix, fix, Fixed, fixed, Fixes, fixes keyword followed by any # 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. + # 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 keyword for each of them. + # + # Second regex matches pull request numbers from pull reques url. + # # Example: + # # fixed: #1 #2 #3 # resolved ::: ::: :: #4 # closes: ##5 @@ -60,6 +63,17 @@ def parse_commit_message(commit_message): # closeasdf: #8 # closes #9 fixed #10 # fixing #11 - # Matches: [1, 4, 7, 9, 10] - pattern = re.compile('(?:[Ff]ix(?:e[sd]?)(?:\ |:)+|(?:[Cc]los(?:e[sd]?)(?:\ |:)+)|(?:[Rr]esolv(?:e[sd]?)(?:\ |:)+))#(\d+)') - return pattern.findall(commit_message) \ No newline at end of file + # https://github.com/espressif/esp-idf/pull/12\ + # https://github.com/espressif/esp-idf/pull/ 13\ + # https://github.com/espressif/esp-idf/pull/#14\ + # https://github.com/ESPRESSIF/esp-idf/pull/15 + # + # Above example matches: [1, 4, 9, 10, 12, 15] + + # + reference_pattern = re.compile(r'(?:closes?|closed|fixes|fix|fixed|resolves?|resolved)(?:\s|:)+#(\d+)', re.IGNORECASE) + url_pattern = re.compile(r'(?:https://github.com/espressif/esp-idf/pull/)(\d+)', re.IGNORECASE) + matches = [] + matches += reference_pattern.findall(commit_message) + matches += url_pattern.findall(commit_message) + return matches \ No newline at end of file From f84722b187c1712daadc8c63c2959aaace61f984 Mon Sep 17 00:00:00 2001 From: Tomas Borcin Date: Tue, 15 Jun 2021 08:09:41 +0200 Subject: [PATCH 4/4] remove check for PR being opened and add check for PR being already merged --- sync_issues_to_jira/push_event.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sync_issues_to_jira/push_event.py b/sync_issues_to_jira/push_event.py index db9ea9a..c57ea9f 100644 --- a/sync_issues_to_jira/push_event.py +++ b/sync_issues_to_jira/push_event.py @@ -33,10 +33,11 @@ def handle_push_event(event): def update_pull_request(pull_request): print("Updating %s" % pull_request) - if pull_request.state == 'open': - print('Pull request is open, nothing to update.') - return original_title = pull_request.title + # Check if original title already starts with [Merged] keyword. If yes, skip updating pull request. + if original_title.startswith('[Merged]'): + print('Pull request title suggests it was already merged. Skipping...') + return # Prepend [Merged] to the pull request title new_title = '[Merged] ' + original_title pull_request.edit(title=new_title)