From de8648991b6aacbde4a2af4a19bd33180bdc199f Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Thu, 29 Jun 2023 16:19:21 +0200 Subject: [PATCH] Added id based assignee flag for wp update --- cmd/list/work_packages.go | 6 ++-- cmd/update/update.go | 6 ++++ cmd/update/work_package.go | 16 ++++++---- components/common/slice.go | 4 +-- components/paths/paths.go | 10 +++++- components/resources/work_packages/update.go | 33 ++++++++++++++------ go.sum | 4 +++ 7 files changed, 58 insertions(+), 21 deletions(-) diff --git a/cmd/list/work_packages.go b/cmd/list/work_packages.go index c0975ef..5937ae9 100644 --- a/cmd/list/work_packages.go +++ b/cmd/list/work_packages.go @@ -72,9 +72,9 @@ func validatedVersionId(version string) string { if len(filteredVersions) != 1 { printer.Info(fmt.Sprintf( - "No unique available version from input '%s' found for projectId [#%d]. Please use one of the versions listed below.", - version, - project.Id, + "No unique available version from input %s found for projectId %s. Please use one of the versions listed below.", + printer.Cyan(version), + printer.Red(fmt.Sprintf("#%d", project.Id)), )) printer.Versions(versions) diff --git a/cmd/update/update.go b/cmd/update/update.go index f1d3aaf..d6e9fc1 100644 --- a/cmd/update/update.go +++ b/cmd/update/update.go @@ -25,6 +25,12 @@ func addWorkPackageFlags() { "", "Executes a custom action on a work package", ) + workPackageCmd.Flags().Uint64Var( + &assigneeFlag, + "assignee", + 0, + "Assign a user to the work package", + ) workPackageCmd.Flags().StringVar( &attachFlag, "attach", diff --git a/cmd/update/work_package.go b/cmd/update/work_package.go index 9f5e3d9..ad80ec7 100644 --- a/cmd/update/work_package.go +++ b/cmd/update/work_package.go @@ -11,10 +11,11 @@ import ( ) var ( - actionFlag string - attachFlag string - subjectFlag string - typeFlag string + actionFlag string + assigneeFlag uint64 + attachFlag string + subjectFlag string + typeFlag string ) var workPackageCmd = &cobra.Command{ @@ -48,10 +49,13 @@ func updateWorkPackage(_ *cobra.Command, args []string) { func updateOptions() map[work_packages.UpdateOption]string { var options = make(map[work_packages.UpdateOption]string) if len(actionFlag) > 0 { - options[work_packages.UpdateAction] = actionFlag + options[work_packages.UpdateCustomAction] = actionFlag + } + if assigneeFlag > 0 { + options[work_packages.UpdateAssignee] = strconv.FormatUint(assigneeFlag, 10) } if len(attachFlag) > 0 { - options[work_packages.UpdateAttach] = attachFlag + options[work_packages.UpdateAttachment] = attachFlag } if len(subjectFlag) > 0 { options[work_packages.UpdateSubject] = subjectFlag diff --git a/components/common/slice.go b/components/common/slice.go index db9716b..28f08fc 100644 --- a/components/common/slice.go +++ b/components/common/slice.go @@ -18,10 +18,10 @@ func Reduce[T, M any](slice []T, f func(M, T) M, initValue M) M { return acc } -func Filter[T any, M []T](slice M, f func(T) bool) M { +func Filter[T any](slice []T, f func(T) bool) []T { return Reduce( slice, - func(state M, value T) M { + func(state []T, value T) []T { if f(value) { return append(state, value) } else { diff --git a/components/paths/paths.go b/components/paths/paths.go index f6f09bd..fcee2cf 100644 --- a/components/paths/paths.go +++ b/components/paths/paths.go @@ -23,7 +23,15 @@ func ProjectWorkPackages(projectId uint64) string { } func Root() string { - return "api/v3" + return "/api/v3" +} + +func User(id uint64) string { + return Users() + fmt.Sprintf("/%d", id) +} + +func Users() string { + return Root() + "/users" } func WorkPackage(id uint64) string { diff --git a/components/resources/work_packages/update.go b/components/resources/work_packages/update.go index d435199..7cc751d 100644 --- a/components/resources/work_packages/update.go +++ b/components/resources/work_packages/update.go @@ -4,9 +4,11 @@ import ( "bytes" "encoding/json" "fmt" + "strconv" "github.com/opf/openproject-cli/components/common" "github.com/opf/openproject-cli/components/parser" + "github.com/opf/openproject-cli/components/paths" "github.com/opf/openproject-cli/components/printer" "github.com/opf/openproject-cli/components/requests" "github.com/opf/openproject-cli/dtos" @@ -16,17 +18,19 @@ import ( type UpdateOption int const ( - UpdateAction UpdateOption = iota - UpdateAttach + UpdateCustomAction UpdateOption = iota + UpdateAssignee + UpdateAttachment UpdateSubject UpdateType ) -var patchableUpdates = []UpdateOption{UpdateSubject, UpdateType} +var patchableUpdates = []UpdateOption{UpdateSubject, UpdateType, UpdateAssignee} var patchMap = map[UpdateOption]func(patch, workPackage *dtos.WorkPackageDto, input string) (string, error){ - UpdateType: typePatch, - UpdateSubject: subjectPatch, + UpdateAssignee: assigneePatch, + UpdateType: typePatch, + UpdateSubject: subjectPatch, } func Update(id uint64, options map[UpdateOption]string) (*models.WorkPackage, error) { @@ -35,7 +39,7 @@ func Update(id uint64, options map[UpdateOption]string) (*models.WorkPackage, er return nil, err } - if customAction, ok := options[UpdateAction]; ok { + if customAction, ok := options[UpdateCustomAction]; ok { err = action(workPackage, customAction) if err != nil { printer.Error(err) @@ -53,7 +57,7 @@ func Update(id uint64, options map[UpdateOption]string) (*models.WorkPackage, er printer.Error(err) } - if file, ok := options[UpdateAttach]; ok { + if file, ok := options[UpdateAttachment]; ok { err = upload(workPackage, file) if err != nil { printer.Error(err) @@ -138,10 +142,21 @@ func typePatch(patch, workPackage *dtos.WorkPackageDto, input string) (string, e } patch.Links.Type = foundType.Links.Self - return fmt.Sprintf("UpdateType -> %s", foundType.Name), nil + return fmt.Sprintf("Type -> %s", foundType.Name), nil } func subjectPatch(patch, _ *dtos.WorkPackageDto, input string) (string, error) { patch.Subject = input - return fmt.Sprintf("UpdateSubject -> %s", input), nil + return fmt.Sprintf("Subject -> %s", input), nil +} + +func assigneePatch(patch, _ *dtos.WorkPackageDto, input string) (string, error) { + userId, _ := strconv.ParseUint(input, 10, 64) + + if patch.Links == nil { + patch.Links = &dtos.WorkPackageLinksDto{} + } + + patch.Links.Assignee = &dtos.LinkDto{Href: paths.User(userId)} + return fmt.Sprintf("Assignee -> %s", input), nil } diff --git a/go.sum b/go.sum index f3366a9..4dbfa66 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,14 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=