Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist prefix as a unique id in rageshakes #54

Merged
merged 6 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/54.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pass the prefix as a unique ID for the rageshake to the generic webhook mechanism.
18 changes: 8 additions & 10 deletions logserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func (f *logServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
serveFile(w, r, upath)
}


func serveFile(w http.ResponseWriter, r *http.Request, path string) {
d, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -144,9 +143,9 @@ func serveDirectory(w http.ResponseWriter, r *http.Request, path string) {
}

// Streams a dynamically created tar.gz file with the contents of the given directory
// Will serve a partial, corrupted response if there is a error partway through the
// Will serve a partial, corrupted response if there is a error partway through the
// operation as we stream the response.
//
//
// The resultant tarball will contain a single directory containing all the files
// so it can unpack cleanly without overwriting other files.
//
Expand All @@ -163,14 +162,14 @@ func serveTarball(w http.ResponseWriter, r *http.Request, dir string) error {
// and removes leading and trailing `/` and replaces internal `/` with `_`
// to form a suitable filename for use in the content-disposition header
// dfilename would turn into `2022-01-10_184843-BZZXEGYH`
dfilename := strings.Trim(r.URL.Path,"/")
dfilename = strings.Replace(dfilename, "/","_",-1)
dfilename := strings.Trim(r.URL.Path, "/")
dfilename = strings.Replace(dfilename, "/", "_", -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for future reference: it would be great to put this sort of reformatting in a separate PR, so that we can easily see what's changing and what's not.


// There is no application/tgz or similar; return a gzip file as best option.
// This tends to trigger archive type tools, which will then use the filename to
// There is no application/tgz or similar; return a gzip file as best option.
// This tends to trigger archive type tools, which will then use the filename to
// identify the contents correctly.
w.Header().Set("Content-Type", "application/gzip")
w.Header().Set("Content-Disposition", "attachment; filename=" + dfilename + ".tar.gz")
w.Header().Set("Content-Disposition", "attachment; filename="+dfilename+".tar.gz")

files, err := directory.Readdir(-1)
if err != nil {
Expand All @@ -182,7 +181,6 @@ func serveTarball(w http.ResponseWriter, r *http.Request, dir string) error {
targz := tar.NewWriter(gzip)
defer targz.Close()


for _, file := range files {
if file.IsDir() {
// We avoid including nested directories
Expand All @@ -206,7 +204,7 @@ func serveTarball(w http.ResponseWriter, r *http.Request, dir string) error {
return nil
}

// Add a single file into the archive.
// Add a single file into the archive.
func addToArchive(targz *tar.Writer, dfilename string, filename string) error {
file, err := os.Open(filename)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func main() {
log.Fatal(http.ListenAndServe(*bindAddr, nil))
}

func configureGenericWebhookClient(cfg *config) (*http.Client) {
func configureGenericWebhookClient(cfg *config) *http.Client {
if len(cfg.GenericWebhookURLs) == 0 {
fmt.Println("No generic_webhook_urls configured.")
return nil
Expand Down
20 changes: 12 additions & 8 deletions submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type submitServer struct {
slack *slackClient

genericWebhookClient *http.Client
cfg *config
cfg *config
}

// the type of payload which can be uploaded as JSON to the submit endpoint
Expand All @@ -77,15 +77,15 @@ type jsonLogEntry struct {
Lines string `json:"lines"`
}


type genericWebhookPayload struct {
parsedPayload
ReportURL string `json:"report_url"`
ListingURL string `json:"listing_url"`
ReportURL string `json:"report_url"`
ListingURL string `json:"listing_url"`
}

// the payload after parsing
type parsedPayload struct {
michaelkaye marked this conversation as resolved.
Show resolved Hide resolved
ID string `json:"id"`
richvdh marked this conversation as resolved.
Show resolved Hide resolved
UserText string `json:"user_text"`
AppName string `json:"app"`
Data map[string]string `json:"data"`
Expand Down Expand Up @@ -179,6 +179,11 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

// We use this prefix (eg, 2022-05-01/125223-abcde) as a unique identifier for this rageshake.
// This is going to be used to uniquely identify rageshakes, even if they are not submitted to
// an issue tracker for instance with automatic rageshakes that can be plentiful
p.ID = prefix

resp, err := s.saveReport(req.Context(), *p, reportDir, listingURL)
if err != nil {
log.Println("Error handling report submission:", err)
Expand Down Expand Up @@ -509,7 +514,7 @@ func (s *submitServer) saveReport(ctx context.Context, p parsedPayload, reportDi

// submitGenericWebhook submits a basic JSON body to an endpoint configured in the config
//
// The request does not include the log body, only the metadata in the parsedPayload,
// The request does not include the log body, only the metadata in the parsedPayload,
// with the required listingURL to obtain the logs over http if required.
//
// If a github or gitlab issue was previously made, the reportURL will also be passed.
Expand All @@ -523,8 +528,8 @@ func (s *submitServer) submitGenericWebhook(p parsedPayload, listingURL string,
}
genericHookPayload := genericWebhookPayload{
parsedPayload: p,
ReportURL: reportURL,
ListingURL: listingURL,
ReportURL: reportURL,
ListingURL: listingURL,
}
for _, url := range s.cfg.GenericWebhookURLs {
// Enrich the parsedPayload with a reportURL and listingURL, to convert a single struct
Expand Down Expand Up @@ -554,7 +559,6 @@ func (s *submitServer) sendGenericWebhook(req *http.Request) {
}
}


func (s *submitServer) submitGithubIssue(ctx context.Context, p parsedPayload, listingURL string, resp *submitResponse) error {
if s.ghClient == nil {
return nil
Expand Down