-
-
Save bgamari/72020a6186be205d0f33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
from collections import named_tuple | |
import re | |
import mailbox | |
import time | |
from subprocess import check_call, check_output | |
import tempfile | |
maildir_path = '.' | |
mbox = mailbox.Maildir(maildir_path) | |
handled = set() | |
user = 'haskell' | |
repo = 'ghc' | |
pull_re = re.compile(r'git pull (https:\/\/github.com/[\w\d-]+/[\w\d-]+) ([\w\d-]+)') | |
PullRequest = named_tuple('PullRequest', 'pull_repo pull_branch') | |
def parse_message(msg): | |
to = msg.get('To') | |
if to is None: | |
raise RuntimeError('No To header, ignoring') | |
m = re.find('%s/%s' % (user, repo), to) | |
if m is None: | |
raise RuntimeError('To header %s malformed, ignoring' % to) | |
reply_to = msg.get('Reply-To') | |
if reply_to is None: | |
raise RuntimeError('No Reply-To header, ignoring') | |
m = pull_re.find(msg.as_string()) | |
if m is None: | |
raise RuntimeError("Couldn't find git pull command, ignoring") | |
pull_repo = m.group(1) | |
pull_branch = m.group(2) | |
return PullRequest(pull_repo, pull_branch) | |
def handle_message(msg): | |
pr = parse_message(msg) | |
# TODO: Look up using git pull request API | |
target_branch = '' | |
tmp = tempfile.mkdtemp() | |
repo_path = os.path.join(tmp, 'repo') | |
check_call(['git', 'clone', repo_url, 'repo'], cwd=tmp) | |
check_call(['git', 'remote', 'add', 'pr', pull_repo], cwd=repo_path) | |
check_call(['git', 'remote', 'update', 'pr'], cwd=repo_path) | |
merge_base = check_output(['git', 'merge-base', | |
'origin/%s' % target_branch, | |
'pr/%s' % pull_branch], cwd=repo_path) | |
if merge_base == '': | |
return | |
os.rmdir(tmp) | |
check_call(['git', 'checkout', 'pr/%s' % pull_branch], cwd=repo_path) | |
arc_output = check_output(['arc', 'diff', '--never-amend', '--verbatim', merge_base]) | |
diff_id = 'D42' # TODO: parse arc output | |
send_notification(diff_id) | |
msg.set_flags('SR') | |
def send_notification(diff_id): | |
diff_url = 'http://phabricator.haskell.org/differential/%s' % diff_id | |
msg = """ | |
Hi ghc-newbie, | |
We've received your pull request on GitHub. Thanks! It's great that | |
you've contributed. | |
GHC, for a variety of well-considered reasons [1], uses a tool called | |
Phabricator [2] -- not GitHub -- to process contributions. We have | |
taken the liberty of posting your pull request to Phab for further | |
review. You can see it here: %s | |
If you have modifications to make, please use our Phab workflow, | |
documented in full here [3] and in brief here [4]. | |
Thanks again, and we look forward to reviewing and hopefully merging | |
your patch! | |
- The GHC team | |
[1]: <find appropriate destination> | |
[2]: http://phabricator.org/ | |
[3]: https://ghc.haskell.org/trac/ghc/wiki/Phabricator | |
[4]: <find appropriate destination> | |
""" % (diff_url) | |
sendmail(reply_to, msg) | |
while True: | |
for key,msg in mbox.items(): | |
if key in handled: | |
continue | |
try: | |
handle_message(msg) | |
except Exception as e: | |
print "Encountered exception while handling message %s: %s" % (key,e) | |
finally: | |
handled.add(key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment