-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate emails and github/gitlab issue bodies from a template (#84)
The driver for this is that I want to add a link to our internal rageshake-search tool to the body of reported issues. Rather than add more magic special-casing that, making the format of issue bodies more configurable seems a good thing.
- Loading branch information
Showing
9 changed files
with
255 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ import ( | |
"sort" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/google/go-github/github" | ||
|
@@ -47,6 +48,12 @@ import ( | |
var maxPayloadSize = 1024 * 1024 * 55 // 55 MB | ||
|
||
type submitServer struct { | ||
// Template for building github and gitlab issues | ||
issueTemplate *template.Template | ||
|
||
// Template for building emails | ||
emailTemplate *template.Template | ||
|
||
// github client for reporting bugs. may be nil, in which case, | ||
// reporting is disabled. | ||
ghClient *github.Client | ||
|
@@ -78,6 +85,16 @@ type jsonLogEntry struct { | |
Lines string `json:"lines"` | ||
} | ||
|
||
// `issueBodyTemplatePayload` contains the data made available to the `issue_body_template` and | ||
// `email_body_template`. | ||
// | ||
// !!! Keep in step with the documentation in `templates/README.md` !!! | ||
type issueBodyTemplatePayload struct { | ||
payload | ||
// Complete link to the listing URL that contains all uploaded logs | ||
ListingURL string | ||
} | ||
|
||
// Stores additional information created during processing of a payload | ||
type genericWebhookPayload struct { | ||
payload | ||
|
@@ -87,7 +104,10 @@ type genericWebhookPayload struct { | |
ListingURL string `json:"listing_url"` | ||
} | ||
|
||
// Stores information about a request made to this server | ||
// `payload` stores information about a request made to this server. | ||
// | ||
// !!! Since this is inherited by `issueBodyTemplatePayload`, remember to keep it in step | ||
// with the documentation in `templates/README.md` !!! | ||
type payload struct { | ||
// A unique ID for this payload, generated within this server | ||
ID string `json:"id"` | ||
|
@@ -505,7 +525,7 @@ func (s *submitServer) saveReport(ctx context.Context, p payload, reportDir, lis | |
return nil, err | ||
} | ||
|
||
if err := s.sendEmail(p, reportDir); err != nil { | ||
if err := s.sendEmail(p, reportDir, listingURL); err != nil { | ||
return nil, err | ||
} | ||
|
||
|
@@ -580,9 +600,12 @@ func (s *submitServer) submitGithubIssue(ctx context.Context, p payload, listing | |
} | ||
owner, repo := splits[0], splits[1] | ||
|
||
issueReq := buildGithubIssueRequest(p, listingURL) | ||
issueReq, err := buildGithubIssueRequest(p, listingURL, s.issueTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
issue, _, err := s.ghClient.Issues.Create(ctx, owner, repo, &issueReq) | ||
issue, _, err := s.ghClient.Issues.Create(ctx, owner, repo, issueReq) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -602,7 +625,10 @@ func (s *submitServer) submitGitlabIssue(p payload, listingURL string, resp *sub | |
glProj := s.cfg.GitlabProjectMappings[p.AppName] | ||
glLabels := s.cfg.GitlabProjectLabels[p.AppName] | ||
|
||
issueReq := buildGitlabIssueRequest(p, listingURL, glLabels, s.cfg.GitlabIssueConfidential) | ||
issueReq, err := buildGitlabIssueRequest(p, listingURL, s.issueTemplate, glLabels, s.cfg.GitlabIssueConfidential) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
issue, _, err := s.glClient.Issues.CreateIssue(glProj, issueReq) | ||
|
||
|
@@ -649,80 +675,72 @@ func buildReportTitle(p payload) string { | |
return trimmedUserText | ||
} | ||
|
||
func buildReportBody(p payload, newline, quoteChar string) *bytes.Buffer { | ||
func buildGenericIssueRequest(p payload, listingURL string, bodyTemplate *template.Template) (title string, body []byte, err error) { | ||
var bodyBuf bytes.Buffer | ||
fmt.Fprintf(&bodyBuf, "User message:\n\n%s\n\n", p.UserText) | ||
var dataKeys []string | ||
for k := range p.Data { | ||
dataKeys = append(dataKeys, k) | ||
} | ||
sort.Strings(dataKeys) | ||
for _, k := range dataKeys { | ||
v := p.Data[k] | ||
fmt.Fprintf(&bodyBuf, "%s: %s%s%s%s", k, quoteChar, v, quoteChar, newline) | ||
} | ||
|
||
return &bodyBuf | ||
} | ||
|
||
func buildGenericIssueRequest(p payload, listingURL string) (title, body string) { | ||
bodyBuf := buildReportBody(p, " \n", "`") | ||
|
||
// Add log links to the body | ||
fmt.Fprintf(bodyBuf, "\n[Logs](%s)", listingURL) | ||
fmt.Fprintf(bodyBuf, " ([archive](%s))", listingURL+"?format=tar.gz") | ||
issuePayload := issueBodyTemplatePayload{ | ||
payload: p, | ||
ListingURL: listingURL, | ||
} | ||
|
||
for _, file := range p.Files { | ||
fmt.Fprintf( | ||
bodyBuf, | ||
" / [%s](%s)", | ||
file, | ||
listingURL+"/"+file, | ||
) | ||
if err = bodyTemplate.Execute(&bodyBuf, issuePayload); err != nil { | ||
return | ||
} | ||
|
||
title = buildReportTitle(p) | ||
|
||
body = bodyBuf.String() | ||
body = bodyBuf.Bytes() | ||
|
||
return | ||
} | ||
|
||
func buildGithubIssueRequest(p payload, listingURL string) github.IssueRequest { | ||
title, body := buildGenericIssueRequest(p, listingURL) | ||
func buildGithubIssueRequest(p payload, listingURL string, bodyTemplate *template.Template) (*github.IssueRequest, error) { | ||
title, body, err := buildGenericIssueRequest(p, listingURL, bodyTemplate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
labels := p.Labels | ||
// go-github doesn't like nils | ||
if labels == nil { | ||
labels = []string{} | ||
} | ||
return github.IssueRequest{ | ||
bodyStr := string(body) | ||
return &github.IssueRequest{ | ||
Title: &title, | ||
Body: &body, | ||
Body: &bodyStr, | ||
Labels: &labels, | ||
} | ||
}, nil | ||
} | ||
|
||
func buildGitlabIssueRequest(p payload, listingURL string, labels []string, confidential bool) *gitlab.CreateIssueOptions { | ||
title, body := buildGenericIssueRequest(p, listingURL) | ||
func buildGitlabIssueRequest(p payload, listingURL string, bodyTemplate *template.Template, labels []string, confidential bool) (*gitlab.CreateIssueOptions, error) { | ||
title, body, err := buildGenericIssueRequest(p, listingURL, bodyTemplate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if p.Labels != nil { | ||
labels = append(labels, p.Labels...) | ||
} | ||
|
||
bodyStr := string(body) | ||
return &gitlab.CreateIssueOptions{ | ||
Title: &title, | ||
Description: &body, | ||
Description: &bodyStr, | ||
Confidential: &confidential, | ||
Labels: labels, | ||
} | ||
}, nil | ||
} | ||
|
||
func (s *submitServer) sendEmail(p payload, reportDir string) error { | ||
func (s *submitServer) sendEmail(p payload, reportDir string, listingURL string) error { | ||
if len(s.cfg.EmailAddresses) == 0 { | ||
return nil | ||
} | ||
|
||
title, body, err := buildGenericIssueRequest(p, listingURL, s.emailTemplate) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
e := email.NewEmail() | ||
|
||
e.From = "Rageshake <[email protected]>" | ||
|
@@ -731,10 +749,8 @@ func (s *submitServer) sendEmail(p payload, reportDir string) error { | |
} | ||
|
||
e.To = s.cfg.EmailAddresses | ||
|
||
e.Subject = fmt.Sprintf("[%s] %s", p.AppName, buildReportTitle(p)) | ||
|
||
e.Text = buildReportBody(p, "\n", "\"").Bytes() | ||
e.Subject = fmt.Sprintf("[%s] %s", p.AppName, title) | ||
e.Text = body | ||
|
||
allFiles := append(p.Files, p.Logs...) | ||
for _, file := range allFiles { | ||
|
@@ -746,7 +762,7 @@ func (s *submitServer) sendEmail(p payload, reportDir string) error { | |
if s.cfg.SMTPPassword != "" || s.cfg.SMTPUsername != "" { | ||
auth = smtp.PlainAuth("", s.cfg.SMTPUsername, s.cfg.SMTPPassword, s.cfg.SMTPServer) | ||
} | ||
err := e.Send(s.cfg.SMTPServer, auth) | ||
err = e.Send(s.cfg.SMTPServer, auth) | ||
if err != nil { | ||
return err | ||
} | ||
|
Oops, something went wrong.