Skip to content

Commit f6273f8

Browse files
send metadata command
1 parent d30aa8c commit f6273f8

File tree

5 files changed

+197
-2
lines changed

5 files changed

+197
-2
lines changed

cmd/processResult/process_result.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package processResult
2+
3+
import (
4+
argo "cf-argo-plugin/pkg/argo"
5+
codefresh "cf-argo-plugin/pkg/codefresh"
6+
"cf-argo-plugin/pkg/context"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var processResultArgsOptions struct {
11+
PipelineId string
12+
}
13+
14+
var Cmd = &cobra.Command{
15+
Use: "process-result [app]",
16+
Short: "Process plugin execution result",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
name := args[0]
19+
20+
argoApi := argo.Argo{
21+
Host: context.PluginArgoCredentials.Host,
22+
Username: context.PluginArgoCredentials.Username,
23+
Password: context.PluginArgoCredentials.Password,
24+
}
25+
26+
revision, _ := argoApi.GetLatestHistoryRevision(name)
27+
28+
cf := codefresh.New(&codefresh.ClientOptions{
29+
Token: context.PluginCodefreshCredentials.Token,
30+
Host: context.PluginCodefreshCredentials.Host,
31+
})
32+
33+
_ = cf.SendMetadata(&codefresh.ArgoApplicationMetadata{
34+
PipelineId: processResultArgsOptions.PipelineId,
35+
Revision: revision,
36+
ApplicationName: name,
37+
})
38+
39+
return nil
40+
},
41+
}
42+
43+
func init() {
44+
f := Cmd.Flags()
45+
f.StringVar(&processResultArgsOptions.PipelineId, "pipeline-id", "", "Pipeline id where argo sync was executed")
46+
}

cmd/root/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package root
22

