如何使用 GitLab API 获取特定问题或任务的详细时间跟踪?

问题描述 投票:0回答:2

我在官方GitLab文档中只找到了这样的方法。

GET /projects/:id/issues/:issue_iid/time_stats
{
  "human_time_estimate": "2h",
  "human_total_time_spent": "1h",
  "time_estimate": 7200,
  "total_time_spent": 3600
}

但我需要一份详细的报告。正如这个截图所示。

我很抱歉模糊了。这是来自公司 GitLab 的屏幕截图。

enter image description here

gitlab-api time-tracking
2个回答
0
投票

搜索更多后,我在官方文档中找到了关于graphql查询的部分。

在一些奇迹和运气的帮助下,我成功地得到了这样的请求:

POST https://holivarius.ru/gitlab/api/graphql?access_token=`your-access-token`
Content-Type: application/json

{
  "operationName": "issueTimeTracking",
  "variables": {
    "iid": "`your-issue-iid`",
    "fullPath": "`your-project-full-path`"
  },
  "query": "query issueTimeTracking($fullPath: ID!, $iid: String) {\n  workspace: project(fullPath: $fullPath) {\n    id\n    issuable: issue(iid: $iid) {\n      ...IssueTimeTrackingFragment\n      humanTimeEstimate\n      timeEstimate\n      __typename\n    }\n    __typename\n  }\n}\n\nfragment IssueTimeTrackingFragment on Issue {\n  __typename\n  id\n  humanTotalTimeSpent\n  totalTimeSpent\n  timelogs {\n    nodes {\n      ...TimelogFragment\n      __typename\n    }\n    __typename\n  }\n}\n\nfragment TimelogFragment on Timelog {\n  __typename\n  id\n  timeSpent\n  user {\n    id\n    name\n    __typename\n  }\n  spentAt\n  note {\n    id\n    body\n    __typename\n  }\n  summary\n  userPermissions {\n    adminTimelog\n    __typename\n  }\n}\n"
}

其中需要更换以下东西:

  • your-access-token
    - 您的访问令牌。
  • your-issue-iid
    - 项目中问题的 ID。
  • your-project-full-path
    - 项目的完整路径。

PS:这个方法不太靠谱,但我找到的就这些了。

因此,如果有人知道更好的方法或可以解释其工作原理,请写下您的答案或对我的答案发表评论。


0
投票

如果有人想通过比这个答案更简单的查询来获得此结果,您可以使用这个(您可以在 gitlab 实例上使用 GraphiQL 进行尝试。

query {
  group(fullPath: "GROUP_NAME") {
    issues {
      nodes {
        title
        iid
        timelogs(first: 100000) {
          nodes {
            summary
            timeSpent
            spentAt
            user {
              username
            }
            
          }
        }
      }
    }
  }
}

答案如下:

{
  "data": {
    "group": {
      "issues": {
        "nodes": [
          {
            "title": "Issue 2",
            "iid": "2",
            "timelogs": {
              "nodes": [
                {
                  "summary": "",
                  "timeSpent": 72000,
                  "spentAt": "2024-05-23T11:18:58Z",
                  "user": {
                    "username": "adrienpacificopro"
                  }
                }
              ]
            }
          },
          {
            "title": "Issue 1",
            "iid": "1",
            "timelogs": {
              "nodes": [
                {
                  "summary": "A quelle heure va être mis le spent at",
                  "timeSpent": 1320,
                  "spentAt": "2024-05-04T22:00:00Z",
                  "user": {
                    "username": "adrienpacificopro"
                  }
                },
                {
                  "summary": "Now",
                  "timeSpent": 120,
                  "spentAt": "2024-05-23T11:16:26Z",
                  "user": {
                    "username": "adrienpacificopro"
                  }
                },
                {
                  "summary": "Ceci est un très long summary pour voir ce qu'il en est de la vie des poissons d'eau douce ! Mais on sait jamais ce qu'il peut se passer deans la vie, n'est-ce pas ?è!!!!!",
                  "timeSpent": 115200,
                  "spentAt": "2024-05-23T11:07:56Z",
                  "user": {
                    "username": "adrienpacificopro"
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

然后,您可以轻松地将其转换为数据框(或将其导出到 Excel),并按用户、问题、日期范围等对其进行过滤。

### Copy paste the output and assign it to data, or integrate it trough api calls
df = pd.json_normalize(data['issues']['nodes'], record_path=['timelogs', 'nodes'], meta=['title', 'iid'])

结果:

总结 花费时间 花费 用户.用户名 标题 iid
72000 2024-05-23T11:18:58Z 阿德里安太平洋专业人士 第2期 2
这是我度过的时光 1320 2024-05-04T22:00:00Z 阿德里安太平洋专业人士 第 1 期 1
现在 120 2024-05-23T11:16:26Z 阿德里安太平洋专业人士 第 1 期 1
Ceci 是 très 的长篇摘要,是您的幸福生活! Mais on sait jamais ce qu'il peut se passer deans la vie, n'est-ce pas ?è!!!!!! 115200 2024-05-23T11:07:56Z 阿德里安太平洋专业人士 第 1 期 1
© www.soinside.com 2019 - 2024. All rights reserved.