python 脚本将github提交统计信息推迟
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 脚本将github提交统计信息推迟相关的知识,希望对你有一定的参考价值。
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
)
以上是关于python 脚本将github提交统计信息推迟的主要内容,如果未能解决你的问题,请参考以下文章
python 统计信息 - 用于相关性之间统计比较的Python脚本