From 03d538ae4c22475741bce70099b93bf7bdba98ef Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 7 Apr 2017 15:43:19 +0100 Subject: [PATCH] Create a (template) github issue on report --- README.md | 16 +++++++-- src/github.com/matrix-org/rageshake/main.go | 20 ++++++++++- src/github.com/matrix-org/rageshake/submit.go | 35 +++++++++++++++++-- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2f4c816..16aaea7 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,26 @@ Web service which collects and serves bug reports. +rageshake requires Go version 1.7 or later. + To run it, do: ``` go get github.com/constabulary/gb/... gb build -BUGS_USER= BUGS_PASS= ./bin/rageshake PORT -# example: -# BUGS_USER=alice BUGS_PASS=secret ./bin/rageshake 8080 +GITHUB_TOKEN= BUGS_USER= BUGS_PASS= ./bin/rageshake ``` +where: + + * `token` is a GitHub personal access token + (https://github.com/settings/tokens), which will be used to create a GitHub + issue for each report. It requires `public_repo` scope. If omitted, no + issues will be created. + * `user` and `password` are a username/password pair which will be required to + access the bug report listings at `/api/listing`, via HTTP basic auth. + If omitted, there will be *no* authentication on this access! + * `port` is the TCP port to listen on. ## HTTP endpoints diff --git a/src/github.com/matrix-org/rageshake/main.go b/src/github.com/matrix-org/rageshake/main.go index bf27c42..d0af000 100644 --- a/src/github.com/matrix-org/rageshake/main.go +++ b/src/github.com/matrix-org/rageshake/main.go @@ -17,8 +17,11 @@ limitations under the License. package main import ( + "context" "crypto/subtle" "fmt" + "github.com/google/go-github/github" + "golang.org/x/oauth2" "log" "net/http" "os" @@ -41,7 +44,22 @@ func basicAuth(handler http.Handler, username, password, realm string) http.Hand } func main() { - http.Handle("/api/submit", &submitServer{}) + ghToken := os.Getenv("GITHUB_TOKEN") + + var ghClient *github.Client + + if ghToken == "" { + fmt.Println("No GITHUB_TOKEN env var set. Reporting bugs to github is disabled.") + } else { + ctx := context.Background() + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: ghToken}, + ) + tc := oauth2.NewClient(ctx, ts) + ghClient = github.NewClient(tc) + } + + http.Handle("/api/submit", &submitServer{ghClient}) // Make sure bugs directory exists _ = os.Mkdir("bugs", os.ModePerm) diff --git a/src/github.com/matrix-org/rageshake/submit.go b/src/github.com/matrix-org/rageshake/submit.go index c10f189..d05e812 100644 --- a/src/github.com/matrix-org/rageshake/submit.go +++ b/src/github.com/matrix-org/rageshake/submit.go @@ -19,9 +19,12 @@ package main import ( "bytes" "compress/gzip" + "context" "encoding/json" "fmt" + "github.com/google/go-github/github" "io/ioutil" + "log" "net/http" "os" "path/filepath" @@ -32,6 +35,9 @@ import ( var maxPayloadSize = 1024 * 1024 * 55 // 55 MB type submitServer struct { + // github client for reporting bugs. may be nil, in which case, + // reporting is disabled. + ghClient *github.Client } type payload struct { @@ -51,6 +57,7 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { respond(405, w) return } + // Set CORS w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS") @@ -69,15 +76,16 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } - if err := saveReport(p); err != nil { - respond(500, w) + if err := s.saveReport(req.Context(), p); err != nil { + log.Println("Error handling report", err) + http.Error(w, "Internal error", 500) return } respond(200, w) } -func saveReport(p payload) error { +func (s *submitServer) saveReport(ctx context.Context, p payload) error { // 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 @@ -96,6 +104,27 @@ func saveReport(p payload) error { return err // TODO: Rollback? } } + + if s.ghClient == nil { + // we're done here + return nil + } + + // submit a github issue + owner := "richvdh" + repo := "test" + title := "Automated bug report" + issueReq := github.IssueRequest{ + Title: &title, + } + + issue, _, err := s.ghClient.Issues.Create(ctx, owner, repo, &issueReq) + if err != nil { + return err + } + + log.Println("Created issue:", *issue.HTMLURL) + return nil }