forked from gengo/goship
/
proj_commits_handler.go
61 lines (57 loc) · 1.65 KB
/
proj_commits_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/coreos/go-etcd/etcd"
goship "github.com/gengo/goship/lib"
"github.com/gengo/goship/lib/acl"
"github.com/gengo/goship/lib/auth"
githublib "github.com/gengo/goship/lib/github"
)
type ProjCommitsHandler struct {
ac acl.AccessControl
gcl githublib.Client
ecl *etcd.Client
}
func (h ProjCommitsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, projName string) {
c, err := goship.ParseETCD(h.ecl)
if err != nil {
log.Println("ERROR: Parsing etc ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := auth.CurrentUser(r)
if err != nil {
log.Println("ERROR: Getting User", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
proj, err := goship.ProjectFromName(c.Projects, projName)
if err != nil {
log.Println("ERROR: Getting Project from name", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Remove projects that the user is not a collaborator on...
fp := acl.ReadableProjects(h.ac, []goship.Project{*proj}, u)
p, err := retrieveCommits(h.gcl, h.ac, r, fp[0], c.DeployUser)
if err != nil {
log.Println("ERROR: Retrieving Commits ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
j, err := json.Marshal(p)
if err != nil {
log.Println("ERROR: Marshalling Retrieving Commits ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(j)
if err != nil {
log.Println("ERROR: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}