Skip to content

Commit 5e45f1c

Browse files
Merge branch 'master' of github.com:codefresh-io/go-sdk into CR-5460
� Conflicts: � VERSION � pkg/codefresh/gitops.go
2 parents 4f23d78 + 78b2962 commit 5e45f1c

13 files changed

+478
-74
lines changed

.goreleaser.yml

-27
This file was deleted.

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.27.0
1+
0.26.6

codefresh.yaml

+12-31
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,27 @@
11
version: '1.0'
22

33
stages:
4+
- Prepare
45
- Release
56

6-
mode: parallel
7-
87
steps:
98

10-
CreatingGitTag:
11-
title: Push tag to git
12-
image: codefresh/cli
13-
stage: Release
14-
commands:
15-
- export VERSION=$(cat VERSION)
16-
- export OLD_ORIGIN=$(git remote get-url origin)
17-
- git remote rm origin
18-
- git remote add origin https://${{GITHUB_TOKEN}}@github.com/codefresh-io/go-sdk.git
19-
- git tag v$VERSION
20-
- git push --tags
21-
- git remote rm origin
22-
- git remote add origin $OLD_ORIGIN
23-
fail_fast: false
24-
when:
25-
steps:
26-
- name: main_clone
27-
branch:
28-
only:
29-
- master
9+
main_clone:
10+
stage: Prepare
11+
title: clone repository
12+
type: git-clone
13+
git: cf_github
14+
repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}
15+
revision: ${{CF_BRANCH}}
3016

3117
ReleasingBinaries:
3218
title: Create release in Github
33-
image: goreleaser/goreleaser
19+
image: quay.io/codefresh/golang-ci-helper:latest
3420
stage: Release
35-
fail_fast: false
3621
commands:
37-
- go mod download
38-
- goreleaser release -f .goreleaser.yml --rm-dist --skip-validate
22+
- export VERSION=$(cat VERSION)
23+
- gh release create --repo ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}} -t $VERSION -n $VERSION $VERSION
3924
when:
40-
steps:
41-
- name: CreatingGitTag
42-
on:
43-
- finished
4425
branch:
4526
only:
46-
- master
27+
- master

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/codefresh-io/go-sdk
22

33
require (
44
github.com/BurntSushi/toml v0.3.1 // indirect
5+
github.com/codefresh-io/argo-platform v1.13.0
56
github.com/dustin/go-humanize v1.0.0
67
github.com/google/go-querystring v1.1.0
78
github.com/inconshreveable/mousetrap v1.0.0 // indirect
@@ -11,6 +12,8 @@ require (
1112
github.com/spf13/afero v1.2.0 // indirect
1213
github.com/spf13/cobra v0.0.3
1314
github.com/spf13/viper v1.3.1
15+
github.com/stretchr/objx v0.3.0 // indirect
16+
github.com/stretchr/testify v1.3.0
1417
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb // indirect
1518
gopkg.in/yaml.v2 v2.2.2
1619
)

go.sum

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
4+
github.com/codefresh-io/argo-platform v1.13.0 h1:eWAWkxtO0nGZuekrQJsfFT5KfBMdWby0cOucy4yTExs=
5+
github.com/codefresh-io/argo-platform v1.13.0/go.mod h1:u/eLWAySJ1nRzNWnB5baeyIc5vqINghwYJvydU+EC3c=
46
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
57
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
68
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
710
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
811
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
912
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
@@ -45,8 +48,12 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
4548
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
4649
github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=
4750
github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
48-
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
51+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
52+
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
53+
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
4954
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
55+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
56+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5057
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
5158
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
5259
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

pkg/codefresh/argo_runtime.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package codefresh
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
8+
"github.com/codefresh-io/argo-platform/libs/ql/graph/model"
9+
)
10+
11+
type (
12+
IArgoRuntimeAPI interface {
13+
List() ([]model.Runtime, error)
14+
}
15+
argoRuntime struct {
16+
codefresh *codefresh
17+
}
18+
graphqlRuntimesResponse struct {
19+
Data struct {
20+
Runtimes model.RuntimePage
21+
}
22+
Errors []graphqlError
23+
}
24+
)
25+
26+
func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
27+
return &argoRuntime{codefresh: codefresh}
28+
}
29+
func (r *argoRuntime) List() ([]model.Runtime, error) {
30+
31+
jsonData := map[string]interface{}{
32+
"query": `
33+
{
34+
runtimes{
35+
edges{
36+
node {
37+
id
38+
namespace
39+
objectMeta {
40+
name
41+
description
42+
}
43+
}
44+
}
45+
}
46+
}
47+
`,
48+
}
49+
50+
response, err := r.codefresh.requestAPI(&requestOptions{
51+
method: "POST",
52+
path: "/argo/api/graphql",
53+
body: jsonData,
54+
})
55+
defer response.Body.Close()
56+
if err != nil {
57+
fmt.Printf("The HTTP request failed with error %s\n", err)
58+
return nil, err
59+
}
60+
data, err := ioutil.ReadAll(response.Body)
61+
if err != nil {
62+
fmt.Printf("failed to read from response body")
63+
return nil, err
64+
}
65+
res := graphqlRuntimesResponse{}
66+
err = json.Unmarshal(data, &res)
67+
if err != nil {
68+
return nil, err
69+
}
70+
runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
71+
for i := range res.Data.Runtimes.Edges {
72+
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
73+
}
74+
75+
if len(res.Errors) > 0 {
76+
return nil, graphqlErrorResponse{errors: res.Errors}
77+
}
78+
79+
return runtimes, nil
80+
81+
}

