Index: tools/phabricator-helpers/new-contributors.py =================================================================== --- /dev/null +++ tools/phabricator-helpers/new-contributors.py @@ -0,0 +1,78 @@ +""" + The LLVM Compiler Infrastructure + +This file is distributed under the University of Illinois Open Source +License. See LICENSE.TXT for details. + + +Requires the phabricator API package + pip install phabricator==0.7 +Add API token to ~/.arcrc + cat ~/.arcrc +{ + "hosts": { + "https://reviews.llvm.org/api/": { + "token": "api-insertYourAPITokenHere" + } + } +} +""" + +import os +import sys + +from phabricator import Phabricator + +def read_time(): + if not os.path.isfile('last_review.txt'): + print("no last_review.txt file found, starting from the beginning") + return 0 + + with open('last_review.txt', 'rt') as f: + return int(f.read()) + + +def write_time(t): + with open('last_review.txt', 'wt') as f: + f.write(t) + + +SUBSCRIBER_FIELD = 'ccs' +def add_subscriber(phab, review, user_id): + if user_id in review[SUBSCRIBER_FIELD]: + return + + fields = { + 'ccPHIDs': review[SUBSCRIBER_FIELD] + [user_id,], + } + phab.differential.updaterevision( + id=int(review['id']), + diffid=int(review['diffs'][0]), + fields=fields, message='') + + +def handle_reviews(): + phab = Phabricator() # This will use your ~/.arcrc file + + last_review = read_time() + new_reviews = phab.differential.query( + limit=100, order="order-created", status="status-open" + ) + + print('Last review timestamp: {}'.format(last_review)) + new_reviews = [r for r in new_reviews if int(r['dateCreated']) > last_review] + + if len(new_reviews) == 0: + print("No new reviews") + return + + for r in new_reviews: + if len(phab.differential.query(limit=2, authors=[r['authorPHID']])) < 2: + print("Welcome new user "+r['uri']) + print(' * adding new subscriber') + # FIXME: user hardcoded to fhahn, add parameter. + add_subscriber(phab, r, 'PHID-USER-n4qjk3mn5cpkqytjhy7d') + + write_time(new_reviews[0]['dateCreated']) + +handle_reviews()