Skip to content

Commit

Permalink
feat: search-project-with-client config
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Feb 13, 2024
1 parent 844eac3 commit b464f37
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- new config `search-project-with-client` to set whether or not the cli should lookup projects using the
client's name too

## [v0.47.0] - 2024-02-09

### Added
Expand Down
45 changes: 45 additions & 0 deletions internal/mocks/mock_Config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 21 additions & 14 deletions internal/mocks/simple_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import "github.com/lucassabreu/clockify-cli/pkg/cmdutil"
// SimpleConfig is used to set configs for tests were changing the config or
// accessing them with Get and All is not important
type SimpleConfig struct {
WorkweekDays []string
Interactive bool
InteractivePageSizeNumber int
AllowNameForID bool
UserID string
Workspace string
Token string
AllowIncomplete bool
ShowTask bool
DescriptionAutocomplete bool
DescriptionAutocompleteDays int
ShowTotalDuration bool
LogLevelValue string
AllowArchivedTags bool
WorkweekDays []string
Interactive bool
InteractivePageSizeNumber int
AllowNameForID bool
UserID string
Workspace string
Token string
AllowIncomplete bool
ShowTask bool
DescriptionAutocomplete bool
DescriptionAutocompleteDays int
ShowTotalDuration bool
LogLevelValue string
AllowArchivedTags bool
SearchProjectWithClientsName bool
}

// IsSearchProjectWithClientName defines if the project name for ID should
// include the client's name
func (s *SimpleConfig) IsSearchProjectWithClientsName() bool {
return s.SearchProjectWithClientsName
}

// InteractivePageSize sets how many items are shown when prompting
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/config/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ func NewCmdInit(f cmdutil.Factory) *cobra.Command {
return err
}

if config.IsAllowNameForID() {
if err := updateFlag(i, config,
cmdutil.CONF_SEARCH_PROJECTS_WITH_CLIENT_NAME,
`Should search projects looking into their `+
`client's name too?`,
); err != nil {
return err
}
}

