diff --git a/cmd/drone-server/drone.go b/cmd/drone-server/drone.go index ea5374b9d..3e5977926 100644 --- a/cmd/drone-server/drone.go +++ b/cmd/drone-server/drone.go @@ -185,15 +185,6 @@ func main() { repo.GET("/logs/:number/:task", server.GetLogs) // repo.POST("/status/:number", server.PostBuildStatus) } - - // Routes for external services - repoExternal := repos.Group("") - { - repoExternal.Use(server.SetRepo()) - - repoExternal.GET("/commits/:sha", server.GetCommit) - repoExternal.GET("/pulls/:number", server.GetPullRequest) - } } badges := api.Group("/badges/:owner/:name") @@ -245,13 +236,19 @@ func main() { auth.POST("", server.GetLogin) } - redirects := r.Group("/redirect") + gitlab := r.Group("/gitlab/:owner/:name") { - redirects.Use(server.SetDatastore(store)) - redirects.Use(server.SetRepo()) + gitlab.Use(server.SetDatastore(store)) + gitlab.Use(server.SetRepo()) - redirects.GET("/:owner/:name/commits/:sha", server.RedirectSha) - redirects.GET("/:owner/:name/pulls/:number", server.RedirectPullRequest) + gitlab.GET("/commits/:sha", server.GetCommit) + gitlab.GET("/pulls/:number", server.GetPullRequest) + + redirects := gitlab.Group("/redirect") + { + redirects.GET("/commits/:sha", server.RedirectSha) + redirects.GET("/pulls/:number", server.RedirectPullRequest) + } } r.SetHTMLTemplate(index()) diff --git a/pkg/server/commits.go b/pkg/server/commits.go index 8455f0b54..3c75faad5 100644 --- a/pkg/server/commits.go +++ b/pkg/server/commits.go @@ -57,84 +57,6 @@ func GetBuilds(c *gin.Context) { } } -// GetPullRequest accepts a requests to retvie a pull request -// from the datastore for the given repository and -// pull request number -// -// GET /api/repos/:owner/:name/pulls/:number -// -// REASON: It required by GitLab, becuase we get only -// sha and ref name, but drone uses build numbers -func GetPullRequest(c *gin.Context) { - store := ToDatastore(c) - repo := ToRepo(c) - - // get the token and verify the hook is authorized - if c.Request.FormValue("access_token") != hash(repo.FullName, repo.Hash) { - c.AbortWithStatus(403) - return - } - - num, err := strconv.Atoi(c.Params.ByName("number")) - if err != nil { - c.Fail(400, err) - return - } - build, err := store.BuildPullRequestNumber(repo, num) - if err != nil { - c.Fail(404, err) - return - } - build.Jobs, err = store.JobList(build) - if err != nil { - c.Fail(404, err) - } else { - c.JSON(200, build) - } -} - -// GetCommit accepts a requests to retvie a sha and branch -// from the datastore for the given repository and -// pull request number -// -// GET /api/repos/:owner/:name/commits/:sha -// -// REASON: It required by GitLab, becuase we get only -// sha and ref name, but drone uses build numbers -func GetCommit(c *gin.Context) { - var branch string - - store := ToDatastore(c) - repo := ToRepo(c) - sha := c.Params.ByName("sha") - - // get the token and verify the hook is authorized - if c.Request.FormValue("access_token") != hash(repo.FullName, repo.Hash) { - c.AbortWithStatus(403) - return - } - - branch = c.Request.FormValue("branch") - if branch == "" { - branch = repo.Branch - } - - build, err := store.BuildSha(repo, sha, branch) - if err != nil { - c.Fail(404, err) - return - } - - build.Jobs, err = store.JobList(build) - if err != nil { - c.Fail(404, err) - } else { - c.JSON(200, build) - } - - return -} - // GetLogs accepts a request to retrieve logs from the // datastore for the given repository, build and task // number. diff --git a/pkg/server/gitlab.go b/pkg/server/gitlab.go new file mode 100644 index 000000000..69a4caf1f --- /dev/null +++ b/pkg/server/gitlab.go @@ -0,0 +1,144 @@ +package server + +import ( + "fmt" + "strconv" + + "github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin" +) + +// RedirectSha accepts a request to retvie a redirect +// to job from the datastore for the given repository +// and commit sha +// +// GET /gitlab/:owner/:name/redirect/commits/:sha +// +// REASON: It required by GitLab, becuase we get only +// sha and ref name, but drone uses build numbers +func RedirectSha(c *gin.Context) { + var branch string + + store := ToDatastore(c) + repo := ToRepo(c) + sha := c.Params.ByName("sha") + + branch = c.Request.FormValue("branch") + if branch == "" { + branch = repo.Branch + } + + build, err := store.BuildSha(repo, sha, branch) + if err != nil { + c.Redirect(301, "/") + return + } + + c.Redirect(301, fmt.Sprintf("/%s/%s/%d", repo.Owner, repo.Name, build.ID)) + return +} + +// RedirectPullRequest accepts a request to retvie a redirect +// to job from the datastore for the given repository +// and pull request number +// +// GET /gitlab/:owner/:name/redirect/pulls/:number +// +// REASON: It required by GitLab, because we get only +// internal merge request id/ref/sha, but drone uses +// build numbers +func RedirectPullRequest(c *gin.Context) { + store := ToDatastore(c) + repo := ToRepo(c) + num, err := strconv.Atoi(c.Params.ByName("number")) + if err != nil { + c.Redirect(301, "/") + return + } + + build, err := store.BuildPullRequestNumber(repo, num) + if err != nil { + c.Redirect(301, "/") + return + } + + c.Redirect(301, fmt.Sprintf("/%s/%s/%d", repo.Owner, repo.Name, build.ID)) + return +} + +// GetPullRequest accepts a requests to retvie a pull request +// from the datastore for the given repository and +// pull request number +// +// GET /gitlab/:owner/:name/pulls/:number +// +// REASON: It required by GitLab, becuase we get only +// sha and ref name, but drone uses build numbers +func GetPullRequest(c *gin.Context) { + store := ToDatastore(c) + repo := ToRepo(c) + + // get the token and verify the hook is authorized + if c.Request.FormValue("access_token") != hash(repo.FullName, repo.Hash) { + c.AbortWithStatus(403) + return + } + + num, err := strconv.Atoi(c.Params.ByName("number")) + if err != nil { + c.Fail(400, err) + return + } + build, err := store.BuildPullRequestNumber(repo, num) + if err != nil { + c.Fail(404, err) + return + } + build.Jobs, err = store.JobList(build) + if err != nil { + c.Fail(404, err) + } else { + c.JSON(200, build) + } +} + +// GetCommit accepts a requests to retvie a sha and branch +// from the datastore for the given repository and +// pull request number +// +// GET /gitlab/:owner/:name/commits/:sha +// +// REASON: It required by GitLab, becuase we get only +// sha and ref name, but drone uses build numbers +func GetCommit(c *gin.Context) { + var branch string + + store := ToDatastore(c) + repo := ToRepo(c) + sha := c.Params.ByName("sha") + + // get the token and verify the hook is authorized + if c.Request.FormValue("access_token") != hash(repo.FullName, repo.Hash) { + c.AbortWithStatus(403) + return + } + + branch = c.Request.FormValue("branch") + if branch == "" { + branch = repo.Branch + } + + build, err := store.BuildSha(repo, sha, branch) + if err != nil { + c.Fail(404, err) + return + } + + build.Jobs, err = store.JobList(build) + if err != nil { + c.Fail(404, err) + } else { + c.JSON(200, build) + } + + return +} diff --git a/pkg/server/redirect.go b/pkg/server/redirect.go deleted file mode 100644 index cb5657438..000000000 --- a/pkg/server/redirect.go +++ /dev/null @@ -1,66 +0,0 @@ -package server - -import ( - "fmt" - "strconv" - - "github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin" -) - -// RedirectSha accepts a request to retvie a redirect -// to job from the datastore for the given repository -// and commit sha -// -// GET /redirect/:owner/:name/commits/:sha -// -// REASON: It required by GitLab, becuase we get only -// sha and ref name, but drone uses build numbers -func RedirectSha(c *gin.Context) { - var branch string - - store := ToDatastore(c) - repo := ToRepo(c) - sha := c.Params.ByName("sha") - - branch = c.Request.FormValue("branch") - if branch == "" { - branch = repo.Branch - } - - build, err := store.BuildSha(repo, sha, branch) - if err != nil { - c.Redirect(301, "/") - return - } - - c.Redirect(301, fmt.Sprintf("/%s/%s/%d", repo.Owner, repo.Name, build.ID)) - return -} - -// RedirectPullRequest accepts a request to retvie a redirect -// to job from the datastore for the given repository -// and pull request number -// -// GET /redirect/:owner/:name/pulls/:number -// -// REASON: It required by GitLab, because we get only -// internal merge request id/ref/sha, but drone uses -// build numbers -func RedirectPullRequest(c *gin.Context) { - store := ToDatastore(c) - repo := ToRepo(c) - num, err := strconv.Atoi(c.Params.ByName("number")) - if err != nil { - c.Redirect(301, "/") - return - } - - build, err := store.BuildPullRequestNumber(repo, num) - if err != nil { - c.Redirect(301, "/") - return - } - - c.Redirect(301, fmt.Sprintf("/%s/%s/%d", repo.Owner, repo.Name, build.ID)) - return -}