@@ -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 ( ctx context. Context , bean * Webhook ) ( * Webhook , error ) {
398
- has , err := db .GetEngine (ctx ).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 (ctx context.Context , id int64 ) (* Webhook , error ) {
409
- return getWebhook (ctx , & Webhook {
410
- ID : id ,
411
- })
412
- }
413
-
414
407
// GetWebhookByRepoID returns webhook of repository by given ID.
415
408
func GetWebhookByRepoID (ctx context.Context , repoID , id int64 ) (* Webhook , error ) {
416
- return getWebhook (ctx , & Webhook {
417
- ID : id ,
418
- RepoID : repoID ,
419
- })
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
420
func GetWebhookByOwnerID (ctx context.Context , ownerID , id int64 ) (* Webhook , error ) {
424
- return getWebhook (ctx , & Webhook {
425
- ID : id ,
426
- OwnerID : ownerID ,
427
- })
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
@@ -461,20 +462,20 @@ func UpdateWebhookLastStatus(ctx context.Context, w *Webhook) error {
461
462
return err
462
463
}
463
464
464
- // deleteWebhook uses argument bean as query condition,
465
+ // DeleteWebhookByID uses argument bean as query condition,
465
466
// ID must be specified and do not assign unnecessary fields.
466
- func deleteWebhook (ctx context.Context , bean * Webhook ) (err error ) {
467
+ func DeleteWebhookByID (ctx context.Context , id int64 ) (err error ) {
467
468
ctx , committer , err := db .TxContext (ctx )
468
469
if err != nil {
469
470
return err
470
471
}
471
472
defer committer .Close ()
472
473
473
- if count , err := db .DeleteByBean (ctx , bean ); err != nil {
474
+ if count , err := db .DeleteByID (ctx , id , new ( Webhook ) ); err != nil {
474
475
return err
475
476
} else if count == 0 {
476
- return ErrWebhookNotExist {ID : bean . ID }
477
- } else if _ , err = db .DeleteByBean (ctx , & HookTask {HookID : bean . ID }); err != nil {
477
+ return ErrWebhookNotExist {ID : id }
478
+ } else if _ , err = db .DeleteByBean (ctx , & HookTask {HookID : id }); err != nil {
478
479
return err
479
480
}
480
481
@@ -483,16 +484,16 @@ func deleteWebhook(ctx context.Context, bean *Webhook) (err error) {
483
484
484
485
// DeleteWebhookByRepoID deletes webhook of repository by given ID.
485
486
func DeleteWebhookByRepoID (ctx context.Context , repoID , id int64 ) error {
486
- return deleteWebhook (ctx , & Webhook {
487
- ID : id ,
488
- RepoID : repoID ,
489
- } )
487
+ if _ , err := GetWebhookByRepoID (ctx , repoID , id ); err != nil {
488
+ return err
489
+ }
490
+ return DeleteWebhookByID ( ctx , id )
490
491
}
491
492
492
493
// DeleteWebhookByOwnerID deletes webhook of a user or organization by given ID.
493
494
func DeleteWebhookByOwnerID (ctx context.Context , ownerID , id int64 ) error {
494
- return deleteWebhook (ctx , & Webhook {
495
- ID : id ,
496
- OwnerID : ownerID ,
497
- } )
495
+ if _ , err := GetWebhookByOwnerID (ctx , ownerID , id ); err != nil {
496
+ return err
497
+ }
498
+ return DeleteWebhookByID ( ctx , id )
498
499
}
0 commit comments