Loading

Gitlab Burndown Chart

一、说明

通过调用gitlab api直接获取相应project的所有issues,然后对其进行统计以制作燃尽图

二、方法

1.生成 Personal access token

Gitlab > Profile > Access Tokens

设置Name, Expires at, Scopes后Create personal access token

2.获取Project ID

curl https://gitlab.buaaoo.top/api/v4/projects?private_token=$your_personal_access_token$&search=$your_project_name$

将$your_personal_access_token$替换为你的personal access token

将$your_project_name$替换为你的项目名称

获取的数据中,第一个id即为项目id

3.执行脚本生成燃尽图

3.1获取项目的所有issues

curl --header "PRIVATE-TOKEN: $your_personal_access_token$" https://gitlab.buaaoo.top/api/v4/projects/ $your_project_id$/issues?per_page=$decided by your issues numbers$

将$your_personal_access_token$替换为你的personal access token

将$your_project_id$替换为你的项目id

将$decided by your issues numbers$替换为大于等于项目issues最大数量的值,比如你估计你的项目最多会同时存在80个issues,你可以把值设为100

注:per_page 默认为20,如果你不知道目前项目共有多少issues,也可以直接执行如下命令查看

curl --head --header "PRIVATE-TOKEN: $your_personal_access_token$" https://gitlab.buaaoo.top/api/v4/projects/$your_project_id$/issues

返回的数据中X-Total字段即为当前项目的issues数量

3.2 统计并生成燃尽图

根据issues数量的变化生成期望曲线和实际变化曲线

完整脚本如下:

import json
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from collections import defaultdict
import os

# get all issues
project_id = #your project id#
gitlab_private_token = #your personal access token#

get_issue_cmd = 'curl --header "PRIVATE-TOKEN: ' + gitlab_private_token + '" "https://gitlab.buaaoo.top/api/v4/projects/' \
                + project_id + '/issues?per_page=100" > all_issues.json'

os.system(get_issue_cmd)

# alpha phase start date and end date
start_date = '2021-04-21'
end_date = '2021-05-14'

# get all dates between start date and end date
dates = []
dt = datetime.strptime(start_date, "%Y-%m-%d")
date = start_date
while date <= end_date:
    dates.append(date)
    dt = dt + timedelta(1)
    date = dt.strftime("%Y-%m-%d")

# count issue day by day
daily_issues_cnt = defaultdict(int)
issues_cnt = []
today = datetime.strftime(datetime.now() - timedelta(0), '%Y-%m-%d')

with open('all_issues.json') as json_file:
    data = json.load(json_file)

for date in dates:
    daily_issues_cnt[date] = 0
for i in range(len(data)):
    daily_issues_cnt[data[i]['created_at'][0: 10]] += 1
    closed_at = data[i]['closed_at']
    if closed_at is not None:
        daily_issues_cnt[closed_at[0: 10]] -= 1

issues_cnt.append(daily_issues_cnt[start_date])
for i, key in enumerate(sorted(daily_issues_cnt.keys())):
    if i != 0:
        issues_cnt.append(issues_cnt[i-1] + daily_issues_cnt[key])
        if key == today:
            break

# plot burn down chart
expected_x = [0, len(dates) - 1]
expected_y = [daily_issues_cnt[start_date], 0]

actual_x = range(0, len(issues_cnt))
actual_y = issues_cnt

fig, ax = plt.subplots(figsize=(15, 10))
expected = plt.plot(expected_x, expected_y, color='green', label='expected')
actual = plt.plot(actual_x, actual_y, color='blue', label='actual')
plt.xticks(range(0, len(dates)), dates, rotation=45)
plt.legend()
plt.xlabel('Date')
plt.ylabel('Issues Count')
plt.savefig('burndown_chart.png')

生成的燃尽图:

posted @ 2021-04-22 23:57  Joeye153  阅读(691)  评论(0编辑  收藏  举报