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.
This commit is contained in:
parent
a3e3561467
commit
40a6eec4e1
2 changed files with 24 additions and 12 deletions
|
@ -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
|
* `lines`: log data. Lines should be separated by newline characters (encoded
|
||||||
as `\n`, as normal in JSON).
|
as `\n`, as normal in JSON).
|
||||||
|
|
||||||
* `data`: a set of arbitrary name/value strings to include in the
|
The response (if successful) will be a JSON object with the following fields:
|
||||||
`details.log.gz` file. (Note that the values must be strings; numbers,
|
|
||||||
objects and arrays will be rejected).
|
* `report_url`: A URL where the user can track their bug report. Omitted if
|
||||||
|
issue submission was disabled.
|
||||||
|
|
|
@ -61,6 +61,10 @@ type logEntry struct {
|
||||||
Lines string `json:"lines"`
|
Lines string `json:"lines"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type submitResponse struct {
|
||||||
|
ReportURL string `json:"report_url,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.Method != "POST" && req.Method != "OPTIONS" {
|
if req.Method != "POST" && req.Method != "OPTIONS" {
|
||||||
respond(405, w)
|
respond(405, w)
|
||||||
|
@ -82,13 +86,16 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
return
|
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)
|
log.Println("Error handling report", err)
|
||||||
http.Error(w, "Internal error", 500)
|
http.Error(w, "Internal error", 500)
|
||||||
return
|
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
|
// 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
|
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:
|
// Dump bug report to disk as form:
|
||||||
// "bugreport-20170115-112233.log.gz" => user text, version, user agent, # logs
|
// "bugreport-20170115-112233.log.gz" => user text, version, user agent, # logs
|
||||||
// "bugreport-20170115-112233-0.log.gz" => most recent log
|
// "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)
|
fmt.Fprintf(&summaryBuf, "%s: %s\n", k, v)
|
||||||
}
|
}
|
||||||
if err := gzipAndSave(summaryBuf.Bytes(), prefix, "details.log.gz"); err != nil {
|
if err := gzipAndSave(summaryBuf.Bytes(), prefix, "details.log.gz"); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, log := range p.Logs {
|
for i, log := range p.Logs {
|
||||||
if err := gzipAndSave([]byte(log.Lines), prefix, fmt.Sprintf("logs-%d.log.gz", i)); err != nil {
|
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 {
|
if s.ghClient == nil {
|
||||||
// we're done here
|
// we're done here
|
||||||
log.Println("GH issue submission disabled")
|
log.Println("GH issue submission disabled")
|
||||||
return nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// submit a github issue
|
// submit a github issue
|
||||||
ghProj := s.githubProjectMappings[p.AppName]
|
ghProj := s.githubProjectMappings[p.AppName]
|
||||||
if ghProj == "" {
|
if ghProj == "" {
|
||||||
log.Println("Not creating GH issue for unknown app", p.AppName)
|
log.Println("Not creating GH issue for unknown app", p.AppName)
|
||||||
return nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
splits := strings.SplitN(ghProj, "/", 2)
|
splits := strings.SplitN(ghProj, "/", 2)
|
||||||
if len(splits) < 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)
|
issue, _, err := s.ghClient.Issues.Create(ctx, owner, repo, &issueReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("Created issue:", *issue.HTMLURL)
|
log.Println("Created issue:", *issue.HTMLURL)
|
||||||
|
|
||||||
return nil
|
resp.ReportURL = *issue.HTMLURL
|
||||||
|
|
||||||
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildGithubIssueRequest(p payload, listingURL string) github.IssueRequest {
|
func buildGithubIssueRequest(p payload, listingURL string) github.IssueRequest {
|
||||||
|
|
Reference in a new issue