diff --git a/cmd/drone-server/drone.go b/cmd/drone-server/drone.go index 16001ae55..ea5374b9d 100644 --- a/cmd/drone-server/drone.go +++ b/cmd/drone-server/drone.go @@ -178,8 +178,8 @@ func main() { repo.POST("/watch", server.Subscribe) repo.DELETE("/unwatch", server.Unsubscribe) - repo.GET("/builds", server.GetCommits) - repo.GET("/builds/:number", server.GetCommit) + repo.GET("/builds", server.GetBuilds) + repo.GET("/builds/:number", server.GetBuild) repo.POST("/builds/:number", server.RunBuild) repo.DELETE("/builds/:number", server.KillBuild) repo.GET("/logs/:number/:task", server.GetLogs) @@ -191,7 +191,8 @@ func main() { { repoExternal.Use(server.SetRepo()) - repoExternal.GET("/pr/:number", server.GetPullRequest) + repoExternal.GET("/commits/:sha", server.GetCommit) + repoExternal.GET("/pulls/:number", server.GetPullRequest) } } @@ -249,7 +250,8 @@ func main() { redirects.Use(server.SetDatastore(store)) redirects.Use(server.SetRepo()) - redirects.GET("/:owner/:name/commit/:sha", server.RedirectSha) + redirects.GET("/:owner/:name/commits/:sha", server.RedirectSha) + redirects.GET("/:owner/:name/pulls/:number", server.RedirectPullRequest) } r.SetHTMLTemplate(index()) diff --git a/pkg/remote/builtin/gitlab/gitlab.go b/pkg/remote/builtin/gitlab/gitlab.go index c6b1bacf3..7b67a53c9 100644 --- a/pkg/remote/builtin/gitlab/gitlab.go +++ b/pkg/remote/builtin/gitlab/gitlab.go @@ -199,8 +199,13 @@ func (r *Gitlab) Activate(user *common.User, repo *common.Repo, k *common.Keypai droneUrl := fmt.Sprintf("%s://%s", uri.Scheme, uri.Host) droneToken := uri.Query().Get("access_token") + ssl_verify := strconv.FormatBool(!r.SkipVerify) - return client.AddDroneService(id, map[string]string{"token": droneToken, "drone_url": droneUrl}) + return client.AddDroneService(id, map[string]string{ + "token": droneToken, + "drone_url": droneUrl, + "enable_ssl_verification": ssl_verify, + }) } // Deactivate removes a repository by removing all the post-commit hooks diff --git a/pkg/server/commits.go b/pkg/server/commits.go index cead75400..8455f0b54 100644 --- a/pkg/server/commits.go +++ b/pkg/server/commits.go @@ -20,7 +20,7 @@ import ( // // GET /api/repos/:owner/:name/:number // -func GetCommit(c *gin.Context) { +func GetBuild(c *gin.Context) { store := ToDatastore(c) repo := ToRepo(c) num, err := strconv.Atoi(c.Params.ByName("number")) @@ -41,12 +41,30 @@ func GetCommit(c *gin.Context) { } } +// GetCommits accepts a request to retrieve a list +// of commits from the datastore for the given repository. +// +// GET /api/repos/:owner/:name/builds +// +func GetBuilds(c *gin.Context) { + store := ToDatastore(c) + repo := ToRepo(c) + builds, err := store.BuildList(repo, 20, 0) + if err != nil { + c.Fail(404, err) + } else { + c.JSON(200, builds) + } +} + // 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/pr/: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) @@ -75,20 +93,46 @@ func GetPullRequest(c *gin.Context) { } } -// GetCommits accepts a request to retrieve a list -// of commits from the datastore for the given repository. +// 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/builds +// GET /api/repos/:owner/:name/commits/:sha // -func GetCommits(c *gin.Context) { +// 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) - builds, err := store.BuildList(repo, 20, 0) + 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, builds) + c.JSON(200, build) } + + return } // GetLogs accepts a request to retrieve logs from the diff --git a/pkg/server/redirect.go b/pkg/server/redirect.go index 59a0608f4..cb5657438 100644 --- a/pkg/server/redirect.go +++ b/pkg/server/redirect.go @@ -11,8 +11,10 @@ import ( // to job from the datastore for the given repository // and commit sha // -// GET /redirect/:owner/:name/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 @@ -35,12 +37,15 @@ func RedirectSha(c *gin.Context) { return } -// RedirectSha accepts a request to retvie a redirect +// 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/pr/: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) diff --git a/pkg/store/builtin/build.go b/pkg/store/builtin/build.go index ba14b19e1..5e9ace3eb 100644 --- a/pkg/store/builtin/build.go +++ b/pkg/store/builtin/build.go @@ -112,6 +112,21 @@ func (db *Buildstore) KillBuilds() error { return err2 } +const stmtBuildSelectPullRequestNumber = stmtBuildSelectList + ` +WHERE build_repo_id = ? +AND build_pull_request_number = ? +ORDER BY build_number DESC +LIMIT 1 +` + +const stmtBuildSelectSha = stmtBuildSelectList + ` +WHERE build_repo_id = ? +AND build_commit_sha = ? +AND build_commit_branch = ? +ORDER BY build_number DESC +LIMIT 1 +` + // SQL query to retrieve the latest builds across all branches. const buildListQuery = ` SELECT diff --git a/pkg/store/builtin/build_sql.go b/pkg/store/builtin/build_sql.go index 32b7ce98c..eabc37285 100644 --- a/pkg/store/builtin/build_sql.go +++ b/pkg/store/builtin/build_sql.go @@ -596,79 +596,6 @@ WHERE build_repo_id = ? AND build_number = ? ` -const stmtBuildSelectPullRequestNumber = ` -SELECT - build_id -,build_repo_id -,build_number -,build_status -,build_started -,build_finished -,build_commit_sha -,build_commit_ref -,build_commit_link -,build_commit_branch -,build_commit_message -,build_commit_timestamp -,build_commit_remote -,build_commit_author_login -,build_commit_author_email -,build_pull_request_number -,build_pull_request_title -,build_pull_request_link -,build_pull_request_base_sha -,build_pull_request_base_ref -,build_pull_request_base_link -,build_pull_request_base_branch -,build_pull_request_base_message -,build_pull_request_base_timestamp -,build_pull_request_base_remote -,build_pull_request_base_author_login -,build_pull_request_base_author_email -FROM builds -WHERE build_repo_id = ? -AND build_pull_request_number = ? -ORDER BY build_number DESC -LIMIT 1 -` - -const stmtBuildSelectSha = ` -SELECT - build_id -,build_repo_id -,build_number -,build_status -,build_started -,build_finished -,build_commit_sha -,build_commit_ref -,build_commit_link -,build_commit_branch -,build_commit_message -,build_commit_timestamp -,build_commit_remote -,build_commit_author_login -,build_commit_author_email -,build_pull_request_number -,build_pull_request_title -,build_pull_request_link -,build_pull_request_base_sha -,build_pull_request_base_ref -,build_pull_request_base_link -,build_pull_request_base_branch -,build_pull_request_base_message -,build_pull_request_base_timestamp -,build_pull_request_base_remote -,build_pull_request_base_author_login -,build_pull_request_base_author_email -FROM builds -WHERE build_repo_id = ? -AND build_commit_sha = ? -AND build_commit_branch = ? -ORDER BY build_number DESC -LIMIT 1 -` - const stmtBuildSelectCommitBranch = ` SELECT build_id