Skip to content

Commit

Permalink
enhance: add context to scm (#969)
Browse files Browse the repository at this point in the history
* enhance: add context to scm

* chore: replace TODO with ctx
  • Loading branch information
plyr4 committed Sep 19, 2023
1 parent 76971f6 commit 7ee3ae3
Show file tree
Hide file tree
Showing 44 changed files with 229 additions and 217 deletions.
4 changes: 2 additions & 2 deletions api/auth/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func GetAuthToken(c *gin.Context) {
code := c.Request.FormValue("code")
if len(code) == 0 {
// start the initial OAuth workflow
oAuthState, err = scm.FromContext(c).Login(c.Writer, c.Request)
oAuthState, err = scm.FromContext(c).Login(ctx, c.Writer, c.Request)
if err != nil {
retErr := fmt.Errorf("unable to login user: %w", err)

Expand All @@ -83,7 +83,7 @@ func GetAuthToken(c *gin.Context) {
}

// complete the OAuth workflow and authenticates the user
newUser, err := scm.FromContext(c).Authenticate(c.Writer, c.Request, oAuthState)
newUser, err := scm.FromContext(c).Authenticate(ctx, c.Writer, c.Request, oAuthState)
if err != nil {
retErr := fmt.Errorf("unable to authenticate user: %w", err)

Expand Down
2 changes: 1 addition & 1 deletion api/auth/post_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func PostAuthToken(c *gin.Context) {
ctx := c.Request.Context()

// attempt to get user from source
u, err := scm.FromContext(c).AuthenticateToken(c.Request)
u, err := scm.FromContext(c).AuthenticateToken(ctx, c.Request)
if err != nil {
retErr := fmt.Errorf("unable to authenticate user: %w", err)

Expand Down
10 changes: 5 additions & 5 deletions api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func CreateBuild(c *gin.Context) {
if !strings.EqualFold(input.GetEvent(), constants.EventComment) &&
!strings.EqualFold(input.GetEvent(), constants.EventPull) {
// send API call to capture list of files changed for the commit
files, err = scm.FromContext(c).Changeset(u, r, input.GetCommit())
files, err = scm.FromContext(c).Changeset(ctx, u, r, input.GetCommit())
if err != nil {
retErr := fmt.Errorf("unable to create new build: failed to get changeset for %s: %w", r.GetFullName(), err)

Expand All @@ -210,7 +210,7 @@ func CreateBuild(c *gin.Context) {
}

// send API call to capture list of files changed for the pull request
files, err = scm.FromContext(c).ChangesetPR(u, r, number)
files, err = scm.FromContext(c).ChangesetPR(ctx, u, r, number)
if err != nil {
retErr := fmt.Errorf("unable to create new build: failed to get changeset for %s: %w", r.GetFullName(), err)

Expand All @@ -235,7 +235,7 @@ func CreateBuild(c *gin.Context) {
pipeline, err = database.FromContext(c).GetPipelineForRepo(ctx, input.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet
// send API call to capture the pipeline configuration file
config, err = scm.FromContext(c).ConfigBackoff(u, r, input.GetCommit())
config, err = scm.FromContext(c).ConfigBackoff(ctx, u, r, input.GetCommit())
if err != nil {
retErr := fmt.Errorf("unable to create new build: failed to get pipeline configuration for %s: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -289,7 +289,7 @@ func CreateBuild(c *gin.Context) {
input.SetStatus(constants.StatusSuccess)

// send API call to set the status on the commit
err = scm.FromContext(c).Status(u, input, r.GetOrg(), r.GetName())
err = scm.FromContext(c).Status(ctx, u, input, r.GetOrg(), r.GetName())
if err != nil {
logger.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), input.GetNumber(), err)
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func CreateBuild(c *gin.Context) {
c.JSON(http.StatusCreated, input)

// send API call to set the status on the commit
err = scm.FromContext(c).Status(u, input, r.GetOrg(), r.GetName())
err = scm.FromContext(c).Status(ctx, u, input, r.GetOrg(), r.GetName())
if err != nil {
logger.Errorf("unable to set commit status for build %s/%d: %v", r.GetFullName(), input.GetNumber(), 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 @@ -101,7 +101,7 @@ func GetBuildByID(c *gin.Context) {

// Capture user access from SCM. We do this in order to ensure user has access and is not
// just retrieving any build using a random id number.
perm, err := scm.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName())
perm, err := scm.FromContext(c).RepoAccess(ctx, u, u.GetToken(), r.GetOrg(), r.GetName())
if err != nil {
logrus.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName())
}
Expand Down
2 changes: 1 addition & 1 deletion api/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func ListBuildsForOrg(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// See if the user is an org admin to bypass individual permission checks
perm, err := scm.FromContext(c).OrgAccess(u, o)
perm, err := scm.FromContext(c).OrgAccess(ctx, u, o)
if err != nil {
logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), o)
}
Expand Down
10 changes: 5 additions & 5 deletions api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func RestartBuild(c *gin.Context) {
if !strings.EqualFold(b.GetEvent(), constants.EventComment) &&
!strings.EqualFold(b.GetEvent(), constants.EventPull) {
// send API call to capture list of files changed for the commit
files, err = scm.FromContext(c).Changeset(u, r, b.GetCommit())
files, err = scm.FromContext(c).Changeset(ctx, u, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("unable to restart build: failed to get changeset for %s: %w", r.GetFullName(), err)

Expand All @@ -200,7 +200,7 @@ func RestartBuild(c *gin.Context) {
}

// send API call to capture list of files changed for the pull request
files, err = scm.FromContext(c).ChangesetPR(u, r, number)
files, err = scm.FromContext(c).ChangesetPR(ctx, u, r, number)
if err != nil {
retErr := fmt.Errorf("unable to restart build: failed to get changeset for %s: %w", r.GetFullName(), err)

Expand All @@ -226,7 +226,7 @@ func RestartBuild(c *gin.Context) {
pipeline, err = database.FromContext(c).GetPipelineForRepo(ctx, b.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet (before pipeline support was added)
// send API call to capture the pipeline configuration file
config, err = scm.FromContext(c).ConfigBackoff(u, r, b.GetCommit())
config, err = scm.FromContext(c).ConfigBackoff(ctx, u, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("unable to get pipeline configuration for %s: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -281,7 +281,7 @@ func RestartBuild(c *gin.Context) {
b.SetStatus(constants.StatusSkipped)

// send API call to set the status on the commit
err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName())
err = scm.FromContext(c).Status(ctx, u, b, r.GetOrg(), r.GetName())
if err != nil {
logrus.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err)
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func RestartBuild(c *gin.Context) {
c.JSON(http.StatusCreated, b)

// send API call to set the status on the commit
err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName())
err = scm.FromContext(c).Status(ctx, u, b, r.GetOrg(), r.GetName())
if err != nil {
logger.Errorf("unable to set commit status for build %s: %v", entry, err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/build/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func UpdateBuild(c *gin.Context) {
}

// send API call to set the status on the commit
err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName())
err = scm.FromContext(c).Status(ctx, u, b, r.GetOrg(), r.GetName())
if err != nil {
logrus.Errorf("unable to set commit status for build %s: %v", entry, err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/deployment/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func CreateDeployment(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 @@ -99,7 +100,7 @@ func CreateDeployment(c *gin.Context) {
}

// send API call to create the deployment
err = scm.FromContext(c).CreateDeployment(u, r, input)
err = scm.FromContext(c).CreateDeployment(ctx, u, r, input)
if err != nil {
retErr := fmt.Errorf("unable to create new deployment for %s: %w", r.GetFullName(), err)

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

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

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

// send API call to capture the deployment
d, err := scm.FromContext(c).GetDeployment(u, r, int64(number))
d, err := scm.FromContext(c).GetDeployment(ctx, u, r, int64(number))
if err != nil {
retErr := fmt.Errorf("unable to get deployment %s: %w", entry, err)

Expand Down
4 changes: 2 additions & 2 deletions api/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func ListDeployments(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the total number of deployments for the repo
t, err := scm.FromContext(c).GetDeploymentCount(u, r)
t, err := scm.FromContext(c).GetDeploymentCount(ctx, u, r)
if err != nil {
retErr := fmt.Errorf("unable to get deployment count for %s: %w", r.GetFullName(), err)

Expand All @@ -127,7 +127,7 @@ func ListDeployments(c *gin.Context) {
}

// send API call to capture the list of deployments for the repo
d, err := scm.FromContext(c).GetDeploymentList(u, r, page, perPage)
d, err := scm.FromContext(c).GetDeploymentList(ctx, u, r, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get deployments for %s: %w", r.GetFullName(), err)

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

// retrieve link to template file from github
link, err := scm.FromContext(c).GetHTMLURL(user, src.Org, src.Repo, src.Name, src.Ref)
link, err := scm.FromContext(c).GetHTMLURL(ctx, user, src.Org, src.Repo, src.Name, src.Ref)
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("%s: unable to get html url for %s/%s/%s/@%s: %w", baseErr, src.Org, src.Repo, src.Name, src.Ref, 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 @@ -99,7 +99,7 @@ func CreateRepo(c *gin.Context) {
}).Infof("creating new repo %s", input.GetFullName())

// get repo information from the source
r, err := scm.FromContext(c).GetRepo(u, input)
r, err := scm.FromContext(c).GetRepo(ctx, u, input)
if err != nil {
retErr := fmt.Errorf("unable to retrieve repo info for %s from source: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -255,7 +255,7 @@ func CreateRepo(c *gin.Context) {
// check if we should create the webhook
if c.Value("webhookvalidation").(bool) {
// send API call to create the webhook
h, _, err = scm.FromContext(c).Enable(u, r, h)
h, _, err = scm.FromContext(c).Enable(ctx, u, r, h)
if err != nil {
retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/repo/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func DeleteRepo(c *gin.Context) {
}).Infof("deleting repo %s", r.GetFullName())

// send API call to remove the webhook
err := scm.FromContext(c).Disable(u, r.GetOrg(), r.GetName())
err := scm.FromContext(c).Disable(ctx, u, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to delete webhook for %s: %w", r.GetFullName(), err)

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

// See if the user is an org admin to bypass individual permission checks
perm, err := scm.FromContext(c).OrgAccess(u, o)
perm, err := scm.FromContext(c).OrgAccess(ctx, u, o)
if err != nil {
logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), o)
}
Expand Down
4 changes: 2 additions & 2 deletions api/repo/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func RepairRepo(c *gin.Context) {
// check if we should create the webhook
if c.Value("webhookvalidation").(bool) {
// send API call to remove the webhook
err := scm.FromContext(c).Disable(u, r.GetOrg(), r.GetName())
err := scm.FromContext(c).Disable(ctx, u, r.GetOrg(), r.GetName())
if err != nil {
retErr := fmt.Errorf("unable to delete webhook for %s: %w", r.GetFullName(), err)

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

// send API call to create the webhook
hook, _, err = scm.FromContext(c).Enable(u, r, hook)
hook, _, err = scm.FromContext(c).Enable(ctx, u, r, hook)
if err != nil {
retErr := fmt.Errorf("unable to create 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 @@ -285,7 +285,7 @@ func UpdateRepo(c *gin.Context) {
}).Infof("platform admin %s updating repo webhook events for repo %s", admn, r.GetFullName())
}
// update webhook with new events
_, err = scm.FromContext(c).Update(u, r, lastHook.GetWebhookID())
_, err = scm.FromContext(c).Update(ctx, u, r, lastHook.GetWebhookID())
if err != nil {
retErr := fmt.Errorf("unable to update repo webhook for %s: %w", r.GetFullName(), err)

Expand Down
6 changes: 3 additions & 3 deletions api/scm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func SyncRepo(c *gin.Context) {
logger.Infof("syncing repo %s", r.GetFullName())

// retrieve repo from source code manager service
_, err := scm.FromContext(c).GetRepo(u, r)
_, err := scm.FromContext(c).GetRepo(ctx, u, r)

// if there is an error retrieving repo, we know it is deleted: set to inactive
if err != nil {
Expand All @@ -98,7 +98,7 @@ func SyncRepo(c *gin.Context) {

// verify the user is an admin of the repo
// we cannot use our normal permissions check due to the possibility the repo was deleted
perm, err := scm.FromContext(c).RepoAccess(u, u.GetToken(), o, r.GetName())
perm, err := scm.FromContext(c).RepoAccess(ctx, u, u.GetToken(), o, r.GetName())
if err != nil {
logger.Errorf("unable to get user %s access level for org %s", u.GetName(), o)
}
Expand All @@ -125,7 +125,7 @@ func SyncRepo(c *gin.Context) {
}

// update webhook
webhookExists, err := scm.FromContext(c).Update(u, r, lastHook.GetWebhookID())
webhookExists, err := scm.FromContext(c).Update(ctx, u, r, lastHook.GetWebhookID())
if err != nil {

// if webhook has been manually deleted from GitHub,
Expand Down
6 changes: 3 additions & 3 deletions api/scm/sync_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func SyncReposForOrg(c *gin.Context) {
logger.Infof("syncing repos for org %s", o)

// see if the user is an org admin
perm, err := scm.FromContext(c).OrgAccess(u, o)
perm, err := scm.FromContext(c).OrgAccess(ctx, u, o)
if err != nil {
logger.Errorf("unable to get user %s access level for org %s", u.GetName(), o)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func SyncReposForOrg(c *gin.Context) {

// iterate through captured repos and check if they are in GitHub
for _, repo := range repos {
_, err := scm.FromContext(c).GetRepo(u, repo)
_, err := scm.FromContext(c).GetRepo(ctx, u, repo)
// if repo cannot be captured from GitHub, set to inactive in database
if err != nil {
repo.SetActive(false)
Expand Down Expand Up @@ -138,7 +138,7 @@ func SyncReposForOrg(c *gin.Context) {
}

// update webhook
webhookExists, err := scm.FromContext(c).Update(u, repo, lastHook.GetWebhookID())
webhookExists, err := scm.FromContext(c).Update(ctx, u, repo, lastHook.GetWebhookID())
if err != nil {

// if webhook has been manually deleted from GitHub,
Expand Down
4 changes: 2 additions & 2 deletions api/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func CreateSecret(c *gin.Context) {
// but Org/Repo != org/repo in Vela. So this check ensures that
// what a user inputs matches the casing we expect in Vela since
// the SCM will have the source of truth for casing.
org, err := scm.FromContext(c).GetOrgName(u, o)
org, err := scm.FromContext(c).GetOrgName(ctx, u, o)
if err != nil {
retErr := fmt.Errorf("unable to retrieve organization %s", o)

Expand All @@ -141,7 +141,7 @@ func CreateSecret(c *gin.Context) {
// retrieve org and repo name from SCM
//
// same story as org secret. SCM has accurate casing.
scmOrg, scmRepo, err := scm.FromContext(c).GetOrgAndRepoName(u, o, n)
scmOrg, scmRepo, err := scm.FromContext(c).GetOrgAndRepoName(ctx, u, o, n)
if err != nil {
retErr := fmt.Errorf("unable to retrieve repository %s/%s", o, n)

Expand Down
2 changes: 1 addition & 1 deletion api/secret/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func ListSecrets(c *gin.Context) {
if t == constants.SecretShared && n == "*" {
var err error

teams, err = scm.FromContext(c).ListUsersTeamsForOrg(u, o)
teams, err = scm.FromContext(c).ListUsersTeamsForOrg(ctx, u, o)
if err != nil {
retErr := fmt.Errorf("unable to list users %s teams for org %s: %w", u.GetName(), o, err)

Expand Down
2 changes: 1 addition & 1 deletion api/user/get_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GetSourceRepos(c *gin.Context) {
output := make(map[string][]library.Repo)

// send API call to capture the list of repos for the user
srcRepos, err := scm.FromContext(c).ListUserRepos(u)
srcRepos, err := scm.FromContext(c).ListUserRepos(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to get SCM repos for user %s: %w", u.GetName(), err)

Expand Down
Loading

0 comments on commit 7ee3ae3

Please sign in to comment.