Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad99aea

Browse files
committedAug 17, 2021
resolve merge conflict
2 parents 1f27776 + 532428d commit ad99aea

26 files changed

+2810
-123
lines changed
 

‎.goreleaser.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.24.2
1+
0.32.1

‎codefresh.yaml

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
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+
- VERSION=$(if [[ ${VERSION:0:1} == "v" ]] ; then echo $VERSION; else echo "v${VERSION}"; fi )
24+
- gh release create --repo ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}} -t $VERSION -n $VERSION $VERSION
3925
when:
40-
steps:
41-
- name: CreatingGitTag
42-
on:
43-
- finished
4426
branch:
4527
only:
46-
- master
28+
- master

‎go.mod

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ module github.com/codefresh-io/go-sdk
33
require (
44
github.com/BurntSushi/toml v0.3.1 // indirect
55
github.com/dustin/go-humanize v1.0.0
6+
github.com/google/go-querystring v1.1.0
67
github.com/inconshreveable/mousetrap v1.0.0 // indirect
8+
github.com/kr/pretty v0.1.0 // indirect
79
github.com/mattn/go-runewidth v0.0.4 // indirect
810
github.com/mitchellh/go-homedir v1.0.0
911
github.com/olekukonko/tablewriter v0.0.1
1012
github.com/spf13/afero v1.2.0 // indirect
1113
github.com/spf13/cobra v0.0.3
1214
github.com/spf13/viper v1.3.1
13-
github.com/stretchr/testify v1.7.0 // indirect
14-
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb // indirect
15-
gopkg.in/yaml.v2 v2.2.2
15+
github.com/stretchr/objx v0.3.0 // indirect
16+
github.com/stretchr/testify v1.4.0
17+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
18+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
19+
gopkg.in/yaml.v2 v2.2.4
1620
)
1721

1822
go 1.13

‎go.sum

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
1111
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
1212
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
1313
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
14+
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
15+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
16+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
17+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
1418
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
1519
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1620
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1721
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
22+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
23+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
24+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
25+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
26+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1827
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
1928
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
2029
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
@@ -42,23 +51,26 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
4251
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
4352
github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=
4453
github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
45-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
4654
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
47-
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
55+
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
56+
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
4857
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
49-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
50-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
58+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
59+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
60+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
5161
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
5262
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
5363
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
5464
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
55-
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb h1:pf3XwC90UUdNPYWZdFjhGBE7DUFuK3Ct1zWmZ65QN30=
56-
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
65+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
66+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5767
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
5868
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
59-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
69+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
70+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6071
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
61-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
72+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
73+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6274
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
63-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
64-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
75+
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
76+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

‎pkg/codefresh/argo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type (
1414
}
1515

1616
argo struct {
17-
codefresh Codefresh
17+
codefresh *codefresh
1818
}
1919

2020
IntegrationItem struct {
@@ -50,7 +50,7 @@ type (
5050
}
5151
)
5252

53-
func newArgoAPI(codefresh Codefresh) ArgoAPI {
53+
func newArgoAPI(codefresh *codefresh) ArgoAPI {
5454
return &argo{codefresh}
5555
}
5656

‎pkg/codefresh/argo_runtime.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
8+
)
9+
10+
type (
11+
IRuntimeAPI interface {
12+
List(ctx context.Context) ([]model.Runtime, error)
13+
Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
14+
}
15+
16+
argoRuntime struct {
17+
codefresh *codefresh
18+
}
19+
20+
graphqlRuntimesResponse struct {
21+
Data struct {
22+
Runtimes model.RuntimePage
23+
}
24+
Errors []graphqlError
25+
}
26+
27+
graphQlRuntimeCreationResponse struct {
28+
Data struct {
29+
Runtime model.RuntimeCreationResponse
30+
}
31+
Errors []graphqlError
32+
}
33+
)
34+
35+
func newArgoRuntimeAPI(codefresh *codefresh) IRuntimeAPI {
36+
return &argoRuntime{codefresh: codefresh}
37+
}
38+
39+
func (r *argoRuntime) List(ctx context.Context) ([]model.Runtime, error) {
40+
jsonData := map[string]interface{}{
41+
"query": `{
42+
runtimes {
43+
edges {
44+
node {
45+
metadata {
46+
name
47+
namespace
48+
}
49+
self {
50+
healthStatus
51+
version
52+
}
53+
cluster
54+
}
55+
}
56+
}
57+
}`,
58+
}
59+
60+
res := &graphqlRuntimesResponse{}
61+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
62+
if err != nil {
63+
return nil, fmt.Errorf("failed getting runtime list: %w", err)
64+
}
65+
66+
if len(res.Errors) > 0 {
67+
return nil, graphqlErrorResponse{errors: res.Errors}
68+
}
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+
return runtimes, nil
76+
}
77+
78+
func (r *argoRuntime) Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
79+
jsonData := map[string]interface{}{
80+
"query": `
81+
mutation CreateRuntime(
82+
$name: String!
83+
$cluster: String!
84+
$runtimeVersion: String!
85+
) {
86+
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
87+
name
88+
newAccessToken
89+
}
90+
}
91+
`,
92+
"variables": map[string]interface{}{
93+
"name": runtimeName,
94+
"cluster": cluster,
95+
"runtimeVersion": runtimeVersion,
96+
},
97+
}
98+
99+
res := &graphQlRuntimeCreationResponse{}
100+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
101+
if err != nil {
102+
return nil, fmt.Errorf("failed getting runtime list: %w", err)
103+
}
104+
105+
if len(res.Errors) > 0 {
106+
return nil, graphqlErrorResponse{errors: res.Errors}
107+
}
108+
109+
return &res.Data.Runtime, nil
110+
}

