From b030e5667ccf8d47769dc58cbceacaf736d8b385 Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Wed, 22 Nov 2023 09:03:54 -0600 Subject: [PATCH 1/6] fix archived repair name --- api/repo/repair.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/api/repo/repair.go b/api/repo/repair.go index 55274b158..53434e89b 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -116,6 +116,34 @@ 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 sourceRepo.GetName() != r.GetName() { + r.SetPreviousName(r.GetName()) + r.SetName(sourceRepo.GetName()) + r.SetFullName(sourceRepo.GetFullName()) + r.SetLink(sourceRepo.GetLink()) + r.SetClone(sourceRepo.GetClone()) + + // send API call to update the repo + _, err := database.FromContext(c).UpdateRepo(ctx, r) + if err != nil { + retErr := fmt.Errorf("unable to rename repo %s to %s: %w", r.GetFullName(), sourceRepo.GetFullName(), err) + + util.HandleError(c, http.StatusInternalServerError, retErr) + + return + } + } + // if the repo was previously inactive, mark it as active if !r.GetActive() { r.SetActive(true) From 5dd97f4e27de4b091269a04e43d000e7bb77c5f3 Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Fri, 24 Nov 2023 10:46:35 -0600 Subject: [PATCH 2/6] include org change under transfer --- api/repo/repair.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/repo/repair.go b/api/repo/repair.go index 53434e89b..03c3d8a91 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -126,17 +126,18 @@ func RepairRepo(c *gin.Context) { return } // if repo has a name change, then update DB with new name - if sourceRepo.GetName() != r.GetName() { + // 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) if err != nil { - retErr := fmt.Errorf("unable to rename repo %s to %s: %w", r.GetFullName(), sourceRepo.GetFullName(), err) + retErr := fmt.Errorf("unable to rename repo %s to %s: %w", sourceRepo.GetFullName(), r.GetFullName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) From 8cc88be15172333817faaa9be53228926b034473 Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Tue, 28 Nov 2023 12:38:54 -0600 Subject: [PATCH 3/6] 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 From ba73052b86a8870a3f489f78939a10f76f3bf80e Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Tue, 28 Nov 2023 14:25:35 -0600 Subject: [PATCH 4/6] modify logic --- api/repo/repair.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/repo/repair.go b/api/repo/repair.go index cabc63996..44a8821fa 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -141,8 +141,12 @@ func RepairRepo(c *gin.Context) { return } - // set sourceRepo PreviousName to old name - sourceRepo.SetPreviousName(r.GetName()) + // 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) From d2e77142337d536727d839bd6e4182dd7559942d Mon Sep 17 00:00:00 2001 From: TimHuynh Date: Wed, 29 Nov 2023 10:28:25 -0600 Subject: [PATCH 5/6] fix conflicts --- api/repo/repair.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/repo/repair.go b/api/repo/repair.go index 44a8821fa..1fa8e4e00 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -120,7 +120,7 @@ func RepairRepo(c *gin.Context) { } // get repo information from the source - sourceRepo, err := scm.FromContext(c).GetRepo(ctx, u, r) + 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) From ede30a95f3d28dd1df548e490306093a0d61750f Mon Sep 17 00:00:00 2001 From: Tim Huynh Date: Mon, 11 Dec 2023 14:49:16 -0600 Subject: [PATCH 6/6] Update api/repo/repair.go Co-authored-by: Kelly Merrick --- api/repo/repair.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/repo/repair.go b/api/repo/repair.go index 1fa8e4e00..4a792283d 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -130,7 +130,7 @@ func RepairRepo(c *gin.Context) { } // if repo has a name change, then update DB with new name - // if repo has a org change, update org as well + // 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 {