Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: add context to Hooks #938

Merged
merged 6 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/admin/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ import (
func UpdateHook(c *gin.Context) {
logrus.Info("Admin: updating hook in database")

// capture middleware values
ctx := c.Request.Context()

// capture body from API request
input := new(library.Hook)

Expand All @@ -64,7 +67,7 @@ func UpdateHook(c *gin.Context) {
}

// send API call to update the hook
h, err := database.FromContext(c).UpdateHook(input)
h, err := database.FromContext(c).UpdateHook(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to update hook %d: %w", input.GetID(), err)

Expand Down
5 changes: 3 additions & 2 deletions api/hook/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func CreateHook(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand All @@ -89,7 +90,7 @@ func CreateHook(c *gin.Context) {
}

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(r)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

Expand All @@ -113,7 +114,7 @@ func CreateHook(c *gin.Context) {
}

// send API call to create the webhook
h, err := database.FromContext(c).CreateHook(input)
h, err := database.FromContext(c).CreateHook(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create hook for repo %s: %w", r.GetFullName(), err)

Expand Down
5 changes: 3 additions & 2 deletions api/hook/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func DeleteHook(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)
hook := util.PathParameter(c, "hook")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook)

Expand All @@ -92,7 +93,7 @@ func DeleteHook(c *gin.Context) {
}

// send API call to capture the webhook
h, err := database.FromContext(c).GetHookForRepo(r, number)
h, err := database.FromContext(c).GetHookForRepo(ctx, r, number)
if err != nil {
retErr := fmt.Errorf("unable to get hook %s: %w", hook, err)

Expand All @@ -102,7 +103,7 @@ func DeleteHook(c *gin.Context) {
}

// send API call to remove the webhook
err = database.FromContext(c).DeleteHook(h)
err = database.FromContext(c).DeleteHook(ctx, h)
if err != nil {
retErr := fmt.Errorf("unable to delete hook %s: %w", hook, err)

Expand Down
3 changes: 2 additions & 1 deletion api/hook/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func GetHook(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)
hook := util.PathParameter(c, "hook")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook)

Expand All @@ -88,7 +89,7 @@ func GetHook(c *gin.Context) {
}

// send API call to capture the webhook
h, err := database.FromContext(c).GetHookForRepo(r, number)
h, err := database.FromContext(c).GetHookForRepo(ctx, r, number)
if err != nil {
retErr := fmt.Errorf("unable to get hook %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/hook/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func ListHooks(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -114,7 +115,7 @@ func ListHooks(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the list of steps for the build
h, t, err := database.FromContext(c).ListHooksForRepo(r, page, perPage)
h, t, err := database.FromContext(c).ListHooksForRepo(ctx, r, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get hooks for repo %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/hook/redeliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func RedeliverHook(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)
hook := util.PathParameter(c, "hook")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook)

Expand All @@ -93,7 +94,7 @@ func RedeliverHook(c *gin.Context) {
}

// send API call to capture the webhook
h, err := database.FromContext(c).GetHookForRepo(r, number)
h, err := database.FromContext(c).GetHookForRepo(ctx, r, number)
if err != nil {
retErr := fmt.Errorf("unable to get hook %s: %w", entry, err)

Expand Down
5 changes: 3 additions & 2 deletions api/hook/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func UpdateHook(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)
hook := util.PathParameter(c, "hook")
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), hook)

Expand Down Expand Up @@ -111,7 +112,7 @@ func UpdateHook(c *gin.Context) {
}

// send API call to capture the webhook
h, err := database.FromContext(c).GetHookForRepo(r, number)
h, err := database.FromContext(c).GetHookForRepo(ctx, r, number)
if err != nil {
retErr := fmt.Errorf("unable to get hook %s: %w", entry, err)

Expand Down Expand Up @@ -157,7 +158,7 @@ func UpdateHook(c *gin.Context) {
}

// send API call to update the webhook
h, err = database.FromContext(c).UpdateHook(h)
h, err = database.FromContext(c).UpdateHook(ctx, h)
if err != nil {
retErr := fmt.Errorf("unable to update hook %s: %w", entry, err)

Expand Down
4 changes: 2 additions & 2 deletions api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func CreateRepo(c *gin.Context) {

// err being nil means we have a record of this repo (dbRepo)
if err == nil {
h, _ = database.FromContext(c).LastHookForRepo(dbRepo)
h, _ = database.FromContext(c).LastHookForRepo(ctx, dbRepo)

// make sure our record of the repo allowed events matches what we send to SCM
// what the dbRepo has should override default events on enable
Expand Down Expand Up @@ -309,7 +309,7 @@ func CreateRepo(c *gin.Context) {
// update initialization hook
h.SetRepoID(r.GetID())
// create first hook for repo in the database
_, err = database.FromContext(c).CreateHook(h)
_, err = database.FromContext(c).CreateHook(ctx, h)
if err != nil {
retErr := fmt.Errorf("unable to create initialization webhook for %s: %w", r.GetFullName(), err)

Expand Down
4 changes: 2 additions & 2 deletions api/repo/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func RepairRepo(c *gin.Context) {
return
}

hook, err := database.FromContext(c).LastHookForRepo(r)
hook, err := database.FromContext(c).LastHookForRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to get last hook for %s: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -108,7 +108,7 @@ func RepairRepo(c *gin.Context) {

hook.SetRepoID(r.GetID())

_, err = database.FromContext(c).CreateHook(hook)
_, err = database.FromContext(c).CreateHook(ctx, hook)
if err != nil {
retErr := fmt.Errorf("unable to create initialization webhook for %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func UpdateRepo(c *gin.Context) {
// if webhook validation is not set or events didn't change, skip webhook update
if c.Value("webhookvalidation").(bool) && eventsChanged {
// grab last hook from repo to fetch the webhook ID
lastHook, err := database.FromContext(c).LastHookForRepo(r)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to retrieve last hook for repo %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/scm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func SyncRepo(c *gin.Context) {
// if we have webhook validation, update the repo hook in the SCM
if c.Value("webhookvalidation").(bool) {
// grab last hook from repo to fetch the webhook ID
lastHook, err := database.FromContext(c).LastHookForRepo(r)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to retrieve last hook for repo %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/scm/sync_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func SyncReposForOrg(c *gin.Context) {
// if we have webhook validation, update the repo hook in the SCM
if c.Value("webhookvalidation").(bool) {
// grab last hook from repo to fetch the webhook ID
lastHook, err := database.FromContext(c).LastHookForRepo(repo)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("unable to retrieve last hook for repo %s: %w", repo.GetFullName(), err)

Expand Down
12 changes: 6 additions & 6 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func PostWebhook(c *gin.Context) {

defer func() {
// send API call to update the webhook
_, err = database.FromContext(c).UpdateHook(h)
_, err = database.FromContext(c).UpdateHook(ctx, h)
if err != nil {
logrus.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err)
}
Expand All @@ -202,7 +202,7 @@ func PostWebhook(c *gin.Context) {
h.SetRepoID(repo.GetID())

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(repo)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", repo.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand All @@ -221,7 +221,7 @@ func PostWebhook(c *gin.Context) {
}

// send API call to create the webhook
h, err = database.FromContext(c).CreateHook(h)
h, err = database.FromContext(c).CreateHook(ctx, h)
if err != nil {
retErr := fmt.Errorf("unable to create webhook %s/%d: %w", repo.GetFullName(), h.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down Expand Up @@ -688,7 +688,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadat

defer func() {
// send API call to update the webhook
_, err := database.FromContext(c).CreateHook(h)
_, err := database.FromContext(c).CreateHook(ctx, h)
if err != nil {
logrus.Errorf("unable to create webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err)
}
Expand Down Expand Up @@ -721,7 +721,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadat
}

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(dbRepo)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -797,7 +797,7 @@ func renameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *
h.SetRepoID(r.GetID())

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(dbR)
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbR)
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down
4 changes: 3 additions & 1 deletion database/hook/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package hook

import (
"context"

"github.com/go-vela/types/constants"
)

// CountHooks gets the count of all hooks from the database.
func (e *engine) CountHooks() (int64, error) {
func (e *engine) CountHooks(ctx context.Context) (int64, error) {
e.logger.Tracef("getting count of all hooks from the database")

// variable to store query results
Expand Down
4 changes: 3 additions & 1 deletion database/hook/count_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package hook

import (
"context"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// CountHooksForRepo gets the count of hooks by repo ID from the database.
func (e *engine) CountHooksForRepo(r *library.Repo) (int64, error) {
func (e *engine) CountHooksForRepo(ctx context.Context, r *library.Repo) (int64, error) {
e.logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
Expand Down
7 changes: 4 additions & 3 deletions database/hook/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package hook

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -48,12 +49,12 @@ func TestHook_Engine_CountHooksForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreateHook(_hookOne)
_, err := _sqlite.CreateHook(context.TODO(), _hookOne)
if err != nil {
t.Errorf("unable to create test repo for sqlite: %v", err)
}

_, err = _sqlite.CreateHook(_hookTwo)
_, err = _sqlite.CreateHook(context.TODO(), _hookTwo)
if err != nil {
t.Errorf("unable to create test hook for sqlite: %v", err)
}
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestHook_Engine_CountHooksForRepo(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountHooksForRepo(_repo)
got, err := test.database.CountHooksForRepo(context.TODO(), _repo)

if test.failure {
if err == nil {
Expand Down
7 changes: 4 additions & 3 deletions database/hook/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package hook

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -41,12 +42,12 @@ func TestHook_Engine_CountHooks(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreateHook(_hookOne)
_, err := _sqlite.CreateHook(context.TODO(), _hookOne)
if err != nil {
t.Errorf("unable to create test hook for sqlite: %v", err)
}

_, err = _sqlite.CreateHook(_hookTwo)
_, err = _sqlite.CreateHook(context.TODO(), _hookTwo)
if err != nil {
t.Errorf("unable to create test hook for sqlite: %v", err)
}
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestHook_Engine_CountHooks(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountHooks()
got, err := test.database.CountHooks(context.TODO())

if test.failure {
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion database/hook/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
package hook

import (
"context"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// CreateHook creates a new hook in the database.
func (e *engine) CreateHook(h *library.Hook) (*library.Hook, error) {
func (e *engine) CreateHook(ctx context.Context, h *library.Hook) (*library.Hook, error) {
e.logger.WithFields(logrus.Fields{
"hook": h.GetNumber(),
}).Tracef("creating hook %d in the database", h.GetNumber())
Expand Down
3 changes: 2 additions & 1 deletion database/hook/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package hook

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -57,7 +58,7 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) RETURNING "id"`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := test.database.CreateHook(_hook)
_, err := test.database.CreateHook(context.TODO(), _hook)

if test.failure {
if err == nil {
Expand Down
Loading