diff --git a/api/build/restart.go b/api/build/restart.go index 5d42a9737..3a05ad93b 100644 --- a/api/build/restart.go +++ b/api/build/restart.go @@ -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()) @@ -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()) @@ -138,7 +147,7 @@ func RestartBuild(c *gin.Context) { c, config, database.FromContext(c), - scm.FromContext(c), + scm, compiler.FromContext(c), queue.FromContext(c), ) diff --git a/cmd/vela-server/schedule.go b/cmd/vela-server/schedule.go index 1eb715bd4..6a38998e6 100644 --- a/cmd/vela-server/schedule.go +++ b/cmd/vela-server/schedule.go @@ -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) diff --git a/scm/github/user.go b/scm/github/user.go new file mode 100644 index 000000000..848684521 --- /dev/null +++ b/scm/github/user.go @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: Apache-2.0 + +package github + +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) { + 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 +} diff --git a/scm/service.go b/scm/service.go index b65a529eb..d7a21a78e 100644 --- a/scm/service.go +++ b/scm/service.go @@ -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