diff --git a/api/build/restart.go b/api/build/restart.go index 3a05ad93b..898c2e75f 100644 --- a/api/build/restart.go +++ b/api/build/restart.go @@ -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) diff --git a/api/webhook/post.go b/api/webhook/post.go index 20600d525..c514943b9 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -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()) diff --git a/cmd/vela-server/schedule.go b/cmd/vela-server/schedule.go index 6a38998e6..e7e0df759 100644 --- a/cmd/vela-server/schedule.go +++ b/cmd/vela-server/schedule.go @@ -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) diff --git a/scm/github/user.go b/scm/github/user.go index 848684521..e50cc19ff 100644 --- a/scm/github/user.go +++ b/scm/github/user.go @@ -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 } diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 241f7e6d2..3092ae63e 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -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: @@ -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(), @@ -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 diff --git a/scm/service.go b/scm/service.go index d7a21a78e..88bb5ecf3 100644 --- a/scm/service.go +++ b/scm/service.go @@ -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