Create a (template) github issue on report

This commit is contained in:
Richard van der Hoff 2017-04-07 15:43:19 +01:00 committed by Richard van der Hoff
parent f6003270db
commit 03d538ae4c
3 changed files with 64 additions and 7 deletions

View File

@ -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=<user> BUGS_PASS=<password> ./bin/rageshake PORT
# example:
# BUGS_USER=alice BUGS_PASS=secret ./bin/rageshake 8080
GITHUB_TOKEN=<token> BUGS_USER=<user> BUGS_PASS=<password> ./bin/rageshake <port>
```
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

View File

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

View File

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