Skip to content

Commit

Permalink
version and not-version filters
Browse files Browse the repository at this point in the history
- version filter moved to new format
- not-version filter added
  • Loading branch information
Kharonus committed Aug 6, 2024
1 parent b796b68 commit bbef582
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 28 deletions.
15 changes: 7 additions & 8 deletions cmd/list/work_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import (

var assignee string
var projectId uint64
var version string
var showTotal bool
var statusFilter string
var typeFilter string
var includeSubProjects bool

var activeFilters = map[string]resources.Filter{
"timestamp": filters.NewTimestampFilter(),
"subProject": filters.NewSubProjectFilter(),
"notSubProject": filters.NewNotSubProjectFilter(),
"notVersion": filters.NewNotVersionFilter(),
"subProject": filters.NewSubProjectFilter(),
"timestamp": filters.NewTimestampFilter(),
"version": filters.NewVersionFilter(),
}

var workPackagesCmd = &cobra.Command{
Expand Down Expand Up @@ -65,8 +66,10 @@ func listWorkPackages(_ *cobra.Command, _ []string) {

func validateCommandFlagComposition() (errorText string) {
switch {
case len(version) != 0 && projectId == 0:
case len(activeFilters["version"].Value()) != 0 && projectId == 0:
return "Version flag (--version) can only be used in conjunction with projectId flag (-p or --project-id)."
case len(activeFilters["notVersion"].Value()) != 0 && projectId == 0:
return "Not version filter flag (--not-version) can only be used in conjunction with projectId flag (-p or --project-id)."
case len(activeFilters["subProject"].Value()) > 0 || len(activeFilters["notSubProject"].Value()) > 0:
if !includeSubProjects || projectId == 0 {
return `Sub project filter flags (--sub-project or --not-sub-project) can only be used
Expand Down Expand Up @@ -118,10 +121,6 @@ func filterOptions() *map[work_packages.FilterOption]string {
options[work_packages.Type] = validateFilterValue(work_packages.Type, typeFilter)
}

if len(version) > 0 {
options[work_packages.Version] = validatedVersionId(version)
}

return &options
}

Expand Down
7 changes: 0 additions & 7 deletions cmd/list/work_packages_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ func initWorkPackagesFlags() {
0,
"Show only work packages within the specified projectId")

workPackagesCmd.Flags().StringVarP(
&version,
"version",
"v",
"",
"Show only work packages having the specified version")

workPackagesCmd.Flags().StringVarP(
&statusFilter,
"status",
Expand Down
11 changes: 0 additions & 11 deletions components/resources/work_packages/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type FilterOption int

const (
Assignee FilterOption = iota
Version
Project
Status
Type
Expand All @@ -26,8 +25,6 @@ func (f FilterOption) String() string {
switch f {
case Assignee:
return "assignee"
case Version:
return "version"
case Project:
return "project"
case Status:
Expand All @@ -49,14 +46,6 @@ func AssigneeFilter(name string) requests.Filter {
}
}

func VersionFilter(version string) requests.Filter {
return requests.Filter{
Operator: "=",
Name: "version",
Values: []string{version},
}
}

func TypeFilter(workPackageType string) requests.Filter {
var operator string
var values []string
Expand Down
63 changes: 63 additions & 0 deletions components/resources/work_packages/filters/not_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package filters

import (
"fmt"
"regexp"
"strings"

"github.com/opf/openproject-cli/components/errors"
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/components/requests"
)

type NotVersionFilter struct {
value string
}

func (f *NotVersionFilter) ValuePointer() *string {
return &f.value
}

func (f *NotVersionFilter) Value() string {
return f.value
}

func (f *NotVersionFilter) Name() string {
return "not-version"
}

func (f *NotVersionFilter) ShortHand() string {
return ""
}

func (f *NotVersionFilter) Usage() string {
return `Show only work packages that are not assigned to the given version. The value
can be a single ID or a comma separated array of IDs, i.e. '7,13'.`
}

func (f *NotVersionFilter) ValidateInput() error {
matched, _ := regexp.Match("^([0-9,]+)$", []byte(f.value))
if !matched {
return errors.Custom(fmt.Sprintf("Invalid version value %s.", printer.Yellow(f.value)))
}

return nil
}

func (f *NotVersionFilter) DefaultValue() string {
return ""
}

func (f *NotVersionFilter) Query() requests.Query {
return requests.NewQuery(nil, []requests.Filter{
{
Operator: "!",
Name: "version",
Values: strings.Split(f.value, ","),
},
})
}

func NewNotVersionFilter() *NotVersionFilter {
return &NotVersionFilter{}
}
63 changes: 63 additions & 0 deletions components/resources/work_packages/filters/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package filters

import (
"fmt"
"regexp"
"strings"

"github.com/opf/openproject-cli/components/errors"
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/components/requests"
)

type VersionFilter struct {
value string
}

func (f *VersionFilter) ValuePointer() *string {
return &f.value
}

func (f *VersionFilter) Value() string {
return f.value
}

func (f *VersionFilter) Name() string {
return "version"
}

func (f *VersionFilter) ShortHand() string {
return "v"
}

func (f *VersionFilter) Usage() string {
return `Show only work packages that are assigned to the given version. The value can be
a single ID or a comma separated array of IDs, i.e. '7,13'.`
}

func (f *VersionFilter) ValidateInput() error {
matched, _ := regexp.Match("^([0-9,]+)$", []byte(f.value))
if !matched {
return errors.Custom(fmt.Sprintf("Invalid version value %s.", printer.Yellow(f.value)))
}

return nil
}

func (f *VersionFilter) DefaultValue() string {
return ""
}

func (f *VersionFilter) Query() requests.Query {
return requests.NewQuery(nil, []requests.Filter{
{
Operator: "=",
Name: "version",
Values: strings.Split(f.value, ","),
},
})
}

func NewVersionFilter() *VersionFilter {
return &VersionFilter{}
}
2 changes: 0 additions & 2 deletions components/resources/work_packages/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func All(filterOptions *map[FilterOption]string, query requests.Query, showOnlyT
queryAttributes["includeSubprojects"] = value
case Assignee:
filters = append(filters, AssigneeFilter(value))
case Version:
filters = append(filters, VersionFilter(value))
case Status:
filters = append(filters, StatusFilter(value))
case Type:
Expand Down

0 comments on commit bbef582

Please sign in to comment.