Skip to content

Commit

Permalink
Merge branch 'main' into feat/queue-signing
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Aug 17, 2023
2 parents 4786391 + 26aa0b6 commit 448a95d
Show file tree
Hide file tree
Showing 58 changed files with 361 additions and 189 deletions.
5 changes: 4 additions & 1 deletion api/admin/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import (
func UpdateRepo(c *gin.Context) {
logrus.Info("Admin: updating repo in database")

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

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

Expand All @@ -66,7 +69,7 @@ func UpdateRepo(c *gin.Context) {
}

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

Expand Down
2 changes: 1 addition & 1 deletion api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func CreateBuild(c *gin.Context) {
}

// send API call to update repo for ensuring counter is incremented
r, err = database.FromContext(c).UpdateRepo(r)
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to create new build: failed to update repo %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/build/get_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func GetBuildByID(c *gin.Context) {
}

// Get repo from database using repo ID field from build
r, err = database.FromContext(c).GetRepo(b.GetRepoID())
r, err = database.FromContext(c).GetRepo(ctx, b.GetRepoID())
if err != nil {
retErr := fmt.Errorf("unable to get repo: %w", err)

Expand Down
2 changes: 1 addition & 1 deletion api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func RestartBuild(c *gin.Context) {
}

// send API call to update repo for ensuring counter is incremented
r, err = database.FromContext(c).UpdateRepo(r)
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to restart build: failed to update repo %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
2 changes: 1 addition & 1 deletion api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func recordGauges(c *gin.Context) {
// repo_count
if q.RepoCount {
// send API call to capture the total number of repos
r, err := database.FromContext(c).CountRepos()
r, err := database.FromContext(c).CountRepos(ctx)
if err != nil {
logrus.Errorf("unable to get count of all repos: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/repo/chown.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func ChownRepo(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 @@ -68,7 +69,7 @@ func ChownRepo(c *gin.Context) {
r.SetUserID(u.GetID())

// send API call to update the repo
_, err := database.FromContext(c).UpdateRepo(r)
_, err := database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to change owner of repo %s to %s: %w", r.GetFullName(), u.GetName(), err)

Expand Down
7 changes: 4 additions & 3 deletions api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func CreateRepo(c *gin.Context) {
defaultTimeout := c.Value("defaultTimeout").(int64)
maxBuildLimit := c.Value("maxBuildLimit").(int64)
defaultRepoEvents := c.Value("defaultRepoEvents").([]string)
ctx := c.Request.Context()

// capture body from API request
input := new(library.Repo)
Expand Down Expand Up @@ -221,7 +222,7 @@ func CreateRepo(c *gin.Context) {
}

// send API call to capture the repo from the database
dbRepo, err := database.FromContext(c).GetRepoForOrg(r.GetOrg(), r.GetName())
dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
if err == nil && dbRepo.GetActive() {
retErr := fmt.Errorf("unable to activate repo: %s is already active", r.GetFullName())

Expand Down Expand Up @@ -283,7 +284,7 @@ func CreateRepo(c *gin.Context) {
dbRepo.SetActive(true)

// send API call to update the repo
r, err = database.FromContext(c).UpdateRepo(dbRepo)
r, err = database.FromContext(c).UpdateRepo(ctx, dbRepo)
if err != nil {
retErr := fmt.Errorf("unable to set repo %s to active: %w", dbRepo.GetFullName(), err)

Expand All @@ -293,7 +294,7 @@ func CreateRepo(c *gin.Context) {
}
} else {
// send API call to create the repo
r, err = database.FromContext(c).CreateRepo(r)
r, err = database.FromContext(c).CreateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to create new repo %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/repo/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func DeleteRepo(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 @@ -88,7 +89,7 @@ func DeleteRepo(c *gin.Context) {
// Mark the repo as inactive
r.SetActive(false)

_, err = database.FromContext(c).UpdateRepo(r)
_, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to set repo %s to inactive: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/repo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
func ListRepos(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -108,7 +109,7 @@ func ListRepos(c *gin.Context) {
}

// send API call to capture the list of repos for the user
r, t, err := database.FromContext(c).ListReposForUser(u, sortBy, filters, page, perPage)
r, t, err := database.FromContext(c).ListReposForUser(ctx, u, sortBy, filters, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get repos for user %s: %w", u.GetName(), err)

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

// update engine logger with API metadata
//
Expand Down Expand Up @@ -141,7 +142,7 @@ func ListReposForOrg(c *gin.Context) {
}

// send API call to capture the list of repos for the org
r, t, err := database.FromContext(c).ListReposForOrg(o, sortBy, filters, page, perPage)
r, t, err := database.FromContext(c).ListReposForOrg(ctx, o, sortBy, filters, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get repos for org %s: %w", o, err)

Expand Down
3 changes: 2 additions & 1 deletion api/repo/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func RepairRepo(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 @@ -122,7 +123,7 @@ func RepairRepo(c *gin.Context) {
r.SetActive(true)

// send API call to update the repo
_, err := database.FromContext(c).UpdateRepo(r)
_, err := database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to set repo %s to active: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/repo/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func UpdateRepo(c *gin.Context) {
r := repo.Retrieve(c)
u := user.Retrieve(c)
maxBuildLimit := c.Value("maxBuildLimit").(int64)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -295,7 +296,7 @@ func UpdateRepo(c *gin.Context) {
}

// send API call to update the repo
r, err = database.FromContext(c).UpdateRepo(r)
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/scm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func SyncRepo(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 @@ -80,7 +81,7 @@ func SyncRepo(c *gin.Context) {
r.SetActive(false)

// update repo in database
_, err := database.FromContext(c).UpdateRepo(r)
_, err := database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)

Expand Down
7 changes: 4 additions & 3 deletions api/scm/sync_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func SyncReposForOrg(c *gin.Context) {
// capture middleware values
o := org.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -79,7 +80,7 @@ func SyncReposForOrg(c *gin.Context) {
}

// send API call to capture the total number of repos for the org
t, err := database.FromContext(c).CountReposForOrg(o, map[string]interface{}{})
t, err := database.FromContext(c).CountReposForOrg(ctx, o, map[string]interface{}{})
if err != nil {
retErr := fmt.Errorf("unable to get repo count for org %s: %w", o, err)

Expand All @@ -92,7 +93,7 @@ func SyncReposForOrg(c *gin.Context) {
page := 0
// capture all repos belonging to a certain org in database
for orgRepos := int64(0); orgRepos < t; orgRepos += 100 {
r, _, err := database.FromContext(c).ListReposForOrg(o, "name", map[string]interface{}{}, page, 100)
r, _, err := database.FromContext(c).ListReposForOrg(ctx, o, "name", map[string]interface{}{}, page, 100)
if err != nil {
retErr := fmt.Errorf("unable to get repo count for org %s: %w", o, err)

Expand All @@ -113,7 +114,7 @@ func SyncReposForOrg(c *gin.Context) {
if err != nil {
repo.SetActive(false)

_, err := database.FromContext(c).UpdateRepo(repo)
_, err := database.FromContext(c).UpdateRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)

Expand Down
3 changes: 2 additions & 1 deletion api/user/get_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
func GetSourceRepos(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -88,7 +89,7 @@ func GetSourceRepos(c *gin.Context) {

for page > 0 {
// send API call to capture the list of repos for the org
dbReposPart, _, err := database.FromContext(c).ListReposForOrg(org, "name", filters, page, 100)
dbReposPart, _, err := database.FromContext(c).ListReposForOrg(ctx, org, "name", filters, page, 100)
if err != nil {
retErr := fmt.Errorf("unable to get repos for org %s: %w", org, err)

Expand Down
26 changes: 12 additions & 14 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package webhook

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -135,7 +136,7 @@ func PostWebhook(c *gin.Context) {

// if event is repository event, handle separately and return
if strings.EqualFold(h.GetEvent(), constants.EventRepository) {
r, err = handleRepositoryEvent(c, m, h, r)
r, err = handleRepositoryEvent(ctx, c, m, h, r)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)
return
Expand Down Expand Up @@ -184,7 +185,7 @@ func PostWebhook(c *gin.Context) {
}()

// send API call to capture parsed repo from webhook
repo, err := database.FromContext(c).GetRepoForOrg(r.GetOrg(), r.GetName())
repo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down Expand Up @@ -445,7 +446,7 @@ func PostWebhook(c *gin.Context) {
}

// send API call to capture repo for the counter (grabbing repo again to ensure counter is correct)
repo, err = database.FromContext(c).GetRepoForOrg(repo.GetOrg(), repo.GetName())
repo, err = database.FromContext(c).GetRepoForOrg(ctx, repo.GetOrg(), repo.GetName())
if err != nil {
retErr := fmt.Errorf("%s: unable to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand Down Expand Up @@ -621,7 +622,7 @@ func PostWebhook(c *gin.Context) {
} // end of retry loop

// send API call to update repo for ensuring counter is incremented
repo, err = database.FromContext(c).UpdateRepo(repo)
repo, err = database.FromContext(c).UpdateRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("%s: failed to update repo %s: %w", baseErr, repo.GetFullName(), err)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down Expand Up @@ -689,7 +690,7 @@ func PostWebhook(c *gin.Context) {
)
}

func handleRepositoryEvent(c *gin.Context, m *types.Metadata, h *library.Hook, r *library.Repo) (*library.Repo, error) {
func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadata, h *library.Hook, r *library.Repo) (*library.Repo, error) {
logrus.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName())

defer func() {
Expand All @@ -703,7 +704,7 @@ func handleRepositoryEvent(c *gin.Context, m *types.Metadata, h *library.Hook, r
switch h.GetEventAction() {
// if action is rename, go through rename routine
case constants.ActionRenamed, constants.ActionTransferred:
r, err := renameRepository(h, r, c, m)
r, err := renameRepository(ctx, h, r, c, m)
if err != nil {
h.SetStatus(constants.StatusFailure)
h.SetError(err.Error())
Expand All @@ -716,7 +717,7 @@ func handleRepositoryEvent(c *gin.Context, m *types.Metadata, h *library.Hook, r
case "archived", "unarchived", constants.ActionEdited:
logrus.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName())
// send call to get repository from database
dbRepo, err := database.FromContext(c).GetRepoForOrg(r.GetOrg(), r.GetName())
dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand Down Expand Up @@ -760,7 +761,7 @@ func handleRepositoryEvent(c *gin.Context, m *types.Metadata, h *library.Hook, r
}

// update repo object in the database after applying edits
dbRepo, err = database.FromContext(c).UpdateRepo(dbRepo)
dbRepo, err = database.FromContext(c).UpdateRepo(ctx, dbRepo)
if err != nil {
retErr := fmt.Errorf("%s: failed to update repo %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -781,17 +782,14 @@ func handleRepositoryEvent(c *gin.Context, m *types.Metadata, h *library.Hook, r
// queries the database for the repo that matches that name and org, and updates
// that repo to its new name in order to preserve it. It also updates the secrets
// associated with that repo as well as build links for the UI.
func renameRepository(h *library.Hook, r *library.Repo, c *gin.Context, m *types.Metadata) (*library.Repo, error) {
func renameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *gin.Context, m *types.Metadata) (*library.Repo, error) {
logrus.Infof("renaming repository from %s to %s", r.GetPreviousName(), r.GetName())

// capture context from gin
ctx := c.Request.Context()

// get the old name of the repo
prevOrg, prevRepo := util.SplitFullName(r.GetPreviousName())

// get the repo from the database that matches the old name
dbR, err := database.FromContext(c).GetRepoForOrg(prevOrg, prevRepo)
dbR, err := database.FromContext(c).GetRepoForOrg(ctx, prevOrg, prevRepo)
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s/%s from database", baseErr, prevOrg, prevRepo)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down Expand Up @@ -896,7 +894,7 @@ func renameRepository(h *library.Hook, r *library.Repo, c *gin.Context, m *types
dbR.SetPreviousName(r.GetPreviousName())

// update the repo in the database
dbR, err = database.FromContext(c).UpdateRepo(dbR)
dbR, err = database.FromContext(c).UpdateRepo(ctx, dbR)
if err != nil {
retErr := fmt.Errorf("%s: failed to update repo %s/%s in database", baseErr, prevOrg, prevRepo)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
6 changes: 3 additions & 3 deletions cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func processSchedules(ctx context.Context, start time.Time, compiler compiler.En
//nolint:funlen // ignore function length and number of statements
func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service) error {
// send API call to capture the repo for the schedule
r, err := database.GetRepo(s.GetRepoID())
r, err := database.GetRepo(ctx, s.GetRepoID())
if err != nil {
return fmt.Errorf("unable to fetch repo: %w", err)
}
Expand Down Expand Up @@ -249,7 +249,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
}

// send API call to capture repo for the counter (grabbing repo again to ensure counter is correct)
r, err = database.GetRepoForOrg(r.GetOrg(), r.GetName())
r, err = database.GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
if err != nil {
err = fmt.Errorf("unable to get repo %s: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -374,7 +374,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
} // end of retry loop

// send API call to update repo for ensuring counter is incremented
r, err = database.UpdateRepo(r)
r, err = database.UpdateRepo(ctx, r)
if err != nil {
return fmt.Errorf("unable to update repo %s: %w", r.GetFullName(), err)
}
Expand Down
Loading

0 comments on commit 448a95d

Please sign in to comment.