‎pkg/codefresh/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type (
99
}
1010

1111
cluster struct {
12-
codefresh Codefresh
12+
codefresh *codefresh
1313
}
1414

1515
Cluster struct {
@@ -31,7 +31,7 @@ type (
3131
}
3232
)
3333

34-
func newClusterAPI(codefresh Codefresh) IClusterAPI {
34+
func newClusterAPI(codefresh *codefresh) IClusterAPI {
3535
return &cluster{codefresh}
3636
}
3737

‎pkg/codefresh/codefresh.go

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,41 @@ package codefresh
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
"io/ioutil"
810
"net/http"
911
"strings"
12+
13+
"github.com/google/go-querystring/query"
1014
)
1115

16+
//go:generate mockery -name Codefresh -filename codefresh.go
17+
18+
//go:generate mockery -name UsersAPI -filename users.go
19+
1220
type (
1321
Codefresh interface {
14-
requestAPI(*requestOptions) (*http.Response, error)
15-
decodeResponseInto(*http.Response, interface{}) error
16-
getBodyAsString(*http.Response) (string, error)
17-
getBodyAsBytes(*http.Response) ([]byte, error)
1822
Pipelines() IPipelineAPI
1923
Tokens() ITokenAPI
2024
RuntimeEnvironments() IRuntimeEnvironmentAPI
2125
Workflows() IWorkflowAPI
2226
Progresses() IProgressAPI
2327
Clusters() IClusterAPI
2428
Contexts() IContextAPI
29+
Users() UsersAPI
2530
Argo() ArgoAPI
2631
Gitops() GitopsAPI
2732
Projects() IProjectAPI
33+
V2() V2API
34+
}
35+
36+
V2API interface {
37+
Runtime() IRuntimeAPI
38+
GitSource() IGitSourceAPI
39+
Component() IComponentAPI
2840
}
2941
)
3042

@@ -45,6 +57,10 @@ func (c *codefresh) Pipelines() IPipelineAPI {
4557
return newPipelineAPI(c)
4658
}
4759

60+
func (c *codefresh) Users() UsersAPI {
61+
return newUsersAPI(c)
62+
}
63+
4864
func (c *codefresh) Tokens() ITokenAPI {
4965
return newTokenAPI(c)
5066
}
@@ -80,7 +96,27 @@ func (c *codefresh) Projects() IProjectAPI {
8096
return newProjectAPI(c)
8197
}
8298

