From 2dd31d69c4e25ed6588b1003071c42ce8a0c0b6d Mon Sep 17 00:00:00 2001 From: Tim Huynh Date: Mon, 18 Dec 2023 10:14:21 -0600 Subject: [PATCH] fix(repo): repair update name (#1011) * fix archived repair name * include org change under transfer * working code * modify logic * fix conflicts * Update api/repo/repair.go Co-authored-by: Kelly Merrick --------- Co-authored-by: TimHuynh Co-authored-by: David May <49894298+wass3rw3rk@users.noreply.github.com> Co-authored-by: Easton Crupper <65553218+ecrupper@users.noreply.github.com> Co-authored-by: Kelly Merrick --- api/repo/repair.go | 42 ++++++++++++++++++++++++++++++++++++++++-- api/webhook/post.go | 6 +++--- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/api/repo/repair.go b/api/repo/repair.go index 55274b158..4a792283d 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -4,16 +4,17 @@ package repo import ( "fmt" - "net/http" - "github.com/gin-gonic/gin" + wh "github.com/go-vela/server/api/webhook" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" + "github.com/go-vela/types" "github.com/sirupsen/logrus" + "net/http" ) // swagger:operation PATCH /api/v1/repos/{org}/{repo}/repair repos RepairRepo @@ -54,6 +55,8 @@ func RepairRepo(c *gin.Context) { r := repo.Retrieve(c) u := user.Retrieve(c) ctx := c.Request.Context() + // capture middleware values + m := c.MustGet("metadata").(*types.Metadata) // update engine logger with API metadata // @@ -116,6 +119,41 @@ func RepairRepo(c *gin.Context) { } } + // get repo information from the source + sourceRepo, _, err := scm.FromContext(c).GetRepo(ctx, u, r) + if err != nil { + retErr := fmt.Errorf("unable to retrieve repo info for %s from source: %w", sourceRepo.GetFullName(), err) + + util.HandleError(c, http.StatusBadRequest, retErr) + + return + } + + // if repo has a name change, then update DB with new name + // if repo has an org change, update org as well + if sourceRepo.GetName() != r.GetName() || sourceRepo.GetOrg() != r.GetOrg() { + h, err := database.FromContext(c).LastHookForRepo(ctx, r) + if err != nil { + retErr := fmt.Errorf("unable to get last hook for %s: %w", r.GetFullName(), err) + + util.HandleError(c, http.StatusInternalServerError, retErr) + + return + } + + // set sourceRepo PreviousName to old name if name is changed + // ignore if repo is transferred and name is unchanged + if sourceRepo.GetName() != r.GetName() { + sourceRepo.SetPreviousName(r.GetName()) + } + + r, err = wh.RenameRepository(ctx, h, sourceRepo, c, m) + if err != nil { + util.HandleError(c, http.StatusInternalServerError, err) + return + } + } + // if the repo was previously inactive, mark it as active if !r.GetActive() { r.SetActive(true) diff --git a/api/webhook/post.go b/api/webhook/post.go index 8349c48df..ef538898e 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -774,7 +774,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadat switch h.GetEventAction() { // if action is renamed or transferred, go through rename routine case constants.ActionRenamed, constants.ActionTransferred: - r, err := renameRepository(ctx, h, r, c, m) + r, err := RenameRepository(ctx, h, r, c, m) if err != nil { h.SetStatus(constants.StatusFailure) h.SetError(err.Error()) @@ -848,11 +848,11 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadat } } -// renameRepository is a helper function that takes the old name of the repo, +// RenameRepository is a helper function that takes the old name of the repo, // queries the database for the repo that matches that name and org, and updates // that repo to its new name in order to preserve it. It also updates the secrets // associated with that repo as well as build links for the UI. -func renameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *gin.Context, m *types.Metadata) (*library.Repo, error) { +func RenameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *gin.Context, m *types.Metadata) (*library.Repo, error) { logrus.Infof("renaming repository from %s to %s", r.GetPreviousName(), r.GetName()) // get any matching hook with the repo's unique webhook ID in the SCM