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(router/scm)!: change HTTP method from GET to PATCH for sync endpoints #994

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions api/scm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/sirupsen/logrus"
)

// swagger:operation GET /api/v1/scm/repos/{org}/{repo}/sync scm SyncRepo
// swagger:operation PATCH /api/v1/scm/repos/{org}/{repo}/sync scm SyncRepo
//
// Sync up scm service and database in the context of a specific repo
//
Expand Down Expand Up @@ -125,11 +125,9 @@ func SyncRepo(c *gin.Context) {
// update webhook
webhookExists, err := scm.FromContext(c).Update(ctx, u, r, lastHook.GetWebhookID())
if err != nil {

// if webhook has been manually deleted from GitHub,
// set to inactive in database
if !webhookExists {

r.SetActive(false)

_, err := database.FromContext(c).UpdateRepo(ctx, r)
Expand All @@ -144,15 +142,13 @@ func SyncRepo(c *gin.Context) {
c.JSON(http.StatusOK, fmt.Sprintf("webhook not found, repo %s deactivated", r.GetFullName()))

return
}

} else {

retErr := fmt.Errorf("unable to update repo webhook for %s: %w", r.GetFullName(), err)
retErr := fmt.Errorf("unable to update repo webhook for %s: %w", r.GetFullName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)
util.HandleError(c, http.StatusInternalServerError, retErr)

return
}
return
}
}

Expand Down
14 changes: 5 additions & 9 deletions api/scm/sync_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/sirupsen/logrus"
)

// swagger:operation GET /api/v1/scm/orgs/{org}/sync scm SyncReposForOrg
// swagger:operation PATCH /api/v1/scm/orgs/{org}/sync scm SyncReposForOrg
//
// Sync up repos from scm service and database in a specified org
//
Expand Down Expand Up @@ -138,11 +138,9 @@ func SyncReposForOrg(c *gin.Context) {
// update webhook
webhookExists, err := scm.FromContext(c).Update(ctx, u, repo, lastHook.GetWebhookID())
if err != nil {

// if webhook has been manually deleted from GitHub,
// set to inactive in database
if !webhookExists {

repo.SetActive(false)

_, err := database.FromContext(c).UpdateRepo(ctx, repo)
Expand All @@ -155,15 +153,13 @@ func SyncReposForOrg(c *gin.Context) {
}

continue
}

} else {

retErr := fmt.Errorf("unable to update repo webhook for %s: %w", repo.GetFullName(), err)
retErr := fmt.Errorf("unable to update repo webhook for %s: %w", repo.GetFullName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)
util.HandleError(c, http.StatusInternalServerError, retErr)

return
}
return
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions mock/server/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/go-vela/types"
)

// syncRepo has a param :repo returns mock JSON for a http GET.
// syncRepo has a param :repo returns mock JSON for a http PATCH.
//
// Pass "not-found" to :repo to test receiving a http 404 response.
func syncRepo(c *gin.Context) {
Expand All @@ -27,7 +27,7 @@ func syncRepo(c *gin.Context) {
c.JSON(http.StatusOK, fmt.Sprintf("Repo %s has been synced", r))
}

// syncRepos has a param :org returns mock JSON for a http GET.
// syncRepos has a param :org returns mock JSON for a http PATCH.
//
// Pass "not-found" to :repo to test receiving a http 404 response.
func syncRepos(c *gin.Context) {
Expand Down
4 changes: 2 additions & 2 deletions mock/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func FakeHandler() http.Handler {
e.DELETE("/api/v1/repos/:org/:repo", removeRepo)
e.PATCH("/api/v1/repos/:org/:repo/repair", repairRepo)
e.PATCH("/api/v1/repos/:org/:repo/chown", chownRepo)
e.GET("/api/v1/scm/repos/:org/:repo/sync", syncRepo)
e.GET("/api/v1/scm/orgs/:org/sync", syncRepos)
e.PATCH("/api/v1/scm/repos/:org/:repo/sync", syncRepo)
e.PATCH("/api/v1/scm/orgs/:org/sync", syncRepos)

// mock endpoints for secret calls
e.GET("/api/v1/secrets/:engine/:type/:org/:name/:secret", getSecret)
Expand Down
8 changes: 4 additions & 4 deletions router/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
// ScmHandlers is a function that extends the provided base router group
// with the API handlers for source code management functionality.
//
// GET /api/v1/scm/orgs/:org/sync
// GET /api/v1/scm/repos/:org/:repo/sync .
// PATCH /api/v1/scm/orgs/:org/sync
// PATCH /api/v1/scm/repos/:org/:repo/sync .
func ScmHandlers(base *gin.RouterGroup) {
// SCM orgs endpoints
orgs := base.Group("/scm/orgs")
{
// SCM org endpoints
org := orgs.Group("/:org", org.Establish())
{
org.GET("/sync", scm.SyncReposForOrg)
org.PATCH("/sync", scm.SyncReposForOrg)
} // end of SCM org endpoints
} // end of SCM orgs endpoints

Expand All @@ -31,7 +31,7 @@ func ScmHandlers(base *gin.RouterGroup) {
// SCM repo endpoints
repo := repos.Group("/:org/:repo", org.Establish(), repo.Establish())
{
repo.GET("/sync", scm.SyncRepo)
repo.PATCH("/sync", scm.SyncRepo)
} // end of SCM repo endpoints
} // end of SCM repos endpoints
}