-
-
Save checkaayush/8e27ca0aed17c1d82b925cb029f971c0 to your computer and use it in GitHub Desktop.
Script to push github commit stats to slack
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
import os | |
from datetime import datetime | |
from slacker import Slacker | |
from dateutil.relativedelta import relativedelta | |
import requests | |
from requests.auth import HTTPBasicAuth | |
USERNAME = "prakashpp" | |
GITHUB_TOKEN = os.environ['GITHUB_ACCESS_TOKEN'] | |
ICON_URL = "http://graph.facebook.com/v2.4/fulfil.io/picture" | |
SLACK_TOKEN = os.environ['SLACK_TOKEN'] | |
IGNORED_REPOS = [] | |
def get_number_of_commits_by_user(repo_names): | |
commit_by_user = {} | |
for repo in repo_names: | |
if repo in IGNORED_REPOS: | |
continue | |
resource_url = "https://api.github.com/repos/fulfilio/%s/commits" % ( | |
repo, | |
) | |
while True: | |
commits = requests.get( | |
resource_url, | |
auth=HTTPBasicAuth(USERNAME, GITHUB_TOKEN), | |
params={ | |
'since': ( | |
datetime.utcnow() - relativedelta(day=1) | |
).isoformat() | |
} | |
) | |
for commit in commits.json(): | |
if not commit['author']: | |
print commit | |
continue | |
user = commit['author']['login'] | |
commit_by_user.setdefault(user, []).append(( | |
repo, commit['commit']['message'], commit['html_url'] | |
)) | |
if commits.links.get('next'): | |
resource_url = commits.links['next']['url'] | |
else: | |
break | |
return commit_by_user | |
def get_all_repo_names(org='fulfilio'): | |
repo_names = [] | |
resource_url = "https://api.github.com/orgs/%s/repos" % (org, ) | |
while True: | |
repos = requests.get(resource_url, auth=HTTPBasicAuth(USERNAME, GITHUB_TOKEN)) # noqa | |
for repo in repos.json(): | |
if repo['fork']: | |
continue | |
repo_names.append(repo['name']) | |
if repos.links.get('next'): | |
resource_url = repos.links['next']['url'] | |
else: | |
break | |
return repo_names | |
if __name__ == "__main__": | |
commits_by_user = get_number_of_commits_by_user(get_all_repo_names()) | |
total_commits = 0 | |
repos = set([]) | |
slack_client = Slacker(SLACK_TOKEN) | |
for user, commits in commits_by_user.iteritems(): | |
attachments = [] | |
total_commits += len(commits) | |
for commit in commits: | |
repos.add(commit[0]) | |
slack_client.chat.post_message( | |
'dev', | |
"*Engineering Last Day*", | |
username="Fulfil BOT", | |
icon_url=ICON_URL, | |
attachments=[ | |
{ | |
"title": "Total Number of Commits", | |
"text": "%s" % (total_commits), | |
"color": "#7CD197" | |
}, { | |
"title": "Repository Updated", | |
"text": " \n".join(repos), | |
"color": "#7CD197" | |
} | |
] | |
) | |
for user, commits in commits_by_user.iteritems(): | |
attachments = [] | |
for commit in commits: | |
attachments.append({ | |
"title": commit[1], | |
"title_link": commit[2], | |
"text": "Repostiory: %s" % (commit[0]), | |
"color": "#7CD197" | |
}) | |
slack_client.chat.post_message( | |
'dev', | |
"*Commits by %s : %s*" % (user, len(commits)), | |
username="Fulfil BOT", | |
icon_url=ICON_URL, | |
attachments=attachments | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment