Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(repo): repair update name #1011

Merged
merged 12 commits into from
Dec 18, 2023
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() {
timhuynh94 marked this conversation as resolved.
Show resolved Hide resolved
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 {
timhuynh94 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -177,7 +177,7 @@

defer func() {
// send API call to update the webhook
_, err = database.FromContext(c).UpdateHook(ctx, h)

Check failure on line 180 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L180

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:180:32: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		_, err = database.FromContext(c).UpdateHook(ctx, h)
		                             ^
if err != nil {
logrus.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err)
}
Expand Down Expand Up @@ -774,7 +774,7 @@
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)
timhuynh94 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
h.SetStatus(constants.StatusFailure)
h.SetError(err.Error())
Expand All @@ -787,7 +787,7 @@
case "archived", "unarchived", constants.ActionEdited:
logrus.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName())
// send call to get repository from database
dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())

Check failure on line 790 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L790

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:790:38: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
		                                   ^
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -798,7 +798,7 @@
}

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)

Check failure on line 801 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L801

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:801:40: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)
		                                     ^
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

Expand Down Expand Up @@ -848,11 +848,11 @@
}
}

// 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
Loading