Skip to content

Commit

Permalink
refact reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
tianfeng92 committed Nov 1, 2023
1 parent b3c37f6 commit b6d3a26
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 93 deletions.
3 changes: 1 addition & 2 deletions internal/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ 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 All @@ -314,7 +313,7 @@ func createReporters(c config.Reporters, ntfs config.Notifications, metadata con
}
}

buildReporter := buildtable.New(buildReader)
buildReporter := buildtable.New()
reps = append(reps, &buildReporter)

reps = append(reps, &slack.Reporter{
Expand Down
44 changes: 9 additions & 35 deletions internal/report/buildtable/buildtable.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
package buildtable

import (
"context"
"fmt"
"net/url"
"os"
"strings"

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

// Reporter is an implementation of report.Reporter
// It wraps a table reporter and decorates it with additional metadata
type Reporter struct {
Service build.Reader
VDCTableReport table.Reporter
RDCTableReport table.Reporter
}

func New(svc build.Reader) Reporter {
func New() Reporter {
return Reporter{
Service: svc,
VDCTableReport: table.Reporter{
Dst: os.Stdout,
},
Expand All @@ -49,18 +43,16 @@ func (r *Reporter) Render() {
printTitle()
printPadding(2)

var jURL string
var bURL string
if len(r.VDCTableReport.TestResults) > 0 {
r.VDCTableReport.Render()

for _, tr := range r.VDCTableReport.TestResults {
if tr.URL != "" {
jURL = tr.URL
var bURL string
for _, result := range r.VDCTableReport.TestResults {
if result.BuildURL != "" {
bURL = result.BuildURL
break
}
}
bURL = r.buildURLFromJobURL(jURL, build.VDC)

if bURL == "" {
bURL = "N/A"
Expand All @@ -72,13 +64,13 @@ func (r *Reporter) Render() {
if len(r.RDCTableReport.TestResults) > 0 {
r.RDCTableReport.Render()

for _, tr := range r.RDCTableReport.TestResults {
if tr.URL != "" {
jURL = tr.URL
var bURL string
for _, result := range r.RDCTableReport.TestResults {
if result.BuildURL != "" {
bURL = result.BuildURL
break
}
}
bURL = r.buildURLFromJobURL(jURL, build.RDC)

if bURL == "" {
bURL = "N/A"
Expand All @@ -100,24 +92,6 @@ func (r *Reporter) ArtifactRequirements() []report.ArtifactType {
return nil
}

func (r *Reporter) buildURLFromJobURL(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)
}

func printPadding(repeat int) {
fmt.Print(strings.Repeat("\n", repeat))
}
Expand Down
56 changes: 0 additions & 56 deletions internal/report/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ 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 @@ -32,7 +26,6 @@ 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 @@ -86,52 +79,3 @@ 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)
}
25 changes: 25 additions & 0 deletions internal/saucecloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"net/url"
"os"
"os/signal"
"path"
Expand Down Expand Up @@ -155,6 +156,7 @@ func (r *CloudRunner) collectResults(artifactCfg config.ArtifactDownload, result
if res.job.ID != "" {
url = fmt.Sprintf("%s/tests/%s", r.Region.AppBaseURL(), res.job.ID)
}
buildURL := r.getBuildURL(url, res.job.IsRDC)
tr := report.TestResult{
Name: res.name,
Duration: res.duration,
Expand All @@ -170,6 +172,7 @@ func (r *CloudRunner) collectResults(artifactCfg config.ArtifactDownload, result
RDC: res.job.IsRDC,
TimedOut: res.job.TimedOut,
Attempts: res.attempts,
BuildURL: buildURL,
}
for _, rep := range r.Reporters {
rep.Add(tr)
Expand All @@ -195,6 +198,28 @@ func (r *CloudRunner) collectResults(artifactCfg config.ArtifactDownload, result
return passed
}

func (r *CloudRunner) getBuildURL(jobURL string, isRDC bool) 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]

buildSource := build.RDC
if !isRDC {
buildSource = build.VDC
}
bID, err := r.BuildService.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)
}

func (r *CloudRunner) runJob(opts job.StartOptions) (j job.Job, skipped bool, err error) {
log.Info().Str("suite", opts.DisplayName).Str("region", r.Region.String()).Msg("Starting suite.")

Expand Down

0 comments on commit b6d3a26

Please sign in to comment.