Skip to content

Commit

Permalink
working code
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHuynh committed Nov 28, 2023
1 parent 5dd97f4 commit 8cc88be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
27 changes: 16 additions & 11 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 @@ -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 {

Check failure on line 147 in api/repo/repair.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/repo/repair.go#L147

only one cuddle assignment allowed before if statement (wsl)
Raw output
api/repo/repair.go:147:3: only one cuddle assignment allowed before if statement (wsl)
		if err != nil {
		^
util.HandleError(c, http.StatusInternalServerError, err)
return
}
}

// if the repo was previously inactive, mark it as active
Expand Down
6 changes: 3 additions & 3 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

View workflow job for this annotation

GitHub Actions / golangci

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

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:736: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 {
h.SetStatus(constants.StatusFailure)
h.SetError(err.Error())
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8cc88be

Please sign in to comment.