-
Notifications
You must be signed in to change notification settings - Fork 10
[CLI-22] Add op version list --project command
#21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| package version | ||||||
|
|
||||||
| import ( | ||||||
| "github.com/spf13/cobra" | ||||||
|
|
||||||
| openerrors "github.com/opf/openproject-cli/components/errors" | ||||||
| "github.com/opf/openproject-cli/components/printer" | ||||||
| "github.com/opf/openproject-cli/components/resources/projects" | ||||||
| ) | ||||||
|
|
||||||
| var listProjectId string | ||||||
|
|
||||||
| var listCmd = &cobra.Command{ | ||||||
| Use: "list", | ||||||
| Short: "Lists versions", | ||||||
| Long: "Get a list of a project's versions, scoped by the provided flag (--project).", | ||||||
| RunE: listVersions, | ||||||
| } | ||||||
|
|
||||||
| func listVersions(_ *cobra.Command, _ []string) error { | ||||||
| if err := projects.ValidateIdentifier(listProjectId); err != nil { | ||||||
| printer.ErrorText(err.Error()) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we contextualise the error message? (as
Suggested change
|
||||||
| return openerrors.ErrHandled | ||||||
| } | ||||||
|
|
||||||
| versions, err := projects.AvailableVersions(listProjectId) | ||||||
| if err != nil { | ||||||
| printer.Error(err) | ||||||
| return openerrors.ErrHandled | ||||||
| } | ||||||
|
|
||||||
| printer.Versions(versions) | ||||||
| return nil | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package version | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| openerrors "github.com/opf/openproject-cli/components/errors" | ||
| "github.com/opf/openproject-cli/components/printer" | ||
| "github.com/opf/openproject-cli/components/requests" | ||
| ) | ||
|
Comment on lines
+3
to
+14
|
||
|
|
||
| func TestListVersionsInvalidProjectReturnsError(t *testing.T) { | ||
| testingPrinter := &printer.TestingPrinter{} | ||
| printer.Init(testingPrinter) | ||
| listProjectId = "" | ||
| t.Cleanup(func() { listProjectId = "" }) | ||
|
|
||
| err := listVersions(nil, nil) | ||
| if !errors.Is(err, openerrors.ErrHandled) { | ||
| t.Fatalf("listVersions error = %v, want ErrHandled", err) | ||
| } | ||
| if count := strings.Count(testingPrinter.ErrResult, "[ERROR]"); count != 1 { | ||
| t.Errorf("error diagnostic count = %d, want 1; stderr: %q", count, testingPrinter.ErrResult) | ||
| } | ||
| } | ||
|
|
||
| func TestListVersionsPrintsAvailableVersions(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although an unlikely scenario in real-world usage (at least aT OP), We're missing a test for an empty version collection. This contrasts with |
||
| server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
| response.Header().Set("Content-Type", "application/json") | ||
| _, _ = response.Write([]byte(`{ | ||
|
Comment on lines
+32
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about this finding.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. It was there initially, but I removed it thinking that it's just a stub server. Now I realize that there may be value in testing the verb and http endpoint. |
||
| "_type": "Collection", | ||
| "_embedded": { | ||
| "elements": [ | ||
| {"id": 3, "name": "v17"}, | ||
| {"id": 7, "name": "v42"} | ||
| ] | ||
| } | ||
| }`)) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| host, err := url.Parse(server.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| requests.Init(host, "", false) | ||
|
|
||
| testingPrinter := &printer.TestingPrinter{} | ||
| printer.Init(testingPrinter) | ||
| listProjectId = "example" | ||
| t.Cleanup(func() { listProjectId = "" }) | ||
|
|
||
| if err := listVersions(nil, nil); err != nil { | ||
| t.Fatalf("listVersions error = %v, want nil", err) | ||
| } | ||
|
|
||
| if !strings.Contains(testingPrinter.Result, "v17") || !strings.Contains(testingPrinter.Result, "v42") { | ||
| t.Errorf("expected output to contain versions 'v17' and 'v42', got: %q", testingPrinter.Result) | ||
| } | ||
| } | ||
|
Comment on lines
+52
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the same as #21 (comment) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package version | ||
|
|
||
| import "github.com/spf13/cobra" | ||
|
|
||
| var RootCmd = &cobra.Command{ | ||
| Use: "version [verb]", | ||
| Short: "Manage versions", | ||
| Long: "List a project's versions in OpenProject.", | ||
| } | ||
|
|
||
| func init() { | ||
| listCmd.Flags().StringVarP( | ||
| &listProjectId, | ||
| "project", | ||
| "p", | ||
| "", | ||
| "Project numeric ID or identifier", | ||
| ) | ||
|
|
||
| _ = listCmd.MarkFlagRequired("project") | ||
|
|
||
| RootCmd.AddCommand(listCmd) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,7 @@ | ||
| package printer | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/opf/openproject-cli/models" | ||
| ) | ||
| import "github.com/opf/openproject-cli/models" | ||
|
|
||
| func Versions(versions []*models.Version) { | ||
| for _, a := range versions { | ||
| printVersion(a) | ||
| } | ||
| } | ||
|
|
||
| func printVersion(version *models.Version) { | ||
| id := fmt.Sprintf("#%d", version.Id) | ||
| activePrinter.Printf("[%s] %s\n", Red(id), Cyan(version.Name)) | ||
| activeRenderer.Versions(versions) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,8 @@ 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" | ||
| ) | ||
|
|
@@ -14,27 +12,14 @@ func TestVersions(t *testing.T) { | |
| testingPrinter.Reset() | ||
|
|
||
| versions := []*models.Version{ | ||
| { | ||
| Id: 2, | ||
| Name: "13.0", | ||
| }, | ||
| { | ||
| Id: 4, | ||
| Name: "13.1", | ||
| }, | ||
| { | ||
| Id: 43, | ||
| Name: "45.5", | ||
| }, | ||
| {Id: 2, Name: "13.0"}, | ||
| {Id: 4, Name: "13.1"}, | ||
| {Id: 43, Name: "45.5"}, | ||
| } | ||
|
|
||
| expected := common.Reduce( | ||
| versions, | ||
| func(state string, version *models.Version) string { | ||
| idString := "#" + strconv.FormatUint(version.Id, 10) | ||
| return state + fmt.Sprintf("[%s] %s\n", printer.Red(idString), printer.Cyan(version.Name)) | ||
| }, | ||
| "") | ||
| expected := fmt.Sprintf("%s %s\n", printer.Red(" #2"), printer.Cyan("13.0")) | ||
| expected += fmt.Sprintf("%s %s\n", printer.Red(" #4"), printer.Cyan("13.1")) | ||
| expected += fmt.Sprintf("%s %s\n", printer.Red("#43"), printer.Cyan("45.5")) | ||
|
Comment on lines
+20
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're only testing |
||
|
|
||
| printer.Versions(versions) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import shadowing (claude finding):
cmd/root.go:143'sfunc Execute(version *configuration.Version)parameter strictly shadows the newly addedgithub.com/opf/openproject-cli/cmd/versionimport.