33
import (
4+
"cf-argo-plugin/cmd/processResult"
45
"cf-argo-plugin/cmd/rollout"
56
"cf-argo-plugin/cmd/runTask"
67
"cf-argo-plugin/cmd/sync"
@@ -53,6 +54,8 @@ func init() {
5354
rootCmd.AddCommand(sync.Cmd)
5455
rootCmd.AddCommand(rollout.Cmd)
5556
rootCmd.AddCommand(runTask.Cmd)
57+
rootCmd.AddCommand(processResult.Cmd)
58+
5659
}
5760

5861
func fetchArgoCredentials(cmd *cobra.Command, args []string) error {

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
3737
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
3838
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
3939
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
40+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
4041
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
4142
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
4243
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=

pkg/argo/api.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package argo
2+
3+
import (
4+
"bytes"
5+
"crypto/tls"
6+
"encoding/json"
7+
"fmt"
8+
"net/http"
9+
)
10+
11+
func buildHttpClient() *http.Client {
12+
tr := &http.Transport{
13+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
14+
}
15+
return &http.Client{Transport: tr}
16+
}
17+
18+
type Argo struct {
19+
Host string
20+
Username string
21+
Password string
22+
}
23+
24+
type requestOptions struct {
25+
path string
26+
method string
27+
}
28+
29+
type History struct {
30+
Status struct {
31+
History []struct {
32+
Revision string `json:"revision"`
33+
} `json:"history"`
34+
} `json:"status"`
35+
}
36+
37+
func (c *Argo) GetLatestHistoryRevision(application string) (string, error) {
38+
39+
options := &requestOptions{
40+
path: "/api/v1/applications/" + application,
41+
method: "GET",
42+
}
43+
44+
result := &History{}
45+
_ = c.requestAPI(options, result)
46+
47+
historyList := result.Status.History
48+
return historyList[len(historyList)-1].Revision, nil
49+
}
50+
51+
func (c *Argo) getToken() string {
52+
53+
client := buildHttpClient()
54+
55+
message := map[string]interface{}{
56+
"username": c.Username,
57+
"password": c.Password,
58+
}
59+
60+
bytesRepresentation, err := json.Marshal(message)
61+
if err != nil {
62+
fmt.Println(err)
63+
}
64+
65+
resp, err := client.Post(c.Host+"/api/v1/session", "application/json", bytes.NewBuffer(bytesRepresentation))
66+
if err != nil {
67+
fmt.Println(err)
68+
}
69+
var result map[string]interface{}
70+
71+
json.NewDecoder(resp.Body).Decode(&result)
72+
73+
defer resp.Body.Close()
74+
75+
return result["token"].(string)
76+
}
77+
78+
func (c *Argo) requestAPI(opt *requestOptions, target interface{}) error {
79+
token := c.getToken()
80+
81+
client := buildHttpClient()
82+
83+
var body []byte
84+
finalURL := fmt.Sprintf("%s%s", c.Host, opt.path)
85+
86+
request, err := http.NewRequest(opt.method, finalURL, bytes.NewBuffer(body))
87+
88+
if err != nil {
89+
return err
90+
}
91+
92+
request.Header.Set("Authorization", "Bearer "+token)
93+
request.Header.Set("Content-Type", "application/json")
94+
95+
response, err := client.Do(request)
96+
97+
if err != nil {
98+
return err
99+
}
100+
101+
if response.StatusCode < 200 || response.StatusCode > 299 {
102+
argoError := make(map[string]interface{})
103+
err = json.NewDecoder(response.Body).Decode(argoError)
104+
105+
if err != nil {
106+
return err
107+
}
108+
109+
return fmt.Errorf("%d: %v", response.StatusCode, argoError)
110+
}
111+
112+
defer response.Body.Close()
113+
114+
err = json.NewDecoder(response.Body).Decode(target)
115+
116+
if err != nil {
117+
return err
118+
}
119+
120+
return nil
121+
}

pkg/codefresh/api.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type Codefresh interface {
1111
GetIntegration(name string) (*ArgoIntegration, error)
1212
StartSyncTask(name string) (*TaskResult, error)
13+
SendMetadata(metadata *ArgoApplicationMetadata) error
1314
requestAPI(*requestOptions, interface{}) error
1415
}
1516

@@ -31,6 +32,12 @@ type ArgoIntegration struct {
3132
Data ArgoIntegrationData `json:"data"`
3233
}
3334

35+
type ArgoApplicationMetadata struct {
36+
PipelineId string `json:"pipelineId"`
37+
Revision string `json:"revision"`
38+
ApplicationName string `json:"name"`
39+
}
40+
3441
type TaskResult struct {
3542
BuildId string `json:"id"`
3643
}
@@ -45,6 +52,7 @@ type ArgoIntegrationData struct {
4552
type requestOptions struct {
4653
path string
4754
method string
55+
body []byte
4856
}
4957

5058
type codefresh struct {
@@ -75,6 +83,23 @@ func (c *codefresh) GetIntegration(name string) (*ArgoIntegration, error) {
7583
return r, nil
7684
}
7785

86+
func (c *codefresh) SendMetadata(metadata *ArgoApplicationMetadata) error {
87+
metadataBytes := new(bytes.Buffer)
88+
json.NewEncoder(metadataBytes).Encode(metadata)
89+
90+
err := c.requestAPI(&requestOptions{
91+
path: fmt.Sprintf("/api/environments-v2/argo/metadata"),
92+
method: "POST",
93+
body: metadataBytes.Bytes(),
94+
}, nil)
95+
96+
if err != nil {
97+
return err
98+
}
99+
100+
return nil
101+
}
102+
78103
func (c *codefresh) StartSyncTask(name string) (*TaskResult, error) {
79104
r := &TaskResult{}
80105
err := c.requestAPI(&requestOptions{
@@ -90,10 +115,9 @@ func (c *codefresh) StartSyncTask(name string) (*TaskResult, error) {
90115
}
91116

92117
func (c *codefresh) requestAPI(opt *requestOptions, target interface{}) error {
93-
var body []byte
94118
finalURL := fmt.Sprintf("%s%s", c.host, opt.path)
95119

96-
request, err := http.NewRequest(opt.method, finalURL, bytes.NewBuffer(body))
120+
request, err := http.NewRequest(opt.method, finalURL, bytes.NewBuffer(opt.body))
97121

98122
if err != nil {
99123
return err

0 commit comments

Comments
 (0)