Skip to content

Commit

Permalink
Added list status command to get a list of all status
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharonus committed May 23, 2024
1 parent a049919 commit 34fcf37
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {
"Show only work packages having the specified version")

workPackagesCmd.Flags().StringVarP(
&status,
&statusFilter,
"status",
"s",
"",
Expand All @@ -63,5 +63,6 @@ status.`)
notificationsCmd,
workPackagesCmd,
activitiesCmd,
statusCmd,
)
}
22 changes: 22 additions & 0 deletions cmd/list/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package list

import (
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/components/resources/status"
"github.com/spf13/cobra"
)

var statusCmd = &cobra.Command{
Use: "status",
Short: "Lists status",
Long: "Get a list of all status of the instance.",
Run: listStatus,
}

func listStatus(_ *cobra.Command, _ []string) {
if all, err := status.All(); err == nil {
printer.StatusList(all)
} else {
printer.Error(err)
}
}
6 changes: 3 additions & 3 deletions cmd/list/work_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var assignee string
var projectId uint64
var version string
var showTotal bool
var status string
var statusFilter string

var workPackagesCmd = &cobra.Command{
Use: "workpackages",
Expand Down Expand Up @@ -56,8 +56,8 @@ func filterOptions() *map[work_packages.FilterOption]string {
options[work_packages.Assignee] = assignee
}

if len(status) > 0 {
options[work_packages.Status] = validateStatusFilterValue(status)
if len(statusFilter) > 0 {
options[work_packages.Status] = validateStatusFilterValue(statusFilter)
}

if len(version) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions components/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func Root() string {
return "/api/v3"
}

func Status() string {
return Root() + "/statuses"
}

func User(id uint64) string {
return Users() + fmt.Sprintf("/%d", id)
}
Expand Down
26 changes: 26 additions & 0 deletions components/printer/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package printer

import (
"fmt"
"github.com/opf/openproject-cli/models"
)

func StatusList(statuss []*models.Status) {
for _, p := range statuss {
printStatus(p)
}
}

func Status(status *models.Status) {
printStatus(status)
}

func printStatus(status *models.Status) {
var defaultSuffix string

id := fmt.Sprintf("#%d", status.Id)
if status.IsDefault {
defaultSuffix = fmt.Sprintf(" (%s)", Yellow("default"))
}
activePrinter.Printf("%s %s%s\n", Red(id), Cyan(status.Name), defaultSuffix)
}
74 changes: 74 additions & 0 deletions components/printer/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package printer_test

import (
"fmt"
"strconv"
"testing"

"github.com/opf/openproject-cli/components/common"
"github.com/opf/openproject-cli/components/printer"
"github.com/opf/openproject-cli/models"
)

func TestStatus_Default(t *testing.T) {
testingPrinter.Reset()

status := models.Status{
Id: 42,
Name: "My Default",
IsDefault: true,
}

expected := fmt.Sprintf("%s %s (%s)\n", printer.Red("#42"), printer.Cyan("My Default"), printer.Yellow("default"))

printer.Status(&status)

if testingPrinter.Result != expected {
t.Errorf("Expected %s, but got %s", expected, testingPrinter.Result)
}
}

func TestStatus_NoDefault(t *testing.T) {
testingPrinter.Reset()

status := models.Status{
Id: 42,
Name: "Another Status",
IsDefault: false,
}

expected := fmt.Sprintf("%s %s\n", printer.Red("#42"), printer.Cyan("Another Status"))

printer.Status(&status)

if testingPrinter.Result != expected {
t.Errorf("Expected %s, but got %s", expected, testingPrinter.Result)
}
}

func TestStatusList(t *testing.T) {
testingPrinter.Reset()

status := []*models.Status{
{Id: 42, Name: "First"},
{Id: 45, Name: "Second"},
{Id: 123, Name: "Third", IsDefault: true},
}

expected := common.Reduce[*models.Status, string](
status[:len(status)-1],
func(result string, status *models.Status) string {
idString := "#" + strconv.FormatUint(status.Id, 10)

return result + fmt.Sprintf("%s %s\n", printer.Red(idString), printer.Cyan(status.Name))
},
"")

expected += fmt.Sprintf("%s %s (%s)\n", printer.Red("#123"), printer.Cyan("Third"), printer.Yellow("default"))

printer.StatusList(status)

if testingPrinter.Result != expected {
t.Errorf("Expected %s, but got %s", expected, testingPrinter.Result)
}
}
6 changes: 3 additions & 3 deletions components/resources/projects/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

func All() ([]*models.Project, error) {
response, err := requests.Get(paths.Projects(), nil)
query := requests.NewPagedQuery(-1, nil)
response, err := requests.Get(paths.Projects(), &query)
if err != nil {
return nil, err
}
Expand All @@ -19,8 +20,7 @@ func All() ([]*models.Project, error) {
}

func Lookup(id uint64) (*models.Project, error) {
query := requests.NewPagedQuery(-1, nil)
response, err := requests.Get(paths.Project(id), &query)
response, err := requests.Get(paths.Project(id), nil)
if err != nil {
return nil, err
}
Expand Down
20 changes: 20 additions & 0 deletions components/resources/status/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package status

import (
"github.com/opf/openproject-cli/components/parser"
"github.com/opf/openproject-cli/components/paths"
"github.com/opf/openproject-cli/components/requests"
"github.com/opf/openproject-cli/dtos"
"github.com/opf/openproject-cli/models"
)

func All() ([]*models.Status, error) {
query := requests.NewPagedQuery(-1, nil)
response, err := requests.Get(paths.Status(), &query)
if err != nil {
return nil, err
}

element := parser.Parse[dtos.StatusCollectionDto](response)
return element.Convert(), nil
}
46 changes: 46 additions & 0 deletions dtos/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dtos

import "github.com/opf/openproject-cli/models"

type statusDto struct {
Id uint64 `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
IsDefault bool `json:"isDefault"`
IsClosed bool `json:"isClosed"`
IsReadonly bool `json:"isReadonly"`
Position uint64 `json:"position"`
}

type statusElements struct {
Elements []*statusDto `json:"elements"`
}

type StatusCollectionDto struct {
Embedded *statusElements `json:"_embedded"`
Type string `json:"_type"`
}

/////////////// MODEL CONVERSION ///////////////

func (dto *StatusCollectionDto) Convert() []*models.Status {
var projects = make([]*models.Status, len(dto.Embedded.Elements))

for idx, p := range dto.Embedded.Elements {
projects[idx] = p.Convert()
}

return projects
}

func (dto *statusDto) Convert() *models.Status {
return &models.Status{
Id: dto.Id,
Name: dto.Name,
Color: dto.Color,
IsDefault: dto.IsDefault,
IsClosed: dto.IsClosed,
IsReadonly: dto.IsReadonly,
Position: dto.Position,
}
}
11 changes: 11 additions & 0 deletions models/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

type Status struct {
Id uint64
Name string
Color string
IsDefault bool
IsClosed bool
IsReadonly bool
Position uint64
}

0 comments on commit 34fcf37

Please sign in to comment.