From 8cc88be15172333817faaa9be53228926b034473 Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Tue, 28 Nov 2023 12:38:54 -0600 Subject: [PATCH] working code --- api/repo/repair.go | 27 ++++++++++++++++----------- api/webhook/post.go | 6 +++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/api/repo/repair.go b/api/repo/repair.go index 03c3d8a91..cabc63996 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 // @@ -125,24 +128,26 @@ func RepairRepo(c *gin.Context) { return } + // if repo has a name change, then update DB with new name // if repo has a org change, update org as well if sourceRepo.GetName() != r.GetName() || sourceRepo.GetOrg() != r.GetOrg() { - r.SetPreviousName(r.GetName()) - r.SetName(sourceRepo.GetName()) - r.SetFullName(sourceRepo.GetFullName()) - r.SetLink(sourceRepo.GetLink()) - r.SetClone(sourceRepo.GetClone()) - r.SetOrg(sourceRepo.GetOrg()) - // send API call to update the repo - _, err := database.FromContext(c).UpdateRepo(ctx, r) + h, err := database.FromContext(c).LastHookForRepo(ctx, r) if err != nil { - retErr := fmt.Errorf("unable to rename repo %s to %s: %w", sourceRepo.GetFullName(), r.GetFullName(), err) + 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 + 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 diff --git a/api/webhook/post.go b/api/webhook/post.go index 47562f80f..4409172d9 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -733,7 +733,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()) @@ -807,11 +807,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