Skip to content

Commit 56102d1

Browse files
adrientheboPothulapati
authored andcommitted
deployed-labeler: serialise errors as strings
per golang/go#5161 serializing an `error` object to json emits an empty object; this caused deployed-labeler to emit an empty object when erroring. This commit converts all errors to strings before returning to the user to provide more context. Signed-off-by: Tarun Pothulapati <[email protected]>
1 parent fa3a71a commit 56102d1

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

plugins/deployed-labeler/main.go

+34-20
Original file line numberDiff line numberDiff line change
@@ -92,63 +92,75 @@ func main() {
9292
func (s *server) markDeployedPR(w http.ResponseWriter, req *http.Request) {
9393
var commitSHA string
9494
var team string
95+
9596
for k, v := range req.URL.Query() {
9697
switch k {
9798
case "commit":
9899
commitSHA = v[0]
99100
case "team":
100101
team = v[0]
102+
101103
default:
102104
s.log.Warnf("Unrecognized parameter received: %s", k)
103105
}
104106
}
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))
113107

108+
if team == "" || commitSHA == "" {
114109
s.log.WithFields(
115110
logrus.Fields{
116111
"team": team,
117112
"commit": commitSHA,
118113
},
119114
).Error("team and commit are required parameters")
120115

121-
w.Header().Set("Content-Type", "application/json; charset=utf-8")
122-
w.Header().Set("X-Content-Type-Options", "nosniff")
123116
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+
})
125122
return
126123
}
127-
124+
var err error
128125
switch req.Method {
129126
case "GET":
130127
var msg struct {
131128
PRs struct {
132129
Labeled []string `json:"labeled"`
133130
Unlabeled []string `json:"unlabeled"`
134131
}
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)
136139
}
137-
msg.PRs.Labeled, msg.PRs.Unlabeled, msg.Errors = s.handleGetUnmarkedPRs(req.Context(), commitSHA, team)
138140

139141
enc := json.NewEncoder(w)
140142
enc.SetIndent("", " ")
141143
enc.Encode(msg)
142144

143145
case "POST":
146+
var errs []error
144147
var msg struct {
145148
DeployedPRs struct {
146149
Team []string `json:"team"`
147150
All []string `json:"all"`
148151
} `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)
150163
}
151-
msg.DeployedPRs.Team, msg.DeployedPRs.All, msg.Errors = s.handleMarkDeployedPRs(req.Context(), commitSHA, team)
152164

153165
enc := json.NewEncoder(w)
154166
enc.SetIndent("", " ")
@@ -170,13 +182,15 @@ func (s *server) handleMarkDeployedPRs(ctx context.Context, commitSHA, team stri
170182
return s.updatePullRequests(prs, team)
171183
}
172184

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) {
174186
prs, err := s.getMergedPRs(ctx, commitSHA)
175187
if err != nil {
176-
return nil, nil, []error{err}
188+
return nil, nil, err
177189
}
178190

179-
return s.findTeamPullRequests(prs, team)
191+
labeledPRs, unlabeledPRs = s.findTeamPullRequests(prs, team)
192+
193+
return labeledPRs, unlabeledPRs, nil
180194
}
181195

182196
const (
@@ -233,7 +247,7 @@ func (s *server) updatePullRequests(prs []pullRequest, team string) (teamDeploye
233247
}
234248

235249
// 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) {
237251
for _, pr := range prs {
238252

239253
lblTeam := teamLabel(team)

0 commit comments

Comments
 (0)