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

feature: create a summary count of JIRAs created #24

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
flag.StringVar(&p.slackOutput, "slack-output", "", "Generate JSON output in slack format (use dash [-] for stdout)")
flag.StringVar(&p.htmlOutput, "html-output", "", "Generate HTML report to this file (use dash [-] for stdout)")
flag.StringVar(&p.csvOutput, "csv-output", "", "Convert XML to a CSV file (use dash [-] for stdout)")
flag.StringVar(&p.summaryOutput, "summary-output", "", "Write a summary in JSON to this file (use dash [-] for stdout)")
flag.StringVar(&jiraUrl, "jira-url", "https://issues.redhat.com/", "Url of JIRA instance")
flag.StringVar(&p.jiraProject, "jira-project", "ROX", "The JIRA project for issues")
flag.StringVar(&p.junitReportsDir, "junit-reports-dir", os.Getenv("ARTIFACT_DIR"), "Dir that contains jUnit reports XML files")
Expand Down Expand Up @@ -85,6 +86,7 @@ type junit2jira struct {

type testIssue struct {
issue *jira.Issue
newJIRA bool
testCase testCase
}

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

testSuites, err := junit.IngestDir(p.junitReportsDir)
if err != nil {
log.Fatalf("coud not read files: %s", err)
log.Fatalf("could not read files: %s", err)
}

err = j.createCsv(testSuites)
if err != nil {
log.Fatalf("coud create CSV: %s", err)
log.Fatalf("could not create CSV: %s", err)
}

failedTests, err := j.findFailedTests(testSuites)
if err != nil {
return errors.Wrap(err, "could not find failed tests")
}

issues, err := j.createIssuesOrComments(failedTests)
if err != nil {
return errors.Wrap(err, "could not create issues or comments")
}

err = j.createSlackMessage(issues)
if err != nil {
return errors.Wrap(err, "could not convert to slack")
Expand All @@ -138,6 +142,12 @@ func run(p params) error {
if err != nil {
return errors.Wrap(err, "could not link issues")
}

err = j.writeSummary(issues)
if err != nil {
return errors.Wrap(err, "could not write summary")
}

return errors.Wrap(j.createHtml(jiraIssues), "could not create HTML report")
}

Expand Down Expand Up @@ -281,6 +291,7 @@ func (j junit2jira) createIssueOrComment(tc testCase) (*testIssue, error) {
issue := findMatchingIssue(search, summary)
issueWithTestCase := testIssue{
issue: issue,
newJIRA: false,
gavin-stackrox marked this conversation as resolved.
Show resolved Hide resolved
testCase: tc,
}

Expand All @@ -297,6 +308,7 @@ func (j junit2jira) createIssueOrComment(tc testCase) (*testIssue, error) {
}
logEntry(create.Key, summary).Info("Created new issue")
issueWithTestCase.issue = create
issueWithTestCase.newJIRA = true
return &issueWithTestCase, nil
}

Expand All @@ -320,6 +332,49 @@ func (j junit2jira) createIssueOrComment(tc testCase) (*testIssue, error) {
return &issueWithTestCase, nil
}

func (j junit2jira) writeSummary(tc []*testIssue) error {
if j.summaryOutput == "" {
return nil
}
out := os.Stdout
if j.summaryOutput != "-" {
file, err := os.Create(j.summaryOutput)
if err != nil {
return fmt.Errorf("could not create file %s: %w", j.summaryOutput, err)
}
out = file
defer file.Close()
}

return generateSummary(tc, out)
}

type summary struct {
NewJIRAs int `json:"newJIRAs"`
}

func generateSummary(tc []*testIssue, output io.Writer) error {
newJIRAs := 0

for _, testIssue := range tc {
if testIssue.newJIRA {
newJIRAs++
}
}
summary := summary{
NewJIRAs: newJIRAs,
}

json, err := json.Marshal(summary)
if err != nil {
return err
}

_, err = output.Write(json)

return err
}

func logEntry(id, summary string) *log.Entry {

return log.WithField("ID", id).WithField("summary", summary)
Expand Down Expand Up @@ -546,6 +601,7 @@ type params struct {
csvOutput string
htmlOutput string
slackOutput string
summaryOutput string
}

func NewTestCase(tc junit.Test, p params) testCase {
Expand Down
41 changes: 39 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"bytes"
_ "embed"
"net/url"
"testing"

"github.com/andygrunwald/go-jira"
"github.com/joshdk/go-junit"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"testing"
)

func TestParseJunitReport(t *testing.T) {
Expand Down Expand Up @@ -363,3 +364,39 @@ func TestHtmlOutput(t *testing.T) {

assert.Equal(t, expectedHtmlOutput, buf.String())
}

//go:embed testdata/jira/expected-summary-no-new-jiras.json
gavin-stackrox marked this conversation as resolved.
Show resolved Hide resolved
var expectedSummaryNoNewJIRAs string

func TestSummaryNoNewJIRAs(t *testing.T) {
buf := bytes.NewBufferString("")
require.NoError(t, generateSummary(nil, buf))
assert.Equal(t, expectedSummaryNoNewJIRAs, buf.String())
}

//go:embed testdata/jira/expected-summary-some-new-jiras.json
var expectedSummarySomeNewJIRAs string

func TestSummaryNoFailures(t *testing.T) {
tc := []*testIssue{
{
issue: &jira.Issue{Key: "ROX-1"},
newJIRA: false,
testCase: testCase{},
},
{
issue: &jira.Issue{Key: "ROX-2"},
newJIRA: true,
testCase: testCase{},
},
{
issue: &jira.Issue{Key: "ROX-3"},
newJIRA: true,
testCase: testCase{},
},
}

buf := bytes.NewBufferString("")
require.NoError(t, generateSummary(tc, buf))
assert.Equal(t, expectedSummarySomeNewJIRAs, buf.String())
}
1 change: 1 addition & 0 deletions testdata/jira/expected-summary-no-new-jiras.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"newJIRAs":0}
1 change: 1 addition & 0 deletions testdata/jira/expected-summary-some-new-jiras.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"newJIRAs":2}
Loading