Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 13 additions & 10 deletions openproject/openproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions tmetric/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"fmt"
"net/url"
"strconv"
"strings"
"time"

"github.com/JankariTech/OpenProjectTmetricIntegration/config"
"github.com/go-resty/resty/v2"
)

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 {
Expand Down Expand Up @@ -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
Expand Down