gitlab-apitime-tracking

How can I use the GitLab API to get detailed time tracking for a specific issue or task?


I found only such a method in the official GitLab documentation.

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

But I need a detailed report. As in this screenshot.

I'm sorry for the blurring. This is a screenshot from the corporate GitLab.

enter image description here


Solution

  • If anyone wants to get this through a simpler query than this answer, you can use this one (you can try it with GraphiQL on your gitlab instance.

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

    The answer looks like this:

    {
      "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"
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
    

    Then, you can easily transform it to a dataframe (or export it to excel), and filter it by user, issue, date range, and so on.

    ### 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'])
    

    Result:

    summary timeSpent spentAt user.username title iid
    72000 2024-05-23T11:18:58Z adrienpacificopro Issue 2 2
    A quelle heure va être mis le spent at 1320 2024-05-04T22:00:00Z adrienpacificopro Issue 1 1
    Now 120 2024-05-23T11:16:26Z adrienpacificopro Issue 1 1
    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 ?è!!!!! 115200 2024-05-23T11:07:56Z adrienpacificopro Issue 1 1