Skip to content

Commit 03b0e38

Browse files
feature: create a summary count of JIRAs created (#24)
1 parent 19463f3 commit 03b0e38

2 files changed

Lines changed: 92 additions & 4 deletions

File tree

main.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func main() {
4545
flag.StringVar(&p.slackOutput, "slack-output", "", "Generate JSON output in slack format (use dash [-] for stdout)")
4646
flag.StringVar(&p.htmlOutput, "html-output", "", "Generate HTML report to this file (use dash [-] for stdout)")
4747
flag.StringVar(&p.csvOutput, "csv-output", "", "Convert XML to a CSV file (use dash [-] for stdout)")
48+
flag.StringVar(&p.summaryOutput, "summary-output", "", "Write a summary in JSON to this file (use dash [-] for stdout)")
4849
flag.StringVar(&jiraUrl, "jira-url", "https://issues.redhat.com/", "Url of JIRA instance")
4950
flag.StringVar(&p.jiraProject, "jira-project", "ROX", "The JIRA project for issues")
5051
flag.StringVar(&p.junitReportsDir, "junit-reports-dir", os.Getenv("ARTIFACT_DIR"), "Dir that contains jUnit reports XML files")
@@ -85,6 +86,7 @@ type junit2jira struct {
8586

8687
type testIssue struct {
8788
issue *jira.Issue
89+
newJIRA bool
8890
testCase testCase
8991
}
9092

@@ -108,22 +110,24 @@ func run(p params) error {
108110

109111
testSuites, err := junit.IngestDir(p.junitReportsDir)
110112
if err != nil {
111-
log.Fatalf("coud not read files: %s", err)
113+
log.Fatalf("could not read files: %s", err)
112114
}
113115

114116
err = j.createCsv(testSuites)
115117
if err != nil {
116-
log.Fatalf("coud create CSV: %s", err)
118+
log.Fatalf("could not create CSV: %s", err)
117119
}
118120

119121
failedTests, err := j.findFailedTests(testSuites)
120122
if err != nil {
121123
return errors.Wrap(err, "could not find failed tests")
122124
}
125+
123126
issues, err := j.createIssuesOrComments(failedTests)
124127
if err != nil {
125128
return errors.Wrap(err, "could not create issues or comments")
126129
}
130+
127131
err = j.createSlackMessage(issues)
128132
if err != nil {
129133
return errors.Wrap(err, "could not convert to slack")
@@ -138,6 +142,12 @@ func run(p params) error {
138142
if err != nil {
139143
return errors.Wrap(err, "could not link issues")
140144
}
145+
146+
err = j.writeSummary(issues)
147+
if err != nil {
148+
return errors.Wrap(err, "could not write summary")
149+
}
150+
141151
return errors.Wrap(j.createHtml(jiraIssues), "could not create HTML report")
142152
}
143153

@@ -297,6 +307,7 @@ func (j junit2jira) createIssueOrComment(tc testCase) (*testIssue, error) {
297307
}
298308
logEntry(create.Key, summary).Info("Created new issue")
299309
issueWithTestCase.issue = create
310+
issueWithTestCase.newJIRA = true
300311
return &issueWithTestCase, nil
301312
}
302313

@@ -320,6 +331,49 @@ func (j junit2jira) createIssueOrComment(tc testCase) (*testIssue, error) {
320331
return &issueWithTestCase, nil
321332
}
322333

334+
func (j junit2jira) writeSummary(tc []*testIssue) error {
335+
if j.summaryOutput == "" {
336+
return nil
337+
}
338+
out := os.Stdout
339+
if j.summaryOutput != "-" {
340+
file, err := os.Create(j.summaryOutput)
341+
if err != nil {
342+
return fmt.Errorf("could not create file %s: %w", j.summaryOutput, err)
343+
}
344+
out = file
345+
defer file.Close()
346+
}
347+
348+
return generateSummary(tc, out)
349+
}
350+
351+
type summary struct {
352+
NewJIRAs int `json:"newJIRAs"`
353+
}
354+
355+
func generateSummary(tc []*testIssue, output io.Writer) error {
356+
newJIRAs := 0
357+
358+
for _, testIssue := range tc {
359+
if testIssue.newJIRA {
360+
newJIRAs++
361+
}
362+
}
363+
summary := summary{
364+
NewJIRAs: newJIRAs,
365+
}
366+
367+
json, err := json.Marshal(summary)
368+
if err != nil {
369+
return err
370+
}
371+
372+
_, err = output.Write(json)
373+
374+
return err
375+
}
376+
323377
func logEntry(id, summary string) *log.Entry {
324378

325379
return log.WithField("ID", id).WithField("summary", summary)
@@ -546,6 +600,7 @@ type params struct {
546600
csvOutput string
547601
htmlOutput string
548602
slackOutput string
603+
summaryOutput string
549604
}
550605

551606
func NewTestCase(tc junit.Test, p params) testCase {

main_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package main
33
import (
44
"bytes"
55
_ "embed"
6+
"net/url"
7+
"testing"
8+
69
"github.com/andygrunwald/go-jira"
710
"github.com/joshdk/go-junit"
811
"github.com/stretchr/testify/assert"
912
"github.com/stretchr/testify/require"
10-
"net/url"
11-
"testing"
1213
)
1314

1415
func TestParseJunitReport(t *testing.T) {
@@ -363,3 +364,35 @@ func TestHtmlOutput(t *testing.T) {
363364

364365
assert.Equal(t, expectedHtmlOutput, buf.String())
365366
}
367+
368+
func TestSummaryNoNewJIRAs(t *testing.T) {
369+
expectedSummaryNoNewJIRAs := `{"newJIRAs":0}`
370+
buf := bytes.NewBufferString("")
371+
require.NoError(t, generateSummary(nil, buf))
372+
assert.Equal(t, expectedSummaryNoNewJIRAs, buf.String())
373+
}
374+
375+
func TestSummaryNoFailures(t *testing.T) {
376+
expectedSummarySomeNewJIRAs := `{"newJIRAs":2}`
377+
tc := []*testIssue{
378+
{
379+
issue: &jira.Issue{Key: "ROX-1"},
380+
newJIRA: false,
381+
testCase: testCase{},
382+
},
383+
{
384+
issue: &jira.Issue{Key: "ROX-2"},
385+
newJIRA: true,
386+
testCase: testCase{},
387+
},
388+
{
389+
issue: &jira.Issue{Key: "ROX-3"},
390+
newJIRA: true,
391+
testCase: testCase{},
392+
},
393+
}
394+
395+
buf := bytes.NewBufferString("")
396+
require.NoError(t, generateSummary(tc, buf))
397+
assert.Equal(t, expectedSummarySomeNewJIRAs, buf.String())
398+
}

0 commit comments

Comments
 (0)