Created
April 7, 2017 14:46
-
-
Save MichaelPereira/a30ed5509c88da6908f0bce4298518b9 to your computer and use it in GitHub Desktop.
Python celery task for Pull Requests reminders
This file contains hidden or 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
@shared_task() | |
def daily_pr_reminder(): | |
list_of_repos = [] | |
# Use both pygithub and our Github API since we are using Github API beta endpoints | |
gh = GithubAPI(GithubToken.repo_token) | |
g = Github(GithubToken.repo_token.value) | |
for repo_string in list_of_repos: | |
repo = g.get_repo(repo_string, lazy=False) | |
attachments = [] | |
for pr in repo.get_pulls(): | |
reviews = gh.get_pull_request_reviews('deploy-manager', pr.number) | |
field = generate_field_from_reviews(pr.user, reviews) | |
# The PR state display if the jenkins build is working or not | |
pr_state = gh.get_pull_request_build_state('deploy-manager', pr.head.sha) | |
if pr_state == 'success': | |
color = 'good' | |
else: | |
color = 'danger' | |
converted_age = get_user_friendly_pr_age(pr) | |
title = '#%s <%s|%s> - %s ago - %s'\ | |
% (pr.number, pr.html_url, pr.title, converted_age, pr.user.login) | |
attachments.extend(generate_raw_attachment(color=color, fields=field, | |
text=title)) | |
# Notify only if there are any attachments, | |
# In other words, Ignore when there are no open PRs | |
if attachments: | |
payload = { | |
'text': 'The following pull requests are open in Repo: %s and may need review:' % repo_string, | |
'attachments': attachments, | |
'channel': 'slack_channel' | |
} | |
perform_slack_notification(payload=payload, | |
webhook_url='slack_webhook_url') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment