Skip to content

Commit

Permalink
enhance: add GetUserID to scm interface
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Jun 3, 2024
1 parent 32861c6 commit a8958cf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
19 changes: 14 additions & 5 deletions api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func RestartBuild(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
scm := scm.FromContext(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber())
Expand All @@ -114,10 +115,18 @@ func RestartBuild(c *gin.Context) {

// set sender to the user who initiated the restart and
b.SetSender(cl.Subject)
// todo: sender_scm_id:
// vela username is the claims subject
// - (a) auth with repo token and convert username to scm id
// - (b) attach scm id to claims

// fetch user scm id
senderID, err := scm.GetUserID(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to get user scm id for %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

b.SetSenderSCMID(senderID)

// parent to the previous build
b.SetParent(b.GetNumber())
Expand All @@ -138,7 +147,7 @@ func RestartBuild(c *gin.Context) {
c,
config,
database.FromContext(c),
scm.FromContext(c),
scm,
compiler.FromContext(c),
queue.FromContext(c),
)
Expand Down
17 changes: 14 additions & 3 deletions cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,20 @@ func processSchedule(ctx context.Context, s *api.Schedule, settings *settings.Pl
b.SetRef(fmt.Sprintf("refs/heads/%s", b.GetBranch()))
b.SetRepo(r)
b.SetSender(s.GetUpdatedBy())
// todo: sender_scm_id:
// - (a) auth with repo token and convert username to scm id
// - (b) attach scm user id to schedule updated_by_id (lol)

// send API call to capture the user for the schedule trigger
u, err := database.GetUserForName(ctx, s.GetUpdatedBy())
if err != nil {
return fmt.Errorf("unable to get user for name %s: %w", s.GetUpdatedBy(), err)
}

// fetch user scm id
senderID, err := scm.GetUserID(ctx, u)
if err != nil {
return fmt.Errorf("unable to get user scm id for %s: %w", u.GetName(), err)
}

b.SetSenderSCMID(senderID)

b.SetSource(fmt.Sprintf("%s/tree/%s", url, b.GetBranch()))
b.SetStatus(constants.StatusPending)
Expand Down
29 changes: 29 additions & 0 deletions scm/github/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: Apache-2.0

package github

import (
"context"
"fmt"

api "github.com/go-vela/server/api/types"

Check failure on line 9 in scm/github/user.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/user.go#L9

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
Raw output
scm/github/user.go:9: File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
	api "github.com/go-vela/server/api/types"
"github.com/sirupsen/logrus"

Check failure on line 10 in scm/github/user.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] scm/github/user.go#L10

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
Raw output
scm/github/user.go:10: File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
	"github.com/sirupsen/logrus"
)

// GetUserID captures the user's scm id.
func (c *client) GetUserID(ctx context.Context, u *api.User) (string, error) {
c.Logger.WithFields(logrus.Fields{
"user": u.GetName(),
}).Tracef("capturing scm id for %s", u.GetName())

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// send API call to capture user
user, _, err := client.Users.Get(ctx, u.GetName())
if err != nil {
return "", err
}

return fmt.Sprint(user.GetID()), nil
}
6 changes: 6 additions & 0 deletions scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ type Service interface {
// the OAuth workflow for the session.
Login(context.Context, http.ResponseWriter, *http.Request) (string, error)

// User SCM Interface Functions

// GetUserID defines a function that captures
// the scm user attached to a Vela user.
GetUserID(context.Context, *api.User) (string, error)

// Access SCM Interface Functions

// OrgAccess defines a function that captures
Expand Down

0 comments on commit a8958cf

Please sign in to comment.