|
| 1 | +from gitconsensusservice import app, celery |
| 2 | +from services.githubapp import gh |
| 3 | + |
| 4 | + |
| 5 | +@celery.task |
| 6 | +def process_installs(synchronous=False): |
| 7 | + print('Processing all installations') |
| 8 | + installs = gh.get_installations() |
| 9 | + for install in installs: |
| 10 | + if synchronous: |
| 11 | + process_installation(install, True) |
| 12 | + else: |
| 13 | + process_installation.delay(install) |
| 14 | + |
| 15 | + |
| 16 | +@celery.task |
| 17 | +def process_installation(installation_id, synchronous=False): |
| 18 | + print('Processing installation %s' % (installation_id)) |
| 19 | + installation = gh.get_installation(installation_id) |
| 20 | + repositories = installation.get_repositories() |
| 21 | + for repository in repositories: |
| 22 | + user, repo = repository.split('/') |
| 23 | + process_repository(installation_id, user, repo, True) |
| 24 | + |
| 25 | + |
| 26 | +@celery.task |
| 27 | +def process_repository(installation_id, user, repo, synchronous=False): |
| 28 | + print('Processing %s/%s as installation %s' % (user, repo, installation_id)) |
| 29 | + installation = gh.get_installation(installation_id) |
| 30 | + repository = installation.get_repository(user, repo) |
| 31 | + |
| 32 | + # has consensus rules? |
| 33 | + if not repository.rules: |
| 34 | + print('%s/%s does not have any consensus rules.' % (user, repo)) |
| 35 | + return |
| 36 | + |
| 37 | + prs = installation.get_pr_numbers(user, repo) |
| 38 | + for pr in prs: |
| 39 | + if synchronous: |
| 40 | + process_pull_request(installation_id, user, repo, pr) |
| 41 | + else: |
| 42 | + process_pull_request.delay(installation_id, user, repo, pr) |
| 43 | + |
| 44 | + |
| 45 | +@celery.task |
| 46 | +def process_pull_request(installation_id, user, repo, pull_request): |
| 47 | + print('Processing %s/%s #%s as installation %s' % (user, repo, pull_request, installation_id)) |
| 48 | + installation = gh.get_installation(installation_id) |
| 49 | + repository = installation.get_repository(user, repo) |
| 50 | + request = repository.getPullRequest(pull_request) |
| 51 | + if request.validate(): |
| 52 | + print("Merging PR#%s" % (request.number,)) |
| 53 | + request.vote_merge() |
| 54 | + elif request.shouldClose(): |
| 55 | + print("Closing PR#%s" % (request.number,)) |
| 56 | + request.close() |
| 57 | + else: |
| 58 | + request.addInfoLabels() |
0 commit comments