Merge pull request #42 from matrix-org/speculator-list

speculator: Add list-pull-request functionality
pull/977/head
Kegsay 9 years ago
commit 2a2cd808fb

@ -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.

@ -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.
@ -24,9 +25,12 @@ import (
)
type PullRequest struct {
Number int
Base Commit
Head Commit
Title string
User User
HTMLURL string `json:"html_url"`
}
type Commit struct {
@ -40,6 +44,7 @@ type RequestRepo struct {
type User struct {
Login string
HTMLURL string `json:"html_url"`
}
var (
@ -47,6 +52,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 +75,7 @@ func gitCheckout(path, sha string) error {
}
func lookupPullRequest(prNumber string) (*PullRequest, error) {
resp, err := http.Get("https://api.github.com/repos/matrix-org/matrix-doc/pulls/" + prNumber)
resp, err := http.Get(fmt.Sprintf("%s/%s", pullsPrefix, prNumber))
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("error getting pulls: %v", err)
@ -197,6 +204,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 := "<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 {
if err == nil {
return err
@ -226,6 +259,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))
}

Loading…
Cancel
Save