pkg/codefresh/codefresh.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ package codefresh
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
7-
"github.com/google/go-querystring/query"
88
"io/ioutil"
99
"net/http"
1010
"strings"
11+
12+
"github.com/google/go-querystring/query"
1113
)
1214

15+
//go:generate mockery -name Codefresh -filename codefresh.go
16+
17+
//go:generate mockery -name UsersAPI -filename users.go
18+
1319
type (
1420
Codefresh interface {
1521
Pipelines() IPipelineAPI
@@ -19,8 +25,10 @@ type (
1925
Progresses() IProgressAPI
2026
Clusters() IClusterAPI
2127
Contexts() IContextAPI
28+
Users() UsersAPI
2229
Argo() ArgoAPI
2330
Gitops() GitopsAPI
31+
ArgoRuntime() IArgoRuntimeAPI
2432
}
2533
)
2634

@@ -41,6 +49,10 @@ func (c *codefresh) Pipelines() IPipelineAPI {
4149
return newPipelineAPI(c)
4250
}
4351

52+
func (c *codefresh) Users() UsersAPI {
53+
return newUsersAPI(c)
54+
}
55+
4456
func (c *codefresh) Tokens() ITokenAPI {
4557
return newTokenAPI(c)
4658
}
@@ -73,7 +85,15 @@ func (c *codefresh) Gitops() GitopsAPI {
7385
return newGitopsAPI(c)
7486
}
7587

88+
func (c *codefresh) ArgoRuntime() IArgoRuntimeAPI {
89+
return newArgoRuntimeAPI(c)
90+
}
91+
7692
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
93+
return c.requestAPIWithContext(context.Background(), opt)
94+
}
95+
96+
func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptions) (*http.Response, error) {
7797
var body []byte
7898
finalURL := fmt.Sprintf("%s%s", c.host, opt.path)
7999
if opt.qs != nil {
@@ -82,7 +102,10 @@ func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
82102
if opt.body != nil {
83103
body, _ = json.Marshal(opt.body)
84104
}
85-
request, err := http.NewRequest(opt.method, finalURL, bytes.NewBuffer(body))
105+
request, err := http.NewRequestWithContext(ctx, opt.method, finalURL, bytes.NewBuffer(body))
106+
if err != nil {
107+
return nil, err
108+
}
86109
request.Header.Set("Authorization", c.token)
87110
request.Header.Set("Content-Type", "application/json")
88111

pkg/codefresh/common.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
)
8+
9+
type graphqlError struct {
10+
Message string
11+
Locations [] struct {
12+
Line int
13+
Column int
14+
}
15+
Extensions struct {
16+
Code string
17+
Exception struct {
18+
Stacktrace []string
19+
}
20+
}
21+
}
22+
23+
type graphqlErrorResponse struct {
24+
errors []graphqlError
25+
concatenatedErrors string
26+
}
27+
28+
29+
func (e graphqlErrorResponse) Error() string {
30+
31+
if e.concatenatedErrors != "" {
32+
return e.concatenatedErrors
33+
}
34+
var sb strings.Builder
35+
for _, err := range e.errors {
36+
sb.WriteString(fmt.Sprintln(err.Message))
37+
}
38+
e.concatenatedErrors = sb.String()
39+
return e.concatenatedErrors
40+
}

pkg/codefresh/contexts.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type (
77
GetDefaultGitContext() (error, *ContextPayload)
88
}
99

10-
context struct {
10+
contexts struct {
1111
codefresh *codefresh
1212
}
1313

@@ -42,10 +42,10 @@ type (
4242
)
4343

4444
func newContextAPI(codefresh *codefresh) IContextAPI {
45-
return &context{codefresh}
45+
return &contexts{codefresh}
4646
}
4747

48-
func (c context) GetGitContexts() (error, *[]ContextPayload) {
48+
func (c contexts) GetGitContexts() (error, *[]ContextPayload) {
4949
var result []ContextPayload
5050

5151
qs := GitContextsQs{
@@ -67,7 +67,7 @@ func (c context) GetGitContexts() (error, *[]ContextPayload) {
6767
return err, &result
6868
}
6969

70-
func (c context) GetGitContextByName(name string) (error, *ContextPayload) {
70+
func (c contexts) GetGitContextByName(name string) (error, *ContextPayload) {
7171
var result ContextPayload
7272
var qs = map[string]string{
7373
"decrypt": "true",
@@ -87,7 +87,7 @@ func (c context) GetGitContextByName(name string) (error, *ContextPayload) {
8787
return nil, &result
8888
}
8989

90-
func (c context) GetDefaultGitContext() (error, *ContextPayload) {
90+
func (c contexts) GetDefaultGitContext() (error, *ContextPayload) {
9191
var result ContextPayload
9292

9393
resp, err := c.codefresh.requestAPI(&requestOptions{

0 commit comments

Comments
 (0)