99+
func (c *codefresh) V2() V2API {
100+
return c
101+
}
102+
103+
func (c *codefresh) Runtime() IRuntimeAPI {
104+
return newArgoRuntimeAPI(c)
105+
}
106+
107+
func (c *codefresh) GitSource() IGitSourceAPI {
108+
return newGitSourceAPI(c)
109+
}
110+
111+
func (c *codefresh) Component() IComponentAPI {
112+
return newComponentAPI(c)
113+
}
114+
83115
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
116+
return c.requestAPIWithContext(context.Background(), opt)
117+
}
118+
119+
func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptions) (*http.Response, error) {
84120
var body []byte
85121
finalURL := fmt.Sprintf("%s%s", c.host, opt.path)
86122
if opt.qs != nil {
@@ -89,9 +125,13 @@ func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
89125
if opt.body != nil {
90126
body, _ = json.Marshal(opt.body)
91127
}
92-
request, err := http.NewRequest(opt.method, finalURL, bytes.NewBuffer(body))
128+
request, err := http.NewRequestWithContext(ctx, opt.method, finalURL, bytes.NewBuffer(body))
129+
if err != nil {
130+
return nil, err
131+
}
93132
request.Header.Set("Authorization", c.token)
94133
request.Header.Set("Content-Type", "application/json")
134+
request.Header.Set("origin", c.host)
95135

96136
response, err := c.client.Do(request)
97137
if err != nil {
@@ -100,14 +140,53 @@ func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
100140
return response, nil
101141
}
102142

103-
func toQS(qs map[string]string) string {
143+
func (c *codefresh) graphqlAPI(ctx context.Context, body map[string]interface{}, res interface{}) error {
144+
response, err := c.requestAPIWithContext(ctx, &requestOptions{
145+
method: "POST",
146+
path: "/2.0/api/graphql",
147+
body: body,
148+
})
149+
if err != nil {
150+
return fmt.Errorf("The HTTP request failed: %w", err)
151+
}
152+
defer response.Body.Close()
153+
154+
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
155+
if !statusOK {
156+
return errors.New(response.Status)
157+
}
158+
159+
data, err := ioutil.ReadAll(response.Body)
160+
if err != nil {
161+
return fmt.Errorf("failed to read from response body: %w", err)
162+
}
163+
164+
return json.Unmarshal(data, res)
165+
}
166+
167+
func buildQSFromMap(qs map[string]string) string {
104168
var arr = []string{}
105169
for k, v := range qs {
106170
arr = append(arr, fmt.Sprintf("%s=%s", k, v))
107171
}
108172
return "?" + strings.Join(arr, "&")
109173
}
110174

175+
func toQS(qs interface{}) string {
176+
v, _ := query.Values(qs)
177+
qsStr := v.Encode()
178+
if qsStr != "" {
179+
return "?" + qsStr
180+
}
181+
var qsMap map[string]string
182+
rs, _ := json.Marshal(qs)
183+
err := json.Unmarshal(rs, &qsMap)
184+
if err != nil {
185+
return ""
186+
}
187+
return buildQSFromMap(qsMap)
188+
}
189+
111190
func (c *codefresh) decodeResponseInto(resp *http.Response, target interface{}) error {
112191
return json.NewDecoder(resp.Body).Decode(target)
113192
}

‎pkg/codefresh/common.go

Lines changed: 40 additions & 0 deletions
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/component.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
8+
)
9+
10+
type (
11+
IComponentAPI interface {
12+
List(ctx context.Context, runtimeName string) ([]model.Component, error)
13+
}
14+
15+
component struct {
16+
codefresh *codefresh
17+
}
18+
19+
graphqlComponentsResponse struct {
20+
Data struct {
21+
Components model.ComponentPage
22+
}
23+
Errors []graphqlError
24+
}
25+
)
26+
27+
func newComponentAPI(codefresh *codefresh) IComponentAPI {
28+
return &component{codefresh: codefresh}
29+
}
30+
31+
func (r *component) List(ctx context.Context, runtimeName string) ([]model.Component, error) {
32+
jsonData := map[string]interface{}{
33+
"query": `
34+
query Components($runtime: String!) {
35+
components(runtime: $runtime) {
36+
edges {
37+
node {
38+
metadata {
39+
name
40+
}
41+
version
42+
self {
43+
status {
44+
syncStatus
45+
healthStatus
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}`,
52+
"variables": map[string]interface{}{
53+
"runtime": runtimeName,
54+
},
55+
}
56+
57+
res := &graphqlComponentsResponse{}
58+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed getting components list: %w", err)
61+
}
62+
63+
if len(res.Errors) > 0 {
64+
return nil, graphqlErrorResponse{errors: res.Errors}
65+
}
66+
67+
components := make([]model.Component, len(res.Data.Components.Edges))
68+
for i := range res.Data.Components.Edges {
69+
components[i] = *res.Data.Components.Edges[i].Node
70+
}
71+
72+
return components, nil
73+
}

‎pkg/codefresh/contexts.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ type (
77
GetDefaultGitContext() (error, *ContextPayload)
88
}
99

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

1414
ContextPayload struct {
@@ -19,27 +19,38 @@ type (
1919
Type string `json:"type"`
2020
Data struct {
2121
Auth struct {
22-
Type string `json:"type"`
23-
Username string `json:"username"`
24-
Password string `json:"password"`
25-
ApiHost string `json:"apiHost"`
26-
ApiPathPrefix string `json:"apiPathPrefix"`
27-
SshPrivateKey string `json:"sshPrivateKey"`
22+
Type string `json:"type"`
23+
Username string `json:"username"`
24+
Password string `json:"password"`
25+
ApiHost string `json:"apiHost"`
26+
// for gitlab
27+
ApiURL string `json:"apiURL"`
28+
ApiPathPrefix string `json:"apiPathPrefix"`
29+
SshPrivateKey string `json:"sshPrivateKey"`
30+
AppId string `json:"appId"`
31+
InstallationId string `json:"installationId"`
32+
PrivateKey string `json:"privateKey"`
2833
} `json:"auth"`
2934
} `json:"data"`
3035
} `json:"spec"`
3136
}
37+
38+
GitContextsQs struct {
39+
Type []string `url:"type"`
40+
Decrypt string `url:"decrypt"`
41+
}
3242
)
3343

