Skip to content

Commit e65909a

Browse files
allow filtering timeentries by workpackage ids (#23)
2 parents 8fb10fd + 531272b commit e65909a

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

cmd/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var diffCmd = &cobra.Command{
107107
}
108108
}
109109

110-
openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate)
110+
openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate, nil)
111111
if err != nil {
112112
_, _ = fmt.Fprint(os.Stderr, err)
113113
os.Exit(1)

cmd/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ var exportCmd = &cobra.Command{
8989
startTime, _ := time.Parse("2006-01-02", startDate)
9090
return startTime.Format("01/2006")
9191
},
92-
"AllTimeEntriesFromOpenProject": func(user string) []openproject.TimeEntry {
92+
"AllTimeEntriesFromOpenProject": func(user string, workpackages []any) []openproject.TimeEntry {
9393
var openProjectUser openproject.User
9494
openProjectUser, err := openproject.FindUserByName(config, user)
9595
if err != nil {
9696
_, _ = fmt.Fprint(os.Stderr, err)
9797
os.Exit(1)
9898
}
99-
openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate)
99+
openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate, workpackages)
100100
if err != nil {
101101
_, _ = fmt.Fprint(os.Stderr, err)
102102
os.Exit(1)

openproject/openproject.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ package openproject
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"net/url"
24+
"strconv"
25+
2326
"github.com/JankariTech/OpenProjectTmetricIntegration/config"
2427
"github.com/go-resty/resty/v2"
2528
"github.com/tidwall/gjson"
26-
"net/url"
27-
"strconv"
2829
)
2930

30-
func GetAllTimeEntries(config *config.Config, user User, startDate string, endDate string) ([]TimeEntry, error) {
31+
func GetAllTimeEntries(config *config.Config, user User, startDate string, endDate string, workpackages []any) ([]TimeEntry, error) {
3132
httpClient := resty.New()
3233
openProjectUrl, _ := url.JoinPath(config.OpenProjectUrl, "/api/v3/time_entries")
3334
var userString string
@@ -38,18 +39,20 @@ func GetAllTimeEntries(config *config.Config, user User, startDate string, endDa
3839
} else {
3940
userString = strconv.Itoa(user.Id)
4041
}
42+
43+
filters := "["
44+
if workpackages != nil && len(workpackages) > 0 {
45+
entityIds, _ := json.Marshal(workpackages)
46+
filters += fmt.Sprintf(`{"entity_id": {"operator":"=","values": %v}},`, string(entityIds))
47+
}
48+
// the operator is '<>d' ("\u003c\u003ed") and means between the dates
49+
filters += fmt.Sprintf(`{"user":{"operator":"=","values":["%v"]}},{"spent_on":{"operator":"\u003c\u003ed","values":["%v","%v"]}}]`, userString, startDate, endDate)
4150
resp, err := httpClient.R().
4251
SetBasicAuth("apikey", config.OpenProjectToken).
4352
SetHeader("Content-Type", "application/json").
4453
SetQueryParam("pageSize", "3000").
4554
SetQueryParam("sortBy", "[[\"updated_at\",\"asc\"]]").
46-
// the operator is '<>d' and means between the dates
47-
SetQueryParam("filters", fmt.Sprintf(
48-
`[{"user":{"operator":"=","values":["%v"]}},{"spent_on":{"operator":"\u003c\u003ed","values":["%v","%v"]}}]`,
49-
userString,
50-
startDate,
51-
endDate),
52-
).
55+
SetQueryParam("filters", filters).
5356
Get(openProjectUrl)
5457
if err != nil || resp.StatusCode() != 200 {
5558
return nil, fmt.Errorf(

tmetric/report.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import (
55
"fmt"
66
"net/url"
77
"strconv"
8+
"strings"
89
"time"
910

1011
"github.com/JankariTech/OpenProjectTmetricIntegration/config"
1112
"github.com/go-resty/resty/v2"
1213
)
1314

1415
type ReportItem struct {
15-
StartTime string `json:"startTime"`
16-
EndTime string `json:"endTime"`
17-
User string `json:"user"`
16+
StartTime string `json:"startTime"`
17+
EndTime string `json:"endTime"`
18+
User string `json:"user"`
19+
IssueId string `json:"issueId"`
20+
WorkpackageId string
1821
}
1922

2023
type Report struct {
@@ -118,6 +121,7 @@ func GetDetailedReport(
118121
}
119122
var report Report
120123
for _, item := range reportItems {
124+
item.WorkpackageId = strings.Trim(item.IssueId, "#") // remove leading '#' from issue id
121125
report.ReportItems = append(report.ReportItems, item)
122126
itemDuration, _ := item.getDuration()
123127
report.Duration += itemDuration

0 commit comments

Comments
 (0)