GitLab 项目提交行数统计

想法

使用 http 请求 GitLab 公开的 API 拿到项目提交记录,观察得到的数据结构,解析 json ,对关键行数字段进行累加,得到结果

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
func main() {
var additions, deletions float64
for i := 0; i < 10; i++ {
client := &http.Client{}
msg := make([]map[string]interface{}, 0)

url := fmt.Sprintf("%v/api/v4/projects/%v/repository/commits?ref_name=%v&with_stats=true&per_page=%v&page=%v",
URL, PROJECT_ID, REF_NAME, PER_PAGE, i)

request, _ := http.NewRequest("GET", url, nil)
request.Header.Add("PRIVATE-TOKEN", PRIVATE_TOKEN)

resp, _ := client.Do(request)
body, _ := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()

_ = json.Unmarshal(body, &msg)

for j := 0; j < len(msg); j++ {
if msg[j]["author_name"] == AUTHOR_NAME {
additions += msg[j]["stats"].(map[string]interface{})["additions"].(float64)
deletions += msg[j]["stats"].(map[string]interface{})["deletions"].(float64)
}
}
}
fmt.Println("additions:", additions)
fmt.Println("deletions:", deletions)
fmt.Println("difference:", additions-deletions)
fmt.Println("total:", additions+deletions)
}