@@ -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
8687type 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+
323377func 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
551606func NewTestCase (tc junit.Test , p params ) testCase {
0 commit comments