-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
Fixes #751. Import issue labeler into webcompat app.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
'''Flask Blueprint for our "webhooks" module, which we use to do cool things | ||
with GitHub events and actions. | ||
See https://developer.github.com/webhooks/ for what is possible.''' | ||
|
||
import json | ||
|
||
from flask import Blueprint | ||
from flask import abort | ||
from flask import request | ||
|
||
from helpers import parse_and_set_label | ||
|
||
webhooks = Blueprint('webhooks', __name__, url_prefix='/webhooks') | ||
|
||
|
||
@webhooks.route('/labeler', methods=['GET', 'POST']) | ||
def hooklistener(): | ||
'''Listen for the "issues" webhook event, parse the body, | ||
post back labels. But only do that for the 'opened' action.''' | ||
if request.method == 'GET': | ||
abort(403) | ||
elif (request.method == 'POST' and | ||
request.headers.get('X-GitHub-Event') == 'issues'): | ||
payload = json.loads(request.data) | ||
if payload.get('action') == 'opened': | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
miketaylr
Member
|
||
issue_body = payload.get('issue')['body'] | ||
issue_number = payload.get('issue')['number'] | ||
parse_and_set_label(issue_body, issue_number) | ||
return ('gracias, amigo.', 200) | ||
else: | ||
return ('cool story, bro.', 200) | ||
elif (request.method == 'POST' and | ||
request.headers.get('X-GitHub-Event') == 'ping'): | ||
return ('pong', 200) | ||
else: | ||
abort(403) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import json | ||
import os | ||
import re | ||
import requests | ||
|
||
from webcompat import app | ||
|
||
|
||
def api_post(endpoint, payload, issue): | ||
'''Helper method to post junk to GitHub.''' | ||
headers = { | ||
'Authorization': 'token {0}'.format(app.config['BOT_OAUTH_TOKEN']) | ||
} | ||
uri = 'https://api.github.com/repos/{0}/{1}/{2}'.format( | ||
app.config['ISSUES_REPO_URI'], issue, endpoint) | ||
requests.post(uri, data=json.dumps(payload), headers=headers) | ||
|
||
|
||
def parse_and_set_label(body, issue_number): | ||
'''Parse the labels from the body in comments like so: | ||
<!-- @browser: value -->. Currently this only handles a single label, | ||
because that's all that we set in webcompat.com. | ||
''' | ||
match_list = re.search(r'<!--\s@(\w+):\s([^\d]+?)\s[\d\.]+\s-->', body) | ||
if match_list: | ||
# perhaps we do something more interesting depending on | ||
# what groups(n)[0] is in the future. | ||
# right now, match_list.groups(0) should look like: | ||
# ('browser', 'firefox') | ||
browser = match_list.groups(0)[1].lower() | ||
dash_browser = '-'.join(browser.split()) | ||
set_label('browser-' + dash_browser, issue_number) | ||
|
||
|
||
def set_label(label, issue_number): | ||
'''Do a GitHub POST request using one of our bots, which has push access | ||
and set a label for the issue.''' | ||
# POST /repos/:owner/:repo/issues/:number/labels | ||
# ['Label1', 'Label2'] | ||
payload = [label] | ||
api_post('labels', payload, issue_number) |
hmmm no review ;)
This can be improved. I'm afraid it's too weak with regards to spam.
For example we could listen to the IP address
But it seems now that GitHub is recommending against it and prefers we check the authentification. Reasonable.