speculator: Add list-pull-request functionality

This commit is contained in:
Daniel Wagner-Hall 2015-09-11 14:40:08 +01:00
parent da7c2e7fa8
commit ba780a589a

View file

@ -24,9 +24,12 @@ import (
) )
type PullRequest struct { type PullRequest struct {
Base Commit Number int
Head Commit Base Commit
User User Head Commit
Title string
User User
HTMLURL string `json:"html_url"`
} }
type Commit struct { type Commit struct {
@ -39,7 +42,8 @@ type RequestRepo struct {
} }
type User struct { type User struct {
Login string Login string
HTMLURL string `json:"html_url"`
} }
var ( var (
@ -47,6 +51,8 @@ var (
allowedMembers map[string]bool allowedMembers map[string]bool
) )
const pullsPrefix = "https://api.github.com/repos/matrix-org/matrix-doc/pulls"
func gitClone(url string) (string, error) { func gitClone(url string) (string, error) {
dst := path.Join("/tmp/matrix-doc", strconv.FormatInt(rand.Int63(), 10)) dst := path.Join("/tmp/matrix-doc", strconv.FormatInt(rand.Int63(), 10))
cmd := exec.Command("git", "clone", url, dst) cmd := exec.Command("git", "clone", url, dst)
@ -68,7 +74,7 @@ func gitCheckout(path, sha string) error {
} }
func lookupPullRequest(prNumber string) (PullRequest, error) { func lookupPullRequest(prNumber string) (PullRequest, error) {
resp, _ := http.Get("https://api.github.com/repos/matrix-org/matrix-doc/pulls/" + prNumber) resp, _ := http.Get(fmt.Sprintf("%s/%s", pullsPrefix, prNumber))
defer resp.Body.Close() defer resp.Body.Close()
dec := json.NewDecoder(resp.Body) dec := json.NewDecoder(resp.Body)
var pr PullRequest var pr PullRequest
@ -192,6 +198,32 @@ func serveRstDiff(w http.ResponseWriter, req *http.Request) {
w.Write(diff.Bytes()) w.Write(diff.Bytes())
} }
func listPulls(w http.ResponseWriter, req *http.Request) {
resp, err := http.Get(pullsPrefix)
if err != nil {
writeError(w, err)
return
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var pulls []PullRequest
if err := dec.Decode(&pulls); err != nil {
writeError(w, err)
return
}
if len(pulls) == 0 {
io.WriteString(w, "No pull requests found")
return
}
s := "<body><ul>"
for _, pull := range pulls {
s += fmt.Sprintf(`<li>%d: <a href="%s">%s</a>: <a href="%s">%s</a>: <a href="spec/%d">spec</a> <a href="diff/rst/%d">rst diff</a></li>`,
pull.Number, pull.User.HTMLURL, pull.User.Login, pull.HTMLURL, pull.Title, pull.Number, pull.Number)
}
s += "</ul></body>"
io.WriteString(w, s)
}
func ignoreExitCodeOne(err error) error { func ignoreExitCodeOne(err error) error {
if err == nil { if err == nil {
return err return err
@ -221,6 +253,7 @@ func main() {
http.HandleFunc("/spec/", serveSpec) http.HandleFunc("/spec/", serveSpec)
http.HandleFunc("/diff/rst/", serveRstDiff) http.HandleFunc("/diff/rst/", serveRstDiff)
http.HandleFunc("/healthz", serveText("ok")) http.HandleFunc("/healthz", serveText("ok"))
http.HandleFunc("/", listPulls)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
} }