You'll have to use both the Gitlab Commits API and the GraphQL API to achieve it. Below is some code that's been shorted for brevity.
You'll need to specify a Gitlab instance flag and your personal token.
Say you have a function used to capture all users in your Gitlab instance called "GetUsers":
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
address = kingpin.Flag("address", "The Gitlab URL to use").Required().String()
token = kingpin.Flag("token", "Project token").String()
)
func main () {
timeTracker()
}
func timeTracker(git *gitlab.Client) {
names := GetUsers(git)
for _, name := range names {
jsonData := map[string]interface{}{
"query": `
query($user: String!) {
timelogs(username: $user ) {
edges {
node {
id
user {
id
username
}
timeSpent
issue{
labels{
nodes{
title
}
}
}
}
}
}
}
`, "variables": fmt.Sprintf(`{"user":"%s"}`, name),
}
jsonValue, _ := json.Marshal(jsonData)
request, err := http.NewRequest("POST", "https://" +*address + "/api/graphql", bytes.NewBuffer(jsonValue))
if err != nil {
logrus.Error(err)
}
request.Header.Set("Authorization", "Bearer "+*token)
request.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: time.Second * 10}
response, err := client.Do(request)
response.Body.Close()
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
}
logrus.Print(ioutil.ReadAll(response.Body))
Result (when decode with JSONDecoder):
INFO[0000] User: user1, Time spent: 300 (s)
INFO[0000] User: user2, Time spent: 120 (s)
You can then take this data and decode it into a struct (I would copy and paste the post request to an autogenerator for sanity's sake), then do what you want with it. Or change the POST request to capture users by Project if you're more interested in something granular.
Source: https://docs.gitlab.com/ee/api/graphql/getting_started.html