Skip to content

Commit

Permalink
Added id based assignee flag for wp update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharonus committed Jun 29, 2023
1 parent 61b6bd0 commit de86489
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
6 changes: 3 additions & 3 deletions cmd/list/work_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 10 additions & 6 deletions cmd/update/work_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions components/common/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion components/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 24 additions & 9 deletions components/resources/work_packages/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit de86489

Please sign in to comment.