34-
func newContextAPI(codefresh Codefresh) IContextAPI {
35-
return &context{codefresh}
44+
func newContextAPI(codefresh *codefresh) IContextAPI {
45+
return &contexts{codefresh}
3646
}
3747

38-
func (c context) GetGitContexts() (error, *[]ContextPayload) {
48+
func (c contexts) GetGitContexts() (error, *[]ContextPayload) {
3949
var result []ContextPayload
40-
var qs = map[string]string{
41-
"type": "git.github",
42-
"decrypt": "true",
50+
51+
qs := GitContextsQs{
52+
Type: []string{"git.github", "git.gitlab", "git.github-app"},
53+
Decrypt: "true",
4354
}
4455

4556
resp, err := c.codefresh.requestAPI(&requestOptions{
@@ -56,7 +67,7 @@ func (c context) GetGitContexts() (error, *[]ContextPayload) {
5667
return err, &result
5768
}
5869

59-
func (c context) GetGitContextByName(name string) (error, *ContextPayload) {
70+
func (c contexts) GetGitContextByName(name string) (error, *ContextPayload) {
6071
var result ContextPayload
6172
var qs = map[string]string{
6273
"decrypt": "true",
@@ -76,7 +87,7 @@ func (c context) GetGitContextByName(name string) (error, *ContextPayload) {
7687
return nil, &result
7788
}
7889

79-
func (c context) GetDefaultGitContext() (error, *ContextPayload) {
90+
func (c contexts) GetDefaultGitContext() (error, *ContextPayload) {
8091
var result ContextPayload
8192

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

‎pkg/codefresh/git-source.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
8+
)
9+
10+
type (
11+
IGitSourceAPI interface {
12+
List(ctc context.Context, runtimeName string) ([]model.GitSource, error)
13+
}
14+
15+
gitSource struct {
16+
codefresh *codefresh
17+
}
18+
19+
graphQlGitSourcesListResponse struct {
20+
Data struct {
21+
GitSources model.GitSourcePage
22+
}
23+
Errors []graphqlError
24+
}
25+
)
26+
27+
func newGitSourceAPI(codefresh *codefresh) IGitSourceAPI {
28+
return &gitSource{codefresh: codefresh}
29+
}
30+
31+
func (g *gitSource) List(ctx context.Context, runtimeName string) ([]model.GitSource, error) {
32+
jsonData := map[string]interface{}{
33+
"query": `
34+
query GitSources($runtime: String) {
35+
gitSources(runtime: $runtime) {
36+
edges {
37+
node {
38+
metadata {
39+
name
40+
}
41+
self {
42+
path
43+
repoURL
44+
status {
45+
syncStatus
46+
healthStatus
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}`,
53+
"variables": map[string]interface{}{
54+
"runtime": runtimeName,
55+
},
56+
}
57+
58+
res := &graphQlGitSourcesListResponse{}
59+
err := g.codefresh.graphqlAPI(ctx, jsonData, res)
60+
if err != nil {
61+
return nil, fmt.Errorf("failed getting git-source list: %w", err)
62+
}
63+
64+
gitSources := make([]model.GitSource, len(res.Data.GitSources.Edges))
65+
for i := range res.Data.GitSources.Edges {
66+
gitSources[i] = *res.Data.GitSources.Edges[i].Node
67+
}
68+
69+
if len(res.Errors) > 0 {
70+
return nil, graphqlErrorResponse{errors: res.Errors}
71+
}
72+
73+
return gitSources, nil
74+
}

‎pkg/codefresh/gitops.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package codefresh
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
type (
68
GitopsAPI interface {
@@ -13,7 +15,7 @@ type (
1315
}
1416

1517
gitops struct {
16-
codefresh Codefresh
18+
codefresh *codefresh
1719
}
1820
CodefreshEvent struct {
1921
Event string `json:"event"`
@@ -69,17 +71,19 @@ type (
6971
Current int64 `json:"current"`
7072
Desired int64 `json:"desired"`
7173
}
72-
User struct {
73-
Name string `json:"name"`
74-
Avatar string `json:"avatar"`
75-
}
74+
7675
Annotation struct {
7776
Key string `json:"key"`
7877
Value string `json:"value"`
7978
}
8079

80+
GitopsUser struct {
81+
Name string `json:"name"`
82+
Avatar string `json:"avatar"`
83+
}
84+
8185
Gitops struct {
82-
Comitters []User `json:"comitters"`
86+
Comitters []GitopsUser `json:"comitters"`
8387
Prs []Annotation `json:"prs"`
8488
Issues []Annotation `json:"issues"`
8589
}
@@ -99,6 +103,9 @@ type (
99103
SyncPolicy SyncPolicy `json:"syncPolicy"`
100104
Date string `json:"date"`
101105
ParentApp string `json:"parentApp"`
106+
Namespace string `json:"namespace"`
107+
Server string `json:"server"`
108+
Context *string `json:"context"`
102109
}
103110

104111
EnvironmentActivity struct {
@@ -114,10 +121,11 @@ type (
114121
HistoryId int64 `json:"historyId"`
115122
Revision string `json:"revision, omitempty"`
116123
Resources interface{} `json:"resources"`
124+
Context *string `json:"context"`
117125
}
118126
)
119127

120-
func newGitopsAPI(codefresh Codefresh) GitopsAPI {
128+
func newGitopsAPI(codefresh *codefresh) GitopsAPI {
121129
return &gitops{codefresh}
122130
}
123131

@@ -146,11 +154,19 @@ func (a *gitops) CreateEnvironment(name string, project string, application stri
146154
}
147155

148156
func (a *gitops) SendEnvironment(environment Environment) (map[string]interface{}, error) {
149-
_, err := a.codefresh.requestAPI(&requestOptions{method: "POST", path: "/api/gitops/rollout", body: environment})
157+
var result map[string]interface{}
158+
resp, err := a.codefresh.requestAPI(&requestOptions{method: "POST", path: "/api/environments-v2/argo/events", body: environment})
159+
if err != nil {
160+
return nil, err
161+
}
162+
163+
err = a.codefresh.decodeResponseInto(resp, &result)
164+
150165
if err != nil {
151166
return nil, err
152167
}
153-
return nil, nil
168+
169+
return result, nil
154170
}
155171

156172
func (a *gitops) DeleteEnvironment(name string) error {

‎pkg/codefresh/mocks/codefresh.go

Lines changed: 189 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/codefresh/mocks/users.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/codefresh/model/models_gen.go

Lines changed: 2019 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/codefresh/pipeline.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type (
6161
}
6262

6363
pipeline struct {
64-
codefresh Codefresh
64+
codefresh *codefresh
6565
}
6666

6767
RunOptions struct {
@@ -70,7 +70,7 @@ type (
7070
}
7171
)
7272

73-
func newPipelineAPI(codefresh Codefresh) IPipelineAPI {
73+
func newPipelineAPI(codefresh *codefresh) IPipelineAPI {
7474
return &pipeline{codefresh}
7575
}
7676

‎pkg/codefresh/progress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type (
1010
}
1111

1212
progress struct {
13-
codefresh Codefresh
13+
codefresh *codefresh
1414
}
1515

1616
Progress struct {
@@ -25,7 +25,7 @@ type (
2525
}
2626
)
2727

28-
func newProgressAPI(codefresh Codefresh) IProgressAPI {
28+
func newProgressAPI(codefresh *codefresh) IProgressAPI {
2929
return &progress{codefresh}
3030
}
3131

‎pkg/codefresh/project.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@ type (
55
List() ([]*Project, error)
66
}
77
project struct {
8-
codefresh Codefresh
8+
codefresh *codefresh
99
}
1010
Project struct {
1111
ProjectName string `json:"projectName"`
12-
pipelineNumber int `json:"pipelineNumber"`
12+
PipelineNumber int `json:"pipelineNumber"`
1313
}
1414
getProjectResponse struct {
1515
Total int `json:"limit"`
1616
Projects []*Project `json:"projects"`
1717
}
1818
)
1919

20-
func newProjectAPI(codefresh Codefresh) IProjectAPI {
20+
func newProjectAPI(codefresh *codefresh) IProjectAPI {
2121
return &project{codefresh}
2222
}
2323

2424
func (p *project) List() ([]*Project, error) {
2525
r := &getProjectResponse{}
26+
2627
resp, err := p.codefresh.requestAPI(&requestOptions{
2728
path: "/api/projects",
2829
method: "GET",

‎pkg/codefresh/project_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var clientOptions = ClientOptions{
1414
Host: "https://g.codefresh.io",
1515
}
1616

17-
func TestProject(t *testing.T) {
17+
func SampleTestProject(t *testing.T) {
1818

1919
cf := New(&clientOptions)
2020
projects, err := cf.Projects().List()

‎pkg/codefresh/runtime_enrionment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ type (
9898
}
9999

100100
runtimeEnvironment struct {
101-
codefresh Codefresh
101+
codefresh *codefresh
102102
}
103103
)
104104

105-
func newRuntimeEnvironmentAPI(codefresh Codefresh) IRuntimeEnvironmentAPI {
105+
func newRuntimeEnvironmentAPI(codefresh *codefresh) IRuntimeEnvironmentAPI {
106106
return &runtimeEnvironment{codefresh}
107107
}
108108

‎pkg/codefresh/tokens.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ type (
3030
}
3131

3232
token struct {
33-
codefresh Codefresh
33+
codefresh *codefresh
3434
}
3535
)
3636

3737
const (
3838
runtimeEnvironmentSubject tokenSubjectType = 0
3939
)
4040

41-
func newTokenAPI(codefresh Codefresh) ITokenAPI {
41+
func newTokenAPI(codefresh *codefresh) ITokenAPI {
4242
return &token{codefresh}
4343
}
4444

‎pkg/codefresh/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ type (
2828
path string
2929
method string
3030
body interface{}
31-
qs map[string]string
31+
qs interface{}
3232
}
3333
)

‎pkg/codefresh/users.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
type (
9+
UsersAPI interface {
10+
GetCurrent(ctx context.Context) (*User, error)
11+
}
12+
13+
User struct {
14+
ID string `json:"_id"`
15+
Name string `json:"userName"`
16+
Email string `json:"email"`
17+
Accounts []Account `json:"account"`
18+
ActiveAccountName string `json:"activeAccountName"`
19+
Roles []string `json:"roles"`
20+
UserData struct {
21+
Avatar string `json:"image"`
22+
} `json:"user_data"`
23+
}
24+
25+
Account struct {
26+
Name string `json:"name"`
27+
}
28+
29+
users struct {
30+
*codefresh
31+
}
32+
)
33+
34+
func newUsersAPI(codefresh *codefresh) UsersAPI {
35+
return &users{codefresh}
36+
}
37+
38+
func (u *users) GetCurrent(ctx context.Context) (*User, error) {
39+
result := &User{}
40+
resp, err := u.codefresh.requestAPIWithContext(ctx, &requestOptions{
41+
method: "GET",
42+
path: "/api/user",
43+
})
44+
if err != nil {
45+
return nil, err
46+
}
47+
if resp.StatusCode >= 400 {
48+
return nil, fmt.Errorf(resp.Status)
49+
}
50+
51+
if err := u.codefresh.decodeResponseInto(resp, &result); err != nil {
52+
return nil, err
53+
}
54+
55+
return result, nil
56+
}
57+
58+
func (u *User) GetActiveAccount() *Account {
59+
for i := 0; i < len(u.Accounts); i++ {
60+
if u.Accounts[i].Name == u.ActiveAccountName {
61+
return &u.Accounts[i]
62+
}
63+
}
64+
return nil
65+
}

‎pkg/codefresh/workflow.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type (
1313
}
1414

1515
workflow struct {
16-
codefresh Codefresh
16+
codefresh *codefresh
1717
}
1818

1919
Workflow struct {
@@ -27,7 +27,7 @@ type (
2727
}
2828
)
2929

30-
func newWorkflowAPI(codefresh Codefresh) IWorkflowAPI {
30+
func newWorkflowAPI(codefresh *codefresh) IWorkflowAPI {
3131
return &workflow{codefresh}
3232
}
3333

0 commit comments

Comments
 (0)
Please sign in to comment.