@@ -392,39 +392,40 @@ func CreateWebhooks(ctx context.Context, ws []*Webhook) error {
392
392
return db .Insert (ctx , ws )
393
393
}
394
394
395
- // getWebhook uses argument bean as query condition,
396
- // ID must be specified and do not assign unnecessary fields.
397
- func getWebhook ( bean * Webhook ) ( * Webhook , error ) {
398
- has , err := db .GetEngine (db . DefaultContext ).Get (bean )
395
+ // GetWebhookByID returns webhook of repository by given ID.
396
+ func GetWebhookByID ( ctx context. Context , id int64 ) ( * Webhook , error ) {
397
+ bean := new ( Webhook )
398
+ has , err := db .GetEngine (ctx ). ID ( id ).Get (bean )
399
399
if err != nil {
400
400
return nil , err
401
401
} else if ! has {
402
- return nil , ErrWebhookNotExist {ID : bean . ID }
402
+ return nil , ErrWebhookNotExist {ID : id }
403
403
}
404
404
return bean , nil
405
405
}
406
406
407
- // GetWebhookByID returns webhook of repository by given ID.
408
- func GetWebhookByID (id int64 ) (* Webhook , error ) {
409
- return getWebhook (& Webhook {
410
- ID : id ,
411
- })
412
- }
413
-
414
407
// GetWebhookByRepoID returns webhook of repository by given ID.
415
- func GetWebhookByRepoID (repoID , id int64 ) (* Webhook , error ) {
416
- return getWebhook (& Webhook {
417
- ID : id ,
418
- RepoID : repoID ,
419
- })
408
+ func GetWebhookByRepoID (ctx context.Context , repoID , id int64 ) (* Webhook , error ) {
409
+ webhook := new (Webhook )
410
+ has , err := db .GetEngine (ctx ).Where ("id=? AND repo_id=?" , id , repoID ).Get (webhook )
411
+ if err != nil {
412
+ return nil , err
413
+ } else if ! has {
414
+ return nil , ErrWebhookNotExist {ID : id }
415
+ }
416
+ return webhook , nil
420
417
}
421
418
422
419
// GetWebhookByOwnerID returns webhook of a user or organization by given ID.
423
- func GetWebhookByOwnerID (ownerID , id int64 ) (* Webhook , error ) {
424
- return getWebhook (& Webhook {
425
- ID : id ,
426
- OwnerID : ownerID ,
427
- })
420
+ func GetWebhookByOwnerID (ctx context.Context , ownerID , id int64 ) (* Webhook , error ) {
421
+ webhook := new (Webhook )
422
+ has , err := db .GetEngine (ctx ).Where ("id=? AND owner_id=?" , id , ownerID ).Get (webhook )
423
+ if err != nil {
424
+ return nil , err
425
+ } else if ! has {
426
+ return nil , ErrWebhookNotExist {ID : id }
427
+ }
428
+ return webhook , nil
428
429
}
429
430
430
431
// ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts
@@ -482,38 +483,38 @@ func UpdateWebhookLastStatus(w *Webhook) error {
482
483
return err
483
484
}
484
485
485
- // deleteWebhook uses argument bean as query condition,
486
+ // DeleteWebhookByID uses argument bean as query condition,
486
487
// ID must be specified and do not assign unnecessary fields.
487
- func deleteWebhook ( bean * Webhook ) (err error ) {
488
- ctx , committer , err := db .TxContext (db . DefaultContext )
488
+ func DeleteWebhookByID ( ctx context. Context , id int64 ) (err error ) {
489
+ ctx , committer , err := db .TxContext (ctx )
489
490
if err != nil {
490
491
return err
491
492
}
492
493
defer committer .Close ()
493
494
494
- if count , err := db .DeleteByBean (ctx , bean ); err != nil {
495
+ if count , err := db .DeleteByID (ctx , id , new ( Webhook ) ); err != nil {
495
496
return err
496
497
} else if count == 0 {
497
- return ErrWebhookNotExist {ID : bean . ID }
498
- } else if _ , err = db .DeleteByBean (ctx , & HookTask {HookID : bean . ID }); err != nil {
498
+ return ErrWebhookNotExist {ID : id }
499
+ } else if _ , err = db .DeleteByBean (ctx , & HookTask {HookID : id }); err != nil {
499
500
return err
500
501
}
501
502
502
503
return committer .Commit ()
503
504
}
504
505
505
506
// DeleteWebhookByRepoID deletes webhook of repository by given ID.
506
- func DeleteWebhookByRepoID (repoID , id int64 ) error {
507
- return deleteWebhook ( & Webhook {
508
- ID : id ,
509
- RepoID : repoID ,
510
- } )
507
+ func DeleteWebhookByRepoID (ctx context. Context , repoID , id int64 ) error {
508
+ if _ , err := GetWebhookByRepoID ( ctx , repoID , id ); err != nil {
509
+ return err
510
+ }
511
+ return DeleteWebhookByID ( ctx , id )
511
512
}
512
513
513
514
// DeleteWebhookByOwnerID deletes webhook of a user or organization by given ID.
514
- func DeleteWebhookByOwnerID (ownerID , id int64 ) error {
515
- return deleteWebhook ( & Webhook {
516
- ID : id ,
517
- OwnerID : ownerID ,
518
- } )
515
+ func DeleteWebhookByOwnerID (ctx context. Context , ownerID , id int64 ) error {
516
+ if _ , err := GetWebhookByOwnerID ( ctx , ownerID , id ); err != nil {
517
+ return err
518
+ }
519
+ return DeleteWebhookByID ( ctx , id )
519
520
}
0 commit comments