diff --git a/openedx_webhooks/views/jira.py b/openedx_webhooks/views/jira.py index b29d5f90..1a8aaee9 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(): @@ -137,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? @@ -158,15 +192,26 @@ 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) old_status = status_changelog_items[0]["fromString"] new_status = status_changelog_items[0]["toString"] if new_status == "Rejected": + # 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)) + # 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 @@ -174,5 +219,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: + # 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"