From 40a6eec4e144b974aea829fb200700fd9d931acf Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 18 Apr 2017 11:55:22 +0100 Subject: [PATCH] Send github URL back to application (#10) I'm not sure if this is going to be useful, but it's fairly harmless, and is annoying me by being on a branch. --- README.md | 7 +++-- src/github.com/matrix-org/rageshake/submit.go | 29 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c777f12..67a329d 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ The body of the request should be a JSON object with the following fields: * `lines`: log data. Lines should be separated by newline characters (encoded as `\n`, as normal in JSON). -* `data`: a set of arbitrary name/value strings to include in the - `details.log.gz` file. (Note that the values must be strings; numbers, - objects and arrays will be rejected). +The response (if successful) will be a JSON object with the following fields: + +* `report_url`: A URL where the user can track their bug report. Omitted if + issue submission was disabled. diff --git a/src/github.com/matrix-org/rageshake/submit.go b/src/github.com/matrix-org/rageshake/submit.go index 574cab4..440cec2 100644 --- a/src/github.com/matrix-org/rageshake/submit.go +++ b/src/github.com/matrix-org/rageshake/submit.go @@ -61,6 +61,10 @@ type logEntry struct { Lines string `json:"lines"` } +type submitResponse struct { + ReportURL string `json:"report_url,omitempty"` +} + func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { if req.Method != "POST" && req.Method != "OPTIONS" { respond(405, w) @@ -82,13 +86,16 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } - if err := s.saveReport(req.Context(), *p); err != nil { + resp, err := s.saveReport(req.Context(), *p) + if err != nil { log.Println("Error handling report", err) http.Error(w, "Internal error", 500) return } - respond(200, w) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + json.NewEncoder(w).Encode(resp) } // parseRequest attempts to parse a received request as a bug report. If @@ -144,7 +151,9 @@ func parseRequest(w http.ResponseWriter, req *http.Request) *payload { return &p } -func (s *submitServer) saveReport(ctx context.Context, p payload) error { +func (s *submitServer) saveReport(ctx context.Context, p payload) (*submitResponse, error) { + resp := submitResponse{} + // Dump bug report to disk as form: // "bugreport-20170115-112233.log.gz" => user text, version, user agent, # logs // "bugreport-20170115-112233-0.log.gz" => most recent log @@ -166,26 +175,26 @@ func (s *submitServer) saveReport(ctx context.Context, p payload) error { fmt.Fprintf(&summaryBuf, "%s: %s\n", k, v) } if err := gzipAndSave(summaryBuf.Bytes(), prefix, "details.log.gz"); err != nil { - return err + return nil, err } for i, log := range p.Logs { if err := gzipAndSave([]byte(log.Lines), prefix, fmt.Sprintf("logs-%d.log.gz", i)); err != nil { - return err // TODO: Rollback? + return nil, err // TODO: Rollback? } } if s.ghClient == nil { // we're done here log.Println("GH issue submission disabled") - return nil + return &resp, nil } // submit a github issue ghProj := s.githubProjectMappings[p.AppName] if ghProj == "" { log.Println("Not creating GH issue for unknown app", p.AppName) - return nil + return &resp, nil } splits := strings.SplitN(ghProj, "/", 2) if len(splits) < 2 { @@ -197,12 +206,14 @@ func (s *submitServer) saveReport(ctx context.Context, p payload) error { issue, _, err := s.ghClient.Issues.Create(ctx, owner, repo, &issueReq) if err != nil { - return err + return nil, err } log.Println("Created issue:", *issue.HTMLURL) - return nil + resp.ReportURL = *issue.HTMLURL + + return &resp, nil } func buildGithubIssueRequest(p payload, listingURL string) github.IssueRequest {