@@ -102,24 +102,8 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
102
102
// "403":
103
103
// "$ref": "#/responses/forbidden"
104
104
105
- issue , err := models . GetIssueByIndex (ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
105
+ issue , labels , err := prepareForReplaceOrAdd (ctx , form )
106
106
if err != nil {
107
- if models .IsErrIssueNotExist (err ) {
108
- ctx .NotFound ()
109
- } else {
110
- ctx .Error (http .StatusInternalServerError , "GetIssueByIndex" , err )
111
- }
112
- return
113
- }
114
-
115
- if ! ctx .Repo .CanWriteIssuesOrPulls (issue .IsPull ) {
116
- ctx .Status (http .StatusForbidden )
117
- return
118
- }
119
-
120
- labels , err := models .GetLabelsInRepoByIDs (ctx .Repo .Repository .ID , form .Labels )
121
- if err != nil {
122
- ctx .Error (http .StatusInternalServerError , "GetLabelsInRepoByIDs" , err )
123
107
return
124
108
}
125
109
@@ -204,7 +188,7 @@ func DeleteIssueLabel(ctx *context.APIContext) {
204
188
return
205
189
}
206
190
207
- if err := models . DeleteIssueLabel (issue , label , ctx .User ); err != nil {
191
+ if err := issue_service . RemoveLabel (issue , ctx .User , label ); err != nil {
208
192
ctx .Error (http .StatusInternalServerError , "DeleteIssueLabel" , err )
209
193
return
210
194
}
@@ -248,28 +232,12 @@ func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) {
248
232
// "403":
249
233
// "$ref": "#/responses/forbidden"
250
234
251
- issue , err := models . GetIssueByIndex (ctx . Repo . Repository . ID , ctx . ParamsInt64 ( ":index" ) )
235
+ issue , labels , err := prepareForReplaceOrAdd (ctx , form )
252
236
if err != nil {
253
- if models .IsErrIssueNotExist (err ) {
254
- ctx .NotFound ()
255
- } else {
256
- ctx .Error (http .StatusInternalServerError , "GetIssueByIndex" , err )
257
- }
258
- return
259
- }
260
-
261
- if ! ctx .Repo .CanWriteIssuesOrPulls (issue .IsPull ) {
262
- ctx .Status (http .StatusForbidden )
263
237
return
264
238
}
265
239
266
- labels , err := models .GetLabelsInRepoByIDs (ctx .Repo .Repository .ID , form .Labels )
267
- if err != nil {
268
- ctx .Error (http .StatusInternalServerError , "GetLabelsInRepoByIDs" , err )
269
- return
270
- }
271
-
272
- if err := issue .ReplaceLabels (labels , ctx .User ); err != nil {
240
+ if err := issue_service .ReplaceLabels (issue , ctx .User , labels ); err != nil {
273
241
ctx .Error (http .StatusInternalServerError , "ReplaceLabels" , err )
274
242
return
275
243
}
@@ -339,3 +307,28 @@ func ClearIssueLabels(ctx *context.APIContext) {
339
307
340
308
ctx .Status (http .StatusNoContent )
341
309
}
310
+
311
+ func prepareForReplaceOrAdd (ctx * context.APIContext , form api.IssueLabelsOption ) (issue * models.Issue , labels []* models.Label , err error ) {
312
+ issue , err = models .GetIssueByIndex (ctx .Repo .Repository .ID , ctx .ParamsInt64 (":index" ))
313
+ if err != nil {
314
+ if models .IsErrIssueNotExist (err ) {
315
+ ctx .NotFound ()
316
+ } else {
317
+ ctx .Error (http .StatusInternalServerError , "GetIssueByIndex" , err )
318
+ }
319
+ return
320
+ }
321
+
322
+ labels , err = models .GetLabelsInRepoByIDs (ctx .Repo .Repository .ID , form .Labels )
323
+ if err != nil {
324
+ ctx .Error (http .StatusInternalServerError , "GetLabelsInRepoByIDs" , err )
325
+ return
326
+ }
327
+
328
+ if ! ctx .Repo .CanWriteIssuesOrPulls (issue .IsPull ) {
329
+ ctx .Status (http .StatusForbidden )
330
+ return
331
+ }
332
+
333
+ return
334
+ }
0 commit comments