Skip to content

Commit

Permalink
feat: Add buildURL in JSON report
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 committed Nov 1, 2023
1 parent 5121075 commit b3c37f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func createReporters(c config.Reporters, ntfs config.Notifications, metadata con
}
if c.JSON.Enabled {
reps = append(reps, &json.Reporter{
Service: buildReader,
WebhookURL: c.JSON.WebhookURL,
Filename: c.JSON.Filename,
})
Expand Down
56 changes: 56 additions & 0 deletions internal/report/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ package json

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"

"github.com/rs/zerolog/log"
"github.com/saucelabs/saucectl/internal/build"
"github.com/saucelabs/saucectl/internal/report"
)

// Reporter represents struct to report in json format
type Reporter struct {
Service build.Reader
WebhookURL string
Filename string
Results []report.TestResult
Expand All @@ -26,6 +32,7 @@ func (r *Reporter) Add(t report.TestResult) {
// Render sends the result to specified webhook WebhookURL and log the result to the specified json file
func (r *Reporter) Render() {
r.cleanup()
r.buildData()
body, err := json.Marshal(r.Results)
if err != nil {
log.Error().Msgf("failed to generate test result (%v)", err)
Expand Down Expand Up @@ -79,3 +86,52 @@ func (r *Reporter) Reset() {
func (r *Reporter) ArtifactRequirements() []report.ArtifactType {
return nil
}

func (r *Reporter) buildData() {
if len(r.Results) < 1 {
return
}

var vdcJobURL string
var rdcJobURL string
for _, result := range r.Results {
if !result.RDC && result.URL != "" {
vdcJobURL = result.URL
break
}
}
for _, result := range r.Results {
if result.RDC && result.URL != "" {
rdcJobURL = result.URL
break
}
}
vdcBuildURL := r.getBuildURL(vdcJobURL, build.VDC)
rdcBuildURL := r.getBuildURL(rdcJobURL, build.RDC)
for i, result := range r.Results {
if !result.RDC {
result.BuildURL = vdcBuildURL
} else {
result.BuildURL = rdcBuildURL
}
r.Results[i] = result
}
}

func (r *Reporter) getBuildURL(jobURL string, buildSource build.Source) string {
pURL, err := url.Parse(jobURL)
if err != nil {
log.Debug().Err(err).Msgf("Failed to parse job url (%s)", jobURL)
return ""
}
p := strings.Split(pURL.Path, "/")
jID := p[len(p)-1]

bID, err := r.Service.GetBuildID(context.Background(), jID, buildSource)
if err != nil {
log.Debug().Err(err).Msgf("Failed to retrieve build id for job (%s)", jID)
return ""
}

return fmt.Sprintf("%s://%s/builds/%s/%s", pURL.Scheme, pURL.Host, buildSource, bID)
}
1 change: 1 addition & 0 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type TestResult struct {
URL string `json:"url"`
Artifacts []Artifact `json:"artifacts,omitempty"`
Origin string `json:"origin"`
BuildURL string `json:"buildURL"`
RDC bool `json:"-"`
TimedOut bool `json:"-"`
PassThreshold bool `json:"-"`
Expand Down

0 comments on commit b3c37f6

Please sign in to comment.