Skip to content

Commit

Permalink
fix: apply missing scm id using scm client lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Jun 3, 2024
1 parent a8958cf commit 242ebf9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
6 changes: 3 additions & 3 deletions api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ func RestartBuild(c *gin.Context) {
// set sender to the user who initiated the restart and
b.SetSender(cl.Subject)

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

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

Expand Down
17 changes: 17 additions & 0 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ func PostWebhook(c *gin.Context) {
return
}

// attach a sender SCM id if the webhook payload from the SCM has no sender id
if len(b.GetSenderSCMID()) == 0 || b.GetSenderSCMID() == "0" {
// fetch scm user id for pusher
senderID, err := scm.FromContext(c).GetUserID(ctx, b.GetSender(), repo.GetOwner().GetToken())
if err != nil {
retErr := fmt.Errorf("unable to assign sender SCM id: %w", err)
util.HandleError(c, http.StatusBadRequest, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}

b.SetSenderSCMID(senderID)
}

// set the RepoID fields
b.SetRepo(repo)
h.SetRepoID(repo.GetID())
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 @@ -186,10 +186,10 @@ func processSchedule(ctx context.Context, s *api.Schedule, settings *settings.Pl
return fmt.Errorf("unable to get user for name %s: %w", s.GetUpdatedBy(), err)
}

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

b.SetSenderSCMID(senderID)
Expand Down
11 changes: 5 additions & 6 deletions scm/github/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import (
"context"
"fmt"

api "github.com/go-vela/server/api/types"
"github.com/sirupsen/logrus"
)

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

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

// send API call to capture user
user, _, err := client.Users.Get(ctx, u.GetName())
user, _, err := client.Users.Get(ctx, name)
if err != nil {
return "", err
}
Expand Down
9 changes: 2 additions & 7 deletions scm/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*in
// process the event from the webhook
switch event := event.(type) {
case *github.PushEvent:
return c.processPushEvent(h, event)
return c.processPushEvent(ctx, h, event)
case *github.PullRequestEvent:
return c.processPREvent(h, event)
case *github.DeploymentEvent:
Expand Down Expand Up @@ -128,7 +128,7 @@ func (c *client) RedeliverWebhook(ctx context.Context, u *api.User, r *api.Repo,
}

// processPushEvent is a helper function to process the push event.
func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (*internal.Webhook, error) {
func (c *client) processPushEvent(ctx context.Context, h *library.Hook, payload *github.PushEvent) (*internal.Webhook, error) {
c.Logger.WithFields(logrus.Fields{
"org": payload.GetRepo().GetOwner().GetLogin(),
"repo": payload.GetRepo().GetName(),
Expand Down Expand Up @@ -178,11 +178,6 @@ func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (*
// ensure the build sender is set
if len(b.GetSender()) == 0 {
b.SetSender(payload.GetPusher().GetName())
// todo: sender_scm_id:
// commit_author/pusher has no ID attached in gh api payload
// - (a) auth with repo token and convert username to scm id
// - (b) error out when payload didnt have a sender
// - (c) dont set an scm id...
}

// ensure the build email is set
Expand Down
4 changes: 2 additions & 2 deletions scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type Service interface {
// 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)
// the scm user id attached to the username.
GetUserID(context.Context, string, string) (string, error)

// Access SCM Interface Functions

Expand Down

0 comments on commit 242ebf9

Please sign in to comment.