@@ -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 ( 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 )
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 (id int64 ) (* Webhook , error ) {
409- return getWebhook (& Webhook {
410- ID : id ,
411- })
412- }
413-
414407// 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
420417}
421418
422419// 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
428429}
429430
430431// ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts
@@ -482,38 +483,38 @@ func UpdateWebhookLastStatus(w *Webhook) error {
482483 return err
483484}
484485
485- // deleteWebhook uses argument bean as query condition,
486+ // DeleteWebhookByID uses argument bean as query condition,
486487// 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 )
489490 if err != nil {
490491 return err
491492 }
492493 defer committer .Close ()
493494
494- if count , err := db .DeleteByBean (ctx , bean ); err != nil {
495+ if count , err := db .DeleteByID (ctx , id , new ( Webhook ) ); err != nil {
495496 return err
496497 } 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 {
499500 return err
500501 }
501502
502503 return committer .Commit ()
503504}
504505
505506// 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 )
511512}
512513
513514// 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 )
519520}
0 commit comments