From ba780a589a9311dce856342d978ad3f45d10f392 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Fri, 11 Sep 2015 14:40:08 +0100 Subject: [PATCH 1/2] speculator: Add list-pull-request functionality --- scripts/speculator/main.go | 43 +++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/scripts/speculator/main.go b/scripts/speculator/main.go index 3775ad863..757c48e78 100644 --- a/scripts/speculator/main.go +++ b/scripts/speculator/main.go @@ -24,9 +24,12 @@ import ( ) type PullRequest struct { - Base Commit - Head Commit - User User + Number int + Base Commit + Head Commit + Title string + User User + HTMLURL string `json:"html_url"` } type Commit struct { @@ -39,7 +42,8 @@ type RequestRepo struct { } type User struct { - Login string + Login string + HTMLURL string `json:"html_url"` } var ( @@ -47,6 +51,8 @@ var ( allowedMembers map[string]bool ) +const pullsPrefix = "https://api.github.com/repos/matrix-org/matrix-doc/pulls" + func gitClone(url string) (string, error) { dst := path.Join("/tmp/matrix-doc", strconv.FormatInt(rand.Int63(), 10)) cmd := exec.Command("git", "clone", url, dst) @@ -68,7 +74,7 @@ func gitCheckout(path, sha string) 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() dec := json.NewDecoder(resp.Body) var pr PullRequest @@ -192,6 +198,32 @@ func serveRstDiff(w http.ResponseWriter, req *http.Request) { 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 := "" + io.WriteString(w, s) +} + func ignoreExitCodeOne(err error) error { if err == nil { return err @@ -221,6 +253,7 @@ func main() { http.HandleFunc("/spec/", serveSpec) http.HandleFunc("/diff/rst/", serveRstDiff) http.HandleFunc("/healthz", serveText("ok")) + http.HandleFunc("/", listPulls) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) } From d251f791fbfbe7af675f82689d51b63a79a361ff Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Fri, 11 Sep 2015 17:15:03 +0100 Subject: [PATCH 2/2] speculator: Document / --- scripts/speculator/README | 3 ++- scripts/speculator/main.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/speculator/README b/scripts/speculator/README index ae00c55ad..6ab84f68b 100644 --- a/scripts/speculator/README +++ b/scripts/speculator/README @@ -1,6 +1,7 @@ speculator allows you to preview pull requests to the matrix.org specification. -It serves two HTTP endpoints: +It serves the following HTTP endpoints: + - / lists open pull requests - /spec/123 which renders the spec as html at pull request 123. - /diff/rst/123 which gives a diff of the spec's rst at pull request 123. diff --git a/scripts/speculator/main.go b/scripts/speculator/main.go index bcbe55146..32c015ac2 100644 --- a/scripts/speculator/main.go +++ b/scripts/speculator/main.go @@ -1,5 +1,6 @@ // speculator allows you to preview pull requests to the matrix.org specification. -// It serves two HTTP endpoints: +// It serves the following HTTP endpoints: +// - / lists open pull requests // - /spec/123 which renders the spec as html at pull request 123. // - /diff/rst/123 which gives a diff of the spec's rst at pull request 123. // It is currently woefully inefficient, and there is a lot of low hanging fruit for improvement.