diff --git a/openedx_webhooks/views/jira.py b/openedx_webhooks/views/jira.py index b4df9a73..cac0ac70 100644 --- a/openedx_webhooks/views/jira.py +++ b/openedx_webhooks/views/jira.py @@ -27,6 +27,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(): @@ -156,16 +187,40 @@ 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"] 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: 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"