From 69f4a83b7fbee55fa0672895a37845bfda113d74 Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Wed, 14 Jul 2021 16:55:42 +0300 Subject: [PATCH 1/7] change gitops --- pkg/codefresh/gitops.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index 99a5e7a..9fad6d3 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -69,17 +69,19 @@ type ( Current int64 `json:"current"` Desired int64 `json:"desired"` } - User struct { - Name string `json:"name"` - Avatar string `json:"avatar"` - } + Annotation struct { Key string `json:"key"` Value string `json:"value"` } + GitopsUser struct { + Name string `json:"name"` + Avatar string `json:"avatar"` + } + Gitops struct { - Comitters []User `json:"comitters"` + Comitters []GitopsUser `json:"comitters"` Prs []Annotation `json:"prs"` Issues []Annotation `json:"issues"` } @@ -99,6 +101,9 @@ type ( SyncPolicy SyncPolicy `json:"syncPolicy"` Date string `json:"date"` ParentApp string `json:"parentApp"` + Namespace string `json:"namespace"` + Server string `json:"server"` + Context *string `json:"context"` } EnvironmentActivity struct { @@ -114,6 +119,7 @@ type ( HistoryId int64 `json:"historyId"` Revision string `json:"revision, omitempty"` Resources interface{} `json:"resources"` + Context *string `json:"context"` } ) From e38b0e7211f4f7b009ab5bbbfc14e016ff146e8c Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Fri, 16 Jul 2021 08:20:00 +0300 Subject: [PATCH 2/7] add context field --- pkg/codefresh/gitops.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index 9fad6d3..fdee79f 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -31,6 +31,7 @@ type ( Spec struct { Type string `json:"type"` Application string `json:"application"` + Context string `json:"context"` } `json:"spec"` } From 66a3dc0cbb352d0df59684c8aeaa32fb3086b428 Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Fri, 16 Jul 2021 08:39:38 +0300 Subject: [PATCH 3/7] change routes --- pkg/codefresh/gitops.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index fdee79f..32ac7d0 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -131,7 +131,7 @@ func newGitopsAPI(codefresh *codefresh) GitopsAPI { func (a *gitops) CreateEnvironment(name string, project string, application string, integration string) error { _, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", - path: "/api/environments-v2", + path: "/api/gitops/application", body: &EnvironmentPayload{ Version: "1.0", Metadata: EnvironmentMetadata{ @@ -184,7 +184,7 @@ func (a *gitops) GetEnvironments() ([]CFEnvironment, error) { var result MongoCFEnvWrapper resp, err := a.codefresh.requestAPI(&requestOptions{ method: "GET", - path: "/api/environments-v2?plain=true&isEnvironment=false", + path: "/api/gitops/application?plain=true&isEnvironment=false", }) if err != nil { return nil, err From 97f12d9633cfde25f904bc93930b28c163b513f3 Mon Sep 17 00:00:00 2001 From: olegz-codefresh Date: Fri, 17 Sep 2021 11:53:53 +0300 Subject: [PATCH 4/7] CR-5892 --- VERSION | 2 +- pkg/codefresh/gitops.go | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index 30f6cf8..894542a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.26.1 +0.26.2 diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index 32ac7d0..2c32690 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -1,6 +1,9 @@ package codefresh -import "fmt" +import ( + "fmt" + "time" +) type ( GitopsAPI interface { @@ -57,8 +60,9 @@ type ( } Commit struct { - Message *string `json:"message"` - Avatar *string `json:"avatar"` + Time *time.Time `json:"time,omitempty"` + Message *string `json:"message"` + Avatar *string `json:"avatar"` } EnvironmentActivityRS struct { From e380eae8632ef7fbfa4a59f2515cae051a335ff7 Mon Sep 17 00:00:00 2001 From: olegz-codefresh Date: Mon, 20 Sep 2021 13:02:57 +0300 Subject: [PATCH 5/7] CR-5892 --- VERSION | 2 +- pkg/codefresh/gitops.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 894542a..3f45a64 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.26.2 +0.26.3 diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index 2c32690..2bbf2b6 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -7,7 +7,7 @@ import ( type ( GitopsAPI interface { - CreateEnvironment(name string, project string, application string, integration string) error + CreateEnvironment(name string, project string, application string, integration string, namespace string) error SendEnvironment(environment Environment) (map[string]interface{}, error) DeleteEnvironment(name string) error GetEnvironments() ([]CFEnvironment, error) @@ -47,6 +47,7 @@ type ( Context string `json:"context"` Project string `json:"project"` Application string `json:"application"` + Namespace string `json:"namespace,omitempty"` } EnvironmentPayload struct { @@ -132,7 +133,7 @@ func newGitopsAPI(codefresh *codefresh) GitopsAPI { return &gitops{codefresh} } -func (a *gitops) CreateEnvironment(name string, project string, application string, integration string) error { +func (a *gitops) CreateEnvironment(name string, project string, application string, integration string, namespace string) error { _, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", path: "/api/gitops/application", @@ -146,6 +147,7 @@ func (a *gitops) CreateEnvironment(name string, project string, application stri Context: integration, Project: project, Application: application, + Namespace: namespace, }, }, }) From 672c9ec6c76a7045791104ac1b025b5dab8db671 Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Fri, 4 Mar 2022 23:46:23 +0200 Subject: [PATCH 6/7] close connections --- pkg/codefresh/argo.go | 12 ++++++++++-- pkg/codefresh/cluster.go | 6 ++++++ pkg/codefresh/contexts.go | 6 ++++++ pkg/codefresh/gitops.go | 18 ++++++++++++++---- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/pkg/codefresh/argo.go b/pkg/codefresh/argo.go index 02d5044..935b466 100644 --- a/pkg/codefresh/argo.go +++ b/pkg/codefresh/argo.go @@ -102,6 +102,8 @@ func (a *argo) GetIntegrations() ([]*IntegrationPayload, error) { return nil, err } + defer resp.Body.Close() + return result, nil } @@ -123,6 +125,8 @@ func (a *argo) GetIntegrationByName(name string) (*IntegrationPayload, error) { return nil, err } + defer resp.Body.Close() + return &result, nil } @@ -150,7 +154,7 @@ func (a *argo) HeartBeat(error string, version string, integration string) error body.AgentVersion = version } - _, err := a.codefresh.requestAPI(&requestOptions{ + resp, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", path: fmt.Sprintf("/api/argo-agent/%s/heartbeat", integration), body: body, @@ -159,6 +163,8 @@ func (a *argo) HeartBeat(error string, version string, integration string) error return err } + defer resp.Body.Close() + return nil } @@ -167,7 +173,7 @@ func (a *argo) SendResources(kind string, items interface{}, amount int, integra return nil } - _, err := a.codefresh.requestAPI(&requestOptions{ + resp, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", path: fmt.Sprintf("/api/argo-agent/%s", integration), body: &AgentState{Kind: kind, Items: items}, @@ -176,5 +182,7 @@ func (a *argo) SendResources(kind string, items interface{}, amount int, integra return err } + defer resp.Body.Close() + return nil } diff --git a/pkg/codefresh/cluster.go b/pkg/codefresh/cluster.go index 6e6ec62..86773ce 100644 --- a/pkg/codefresh/cluster.go +++ b/pkg/codefresh/cluster.go @@ -42,6 +42,9 @@ func (p *cluster) GetClusterCredentialsByAccountId(selector string) (*Cluster, e method: "GET", }) err = p.codefresh.decodeResponseInto(resp, &r) + + defer resp.Body.Close() + return r, err } @@ -52,5 +55,8 @@ func (p *cluster) GetAccountClusters() ([]*ClusterMinified, error) { method: "GET", }) err = p.codefresh.decodeResponseInto(resp, &r) + + defer resp.Body.Close() + return r, err } diff --git a/pkg/codefresh/contexts.go b/pkg/codefresh/contexts.go index c4982c2..6e01ecf 100644 --- a/pkg/codefresh/contexts.go +++ b/pkg/codefresh/contexts.go @@ -64,6 +64,8 @@ func (c context) GetGitContexts() (error, *[]ContextPayload) { err = c.codefresh.decodeResponseInto(resp, &result) + defer resp.Body.Close() + return err, &result } @@ -84,6 +86,8 @@ func (c context) GetGitContextByName(name string) (error, *ContextPayload) { err = c.codefresh.decodeResponseInto(resp, &result) + defer resp.Body.Close() + return nil, &result } @@ -101,5 +105,7 @@ func (c context) GetDefaultGitContext() (error, *ContextPayload) { err = c.codefresh.decodeResponseInto(resp, &result) + defer resp.Body.Close() + return err, &result } diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index 2bbf2b6..1313143 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -134,7 +134,7 @@ func newGitopsAPI(codefresh *codefresh) GitopsAPI { } func (a *gitops) CreateEnvironment(name string, project string, application string, integration string, namespace string) error { - _, err := a.codefresh.requestAPI(&requestOptions{ + resp, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", path: "/api/gitops/application", body: &EnvironmentPayload{ @@ -155,6 +155,8 @@ func (a *gitops) CreateEnvironment(name string, project string, application stri return err } + defer resp.Body.Close() + return nil } @@ -171,17 +173,20 @@ func (a *gitops) SendEnvironment(environment Environment) (map[string]interface{ return nil, err } + defer resp.Body.Close() + return result, nil } func (a *gitops) DeleteEnvironment(name string) error { - _, err := a.codefresh.requestAPI(&requestOptions{ + resp, err := a.codefresh.requestAPI(&requestOptions{ method: "DELETE", path: fmt.Sprintf("/api/environments-v2/%s", name), }) if err != nil { return err } + defer resp.Body.Close() return nil } @@ -196,6 +201,8 @@ func (a *gitops) GetEnvironments() ([]CFEnvironment, error) { return nil, err } + defer resp.Body.Close() + err = a.codefresh.decodeResponseInto(resp, &result) if err != nil { @@ -208,7 +215,7 @@ func (a *gitops) GetEnvironments() ([]CFEnvironment, error) { func (a *gitops) SendEvent(name string, props map[string]string) error { event := CodefreshEvent{Event: name, Props: props} - _, err := a.codefresh.requestAPI(&requestOptions{ + resp, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", path: "/api/gitops/system/events", body: event, @@ -217,11 +224,13 @@ func (a *gitops) SendEvent(name string, props map[string]string) error { return err } + defer resp.Body.Close() + return nil } func (a *gitops) SendApplicationResources(resources *ApplicationResources) error { - _, err := a.codefresh.requestAPI(&requestOptions{ + resp, err := a.codefresh.requestAPI(&requestOptions{ method: "POST", path: fmt.Sprintf("/api/gitops/resources"), body: &resources, @@ -229,5 +238,6 @@ func (a *gitops) SendApplicationResources(resources *ApplicationResources) error if err != nil { return err } + defer resp.Body.Close() return nil } From e5cef8e56843f911e008637598d9b033da2570b2 Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Fri, 25 Mar 2022 17:26:32 +0200 Subject: [PATCH 7/7] add better error handler --- pkg/codefresh/gitops.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/codefresh/gitops.go b/pkg/codefresh/gitops.go index 1313143..ac4532c 100644 --- a/pkg/codefresh/gitops.go +++ b/pkg/codefresh/gitops.go @@ -1,6 +1,7 @@ package codefresh import ( + "errors" "fmt" "time" ) @@ -151,6 +152,10 @@ func (a *gitops) CreateEnvironment(name string, project string, application stri }, }, }) + if resp != nil && resp.StatusCode >= 400 { + return errors.New(fmt.Sprintf("Failed to create environment, reason %v", resp.Status)) + } + if err != nil { return err }