Skip to content
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

implements the project list #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.testFlags": ["-v"]
}
Comment on lines +1 to +3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not include this file in the pr
I see the repo doesn't have a .gitignore file - maybe even include one that ignores visual studio code and golang artifacts? (just a suggestion)

4 changes: 4 additions & 0 deletions pkg/codefresh/codefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type (
Users() UsersAPI
Argo() ArgoAPI
Gitops() GitopsAPI
Projects() IProjectAPI
V2() V2API
}

Expand Down Expand Up @@ -91,6 +92,9 @@ func (c *codefresh) Argo() ArgoAPI {
func (c *codefresh) Gitops() GitopsAPI {
return newGitopsAPI(c)
}
func (c *codefresh) Projects() IProjectAPI {
return newProjectAPI(c)
}

func (c *codefresh) V2() V2API {
return c
Expand Down
39 changes: 39 additions & 0 deletions pkg/codefresh/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package codefresh

type (
IProjectAPI interface {
List() ([]*Project, error)
}
project struct {
codefresh *codefresh
}
Project struct {
ProjectName string `json:"projectName"`
PipelineNumber int `json:"pipelineNumber"`
}
getProjectResponse struct {
Total int `json:"limit"`
Projects []*Project `json:"projects"`
}
)

func newProjectAPI(codefresh *codefresh) IProjectAPI {
return &project{codefresh}
}

func (p *project) List() ([]*Project, error) {
r := &getProjectResponse{}

resp, err := p.codefresh.requestAPI(&requestOptions{
path: "/api/projects",
method: "GET",
})
if err != nil {
return nil, err
}
err = p.codefresh.decodeResponseInto(resp, r)
if err != nil {
return nil, err
}
return r.Projects, nil
}
25 changes: 25 additions & 0 deletions pkg/codefresh/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package codefresh

import (
"testing"

"github.com/stretchr/testify/assert"
)

var clientOptions = ClientOptions{
Auth: AuthOptions{
Token: "",
},
Debug: true,
Host: "https://g.codefresh.io",
}

func TestProject(t *testing.T) {

cf := New(&clientOptions)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can use the codefresh/mock/codefresh struct to make this test better?
you will also need to regenerate the mock itself to include your Projects() func. You will need mockery 1.1.1 installed, and run go generate ./... for it.
let me know if you need any help with it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noam-codefresh is there an example that shows the coddfresh mock structs use.

For an API client that should have been code generated, this abstraction is proving to be rather tedious, and hard to follow, it most certainly confuses the compiler

projects, err := cf.Projects().List()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(projects), 0)
}