@@ -392,39 +392,40 @@ func CreateWebhooks(ctx context.Context, ws []*Webhook) error {
392392 return db .Insert (ctx , ws )
393393}
394394
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 )
399399 if err != nil {
400400 return nil , err
401401 } else if ! has {
402- return nil , ErrWebhookNotExist {ID : bean . ID }
402+ return nil , ErrWebhookNotExist {ID : id }
403403 }
404404 return bean , nil
405405}
406406
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-
414407// GetWebhookByRepoID returns webhook of repository by given ID.
415408func 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
420417}
421418
422419// GetWebhookByOwnerID returns webhook of a user or organization by given ID.
423420func 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
428429}
429430
430431// ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts
@@ -461,20 +462,20 @@ func UpdateWebhookLastStatus(ctx context.Context, w *Webhook) error {
461462 return err
462463}
463464
464- // deleteWebhook uses argument bean as query condition,
465+ // DeleteWebhookByID uses argument bean as query condition,
465466// 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 ) {
467468 ctx , committer , err := db .TxContext (ctx )
468469 if err != nil {
469470 return err
470471 }
471472 defer committer .Close ()
472473
473- if count , err := db .DeleteByBean (ctx , bean ); err != nil {
474+ if count , err := db .DeleteByID (ctx , id , new ( Webhook ) ); err != nil {
474475 return err
475476 } 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 {
478479 return err
479480 }
480481
@@ -483,16 +484,16 @@ func deleteWebhook(ctx context.Context, bean *Webhook) (err error) {
483484
484485// DeleteWebhookByRepoID deletes webhook of repository by given ID.
485486func 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 )
490491}
491492
492493// DeleteWebhookByOwnerID deletes webhook of a user or organization by given ID.
493494func 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 )
498499}
0 commit comments