2 Star 0 Fork 3

nicliuqi / daily

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
output_statistics.py 5.60 KB
一键复制 编辑 原始数据 按行查看 历史
Se7en 提交于 2023-03-25 06:27 . update output_statistics.py.
import argparse
import codecs
import csv
import requests
import smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('s', '--state', help='pull requests state to query')
parser.add_argument('-r', '--receivers', help='addresses to receive the message', required=True)
parser.add_argument('-b', '--branch', help='branch to query')
parser.add_argument('-host', '--host', help='SMTP host', required=True)
parser.add_argument('-p', '--port', help='SMTP port', required=True)
parser.add_argument('-u', '--user', help='SMTP user', required=True)
parser.add_argument('-pswd', '--password', help='SMTP password', required=True)
args = parser.parse_args()
return args
def fill_status(status, insert_string):
if status == '待合入':
status = insert_string
else:
status += '、{}'.format(insert_string)
return status
def send_mail(csv_file, host, port, sender, password, receivers):
msg = MIMEMultipart()
body_of_email = '各SIG的PR处理情况统计见附件'
content = MIMEText(body_of_email, 'plain', 'utf-8')
msg.attach(content)
file = MIMEApplication(open(csv_file, 'rb').read())
file.add_header('Content-Disposition', 'attachment', filename=csv_file)
msg.attach(file)
msg['Subject'] = '各SIG的PR处理情况统计'
msg['From'] = 'QuickIssue<{}>'.format(sender)
msg['To'] = receivers
try:
server = smtplib.SMTP(host, port)
server.ehlo()
server.starttls()
server.login(sender, password)
server.sendmail(sender, receivers.split(','), msg.as_string())
print('Sent statistics email to receivers: {}'.format(receivers))
except smtplib.SMTPException as e:
print('Fail to send statistics email, the reason is {}'.format(e))
def gene_csv(args):
now = datetime.now()
state = args.state.replace(',', ',')
branch = args.branch
if state == 'all':
state = ''
else:
for i in state.split(','):
if i not in ['open', 'closed', 'merged']:
print('Find unexpected state {} to search'.format(i))
print('Fail to check params, exit.')
return
if branch == 'all' or not branch:
branch = ''
print('Success to check params, continue.')
print('Starting to get pulls info...')
statistics_file = 'pulls_statistics.csv'
f = codecs.open(statistics_file, 'w')
writer = csv.writer(f)
writer.writerow(['SIG组', '仓库', '目标分支', 'PR链接', 'PR标题', 'PR状态', 'PR开启天数', 'PR最近未响应天数', 'PR提交人'])
page = 1
while True:
url = 'https://ipb.osinfra.cn/pulls?state={}&ref={}&per_page=100&page={}&direction=asc'.format(state, branch, page)
r = requests.get(url)
if r.status_code != 200:
print('Get unexpected response when getting pulls info, the response is: {}'.format(r.json()))
print('Fail to get pulls info, exit.')
break
for pull in r.json()['data']:
sig = pull['sig']
repo = pull['repo']
ref = pull['ref']
link = pull['link']
title = pull['title']
status = pull['state']
author = pull['author']
created_at = pull['created_at']
updated_at = pull['updated_at']
labels = pull['labels']
if status == 'closed':
status = '已关闭'
elif status == 'merged':
status = '已合入'
else:
status = '待合入'
if 'openeuler-cla/yes' not in labels:
status = fill_status(status, 'CLA认证失败')
if 'ci-failed' in labels:
status = fill_status(status, '门禁检查失败')
if 'conflicted' in labels:
status = fill_status(status, '存在冲突')
if 'kind/wait_for_update' in labels:
status = fill_status(status, '等待更新')
open_days = (now - datetime.strptime(created_at, '%Y-%m-%d %H:%M:%S')).days
response_days = (now - datetime.strptime(updated_at, '%Y-%m-%d %H:%M:%S')).days
writer.writerow([sig, repo, ref, link, title, status, open_days, response_days, author])
if len(r.json()['data']) < 100:
break
page += 1
print('Success to get pulls info and save it as a csv file.')
return statistics_file
def main():
print('Starting to check params...')
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--state', help='pull requests state to query')
parser.add_argument('-r', '--receivers', help='addresses to receive the message', required=True)
parser.add_argument('-b', '--branch', help='branch to query')
parser.add_argument('-host', '--host', help='SMTP host', required=True)
parser.add_argument('-p', '--port', help='SMTP port', required=True)
parser.add_argument('-u', '--user', help='SMTP user', required=True)
parser.add_argument('-pswd', '--password', help='SMTP password', required=True)
args = parser.parse_args()
csv_file = gene_csv(args)
print('Starting to send email to receivers...')
send_mail(csv_file, args.host, args.port, args.user, args.password, args.receivers.replace(',', ','))
if __name__ == '__main__':
main()
Python
1
https://gitee.com/nicliuqi/daily.git
git@gitee.com:nicliuqi/daily.git
nicliuqi
daily
daily
master

搜索帮助