Skip to content

Commit

Permalink
enhance: add context to Users
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Aug 28, 2023
1 parent 6a15df6 commit bdfdff3
Show file tree
Hide file tree
Showing 54 changed files with 251 additions and 128 deletions.
5 changes: 4 additions & 1 deletion api/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ import (
func UpdateUser(c *gin.Context) {
logrus.Info("Admin: updating user in database")

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

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

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

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

Expand Down
8 changes: 5 additions & 3 deletions api/auth/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func GetAuthToken(c *gin.Context) {
var err error

tm := c.MustGet("token-manager").(*token.Manager)
// capture middleware values
ctx := c.Request.Context()

// capture the OAuth state if present
oAuthState := c.Request.FormValue("state")
Expand Down Expand Up @@ -97,7 +99,7 @@ func GetAuthToken(c *gin.Context) {
}

// send API call to capture the user logging in
u, err := database.FromContext(c).GetUserForName(newUser.GetName())
u, err := database.FromContext(c).GetUserForName(ctx, newUser.GetName())
// create a new user account
if len(u.GetName()) == 0 || err != nil {
// create the user account
Expand All @@ -121,7 +123,7 @@ func GetAuthToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to create the user in the database
err = database.FromContext(c).CreateUser(u)
err = database.FromContext(c).CreateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to create user %s: %w", u.GetName(), err)

Expand Down Expand Up @@ -154,7 +156,7 @@ func GetAuthToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to update the user in the database
err = database.FromContext(c).UpdateUser(u)
err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/auth/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Logout(c *gin.Context) {
m := c.MustGet("metadata").(*types.Metadata)
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -75,7 +76,7 @@ func Logout(c *gin.Context) {
u.SetRefreshToken("")

// send API call to update the user in the database
err = database.FromContext(c).UpdateUser(u)
err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

Expand Down
5 changes: 4 additions & 1 deletion api/auth/post_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ import (
// process a user logging in using PAT to Vela from
// the API.
func PostAuthToken(c *gin.Context) {
// capture middleware values
ctx := c.Request.Context()

// attempt to get user from source
u, err := scm.FromContext(c).AuthenticateToken(c.Request)
if err != nil {
Expand All @@ -60,7 +63,7 @@ func PostAuthToken(c *gin.Context) {
}

// check if the user exists
u, err = database.FromContext(c).GetUserForName(u.GetName())
u, err = database.FromContext(c).GetUserForName(ctx, u.GetName())
if err != nil {
retErr := fmt.Errorf("user %s not found", u.GetName())

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

// send API call to capture the repo owner
u, err = database.FromContext(c).GetUser(r.GetUserID())
u, err = database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), 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 @@ -103,7 +103,7 @@ func RestartBuild(c *gin.Context) {
logger.Infof("restarting build %s", entry)

// send API call to capture the repo owner
u, err := database.FromContext(c).GetUser(r.GetUserID())
u, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), 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 @@ -170,7 +170,7 @@ func UpdateBuild(c *gin.Context) {
b.GetStatus() == constants.StatusKilled ||
b.GetStatus() == constants.StatusError {
// send API call to capture the repo owner
u, err := database.FromContext(c).GetUser(r.GetUserID())
u, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
logrus.Errorf("unable to get owner for build %s: %v", entry, err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func recordGauges(c *gin.Context) {
// user_count
if q.UserCount {
// send API call to capture the total number of users
u, err := database.FromContext(c).CountUsers()
u, err := database.FromContext(c).CountUsers(ctx)
if err != nil {
logrus.Errorf("unable to get count of all users: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func GetTemplates(c *gin.Context) {
p := pipeline.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

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

Expand All @@ -107,7 +108,7 @@ func GetTemplates(c *gin.Context) {
}

// send API call to capture the repo owner
user, err := database.FromContext(c).GetUser(r.GetUserID())
user, err := database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to get owner 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 @@ -268,7 +268,7 @@ func UpdateRepo(c *gin.Context) {
// capture admin name for logging
admn := u.GetName()

u, err = database.FromContext(c).GetUser(r.GetUserID())
u, err = database.FromContext(c).GetUser(ctx, r.GetUserID())
if err != nil {
retErr := fmt.Errorf("unable to get repo owner of %s for platform admin webhook update: %w", r.GetFullName(), err)

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

// capture body from API request
input := new(library.User)
Expand All @@ -72,7 +73,7 @@ func CreateUser(c *gin.Context) {
}).Infof("creating new user %s", input.GetName())

// send API call to create the user
err = database.FromContext(c).CreateUser(input)
err = database.FromContext(c).CreateUser(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create user: %w", err)

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

// send API call to capture the created user
user, _ := database.FromContext(c).GetUserForName(input.GetName())
user, _ := database.FromContext(c).GetUserForName(ctx, input.GetName())

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

// update engine logger with API metadata
//
Expand All @@ -65,7 +66,7 @@ func CreateToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to update the user
err = database.FromContext(c).UpdateUser(u)
err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

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

// update engine logger with API metadata
//
Expand All @@ -59,7 +60,7 @@ func DeleteUser(c *gin.Context) {
}).Infof("deleting user %s", user)

// send API call to capture the user
u, err := database.FromContext(c).GetUserForName(user)
u, err := database.FromContext(c).GetUserForName(ctx, user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

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

// send API call to remove the user
err = database.FromContext(c).DeleteUser(u)
err = database.FromContext(c).DeleteUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to delete user %s: %w", u.GetName(), err)

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

// update engine logger with API metadata
//
Expand All @@ -65,7 +66,7 @@ func DeleteToken(c *gin.Context) {
u.SetRefreshToken(rt)

// send API call to update the user
err = database.FromContext(c).UpdateUser(u)
err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

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

// update engine logger with API metadata
//
Expand All @@ -55,7 +56,7 @@ func GetUser(c *gin.Context) {
}).Infof("reading user %s", user)

// send API call to capture the user
u, err := database.FromContext(c).GetUserForName(user)
u, err := database.FromContext(c).GetUserForName(ctx, user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

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

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

// send API call to capture the list of users
users, t, err := database.FromContext(c).ListLiteUsers(page, perPage)
users, t, err := database.FromContext(c).ListLiteUsers(ctx, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get users: %w", err)

Expand Down
7 changes: 4 additions & 3 deletions api/user/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func UpdateUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
user := util.PathParameter(c, "user")
ctx := c.Request.Context()

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

// send API call to capture the user
u, err = database.FromContext(c).GetUserForName(user)
u, err = database.FromContext(c).GetUserForName(ctx, user)
if err != nil {
retErr := fmt.Errorf("unable to get user %s: %w", user, err)

Expand All @@ -108,7 +109,7 @@ func UpdateUser(c *gin.Context) {
}

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

Expand All @@ -118,7 +119,7 @@ func UpdateUser(c *gin.Context) {
}

// send API call to capture the updated user
u, _ = database.FromContext(c).GetUserForName(user)
u, _ = database.FromContext(c).GetUserForName(ctx, user)

c.JSON(http.StatusOK, u)
}
5 changes: 3 additions & 2 deletions api/user/update_current.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
func UpdateCurrentUser(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

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

// send API call to update the user
err = database.FromContext(c).UpdateUser(u)
err = database.FromContext(c).UpdateUser(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to update user %s: %w", u.GetName(), err)

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

// send API call to capture the updated user
u, err = database.FromContext(c).GetUserForName(u.GetName())
u, err = database.FromContext(c).GetUserForName(ctx, u.GetName())
if err != nil {
retErr := fmt.Errorf("unable to get updated user %s: %w", u.GetName(), err)

Expand Down
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func PostWebhook(c *gin.Context) {
// send API call to capture repo owner
logrus.Debugf("capturing owner of repository %s", repo.GetFullName())

u, err := database.FromContext(c).GetUser(repo.GetUserID())
u, err := database.FromContext(c).GetUser(ctx, repo.GetUserID())
if err != nil {
retErr := fmt.Errorf("%s: failed to get owner for %s: %w", baseErr, repo.GetFullName(), err)
util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
}

// send API call to capture the owner for the repo
u, err := database.GetUser(r.GetUserID())
u, err := database.GetUser(ctx, r.GetUserID())
if err != nil {
return fmt.Errorf("unable to get owner for repo %s: %w", r.GetFullName(), err)
}
Expand Down
Loading

0 comments on commit bdfdff3

Please sign in to comment.