【问题标题】:How to get total time spend in Gitlab?如何获得在 Gitlab 中花费的总时间?
【发布时间】:2017-10-25 05:39:48
【问题描述】:

有没有办法获得用户在时间跟踪/spend斜线命令上花费的所有问题的总时间?

使用 API 的时间跟踪统计仅获取少量数据:https://docs.gitlab.com/ce/api/issues.html#get-time-tracking-stats

Gitlab CE 9.1.4

【问题讨论】:

  • GitLab 13.12(2021 年 5 月)中的新时间跟踪报告可能令人感兴趣:请参阅 my answer here

标签: gitlab gitlab-ce


【解决方案1】:

正如我所见,可以从 API v3 解析 cmets 并计算总数。

例如,

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

更新

目前我已经创建了一个工具来计算每个贡献者所花费的时间。我希望它会有所帮助:

https://github.com/zubroide/gitpab

【讨论】:

    【解决方案2】:

    这是一个非常简单的 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 上出现的第一个答案,而且实际上找不到任何其他现成的解决方案。

    【讨论】:

    【解决方案3】:

    基于@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_USERNAMEGITLAB_REPORTING_PROJECTIDGITLAB_REPORTING_TOKEN 设置环境变量。

    我们用它来支付承包商。希望这会有所帮助!

    【讨论】:

    • 谢谢,真的很有用。
    • 您知道获得小时 pr 用户 pr 问题的方法吗?因此,如果多个用户跟踪同一问题的时间,您可以提取 pr 用户吗?
    【解决方案4】:

    我自己也在寻找相同的东西,经过更多搜索,我发现了这个出色的 CLI 工具,名为 gitlab-time-tracker。它生成全面的跟踪时间reports,您可以通过多个选项自定义并可以打印它们even as PDFs

    为了使这个答案与 OP 的问题相关,您可以使用以下命令打印(在终端中)用户花费的总时间**:

    gtt report "namespace/project" --user username --closed --from="2017-03-01" --to="2017-04-01"
    

    ** 这假设您已安装此工具 (gtt) 并在其配置文件中设置了 Gitlab PAT(激活了“api”范围)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 2014-05-25
      • 2017-12-24
      • 2018-01-03
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多