@@ -92,63 +92,75 @@ func main() {
92
92
func (s * server ) markDeployedPR (w http.ResponseWriter , req * http.Request ) {
93
93
var commitSHA string
94
94
var team string
95
+
95
96
for k , v := range req .URL .Query () {
96
97
switch k {
97
98
case "commit" :
98
99
commitSHA = v [0 ]
99
100
case "team" :
100
101
team = v [0 ]
102
+
101
103
default :
102
104
s .log .Warnf ("Unrecognized parameter received: %s" , k )
103
105
}
104
106
}
105
- if team == "" || commitSHA == "" {
106
-
107
- var preconditionFailed struct {
108
- Errors []string
109
- }
110
-
111
- preconditionFailed .Errors = make ([]string , 0 , 1 )
112
- preconditionFailed .Errors = append (preconditionFailed .Errors , fmt .Sprintf ("team and commit are required parameters. Team: '%v', commit: '%v'" , team , commitSHA ))
113
107
108
+ if team == "" || commitSHA == "" {
114
109
s .log .WithFields (
115
110
logrus.Fields {
116
111
"team" : team ,
117
112
"commit" : commitSHA ,
118
113
},
119
114
).Error ("team and commit are required parameters" )
120
115
121
- w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
122
- w .Header ().Set ("X-Content-Type-Options" , "nosniff" )
123
116
w .WriteHeader (http .StatusPreconditionFailed )
124
- json .NewEncoder (w ).Encode (preconditionFailed )
117
+ enc := json .NewEncoder (w )
118
+ enc .SetIndent ("" , " " )
119
+ enc .Encode (map [string ]string {
120
+ "error" : "team and commit are required parameters" ,
121
+ })
125
122
return
126
123
}
127
-
124
+ var err error
128
125
switch req .Method {
129
126
case "GET" :
130
127
var msg struct {
131
128
PRs struct {
132
129
Labeled []string `json:"labeled"`
133
130
Unlabeled []string `json:"unlabeled"`
134
131
}
135
- Errors []error `json:"errors"`
132
+ Error string `json:"error"`
133
+ }
134
+
135
+ msg .PRs .Labeled , msg .PRs .Unlabeled , err = s .handleGetUnmarkedPRs (req .Context (), commitSHA , team )
136
+ if err != nil {
137
+ msg .Error = err .Error ()
138
+ w .WriteHeader (http .StatusUnprocessableEntity )
136
139
}
137
- msg .PRs .Labeled , msg .PRs .Unlabeled , msg .Errors = s .handleGetUnmarkedPRs (req .Context (), commitSHA , team )
138
140
139
141
enc := json .NewEncoder (w )
140
142
enc .SetIndent ("" , " " )
141
143
enc .Encode (msg )
142
144
143
145
case "POST" :
146
+ var errs []error
144
147
var msg struct {
145
148
DeployedPRs struct {
146
149
Team []string `json:"team"`
147
150
All []string `json:"all"`
148
151
} `json:"deployedPRs"`
149
- Errors []error `json:"errors"`
152
+ Errors []string `json:"errors"`
153
+ }
154
+ msg .DeployedPRs .Team , msg .DeployedPRs .All , errs = s .handleMarkDeployedPRs (req .Context (), commitSHA , team )
155
+
156
+ msg .Errors = make ([]string , 0 , len (errs ))
157
+ for _ , err := range errs {
158
+ msg .Errors = append (msg .Errors , err .Error ())
159
+ }
160
+
161
+ if len (msg .Errors ) > 0 {
162
+ w .WriteHeader (http .StatusUnprocessableEntity )
150
163
}
151
- msg .DeployedPRs .Team , msg .DeployedPRs .All , msg .Errors = s .handleMarkDeployedPRs (req .Context (), commitSHA , team )
152
164
153
165
enc := json .NewEncoder (w )
154
166
enc .SetIndent ("" , " " )
@@ -170,13 +182,15 @@ func (s *server) handleMarkDeployedPRs(ctx context.Context, commitSHA, team stri
170
182
return s .updatePullRequests (prs , team )
171
183
}
172
184
173
- func (s * server ) handleGetUnmarkedPRs (ctx context.Context , commitSHA , team string ) (labeledPRs , unlabeledPRs []string , errors [] error ) {
185
+ func (s * server ) handleGetUnmarkedPRs (ctx context.Context , commitSHA , team string ) (labeledPRs , unlabeledPRs []string , err error ) {
174
186
prs , err := s .getMergedPRs (ctx , commitSHA )
175
187
if err != nil {
176
- return nil , nil , [] error { err }
188
+ return nil , nil , err
177
189
}
178
190
179
- return s .findTeamPullRequests (prs , team )
191
+ labeledPRs , unlabeledPRs = s .findTeamPullRequests (prs , team )
192
+
193
+ return labeledPRs , unlabeledPRs , nil
180
194
}
181
195
182
196
const (
@@ -233,7 +247,7 @@ func (s *server) updatePullRequests(prs []pullRequest, team string) (teamDeploye
233
247
}
234
248
235
249
// Find labeled and unlabeled pull requests for the given team.
236
- func (s * server ) findTeamPullRequests (prs []pullRequest , team string ) (labeled , unlabeled []string , errs [] error ) {
250
+ func (s * server ) findTeamPullRequests (prs []pullRequest , team string ) (labeled , unlabeled []string ) {
237
251
for _ , pr := range prs {
238
252
239
253
lblTeam := teamLabel (team )
0 commit comments