[英]How to get total time spend in Gitlab?
有沒有辦法獲得用戶使用時間跟蹤/spend
slash 命令花費在所有問題上的總時間?
使用 API 的時間跟蹤統計信息僅獲得少量數據: https://docs.gitlab.com/ce/api/issues.html#get-time
Gitlab CE 9.1.4
我認為可以從API v3解析注釋並計算總數。
例如,
https://gitlab.com/api/v3/projects/:id/issues/:issue_id/notes?private_token=your_token
{
id: 73113225,
body: "added 1h of time spent at 2018-05-15",
attachment: null,
author: {
...
username: "mnvxxx",
},
...
}
更多信息: https : //docs.gitlab.com/ee/api/notes.html
UPDATE
目前,我已經創建了用於計算每個貢獻者的花費時間的工具。 我希望它會有所幫助:
這是一個非常簡單的Python腳本,適用於API v4:
import requests
API_KEY = "" # Enter your API key here
BASE_URL = "https://{{enter gitlab url here}}/api/v4/"
item_counter = 0
total_seconds = 0
for i in range(1, 57): # manually set range of issues here. All issues doesn't work well.
issue = requests.get(BASE_URL + 'projects/2/issues/' + str(i) + '/time_stats')
total_seconds += issue.json()['total_time_spent']
item_counter += 1
print("Hours on all issues: %.2f" % float((total_seconds / 60) / 60))
print("Total issues: " + str(item_counter))
我發帖到這個主題,因為這是Google上出現的第一個答案,並沒有真正找到任何其他現成的解決方案。
在@ josh-harkema提供的基礎上,這個版本列出了分配給在給定時間段內更新的特定username
所有closed
問題(並且沒有設置標簽'付費'):
import requests
import os
username = os.environ.get('GITLAB_REPORTING_USERNAME')
project_id = os.environ.get('GITLAB_REPORTING_PROJECTID') # in the top of your project page
access_token = os.environ.get('GITLAB_REPORTING_TOKEN') # https://gitlab.com/profile/personal_access_tokens
base_url = "https://gitlab.com/api/v4"
updated_after = "2019-06-01T00:00:00.00Z"
updated_before = "2019-07-01T00:00:00.00Z"
item_counter = 0
total_seconds = 0
headers = { 'Private-Token': access_token }
url_template = "{base_url}/projects/{project_id}/issues?" \
"state=closed&assignee_username={username}&updated_after={updated_after}&updated_before={updated_before}"
url = url_template.format(base_url=base_url, project_id=project_id, username=username,
updated_after=updated_after, updated_before=updated_before)
# call API
issues = requests.get(url, headers = headers)
total_seconds = 0
issues_to_pay = []
line_template = "id: {id} closed: {closed_at} time spent: {time}\ttitle: {title}\turl: {url}"
print("Issue statistics for {u} from {f} to {t}:\n".format(u=username,f=updated_after, t=updated_before))
for issue in issues.json():
time_val = issue['time_stats']['human_total_time_spent']
already_paid = u'paid' in issue['labels'] # you can put a label 'paid' to exclude an issue
if already_paid:
time_val = time_val + " *"
else:
# if the issue has been paid, already, don't add the time, and don't list as to be paid
total_seconds += issue['time_stats']['total_time_spent']
issues_to_pay.append(str(issue['id']))
line = line_template.format(
id=issue['id'],
closed_at=issue['closed_at'],
title=issue['title'],
time=time_val,
url=issue['web_url']
)
print(line)
print("")
print("Hours to pay on all issues: %.2f" % float((float(total_seconds) / 60) / 60))
print("")
print("* = issue has been paid for, already")
print("All issue to pay: {issues}".format(issues=",".join(issues_to_pay)))
注意:您必須對設置環境變量GITLAB_REPORTING_USERNAME
的GITLAB_REPORTING_PROJECTID
,還有GITLAB_REPORTING_TOKEN
。
我們用它來支付承包商。 希望這可以幫助!
我自己也在尋找相同的東西,經過更多的搜索,我找到了這個名為gitlab-time-tracker 的優秀 CLI 工具。 它生成全面的跟蹤時間報告,您可以通過多個選項對其進行自定義, 甚至可以將它們打印為 PDF !
為了使這個答案與 OP 的問題相關,您可以使用以下命令打印(在您的終端中)用戶花費的總時間**:
gtt report "namespace/project" --user username --closed --from="2017-03-01" --to="2017-04-01"
** 這假設你已經安裝了這個工具 (gtt) 並在它的配置文件中設置了你的 Gitlab PAT(激活了“api”范圍)。
因為我喜歡Go的速度:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type AutoGeneratedStuff struct {
TimeEstimate int `json:"time_estimate"`
TotalTimeSpent int `json:"total_time_spent"`
HumanTimeEstimate interface{} `json:"human_time_estimate"`
HumanTotalTimeSpent interface{} `json:"human_total_time_spent"`
}
func main() {
//for performance, specify the range of issues (i)
for i := 1; i <= 5; i++ {
var url string = "https://{{Your_URL_here }}/api/v4/projects/{{ proj_id_here }}/issues/" + fmt.Sprint(i) + "/time_stats"
var timestuff AutoGeneratedStuff
var bearer = "Bearer " + "{{your_API_token_here}}"
// Create a new request using http
req, err := http.NewRequest("GET", url, nil)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error while reading the response bytes:", err)
}
Data := []byte(body)
err2 := json.Unmarshal(Data, ×tuff)
if err2 != nil {
fmt.Println(err)
}
fmt.Println("Time estimate is:", timestuff.TimeEstimate)
fmt.Println("Human time estimate is:", timestuff.HumanTimeEstimate)
fmt.Println("Total time is:", timestuff.TotalTimeSpent)
fmt.Println("Human total time is:", timestuff.HumanTotalTimeSpent)
}
}
示例響應:
Time estimate is: 86400
Human time estimate is: 3d
Total time is: 300
Human total time is: 5m
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.