/
handlers_v2.go
178 lines (161 loc) · 4.64 KB
/
handlers_v2.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package workerruntime
import (
"context"
"encoding/json"
"io"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/ovh/cds/sdk"
"github.com/rockbears/log"
)
func V2_outputHandler(ctx context.Context, wk Runtime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
btes, err := io.ReadAll(r.Body)
if err != nil {
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err))
return
}
var output OutputRequest
if err := sdk.JSONUnmarshal(btes, &output); err != nil {
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err))
return
}
// Create step output
wk.AddStepOutput(ctx, output.Name, output.Value)
// Create run result
if !output.StepOnly {
result := V2RunResultRequest{
RunResult: &sdk.V2WorkflowRunResult{
IssuedAt: time.Now(),
Status: sdk.StatusSuccess,
WorkflowRunID: output.WorkflowRunID,
WorkflowRunJobID: output.WorkflowRunJobID,
Type: sdk.V2WorkflowRunResultTypeVariable,
Detail: sdk.V2WorkflowRunResultDetail{
Data: sdk.V2WorkflowRunResultVariableDetail{
Name: output.Name,
Value: output.Value,
},
},
},
}
response, err := wk.V2AddRunResult(ctx, result)
if err != nil {
writeError(w, r, err)
return
}
log.Info(ctx, "run result %s created", response.RunResult.ID)
}
writeJSON(w, nil, http.StatusNoContent)
}
}
func V2_jobRunHandler(ctx context.Context, wk Runtime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
writeJSON(w, wk.V2GetJobRun(r.Context()), http.StatusOK)
default:
writeError(w, r, sdk.ErrMethodNotAllowed)
return
}
}
}
func V2_contextHandler(ctx context.Context, wk Runtime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
writeJSON(w, wk.V2GetJobContext(r.Context()), http.StatusOK)
default:
writeError(w, r, sdk.ErrMethodNotAllowed)
return
}
}
}
func V2_runResultHandler(ctx context.Context, wk Runtime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
btes, err := io.ReadAll(r.Body)
if err != nil {
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err))
return
}
defer r.Body.Close()
switch r.Method {
case http.MethodGet:
var filter V2FilterRunResult
if err := sdk.JSONUnmarshal(btes, &filter); err != nil {
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err))
return
}
response, err := wk.V2GetRunResult(ctx, filter)
if err != nil {
writeError(w, r, err)
return
}
writeJSON(w, response, http.StatusOK)
case http.MethodPost:
var runResultRequest V2RunResultRequest
if err := sdk.JSONUnmarshal(btes, &runResultRequest); err != nil {
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err))
return
}
response, err := wk.V2AddRunResult(ctx, runResultRequest)
if err != nil {
writeError(w, r, err)
return
}
log.Info(ctx, "run result %s created", response.RunResult.ID)
writeJSON(w, response, http.StatusCreated)
case http.MethodPut:
var runResultRequest V2RunResultRequest
if err := sdk.JSONUnmarshal(btes, &runResultRequest); err != nil {
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err))
return
}
response, err := wk.V2UpdateRunResult(ctx, runResultRequest)
if err != nil {
writeError(w, r, err)
return
}
log.Info(ctx, "run result %s updated", response.RunResult.ID)
writeJSON(w, response, http.StatusOK)
default:
writeError(w, r, sdk.ErrNotFound)
}
}
}
func V2_integrationsHandler(ctx context.Context, wk Runtime) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
if name == "" && len(r.URL.Query()["name"]) > 0 {
name = r.URL.Query()["name"][0] // This part if for unit test
}
if name == "" {
log.Error(ctx, "missing parameter 'name'")
writeError(w, r, sdk.ErrNotFound)
return
}
integ, err := wk.V2GetIntegrationByName(r.Context(), name)
if err != nil {
log.Error(ctx, "unable to get integration %q", name)
writeError(w, r, err)
return
}
writeJSON(w, integ, http.StatusOK)
}
}
func writeJSON(w http.ResponseWriter, data interface{}, status int) {
b, _ := json.Marshal(data)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write(b)
}
func writeError(w http.ResponseWriter, _ *http.Request, err error) {
writePlainText(w, err.Error(), 500)
}
func writePlainText(w http.ResponseWriter, data string, status int) {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(status)
_, _ = w.Write([]byte(data))
}