diff --git a/cmd/diff.go b/cmd/diff.go index 825936b..e1b607c 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -107,7 +107,7 @@ var diffCmd = &cobra.Command{ } } - openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate) + openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate, nil) if err != nil { _, _ = fmt.Fprint(os.Stderr, err) os.Exit(1) diff --git a/cmd/export.go b/cmd/export.go index 97eb97e..7e7e296 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -89,14 +89,14 @@ var exportCmd = &cobra.Command{ startTime, _ := time.Parse("2006-01-02", startDate) return startTime.Format("01/2006") }, - "AllTimeEntriesFromOpenProject": func(user string) []openproject.TimeEntry { + "AllTimeEntriesFromOpenProject": func(user string, workpackages []any) []openproject.TimeEntry { var openProjectUser openproject.User openProjectUser, err := openproject.FindUserByName(config, user) if err != nil { _, _ = fmt.Fprint(os.Stderr, err) os.Exit(1) } - openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate) + openProjectTimeEntries, err := openproject.GetAllTimeEntries(config, openProjectUser, startDate, endDate, workpackages) if err != nil { _, _ = fmt.Fprint(os.Stderr, err) os.Exit(1) diff --git a/openproject/openproject.go b/openproject/openproject.go index 3aba72a..27641a9 100644 --- a/openproject/openproject.go +++ b/openproject/openproject.go @@ -20,14 +20,15 @@ package openproject import ( "encoding/json" "fmt" + "net/url" + "strconv" + "github.com/JankariTech/OpenProjectTmetricIntegration/config" "github.com/go-resty/resty/v2" "github.com/tidwall/gjson" - "net/url" - "strconv" ) -func GetAllTimeEntries(config *config.Config, user User, startDate string, endDate string) ([]TimeEntry, error) { +func GetAllTimeEntries(config *config.Config, user User, startDate string, endDate string, workpackages []any) ([]TimeEntry, error) { httpClient := resty.New() openProjectUrl, _ := url.JoinPath(config.OpenProjectUrl, "/api/v3/time_entries") var userString string @@ -38,18 +39,20 @@ func GetAllTimeEntries(config *config.Config, user User, startDate string, endDa } else { userString = strconv.Itoa(user.Id) } + + filters := "[" + if workpackages != nil && len(workpackages) > 0 { + entityIds, _ := json.Marshal(workpackages) + filters += fmt.Sprintf(`{"entity_id": {"operator":"=","values": %v}},`, string(entityIds)) + } + // the operator is '<>d' ("\u003c\u003ed") and means between the dates + filters += fmt.Sprintf(`{"user":{"operator":"=","values":["%v"]}},{"spent_on":{"operator":"\u003c\u003ed","values":["%v","%v"]}}]`, userString, startDate, endDate) resp, err := httpClient.R(). SetBasicAuth("apikey", config.OpenProjectToken). SetHeader("Content-Type", "application/json"). SetQueryParam("pageSize", "3000"). SetQueryParam("sortBy", "[[\"updated_at\",\"asc\"]]"). - // the operator is '<>d' and means between the dates - SetQueryParam("filters", fmt.Sprintf( - `[{"user":{"operator":"=","values":["%v"]}},{"spent_on":{"operator":"\u003c\u003ed","values":["%v","%v"]}}]`, - userString, - startDate, - endDate), - ). + SetQueryParam("filters", filters). Get(openProjectUrl) if err != nil || resp.StatusCode() != 200 { return nil, fmt.Errorf( diff --git a/tmetric/report.go b/tmetric/report.go index 00d9403..cf76fd5 100644 --- a/tmetric/report.go +++ b/tmetric/report.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "strconv" + "strings" "time" "github.com/JankariTech/OpenProjectTmetricIntegration/config" @@ -12,9 +13,11 @@ import ( ) type ReportItem struct { - StartTime string `json:"startTime"` - EndTime string `json:"endTime"` - User string `json:"user"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + User string `json:"user"` + IssueId string `json:"issueId"` + WorkpackageId string } type Report struct { @@ -118,6 +121,7 @@ func GetDetailedReport( } var report Report for _, item := range reportItems { + item.WorkpackageId = strings.Trim(item.IssueId, "#") // remove leading '#' from issue id report.ReportItems = append(report.ReportItems, item) itemDuration, _ := item.getDuration() report.Duration += itemDuration