speculator: Allow spec viewing for multi-page spec

pull/977/head
Daniel Wagner-Hall 9 years ago
parent e171acf01f
commit 30ed918633

@ -57,8 +57,8 @@ var (
port = flag.Int("port", 9000, "Port on which to listen for HTTP") port = flag.Int("port", 9000, "Port on which to listen for HTTP")
includesDir = flag.String("includes_dir", "", "Directory containing include files for styling like matrix.org") includesDir = flag.String("includes_dir", "", "Directory containing include files for styling like matrix.org")
allowedMembers map[string]bool allowedMembers map[string]bool
specCache *lru.Cache // string -> []byte specCache *lru.Cache // string -> map[string][]byte filename -> contents
styledSpecCache *lru.Cache // string -> []byte styledSpecCache *lru.Cache // string -> map[string][]byte filename -> contents
) )
func (u *User) IsTrusted() bool { func (u *User) IsTrusted() bool {
@ -105,10 +105,7 @@ func lookupPullRequest(url url.URL, pathPrefix string) (*PullRequest, error) {
if !strings.HasPrefix(url.Path, pathPrefix+"/") { if !strings.HasPrefix(url.Path, pathPrefix+"/") {
return nil, fmt.Errorf("invalid path passed: %s expect %s/123", url.Path, pathPrefix) return nil, fmt.Errorf("invalid path passed: %s expect %s/123", url.Path, pathPrefix)
} }
prNumber := url.Path[len(pathPrefix)+1:] prNumber := strings.Split(url.Path[len(pathPrefix)+1:], "/")[0]
if strings.Contains(prNumber, "/") {
return nil, fmt.Errorf("invalid path passed: %s expect %s/123", url.Path, pathPrefix)
}
resp, err := http.Get(fmt.Sprintf("%s/%s", pullsPrefix, prNumber)) resp, err := http.Get(fmt.Sprintf("%s/%s", pullsPrefix, prNumber))
defer resp.Body.Close() defer resp.Body.Close()
@ -196,6 +193,17 @@ func (s *server) getSHAOf(ref string) (string, error) {
return strings.TrimSpace(b.String()), nil return strings.TrimSpace(b.String()), nil
} }
func extractPath(path, base string) string {
// Assume exactly one flat directory
max := strings.Count(base, "/") + 2
parts := strings.SplitN(path, "/", max)
if len(parts) < max || parts[max-1] == "" {
return "index.html"
}
return parts[max-1]
}
func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) { func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) {
var sha string var sha string
@ -206,7 +214,7 @@ func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) {
return return
} }
if strings.ToLower(req.URL.Path) == "/spec/head" { if strings.HasPrefix(strings.ToLower(req.URL.Path), "/spec/head") {
// err may be non-nil here but if headSha is non-empty we will serve a possibly-stale result in favour of erroring. // err may be non-nil here but if headSha is non-empty we will serve a possibly-stale result in favour of erroring.
// This is to deal with cases like where github is down but we still want to serve the spec. // This is to deal with cases like where github is down but we still want to serve the spec.
if headSha, err := s.lookupHeadSHA(); headSha == "" { if headSha, err := s.lookupHeadSHA(); headSha == "" {
@ -236,36 +244,59 @@ func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) {
cache = styledSpecCache cache = styledSpecCache
} }
var pathToContent map[string][]byte
if cached, ok := cache.Get(sha); ok { if cached, ok := cache.Get(sha); ok {
w.Write(cached.([]byte)) pathToContent = cached.(map[string][]byte)
return } else {
} dst, err := s.generateAt(sha)
defer os.RemoveAll(dst)
if err != nil {
writeError(w, 500, err)
return
}
dst, err := s.generateAt(sha) if styleLikeMatrixDotOrg {
defer os.RemoveAll(dst) cmd := exec.Command("./add-matrix-org-stylings.sh", *includesDir)
if err != nil { cmd.Dir = path.Join(dst, "scripts")
writeError(w, 500, err) var b bytes.Buffer
return cmd.Stderr = &b
} if err := cmd.Run(); err != nil {
writeError(w, 500, fmt.Errorf("error styling spec: %v\nOutput:\n%v", err, b.String()))
return
}
}
if styleLikeMatrixDotOrg { fis, err := ioutil.ReadDir(path.Join(dst, "scripts", "gen"))
cmd := exec.Command("./add-matrix-org-stylings.sh", *includesDir) if err != nil {
cmd.Dir = path.Join(dst, "scripts") writeError(w, 500, fmt.Errorf("Error reading directory: %v", err))
var b bytes.Buffer }
cmd.Stderr = &b pathToContent = make(map[string][]byte)
if err := cmd.Run(); err != nil { for _, fi := range fis {
writeError(w, 500, fmt.Errorf("error styling spec: %v\nOutput:\n%v", err, b.String())) b, err := ioutil.ReadFile(path.Join(dst, "scripts", "gen", fi.Name()))
return if err != nil {
writeError(w, 500, fmt.Errorf("Error reading spec: %v", err))
return
}
pathToContent[fi.Name()] = b
} }
cache.Add(sha, pathToContent)
} }
b, err := ioutil.ReadFile(path.Join(dst, "scripts/gen/specification.html")) requestedPath := extractPath(req.URL.Path, "/spec/pr")
if err != nil { if b, ok := pathToContent[requestedPath]; ok {
writeError(w, 500, fmt.Errorf("Error reading spec: %v", err)) w.Write(b)
return return
} }
w.Write(b) if requestedPath == "index.html" {
cache.Add(sha, b) // Fall back to single-page spec for old PRs
if b, ok := pathToContent["specification.html"]; ok {
w.Write(b)
return
}
}
w.WriteHeader(404)
w.Write([]byte("Not found"))
} }
// lookupHeadSHA looks up what origin/master's HEAD SHA is. // lookupHeadSHA looks up what origin/master's HEAD SHA is.
@ -322,7 +353,7 @@ func (s *server) serveRSTDiff(w http.ResponseWriter, req *http.Request) {
return return
} }
diffCmd := exec.Command("diff", "-u", path.Join(base, "scripts", "tmp", "full_spec.rst"), path.Join(head, "scripts", "tmp", "full_spec.rst")) diffCmd := exec.Command("diff", "-r", "-u", path.Join(base, "scripts", "tmp"), path.Join(head, "scripts", "tmp"))
var diff bytes.Buffer var diff bytes.Buffer
diffCmd.Stdout = &diff diffCmd.Stdout = &diff
if err := ignoreExitCodeOne(diffCmd.Run()); err != nil { if err := ignoreExitCodeOne(diffCmd.Run()); err != nil {
@ -366,7 +397,8 @@ func (s *server) serveHTMLDiff(w http.ResponseWriter, req *http.Request) {
return return
} }
cmd := exec.Command(htmlDiffer, path.Join(base, "scripts", "gen", "specification.html"), path.Join(head, "scripts", "gen", "specification.html")) requestedPath := extractPath(req.URL.Path, "/diff/spec/pr")
cmd := exec.Command(htmlDiffer, path.Join(base, "scripts", "gen", requestedPath), path.Join(head, "scripts", "gen", requestedPath))
var b bytes.Buffer var b bytes.Buffer
cmd.Stdout = &b cmd.Stdout = &b
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {

Loading…
Cancel
Save