From 2d837cc3db02bf82d4952bd8ae7a8fda56e94aef Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Fri, 14 Mar 2014 11:34:43 -0700 Subject: [PATCH] query and show commits by branch This allows the same SHA to have different builds on different branches, each separately viewable. This is useful for expressing a "pipeline" in terms of branches, e.g. a commit starts on branch A and progress through B and C to master, with the build script switching on branch name. Previously viewing each build would arbitrarily choose which branch's commit to show. --- cmd/droned/drone.go | 6 +++--- pkg/database/commits.go | 20 ++++++++++++++++++-- pkg/database/testing/commits_test.go | 26 ++++++++++++++++++++++++++ pkg/database/testing/testing.go | 10 ++++++++++ pkg/handler/builds.go | 3 ++- pkg/handler/commits.go | 5 +++-- pkg/handler/hooks.go | 2 +- pkg/queue/worker.go | 6 +++--- pkg/template/emails/failure.html | 4 ++-- pkg/template/emails/success.html | 2 +- pkg/template/pages/repo_commit.html | 6 +++--- pkg/template/pages/repo_dashboard.html | 6 +++--- pkg/template/pages/team_dashboard.html | 10 +++++----- pkg/template/pages/user_dashboard.html | 10 +++++----- 14 files changed, 85 insertions(+), 31 deletions(-) diff --git a/cmd/droned/drone.go b/cmd/droned/drone.go index a2a9b86ab..a62a844d5 100644 --- a/cmd/droned/drone.go +++ b/cmd/droned/drone.go @@ -205,9 +205,9 @@ func setupHandlers() { m.Post("/install", handler.ErrorHandler(handler.InstallPost)) // handlers for repository, commits and build details - m.Get("/:host/:owner/:name/commit/:commit/build/:label/out.txt", handler.RepoHandler(handler.BuildOut)) - m.Get("/:host/:owner/:name/commit/:commit/build/:label", handler.RepoHandler(handler.CommitShow)) - m.Get("/:host/:owner/:name/commit/:commit", handler.RepoHandler(handler.CommitShow)) + m.Get("/:host/:owner/:name/commit/:branch/:commit/build/:label/out.txt", handler.RepoHandler(handler.BuildOut)) + m.Get("/:host/:owner/:name/commit/:branch/:commit/build/:label", handler.RepoHandler(handler.CommitShow)) + m.Get("/:host/:owner/:name/commit/:branch/:commit", handler.RepoHandler(handler.CommitShow)) m.Get("/:host/:owner/:name/tree", handler.RepoHandler(handler.RepoDashboard)) m.Get("/:host/:owner/:name/status.png", handler.ErrorHandler(handler.Badge)) m.Get("/:host/:owner/:name/settings", handler.RepoAdminHandler(handler.RepoSettingsForm)) diff --git a/pkg/database/commits.go b/pkg/database/commits.go index 74e0d86b4..e99a0a11a 100644 --- a/pkg/database/commits.go +++ b/pkg/database/commits.go @@ -38,7 +38,7 @@ FROM commits WHERE id = ? ` -// SQL Queries to retrieve a Commit by name and repo id. +// SQL Queries to retrieve a Commit by hash and repo id. const commitFindHashStmt = ` SELECT id, repo_id, status, started, finished, duration, hash, branch, pull_request, author, gravatar, timestamp, message, created, updated @@ -47,6 +47,15 @@ WHERE hash = ? AND repo_id = ? LIMIT 1 ` +// SQL Queries to retrieve a Commit by branch, hash, and repo id. +const commitFindBranchHashStmt = ` +SELECT id, repo_id, status, started, finished, duration, +hash, branch, pull_request, author, gravatar, timestamp, message, created, updated +FROM commits +WHERE branch = ? AND hash = ? AND repo_id = ? +LIMIT 1 +` + // SQL Query to retrieve a list of recent commits by user. const userCommitRecentStmt = ` SELECT r.slug, r.host, r.owner, r.name, @@ -101,7 +110,7 @@ WHERE id IN ( SELECT MAX(id) FROM commits WHERE repo_id = ? - AND branch = ? + AND branch = ? GROUP BY branch) LIMIT 1 ` @@ -120,6 +129,13 @@ func GetCommitHash(hash string, repo int64) (*Commit, error) { return &commit, err } +// Returns the Commit on the given branch with the given hash. +func GetCommitBranchHash(branch string, hash string, repo int64) (*Commit, error) { + commit := Commit{} + err := meddler.QueryRow(db, &commit, commitFindBranchHashStmt, branch, hash, repo) + return &commit, err +} + // Returns the most recent Commit for the given branch. func GetBranch(repo int64, branch string) (*Commit, error) { commit := Commit{} diff --git a/pkg/database/testing/commits_test.go b/pkg/database/testing/commits_test.go index 1f6b8b9de..518afb3d3 100644 --- a/pkg/database/testing/commits_test.go +++ b/pkg/database/testing/commits_test.go @@ -44,6 +44,32 @@ func TestGetCommit(t *testing.T) { } } +func TestGetCommitBranchHash(t *testing.T) { + Setup() + defer Teardown() + + commit, err := database.GetCommitBranchHash("develop", "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9", 2) + if err != nil { + t.Error(err) + } + + if commit.ID != 5 { + t.Errorf("Exepected ID %d, got %d", 5, commit.ID) + } + + if commit.Branch != "develop" { + t.Errorf("Exepected Branch %s, got %s", "develop", commit.Branch) + } + + if commit.Hash != "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9" { + t.Errorf("Exepected Hash %s, got %s", "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9", commit.Hash) + } + + if commit.Status != "Success" { + t.Errorf("Exepected Status %s, got %s", "Success", commit.Status) + } +} + func TestGetCommitHash(t *testing.T) { Setup() defer Teardown() diff --git a/pkg/database/testing/testing.go b/pkg/database/testing/testing.go index 69fde3787..14c715b83 100644 --- a/pkg/database/testing/testing.go +++ b/pkg/database/testing/testing.go @@ -191,12 +191,22 @@ func Setup() { Gravatar: user1.Gravatar, Message: "commit message", } + commit5 := Commit{ + RepoID: repo2.ID, + Status: "Success", + Hash: "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9", + Branch: "develop", + Author: user1.Email, + Gravatar: user1.Gravatar, + Message: "commit message", + } // create dummy commit data database.SaveCommit(&commit1) database.SaveCommit(&commit2) database.SaveCommit(&commit3) database.SaveCommit(&commit4) + database.SaveCommit(&commit5) // create dummy build data database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.10", Status: "Success", Duration: 60}) diff --git a/pkg/handler/builds.go b/pkg/handler/builds.go index 090ce50bb..982cf8cc6 100644 --- a/pkg/handler/builds.go +++ b/pkg/handler/builds.go @@ -9,11 +9,12 @@ import ( // Returns the combined stdout / stderr for an individual Build. func BuildOut(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error { + branch := r.FormValue(":branch") hash := r.FormValue(":commit") labl := r.FormValue(":label") // get the commit from the database - commit, err := database.GetCommitHash(hash, repo.ID) + commit, err := database.GetCommitBranchHash(branch, hash, repo.ID) if err != nil { return err } diff --git a/pkg/handler/commits.go b/pkg/handler/commits.go index 15f82bc87..ec81b7874 100644 --- a/pkg/handler/commits.go +++ b/pkg/handler/commits.go @@ -11,11 +11,12 @@ import ( // Display a specific Commit. func CommitShow(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error { + branch := r.FormValue(":branch") hash := r.FormValue(":commit") labl := r.FormValue(":label") // get the commit from the database - commit, err := database.GetCommitHash(hash, repo.ID) + commit, err := database.GetCommitBranchHash(branch, hash, repo.ID) if err != nil { return err } @@ -49,7 +50,7 @@ func CommitShow(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) err // generate a token to connect with the websocket // handler and stream output, if the build is running. data.Token = channel.Token(fmt.Sprintf( - "%s/%s/%s/commit/%s/builds/%s", repo.Host, repo.Owner, repo.Name, commit.Hash, builds[0].Slug)) + "%s/%s/%s/commit/%s/%s/builds/%s", repo.Host, repo.Owner, repo.Name, commit.Branch, commit.Hash, builds[0].Slug)) // render the repository template. return RenderTemplate(w, "repo_commit.html", &data) diff --git a/pkg/handler/hooks.go b/pkg/handler/hooks.go index 8a572d19f..01f4ffde6 100644 --- a/pkg/handler/hooks.go +++ b/pkg/handler/hooks.go @@ -74,7 +74,7 @@ func (h *HookHandler) Hook(w http.ResponseWriter, r *http.Request) error { // Verify that the commit doesn't already exist. // We should never build the same commit twice. - _, err = database.GetCommitHash(hook.Head.Id, repo.ID) + _, err = database.GetCommitBranchHash(hook.Branch(), hook.Head.Id, repo.ID) if err != nil && err != sql.ErrNoRows { println("commit already exists") return RenderText(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway) diff --git a/pkg/queue/worker.go b/pkg/queue/worker.go index 174eb69eb..e81298582 100644 --- a/pkg/queue/worker.go +++ b/pkg/queue/worker.go @@ -95,8 +95,8 @@ func (w *worker) execute(task *BuildTask) error { // make sure a channel exists for the repository, // the commit, and the commit output (TODO) reposlug := fmt.Sprintf("%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name) - commitslug := fmt.Sprintf("%s/%s/%s/commit/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Hash) - consoleslug := fmt.Sprintf("%s/%s/%s/commit/%s/builds/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Hash, task.Build.Slug) + commitslug := fmt.Sprintf("%s/%s/%s/commit/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Hash) + consoleslug := fmt.Sprintf("%s/%s/%s/commit/%s/%s/builds/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Hash, task.Build.Slug) channel.Create(reposlug) channel.Create(commitslug) channel.CreateStream(consoleslug) @@ -223,7 +223,7 @@ func updateGitHubStatus(repo *Repo, commit *Commit) error { client.ApiUrl = settings.GitHubApiUrl var url string - url = settings.URL().String() + "/" + repo.Slug + "/commit/" + commit.Hash + url = settings.URL().String() + "/" + repo.Slug + "/commit/" + commit.Branch + "/" + commit.Hash return client.Repos.CreateStatus(repo.Owner, repo.Name, status, url, message, commit.Hash) } diff --git a/pkg/template/emails/failure.html b/pkg/template/emails/failure.html index fd674fa66..25e4488f0 100644 --- a/pkg/template/emails/failure.html +++ b/pkg/template/emails/failure.html @@ -10,7 +10,7 @@ - + @@ -25,4 +25,4 @@
commit:{{ .Commit.HashShort }}{{ .Commit.HashShort }}
branch:{{ .Commit.Message }}
-{{ end }} \ No newline at end of file +{{ end }} diff --git a/pkg/template/emails/success.html b/pkg/template/emails/success.html index 31facdfe4..522358f10 100644 --- a/pkg/template/emails/success.html +++ b/pkg/template/emails/success.html @@ -10,7 +10,7 @@ - + diff --git a/pkg/template/pages/repo_commit.html b/pkg/template/pages/repo_commit.html index a5b0f1810..262e9cafb 100644 --- a/pkg/template/pages/repo_commit.html +++ b/pkg/template/pages/repo_commit.html @@ -5,7 +5,7 @@
@@ -18,7 +18,7 @@
- + {{ if .Commit.PullRequest }} opened pull request # {{ .Commit.PullRequest }} {{ else }} @@ -78,7 +78,7 @@ }); {{ else }} - $.get("/{{ .Repo.Slug }}/commit/{{ .Commit.Hash }}/build/{{ .Build.Slug }}/out.txt", function( data ) { + $.get("/{{ .Repo.Slug }}/commit/{{ .Commit.Branch }}/{{ .Commit.Hash }}/build/{{ .Build.Slug }}/out.txt", function( data ) { var lineFormatter = new Drone.LineFormatter(); $( "#stdout" ).html(lineFormatter.format(data)); }); diff --git a/pkg/template/pages/repo_dashboard.html b/pkg/template/pages/repo_dashboard.html index 110c4119b..c027cc0f2 100644 --- a/pkg/template/pages/repo_dashboard.html +++ b/pkg/template/pages/repo_dashboard.html @@ -28,12 +28,12 @@
    {{ range .Commits }}
  • - +

    - {{.HashShort}} + {{.HashShort}} {{ if .PullRequest }} -

    opened pull request # {{.PullRequest}}

    +

    opened pull request # {{.PullRequest}}

    {{ else }}

    {{.Message}}  

    {{ end }} diff --git a/pkg/template/pages/team_dashboard.html b/pkg/template/pages/team_dashboard.html index babd8f838..8e39907cb 100644 --- a/pkg/template/pages/team_dashboard.html +++ b/pkg/template/pages/team_dashboard.html @@ -45,14 +45,14 @@

commit:{{ .Commit.HashShort }}{{ .Commit.HashShort }}
branch: