This repository has been archived by the owner on May 25, 2023. It is now read-only.
/
api-func-executions.go
156 lines (144 loc) · 5.09 KB
/
api-func-executions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Package cmd Copyright 2021 VMware, Inc.
SPDX-License-Identifier: BSD-2-Clause
*/
package cmd
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/go-resty/resty/v2"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
)
func getExecutions(id string, project string, status string, name string, nested bool) ([]*CodestreamAPIExecutions, error) {
var arrExecutions []*CodestreamAPIExecutions
if id != "" {
x, err := getExecution("/codestream/api/executions/" + id)
if err != nil {
return nil, err
}
arrExecutions = append(arrExecutions, x)
return arrExecutions, err
}
client := resty.New()
var qParams = make(map[string]string)
qParams["$orderby"] = "_requestTimeInMicros desc"
qParams["$top"] = fmt.Sprint(count)
qParams["$skip"] = fmt.Sprint(skip)
var filters []string
if status != "" {
filters = append(filters, "(status eq '"+strings.ToUpper(status)+"')")
}
if name != "" {
filters = append(filters, "(name eq '"+name+"')")
}
if nested {
filters = append(filters, "(_nested eq '"+strconv.FormatBool(nested)+"')")
}
if project != "" {
filters = append(filters, "(project eq '"+project+"')")
}
if len(filters) > 0 {
qParams["$filter"] = "(" + strings.Join(filters, ") and (") + ")"
log.Debugln(qParams["$filter"])
}
log.Debug(qParams)
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Accept", "application/json").
SetResult(&documentsList{}).
SetError(&CodeStreamException{}).
SetAuthToken(targetConfig.accesstoken).
Get("https://" + targetConfig.server + "/pipeline/api/executions")
if queryResponse.IsError() {
//return nil, queryResponse.Error().(error)
return nil, errors.New(queryResponse.Error().(*CodeStreamException).Message)
}
for _, value := range queryResponse.Result().(*documentsList).Documents {
c := CodestreamAPIExecutions{}
mapstructure.Decode(value, &c)
arrExecutions = append(arrExecutions, &c)
}
return arrExecutions, err
}
func getExecution(executionLink string) (*CodestreamAPIExecutions, error) {
client := resty.New()
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Accept", "application/json").
SetResult(&CodestreamAPIExecutions{}).
SetAuthToken(targetConfig.accesstoken).
Get("https://" + targetConfig.server + executionLink)
if queryResponse.IsError() {
return nil, queryResponse.Error().(error)
}
return queryResponse.Result().(*CodestreamAPIExecutions), err
}
func deleteExecution(id string) (*CodestreamAPIExecutions, error) {
client := resty.New()
queryResponse, err := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Accept", "application/json").
SetResult(&CodestreamAPIExecutions{}).
SetAuthToken(targetConfig.accesstoken).
Delete("https://" + targetConfig.server + "/pipeline/api/executions/" + id)
if queryResponse.IsError() {
return nil, errors.New(queryResponse.Error().(*CodeStreamException).Message)
}
return queryResponse.Result().(*CodestreamAPIExecutions), err
}
func deleteExecutions(project string, status string, name string, nested bool) ([]*CodestreamAPIExecutions, error) {
var deletedExecutions []*CodestreamAPIExecutions
Executions, err := getExecutions("", project, status, name, nested)
if err != nil {
return nil, err
}
confirm := askForConfirmation("This will attempt to delete " + fmt.Sprint(len(Executions)) + " Executions in " + project + ", are you sure?")
if confirm {
for _, Execution := range Executions {
deletedExecution, err := deleteExecution(Execution.ID)
if err != nil {
log.Warnln("Unable to delete "+Execution.ID, err)
}
deletedExecutions = append(deletedExecutions, deletedExecution)
}
return deletedExecutions, nil
} else {
return nil, errors.New("user declined")
}
}
func createExecution(id string, inputs string, comment string) (*CodeStreamCreateExecutionResponse, error) {
// Convert JSON string to byte array
var inputBytes = []byte(inputs)
// Unmarshal inputs using a generic interface
var inputsInterface interface{}
err := json.Unmarshal(inputBytes, &inputsInterface)
if err != nil {
return nil, err
}
// Create CodeStreamCreateExecutionRequest struct
var execution CodeStreamCreateExecutionRequest
execution.Comments = comment
execution.Input = inputsInterface
//Marshal struct to JSON []byte
executionBytes, err := json.Marshal(execution)
if err != nil {
return nil, err
}
client := resty.New()
queryResponse, _ := client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: ignoreCert}).R().
SetQueryParams(qParams).
SetHeader("Content-Type", "application/json").
SetBody(executionBytes).
SetResult(&CodeStreamCreateExecutionResponse{}).
SetAuthToken(targetConfig.accesstoken).
Post("https://" + targetConfig.server + "/pipeline/api/pipelines/" + id + "/executions")
if queryResponse.IsError() {
return nil, errors.New(queryResponse.Error().(*CodeStreamException).Message)
}
return queryResponse.Result().(*CodeStreamCreateExecutionResponse), nil
}