@@ -163,26 +163,18 @@ func (p *Plugin) checkAuth(handler http.HandlerFunc) http.HandlerFunc {
163
163
}
164
164
}
165
165
166
- type telemetryAPIRequest struct {
167
- Event string
168
- Properties map [string ]interface {}
169
- }
170
-
171
166
func (p * Plugin ) handleTelemetry (w http.ResponseWriter , r * http.Request ) {
172
167
userID := r .Header .Get ("Mattermost-User-ID" )
173
168
174
- var telemetryRequest * telemetryAPIRequest
175
- decoder := json .NewDecoder (r .Body )
176
- err := decoder .Decode (& telemetryRequest )
169
+ telemetryRequest , err := GetTelemetryPayloadFromJSON (r .Body )
177
170
if err != nil {
178
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
179
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
171
+ p .API .LogError ("Unable to get telemetry payload from JSON err=" + err .Error ())
172
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get telemetry payload from JSON. " , err )
180
173
return
181
174
}
182
175
183
- if telemetryRequest == nil {
184
- p .API .LogError ("Invalid request body" )
185
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
176
+ if err := IsTelemetryPayloadValid (telemetryRequest ); err != nil {
177
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate telemetry payload." , err )
186
178
return
187
179
}
188
180
@@ -191,33 +183,23 @@ func (p *Plugin) handleTelemetry(w http.ResponseWriter, r *http.Request) {
191
183
}
192
184
}
193
185
194
- type addAPIRequest struct {
195
- Message string `json:"message"`
196
- Description string `json:"description"`
197
- SendTo string `json:"send_to"`
198
- PostID string `json:"post_id"`
199
- }
200
-
201
186
func (p * Plugin ) handleAdd (w http.ResponseWriter , r * http.Request ) {
202
187
userID := r .Header .Get ("Mattermost-User-ID" )
203
188
204
- var addRequest * addAPIRequest
205
- decoder := json .NewDecoder (r .Body )
206
- err := decoder .Decode (& addRequest )
189
+ addRequest , err := GetAddIssuePayloadFromJSON (r .Body )
207
190
if err != nil {
208
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
209
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
191
+ p .API .LogError ("Unable to get add issue payload from JSON err=" + err .Error ())
192
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get add issue payload from JSON. " , err )
210
193
return
211
194
}
212
195
213
- senderName := p .listManager .GetUserName (userID )
214
-
215
- if addRequest == nil {
216
- p .API .LogError ("Invalid request body" )
217
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
196
+ if err := IsAddIssuePayloadValid (addRequest ); err != nil {
197
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate add issue payload." , err )
218
198
return
219
199
}
220
200
201
+ senderName := p .listManager .GetUserName (userID )
202
+
221
203
if addRequest .SendTo == "" {
222
204
_ , err = p .listManager .AddIssue (userID , addRequest .Message , addRequest .Description , addRequest .PostID )
223
205
if err != nil {
@@ -358,26 +340,18 @@ func (p *Plugin) handleList(w http.ResponseWriter, r *http.Request) {
358
340
}
359
341
}
360
342
361
- type editAPIRequest struct {
362
- ID string `json:"id"`
363
- Message string `json:"message"`
364
- Description string `json:"description"`
365
- }
366
-
367
343
func (p * Plugin ) handleEdit (w http.ResponseWriter , r * http.Request ) {
368
344
userID := r .Header .Get ("Mattermost-User-ID" )
369
345
370
- var editRequest * editAPIRequest
371
- decoder := json .NewDecoder (r .Body )
372
- if err := decoder .Decode (& editRequest ); err != nil {
373
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
374
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
346
+ editRequest , err := GetEditIssuePayloadFromJSON (r .Body )
347
+ if err != nil {
348
+ p .API .LogError ("Unable to get edit issue payload from JSON err=" + err .Error ())
349
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get edit issue payload from JSON." , err )
375
350
return
376
351
}
377
352
378
- if editRequest == nil {
379
- p .API .LogError ("Invalid request body" )
380
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
353
+ if err := IsEditIssuePayloadValid (editRequest ); err != nil {
354
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate edit issue payload." , err )
381
355
return
382
356
}
383
357
@@ -406,30 +380,18 @@ func (p *Plugin) handleEdit(w http.ResponseWriter, r *http.Request) {
406
380
}
407
381
}
408
382
409
- type changeAssignmentAPIRequest struct {
410
- ID string `json:"id"`
411
- SendTo string `json:"send_to"`
412
- }
413
-
414
383
func (p * Plugin ) handleChangeAssignment (w http.ResponseWriter , r * http.Request ) {
415
384
userID := r .Header .Get ("Mattermost-User-ID" )
416
385
417
- var changeRequest * changeAssignmentAPIRequest
418
- decoder := json .NewDecoder (r .Body )
419
- if err := decoder .Decode (& changeRequest ); err != nil {
420
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
421
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
422
- return
423
- }
424
-
425
- if changeRequest == nil {
426
- p .API .LogError ("Invalid request body" )
427
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
386
+ changeRequest , err := GetChangeAssignmentPayloadFromJSON (r .Body )
387
+ if err != nil {
388
+ p .API .LogError ("Unable to get change request payload from JSON err=" + err .Error ())
389
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get change request from JSON." , err )
428
390
return
429
391
}
430
392
431
- if changeRequest . SendTo == "" {
432
- http . Error (w , "No user specified " , http . StatusBadRequest )
393
+ if err := IsChangeAssignmentPayloadValid ( changeRequest ); err != nil {
394
+ p . handleErrorWithCode (w , http . StatusBadRequest , "Unable to validate change request payload. " , err )
433
395
return
434
396
}
435
397
@@ -464,24 +426,18 @@ func (p *Plugin) handleChangeAssignment(w http.ResponseWriter, r *http.Request)
464
426
}
465
427
}
466
428
467
- type acceptAPIRequest struct {
468
- ID string `json:"id"`
469
- }
470
-
471
429
func (p * Plugin ) handleAccept (w http.ResponseWriter , r * http.Request ) {
472
430
userID := r .Header .Get ("Mattermost-User-ID" )
473
431
474
- var acceptRequest * acceptAPIRequest
475
- decoder := json .NewDecoder (r .Body )
476
- if err := decoder .Decode (& acceptRequest ); err != nil {
477
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
478
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
432
+ acceptRequest , err := GetAcceptRequestPayloadFromJSON (r .Body )
433
+ if err != nil {
434
+ p .API .LogError ("Unable to get accept request payload from JSON err=" + err .Error ())
435
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get accept request from JSON." , err )
479
436
return
480
437
}
481
438
482
- if acceptRequest == nil {
483
- p .API .LogError ("Invalid request body" )
484
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
439
+ if err := IsAcceptRequestPayloadValid (acceptRequest ); err != nil {
440
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate accept request payload." , err )
485
441
return
486
442
}
487
443
@@ -502,24 +458,18 @@ func (p *Plugin) handleAccept(w http.ResponseWriter, r *http.Request) {
502
458
p .PostBotDM (sender , message )
503
459
}
504
460
505
- type completeAPIRequest struct {
506
- ID string `json:"id"`
507
- }
508
-
509
461
func (p * Plugin ) handleComplete (w http.ResponseWriter , r * http.Request ) {
510
462
userID := r .Header .Get ("Mattermost-User-ID" )
511
463
512
- var completeRequest * completeAPIRequest
513
- decoder := json .NewDecoder (r .Body )
514
- if err := decoder .Decode (& completeRequest ); err != nil {
515
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
516
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
464
+ completeRequest , err := GetCompleteIssuePayloadFromJSON (r .Body )
465
+ if err != nil {
466
+ p .API .LogError ("Unable to get complete issue request payload from JSON err=" + err .Error ())
467
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get complete issue request from JSON." , err )
517
468
return
518
469
}
519
470
520
- if completeRequest == nil {
521
- p .API .LogError ("Invalid request body" )
522
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
471
+ if err := IsCompleteIssuePayloadValid (completeRequest ); err != nil {
472
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate complete issue request payload." , err )
523
473
return
524
474
}
525
475
@@ -548,25 +498,18 @@ func (p *Plugin) handleComplete(w http.ResponseWriter, r *http.Request) {
548
498
p .PostBotDM (foreignID , message )
549
499
}
550
500
551
- type removeAPIRequest struct {
552
- ID string `json:"id"`
553
- }
554
-
555
501
func (p * Plugin ) handleRemove (w http.ResponseWriter , r * http.Request ) {
556
502
userID := r .Header .Get ("Mattermost-User-ID" )
557
503
558
- var removeRequest * removeAPIRequest
559
- decoder := json .NewDecoder (r .Body )
560
- err := decoder .Decode (& removeRequest )
504
+ removeRequest , err := GetRemoveIssuePayloadFromJSON (r .Body )
561
505
if err != nil {
562
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
563
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
506
+ p .API .LogError ("Unable to get remove issue request payload from JSON err=" + err .Error ())
507
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get remove issue request from JSON. " , err )
564
508
return
565
509
}
566
510
567
- if removeRequest == nil {
568
- p .API .LogError ("Invalid request body" )
569
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
511
+ if err := IsRemoveIssuePayloadValid (removeRequest ); err != nil {
512
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate remove issue request payload." , err )
570
513
return
571
514
}
572
515
@@ -601,25 +544,18 @@ func (p *Plugin) handleRemove(w http.ResponseWriter, r *http.Request) {
601
544
p .PostBotDM (foreignID , message )
602
545
}
603
546
604
- type bumpAPIRequest struct {
605
- ID string `json:"id"`
606
- }
607
-
608
547
func (p * Plugin ) handleBump (w http.ResponseWriter , r * http.Request ) {
609
548
userID := r .Header .Get ("Mattermost-User-ID" )
610
549
611
- var bumpRequest * bumpAPIRequest
612
- decoder := json .NewDecoder (r .Body )
613
- err := decoder .Decode (& bumpRequest )
550
+ bumpRequest , err := GetBumpIssuePayloadFromJSON (r .Body )
614
551
if err != nil {
615
- p .API .LogError ("Unable to decode JSON err=" + err .Error ())
616
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , err )
552
+ p .API .LogError ("Unable to get bump issue request payload from JSON err=" + err .Error ())
553
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to get bump issue request from JSON. " , err )
617
554
return
618
555
}
619
556
620
- if bumpRequest == nil {
621
- p .API .LogError ("Invalid request body" )
622
- p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to decode JSON" , errors .New ("invalid request body" ))
557
+ if err := IsBumpIssuePayloadValid (bumpRequest ); err != nil {
558
+ p .handleErrorWithCode (w , http .StatusBadRequest , "Unable to validate bump request payload." , err )
623
559
return
624
560
}
625
561
0 commit comments