Skip to content

Commit

Permalink
fix(toggl): add missing user filtering for Toggl integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gabor-boros committed Oct 20, 2021
1 parent 08dff13 commit 649f873
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions internal/pkg/client/toggl/toggl.go
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"regexp"
"strconv"
"strings"
"time"

"github.com/gabor-boros/minutes/internal/pkg/client"
Expand Down Expand Up @@ -49,6 +50,7 @@ type WorklogSearchParams struct {
Since string
Until string
Page int
UserID int
WorkspaceID int
}

Expand All @@ -72,6 +74,7 @@ func (c *togglClient) getSearchURL(params *WorklogSearchParams) (string, error)
queryParams.Add("since", params.Since)
queryParams.Add("until", params.Until)
queryParams.Add("page", strconv.Itoa(params.Page))
queryParams.Add("user_id", strconv.Itoa(params.UserID))
queryParams.Add("workspace_id", strconv.Itoa(params.WorkspaceID))
queryParams.Add("user_agent", "github.com/gabor-boros/minutes")
worklogURL.RawQuery = queryParams.Encode()
Expand Down Expand Up @@ -171,11 +174,17 @@ func (c *togglClient) FetchEntries(ctx context.Context, opts *client.FetchOpts)
currentPage := 1
paginationNeeded := true

userID, err := strconv.Atoi(strings.Split(opts.User, ",")[0])
if err != nil {
return nil, fmt.Errorf("%v: %v", client.ErrFetchEntries, err)
}

for paginationNeeded {
searchParams := &WorklogSearchParams{
Since: opts.Start.Format(DateFormat),
Until: opts.End.Format(DateFormat),
Page: currentPage,
UserID: userID,
WorkspaceID: c.opts.Workspace,
}

Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/client/toggl/toggl_test.go
Expand Up @@ -107,6 +107,7 @@ func TestTogglClient_FetchEntries(t *testing.T) {
"page": {"1"},
"since": {start.Format(toggl.DateFormat)},
"until": {end.Format(toggl.DateFormat)},
"user_id": {"987654321"},
"workspace_id": {"123456789"},
"user_agent": {"github.com/gabor-boros/minutes"},
},
Expand Down Expand Up @@ -162,7 +163,7 @@ func TestTogglClient_FetchEntries(t *testing.T) {
})

entries, err := togglClient.FetchEntries(context.Background(), &client.FetchOpts{
User: "steve-rogers",
User: "987654321",
Start: start,
End: end,
})
Expand Down Expand Up @@ -244,6 +245,7 @@ func TestTogglClient_FetchEntries_TagsAsTasks(t *testing.T) {
"page": {"1"},
"since": {start.Format(toggl.DateFormat)},
"until": {end.Format(toggl.DateFormat)},
"user_id": {"987654321"},
"workspace_id": {"123456789"},
"user_agent": {"github.com/gabor-boros/minutes"},
},
Expand Down Expand Up @@ -303,7 +305,7 @@ func TestTogglClient_FetchEntries_TagsAsTasks(t *testing.T) {
})

entries, err := togglClient.FetchEntries(context.Background(), &client.FetchOpts{
User: "steve-rogers",
User: "987654321",
Start: start,
End: end,
})
Expand Down
41 changes: 41 additions & 0 deletions www/docs/sources/toggl.md
@@ -1,5 +1,15 @@
Source documentation for [Toggl Track](https://track.toggl.com/).

!!! warning

To get the available User IDs, please follow [this instruction](https://github.com/toggl/toggl_api_docs/blob/master/chapters/workspaces.md#get-workspace-users).
Only **workspace admins** can get the User IDs.

!!! info

Toggl Track's detailed report API does support filtering, and `minutes` unexplicitly supports filtering by setting,
the source-user to the desired user ID, however it is not officially supported yet.

## Field mappings

The source makes the following special mappings.
Expand Down Expand Up @@ -36,3 +46,34 @@ The source provides the following extra configuration options.

[^1]: The API key can be generated as described in their [documentation](https://support.toggl.com/en/articles/3116844-where-is-my-api-key-located).
[^2]: The URL defaults to `https://api.track.toggl.com` and Toggl Track cannot be installed privately, though they are changing domains nowadays, so if Toggl track changes domain again or start offering private hosting, it can be set easily.

## Example configuration

```toml
# Source config
source = "toggl"

# To retrieve your user ID, please follow the instructions listed here:
# https://github.com/toggl/toggl_api_docs/blob/master/chapters/workspaces.md#get-workspace-users
source-user = "<YOUR TOGGL USER ID>"

# Toggl config
toggl-api-key = "<YOUR API KEY>"
toggl-url = "https://api.track.toggl.com"
toggl-workspace = "<YOUR WORKSPACE ID>"

# Target config
target = "tempo"
target-user = "<jira username>"

# Tempo config
tempo-url = "https://tasks.opencraft.com"
tempo-username = "<jira username>"
tempo-password = "<jira password>"

# General config
tags-as-tasks = true
tags-as-tasks-regex = '[A-Z]{2,7}-\d{1,6}'
round-to-closest-minute = true
force-billed-duration = true
```

0 comments on commit 649f873

Please sign in to comment.