github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/pkg/handler/commits.go (about) 1 package handler 2 3 import ( 4 "fmt" 5 "net/http" 6 "time" 7 8 "github.com/drone/drone/pkg/channel" 9 "github.com/drone/drone/pkg/database" 10 . "github.com/drone/drone/pkg/model" 11 ) 12 13 // Display a specific Commit. 14 func CommitShow(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error { 15 branch := r.FormValue("branch") 16 if branch == "" { 17 branch = "master" 18 } 19 20 hash := r.FormValue(":commit") 21 labl := r.FormValue(":label") 22 23 // get the commit from the database 24 commit, err := database.GetCommitBranchHash(branch, hash, repo.ID) 25 if err != nil { 26 return err 27 } 28 29 // get the builds from the database. a commit can have 30 // multiple sub-builds (or matrix builds) 31 builds, err := database.ListBuilds(commit.ID) 32 if err != nil { 33 return err 34 } 35 36 data := struct { 37 User *User 38 Repo *Repo 39 Commit *Commit 40 Build *Build 41 Builds []*Build 42 Token string 43 }{u, repo, commit, builds[0], builds, ""} 44 45 // get the specific build requested by the user. instead 46 // of a database round trip, we can just loop through the 47 // list and extract the requested build. 48 for _, b := range builds { 49 if b.Slug == labl { 50 data.Build = b 51 break 52 } 53 } 54 55 // generate a token to connect with the websocket 56 // handler and stream output, if the build is running. 57 data.Token = channel.Token(fmt.Sprintf( 58 "%s/%s/%s/commit/%s/%s/builds/%s", repo.Host, repo.Owner, repo.Name, commit.Branch, commit.Hash, builds[0].Slug)) 59 60 // render the repository template. 61 return RenderTemplate(w, "repo_commit.html", &data) 62 } 63 64 // Helper method for saving a failed build or commit in the case where it never starts to build. 65 // This can happen if the yaml is bad or doesn't exist. 66 func saveFailedBuild(commit *Commit, msg string) error { 67 68 // Set the commit to failed 69 commit.Status = "Failure" 70 commit.Created = time.Now().UTC() 71 commit.Finished = commit.Created 72 commit.Duration = 0 73 if err := database.SaveCommit(commit); err != nil { 74 return err 75 } 76 77 // save the build to the database 78 build := &Build{} 79 build.Slug = "1" // TODO: This should not be hardcoded 80 build.CommitID = commit.ID 81 build.Created = time.Now().UTC() 82 build.Finished = build.Created 83 commit.Duration = 0 84 build.Status = "Failure" 85 build.Stdout = msg 86 if err := database.SaveBuild(build); err != nil { 87 return err 88 } 89 90 // TODO: Should the status be Error instead of Failure? 91 92 // TODO: Do we need to update the branch table too? 93 94 return nil 95 96 }