From 369c120413f810fef814297347876fc19225f432 Mon Sep 17 00:00:00 2001 From: Sarina Canelake Date: Wed, 24 Sep 2014 23:10:07 -0400 Subject: [PATCH 1/4] Add status transition hook to change labels --- openedx_webhooks/views/jira.py | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/openedx_webhooks/views/jira.py b/openedx_webhooks/views/jira.py index b29d5f90..ceb26452 100644 --- a/openedx_webhooks/views/jira.py +++ b/openedx_webhooks/views/jira.py @@ -28,6 +28,37 @@ def get_jira_custom_fields(): if value["custom"] } +# Maps JIRA status : Github label dictionary +# TODO: should load this directly from labels.yaml so the color and name info is up to date. +# Doesn't have the URL info but that is easily constructed. +STATUS_LABEL_DICT = { + 'Product Review': { + "url": "https://api.github.com/repos/edx/edx-platform/labels/product+review", + "name": "product review", + "color": "5319e7" + }, + 'Community Manager Review': { + "url": "https://api.github.com/repos/edx/edx-platform/labels/community+manager+review", + "name": "community manager review", + "color": "207de5" + }, + 'Awaiting Prioritization': { + 'url': 'https://api.github.com/repos/edx/edx-platform/labels/awaiting+prioritization', + 'name': 'awaiting prioritization', + 'color': 'fad8c7' + }, + 'Engineering Review': { + "url": "https://api.github.com/repos/edx/edx-platform/labels/community+manager+review", + "name": "community manager review", + "color": "207de5" + }, + 'Waiting on Author': { + "url": "https://api.github.com/repos/edx/edx-platform/labels/waiting+on+author", + "name": "waiting on author", + "color": "0052cc" + }, +} + @app.route("/jira/issue/created", methods=("POST",)) def jira_issue_created(): @@ -159,6 +190,8 @@ def jira_issue_updated(): fail_msg = '{key} is missing "Repo" or "PR Number" fields'.format(key=issue_key) raise Exception(fail_msg) pr_url = "/repos/{repo}/pulls/{num}".format(repo=pr_repo, num=pr_num) + # Need to use the Issues API for label manipulation + issue_url = "/repos/{repo}/issues/{num}".format(repo=pr_repo, num=pr_num) old_status = status_changelog_items[0]["fromString"] new_status = status_changelog_items[0]["toString"] @@ -174,5 +207,22 @@ def jira_issue_updated(): raise requests.exceptions.RequestException(close_resp.text) return "Closed PR #{num}".format(num=pr_num) + elif new_status in STATUS_LABEL_DICT.keys(): + # Get all the existing labels on this PR + label_list = github.get(issue_url)[labels] + + # Add in the new label and remove the old label + label_list.append(STATUS_LABEL_DICT[new_status]) + try: + label_list.remove(STATUS_LABEL_DICT[old_status]) + except ValueError: + print("PR {num} does not have label {old_label} to remove".format(num=pr_num, old_label=STATUS_LABEL_DICT[old_status])) + + # Post the new set of labels to github + label_resp = github.patch(issue_url, data=json.dumps({"labels": label_list})) + if not label_resp.ok: + raise requests.exceptions.RequestException(close_resp.text) + return "Changed label of PR #{num} to {labels}".format(num=pr_num, labels=label_list) + return "no change necessary" From 4f5ae4b0f1af2dc8e4662d7b3e25febbda66d957 Mon Sep 17 00:00:00 2001 From: Sarina Canelake Date: Fri, 26 Sep 2014 09:54:11 -0400 Subject: [PATCH 2/4] Add some TODOs --- openedx_webhooks/views/jira.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/openedx_webhooks/views/jira.py b/openedx_webhooks/views/jira.py index ceb26452..3ad7f6bb 100644 --- a/openedx_webhooks/views/jira.py +++ b/openedx_webhooks/views/jira.py @@ -198,6 +198,11 @@ def jira_issue_updated(): if new_status == "Rejected": # close the pull request on Github + ## TODO: Should also comment on the PR to explain to look at JIRA + ## Hello @{name}: We are unable to continue with review of your submission + ## at this time. Please see the associated JIRA ticket for more explanation. + + ## TODO: check if this is working (OSPR-35: rejecting ticket did not close PR) close_resp = github.patch(pr_url, data=json.dumps({"state": "closed"})) if not close_resp.ok: bugsnag_context['request_headers'] = close_resp.request.headers From 955bc6fc8057c151140aa0afa9a6e8fcc8519fef Mon Sep 17 00:00:00 2001 From: Sarina Canelake Date: Fri, 26 Sep 2014 12:55:27 -0400 Subject: [PATCH 3/4] Add a comment when closing a PR --- openedx_webhooks/views/jira.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/openedx_webhooks/views/jira.py b/openedx_webhooks/views/jira.py index 3ad7f6bb..935d2e30 100644 --- a/openedx_webhooks/views/jira.py +++ b/openedx_webhooks/views/jira.py @@ -168,6 +168,9 @@ def jira_issue_updated(): # is the issue an open source pull request? if event["issue"]["fields"]["project"]["key"] != "OSPR": + # TODO: if the issue has just been moved from the OSPR project to a new project, + # change the label to "engineering review". Need to figure out if we can tell that + # the ticket has just moved projects. return "I don't care" # is there a changelog? @@ -189,6 +192,7 @@ def jira_issue_updated(): if not pr_repo or not pr_num: fail_msg = '{key} is missing "Repo" or "PR Number" fields'.format(key=issue_key) raise Exception(fail_msg) + pr_url = "/repos/{repo}/pulls/{num}".format(repo=pr_repo, num=pr_num) # Need to use the Issues API for label manipulation issue_url = "/repos/{repo}/issues/{num}".format(repo=pr_repo, num=pr_num) @@ -197,14 +201,17 @@ def jira_issue_updated(): new_status = status_changelog_items[0]["toString"] if new_status == "Rejected": - # close the pull request on Github - ## TODO: Should also comment on the PR to explain to look at JIRA - ## Hello @{name}: We are unable to continue with review of your submission - ## at this time. Please see the associated JIRA ticket for more explanation. + # Comment on the PR to explain to look at JIRA + username = github.get(issue_url)["user"]["login"] + comment = { + "body": "Hello @{username}: We are unable to continue with review of your submission " + "at this time. Please see the associated JIRA ticket for more explanation.".format(username=username) + } + comment_resp = github.post(issue_url + "/comments", data=json.dumps(comment)) - ## TODO: check if this is working (OSPR-35: rejecting ticket did not close PR) + # close the pull request on Github close_resp = github.patch(pr_url, data=json.dumps({"state": "closed"})) - if not close_resp.ok: + if not close_resp.ok or not comment_resp.ok: bugsnag_context['request_headers'] = close_resp.request.headers bugsnag_context['request_url'] = close_resp.request.url bugsnag_context['request_method'] = close_resp.request.method @@ -214,7 +221,7 @@ def jira_issue_updated(): elif new_status in STATUS_LABEL_DICT.keys(): # Get all the existing labels on this PR - label_list = github.get(issue_url)[labels] + label_list = github.get(issue_url)["labels"] # Add in the new label and remove the old label label_list.append(STATUS_LABEL_DICT[new_status]) From 96de4e77287bdf3af91a35ef8c4377ae7f3a8368 Mon Sep 17 00:00:00 2001 From: Sarina Canelake Date: Fri, 26 Sep 2014 12:56:28 -0400 Subject: [PATCH 4/4] fixup! Add status transition hook to change labels --- openedx_webhooks/views/jira.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openedx_webhooks/views/jira.py b/openedx_webhooks/views/jira.py index 935d2e30..1a8aaee9 100644 --- a/openedx_webhooks/views/jira.py +++ b/openedx_webhooks/views/jira.py @@ -219,7 +219,7 @@ def jira_issue_updated(): raise requests.exceptions.RequestException(close_resp.text) return "Closed PR #{num}".format(num=pr_num) - elif new_status in STATUS_LABEL_DICT.keys(): + elif new_status in STATUS_LABEL_DICT: # Get all the existing labels on this PR label_list = github.get(issue_url)["labels"]