Skip to content

Commit

Permalink
fix(repo): repair update name (#1011)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: TimHuynh <[email protected]>
Co-authored-by: David May <[email protected]>
Co-authored-by: Easton Crupper <[email protected]>
Co-authored-by: Kelly Merrick <[email protected]>
  • Loading branch information
5 people authored Dec 18, 2023
1 parent 140d2b1 commit 2dd31d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
42 changes: 40 additions & 2 deletions api/repo/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2dd31d6

Please sign in to comment.