Skip to content
Merged
Changes from all 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
64 changes: 63 additions & 1 deletion openedx_webhooks/views/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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?
Expand All @@ -158,21 +192,49 @@ 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
bugsnag.configure_request(meta_data=bugsnag_context)
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"