if err := updateFlag(i, config, cmdutil.CONF_INTERACTIVE,
`Should use "Interactive Mode" by default?`,
); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion pkg/cmd/config/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func TestInitCmd(t *testing.T) {

setStringFn(config, cmdutil.CONF_USER_ID, "user-1")

setBoolFn(config, cmdutil.CONF_ALLOW_NAME_FOR_ID, false, true)
setBoolFn(config, cmdutil.CONF_ALLOW_NAME_FOR_ID, false, true).
Run(func(args mock.Arguments) {
config.EXPECT().IsAllowNameForID().Return(true)
})
setBoolFn(config, cmdutil.CONF_SEARCH_PROJECTS_WITH_CLIENT_NAME,
false, true)
setBoolFn(config, cmdutil.CONF_INTERACTIVE, false, false)

config.EXPECT().GetInt(cmdutil.CONF_INTERACTIVE_PAGE_SIZE).
Expand Down Expand Up @@ -130,6 +135,10 @@ func TestInitCmd(t *testing.T) {
c.SendLine("y")
c.ExpectString("Yes")

c.ExpectString("search projects looking into their client's name")
c.SendLine("y")
c.ExpectString("Yes")

c.ExpectString("Interactive Mode\" by default?")
c.SendLine("n")
c.ExpectString("No")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/project/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewCmdGet(

if f.Config().IsAllowNameForID() {
if p.ProjectID, err = search.GetProjectByName(
c, p.Workspace, p.ProjectID, ""); err != nil {
c, f.Config(), p.Workspace, p.ProjectID, ""); err != nil {
return err
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/project/get/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestCmdGet(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.EXPECT().Config().Return(cf)
cf.EXPECT().IsAllowNameForID().Return(true)
cf.EXPECT().IsSearchProjectWithClientsName().Return(true)

c := mocks.NewMockClient(t)
f.EXPECT().Client().Return(c, nil)
Expand Down Expand Up @@ -155,6 +156,7 @@ func TestCmdGet(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.EXPECT().Config().Return(cf)
cf.EXPECT().IsAllowNameForID().Return(true)
cf.EXPECT().IsSearchProjectWithClientsName().Return(true)

c := mocks.NewMockClient(t)
f.EXPECT().Client().Return(c, nil)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewCmdDelete(

if f.Config().IsAllowNameForID() {
if project, err = search.GetProjectByName(
c, w, project, ""); err != nil {
c, f.Config(), w, project, ""); err != nil {
return err
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/task/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestCmdDelete(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand All @@ -96,6 +97,7 @@ func TestCmdDelete(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down Expand Up @@ -124,6 +126,7 @@ func TestCmdDelete(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down Expand Up @@ -162,6 +165,7 @@ func TestCmdDelete(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/done/done.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewCmdDone(

if f.Config().IsAllowNameForID() {
if project, err = search.GetProjectByName(
c, workspace, project, ""); err != nil {
c, f.Config(), workspace, project, ""); err != nil {
return err
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/task/done/done_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func TestCmdDone(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c.On("GetProjects", api.GetProjectsParam{
Workspace: "w",
Expand Down Expand Up @@ -178,6 +179,7 @@ func TestCmdDone(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c.On("GetProjects", api.GetProjectsParam{
Workspace: "w",
Expand Down Expand Up @@ -237,6 +239,7 @@ func TestCmdDone(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(true)

c.On("GetProjects", api.GetProjectsParam{
Workspace: "w",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewCmdList(
if f.Config().IsAllowNameForID() &&
p.ProjectID != "" {
if p.ProjectID, err = search.GetProjectByName(
c, workspace, p.ProjectID, ""); err != nil {
c, f.Config(), workspace, p.ProjectID, ""); err != nil {
return err
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/task/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestCmdList(t *testing.T) {
f.On("Config").Return(cf)

cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(false)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down Expand Up @@ -111,6 +112,7 @@ func TestCmdList(t *testing.T) {
f.On("Config").Return(cf)

cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(false)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/task/quick-add/quick-add.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func NewCmdQuickAdd(

p, _ := cmd.Flags().GetString("project")
if f.Config().IsAllowNameForID() {
if p, err = search.GetProjectByName(c, w, p, ""); err != nil {
if p, err = search.GetProjectByName(
c, f.Config(), w, p, ""); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/util/read-flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TaskReadFlags(cmd *cobra.Command, f cmdutil.Factory) (p FlagsDTO, err error
}

if p.ProjectID, err = search.GetProjectByName(
c, p.Workspace, p.ProjectID, ""); err != nil {
c, f.Config(), p.Workspace, p.ProjectID, ""); err != nil {
return p, err
}

Expand Down
28 changes: 27 additions & 1 deletion pkg/cmd/time-entry/in/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,31 @@ func TestNewCmdIn_ShouldLookupProject_WithAndWithoutClient(t *testing.T) {
"No project with id or name containing 'notonclient' " +
"was found for client 'me'"),
},
{
name: "project with client name does not exist",
args: []string{"-s=08:00", "-p", "notonclient me"},
err: errors.New(
"No project with id or name containing 'notonclient me' " +
"was found"),
},
{
name: "project and client's name",
args: []string{"-s=08:00", "-p", "second me"},
param: api.CreateTimeEntryParam{
Workspace: w.ID,
Start: defaultStart,
ProjectID: projects[1].ID,
},
},
{
name: "project and client's name (other)",
args: []string{"-s=08:00", "-p=second clockify"},
param: api.CreateTimeEntryParam{
Workspace: w.ID,
Start: defaultStart,
ProjectID: projects[2].ID,
},
},
}

for i := range tts {
Expand All @@ -235,7 +260,8 @@ func TestNewCmdIn_ShouldLookupProject_WithAndWithoutClient(t *testing.T) {
f.EXPECT().GetWorkspaceID().Return(w.ID, nil)

f.EXPECT().Config().Return(&mocks.SimpleConfig{
AllowNameForID: true,
AllowNameForID: true,
SearchProjectWithClientsName: true,
})

c := mocks.NewMockClient(t)
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/time-entry/report/util/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ func ReportWithRange(
return err
}

cnf := f.Config()
if rf.Project != "" && f.Config().IsAllowNameForID() {
if rf.Project, err = search.GetProjectByName(
c, workspace, rf.Project, rf.Client); err != nil {
c, cnf, workspace, rf.Project, rf.Client); err != nil {
return err
}
}
Expand Down Expand Up @@ -171,7 +172,7 @@ func ReportWithRange(
}

return util.PrintTimeEntries(
log, out, f.Config(), rf.OutputFlags)
log, out, cnf, rf.OutputFlags)
}

func filterBilling(l []dto.TimeEntry, billable bool) []dto.TimeEntry {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/time-entry/report/util/reportwithrange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestReportWithRange(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(false)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down Expand Up @@ -174,6 +175,7 @@ func TestReportWithRange(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(false)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down Expand Up @@ -210,6 +212,7 @@ func TestReportWithRange(t *testing.T) {
cf := mocks.NewMockConfig(t)
f.On("Config").Return(cf)
cf.On("IsAllowNameForID").Return(true)
cf.On("IsSearchProjectWithClientsName").Return(false)

c := mocks.NewMockClient(t)
f.On("Client").Return(c, nil)
Expand Down
Loading

0 comments on commit b464f37

Please sign in to comment.