Skip to content

Commit d85996c

Browse files
committed
plausability checks for the 'check' command
1 parent 0da6716 commit d85996c

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

cmd/tmetric.go

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ package cmd
2020
import (
2121
"errors"
2222
"fmt"
23+
"os"
24+
"strconv"
25+
"strings"
26+
"time"
27+
2328
"github.com/JankariTech/OpenProjectTmetricIntegration/config"
2429
"github.com/JankariTech/OpenProjectTmetricIntegration/openproject"
2530
"github.com/JankariTech/OpenProjectTmetricIntegration/tmetric"
2631
"github.com/manifoldco/promptui"
2732
"github.com/spf13/cobra"
28-
"os"
29-
"strconv"
30-
"strings"
31-
"time"
3233
)
3334

3435
func validateOpenProjectWorkPackage(input string) error {
@@ -51,7 +52,7 @@ func handleEntriesWithoutIssue(timeEntries []tmetric.TimeEntry, tmetricUser tmet
5152
defer spinner.Stop()
5253

5354
for _, entry := range entriesWithoutLinkToOpenProject {
54-
prompt := promptui.Prompt{
55+
getWPPrompt := promptui.Prompt{
5556
Label: fmt.Sprintf(
5657
"%v => %v %v-%v. Provide a WP number to be assigned to this time-entry (Enter to skip)",
5758
entry.Project.Name, entry.Note, entry.StartTime, entry.EndTime,
@@ -62,7 +63,7 @@ func handleEntriesWithoutIssue(timeEntries []tmetric.TimeEntry, tmetricUser tmet
6263
workpackageFoundOnOpenProject := false
6364
for !workpackageFoundOnOpenProject {
6465

65-
workPackageId, err := prompt.Run()
66+
workPackageId, err := getWPPrompt.Run()
6667

6768
if err != nil {
6869
return fmt.Errorf("prompt failed: %v", err)
@@ -82,7 +83,37 @@ func handleEntriesWithoutIssue(timeEntries []tmetric.TimeEntry, tmetricUser tmet
8283
continue
8384
}
8485

85-
prompt = promptui.Prompt{
86+
if !implausibleProjectConfirmation(
87+
workPackage,
88+
workPackage.Embedded.Project.Active,
89+
"This project is NOT active! ",
90+
"Do you want to use a WP from an INACTIVE project?",
91+
) {
92+
workpackageFoundOnOpenProject = false
93+
continue
94+
}
95+
96+
if !implausibleProjectConfirmation(
97+
workPackage,
98+
workPackage.Embedded.Project.Favorited,
99+
"This project is none of your favorite projects!",
100+
"Do you really want to use a WP from a not-favorite project?",
101+
) {
102+
workpackageFoundOnOpenProject = false
103+
continue
104+
}
105+
106+
if !implausibleProjectConfirmation(
107+
workPackage,
108+
workPackage.Embedded.Assignee.Name == tmetricUser.Name || workPackage.Embedded.Assignee.Name == config.OpenProjectTeam,
109+
fmt.Sprintf("This WP is not assigned to you but to '%s'!", workPackage.Embedded.Assignee.Name),
110+
"Do you really want to use a WP that is not assigned to you?",
111+
) {
112+
workpackageFoundOnOpenProject = false
113+
continue
114+
}
115+
116+
prompt := promptui.Prompt{
86117
Label: fmt.Sprintf(
87118
"WP: %v. Subject: %v. Update t-metric entry?", workPackage.Id, workPackage.Subject,
88119
),
@@ -112,6 +143,23 @@ func handleEntriesWithoutIssue(timeEntries []tmetric.TimeEntry, tmetricUser tmet
112143
return nil
113144
}
114145

146+
func implausibleProjectConfirmation(
147+
workPackage openproject.WorkPackage, condition bool, issue string, question string,
148+
) bool {
149+
if condition {
150+
return true
151+
}
152+
prompt := promptui.Prompt{
153+
Label: fmt.Sprintf(
154+
"⚠️ Found WP '%s' in the project '%s'. %s %s",
155+
workPackage.Subject, workPackage.Embedded.Project.Name, issue, question,
156+
),
157+
IsConfirm: true,
158+
}
159+
result, err := prompt.Run()
160+
return err == nil && result == "y"
161+
}
162+
115163
func handleEntriesWithoutWorkType(timeEntries []tmetric.TimeEntry, tmetricUser tmetric.User, config *config.Config) error {
116164
entriesWithoutWorkType := tmetric.GetEntriesWithoutWorkType(timeEntries)
117165
if len(entriesWithoutWorkType) > 0 {

config/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ package config
1919

2020
import (
2121
"fmt"
22-
"github.com/spf13/viper"
2322
"os"
23+
24+
"github.com/spf13/viper"
2425
)
2526

2627
type Config struct {
2728
OpenProjectUrl string
2829
OpenProjectToken string
30+
OpenProjectTeam string
2931
TmetricToken string
3032
ClientIdInTmetric int
3133
TmetricAPIBaseUrl string
@@ -46,6 +48,7 @@ func NewConfig() *Config {
4648
fmt.Fprintln(os.Stderr, "openproject.token not set")
4749
os.Exit(1)
4850
}
51+
openProjectTeam := viper.GetString("openproject.team")
4952
tmetricToken := viper.GetString("tmetric.token")
5053
if tmetricToken == "" {
5154
fmt.Fprintln(os.Stderr, "tmetric.token not set")
@@ -64,6 +67,7 @@ func NewConfig() *Config {
6467
return &Config{
6568
OpenProjectUrl: openProjectUrl,
6669
OpenProjectToken: openProjectToken,
70+
OpenProjectTeam: openProjectTeam,
6771
TmetricToken: tmetricToken,
6872
ClientIdInTmetric: clientIdInTmetric,
6973
TmetricAPIBaseUrl: "https://app.tmetric.com/api/",
@@ -72,6 +76,6 @@ func NewConfig() *Config {
7276
TmetricTagTransferredToOpenProject: "transferred-to-openproject",
7377
// this value has always to be "https://community.openproject.org"
7478
// otherwise tmetric does not recognize the integration and does not allow to create the external task
75-
TmetricExternalTaskLink: "https://community.openproject.org/",
79+
TmetricExternalTaskLink: "https://community.openproject.org/",
7680
}
7781
}

openproject/workpackage.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,27 @@ package openproject
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"net/url"
24+
2325
"github.com/JankariTech/OpenProjectTmetricIntegration/config"
2426
"github.com/go-resty/resty/v2"
2527
"github.com/tidwall/gjson"
26-
"net/url"
2728
)
2829

2930
type WorkPackage struct {
30-
Subject string `json:"subject"`
31-
Id int `json:"id"`
31+
Subject string `json:"subject"`
32+
Id int `json:"id"`
33+
Embedded struct {
34+
Project struct {
35+
Id int `json:"id"`
36+
Name string `json:"name"`
37+
Active bool `json:"active"`
38+
Favorited bool `json:"favorited"`
39+
} `json:"project"`
40+
Assignee struct {
41+
Name string `json:"name"`
42+
} `json:"assignee"`
43+
} `json:"_embedded"`
3244
}
3345

3446
func NewWorkPackage(id int, subject string) WorkPackage {

0 commit comments

Comments
 (0)