-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from kosli-dev/jira-evidence-in-branch-name
Add `--assert` option for reporting jira evidence
- Loading branch information
Showing
6 changed files
with
112 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ type reportEvidenceCommitJiraOptions struct { | |
pat string | ||
srcRepoRoot string | ||
payload JiraEvidencePayload | ||
assert bool | ||
} | ||
|
||
const reportEvidenceCommitJiraShortDesc = `Report Jira evidence for a commit in Kosli flows.` | ||
|
@@ -75,6 +76,20 @@ kosli report evidence commit jira \ | |
--api-token yourAPIToken \ | ||
--org yourOrgName \ | ||
--user-data /path/to/json/file.json | ||
# fail if no issue reference is found, or the issue is not found in your jira instance | ||
kosli report evidence commit jira \ | ||
--commit yourGitCommitSha1 \ | ||
--name yourEvidenceName \ | ||
--jira-base-url https://kosli.atlassian.net \ | ||
--jira-username [email protected] \ | ||
--jira-api-token yourJiraAPIToken \ | ||
--flows yourFlowName \ | ||
--build-url https://exampleci.com \ | ||
--api-token yourAPIToken \ | ||
--org yourOrgName \ | ||
--assert | ||
` | ||
|
||
func newReportEvidenceCommitJiraCmd(out io.Writer) *cobra.Command { | ||
|
@@ -117,6 +132,7 @@ func newReportEvidenceCommitJiraCmd(out io.Writer) *cobra.Command { | |
cmd.Flags().StringVar(&o.srcRepoRoot, "repo-root", ".", repoRootFlag) | ||
cmd.Flags().StringVarP(&o.userDataFilePath, "user-data", "u", "", evidenceUserDataFlag) | ||
cmd.Flags().StringSliceVarP(&o.evidencePaths, "evidence-paths", "e", []string{}, evidencePathsFlag) | ||
cmd.Flags().BoolVar(&o.assert, "assert", false, assertJiraEvidenceFlag) | ||
addDryRunFlag(cmd) | ||
|
||
err := RequireFlags(cmd, []string{"commit", "build-url", "name", "jira-base-url"}) | ||
|
@@ -145,6 +161,7 @@ func (o *reportEvidenceCommitJiraOptions) run(args []string) error { | |
return err | ||
} | ||
|
||
logger.Debug("Resolving commit reference: %s", o.payload.CommitSHA) | ||
o.payload.CommitSHA, err = gv.ResolveRevision(o.payload.CommitSHA) | ||
if err != nil { | ||
return err | ||
|
@@ -157,14 +174,19 @@ func (o *reportEvidenceCommitJiraOptions) run(args []string) error { | |
// more info: https://support.atlassian.com/jira-software-cloud/docs/what-is-an-issue/#Workingwithissues-Projectandissuekeys | ||
jiraIssueKeyPattern := `[A-Z][A-Z0-9]{1,9}-[0-9]+` | ||
|
||
issueIDs, err := gv.MatchPatternInCommitMessageORBranchName(jiraIssueKeyPattern, o.payload.CommitSHA) | ||
issueIDs, commitInfo, err := gv.MatchPatternInCommitMessageORBranchName(jiraIssueKeyPattern, o.payload.CommitSHA) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Debug("Checked for Jira issue references in Git commit %s on branch %s commit message:\n%s", commitInfo.Sha1, commitInfo.Branch, commitInfo.Message) | ||
logger.Debug("the following Jira references are found in commit message or branch name: %v", issueIDs) | ||
|
||
if len(issueIDs) == 0 && o.assert { | ||
return fmt.Errorf("no Jira references are found in commit message or branch name") | ||
} | ||
|
||
issueLog := "" | ||
issueFoundCount := 0 | ||
for _, issueID := range issueIDs { | ||
result, err := jc.GetJiraIssueInfo(issueID) | ||
if err != nil { | ||
|
@@ -174,9 +196,13 @@ func (o *reportEvidenceCommitJiraOptions) run(args []string) error { | |
issueExistLog := "issue not found" | ||
if result.IssueExists { | ||
issueExistLog = "issue found" | ||
issueFoundCount++ | ||
} | ||
issueLog += fmt.Sprintf("\n\t%s: %s", result.IssueID, issueExistLog) | ||
} | ||
if issueFoundCount != len(issueIDs) && o.assert { | ||
return fmt.Errorf("missing Jira issues from references found in commit message or branch name%s", issueLog) | ||
} | ||
|
||
form, cleanupNeeded, evidencePath, err := newEvidenceForm(o.payload, o.evidencePaths) | ||
// if we created a tar package, remove it after uploading it | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TearDownSuite() { | |
|
||
type jiraTestsAdditionalConfig struct { | ||
commitMessage string | ||
branchName string | ||
} | ||
|
||
func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCmd() { | ||
|
@@ -122,6 +123,18 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCm | |
commitMessage: "Lets test EX-1", | ||
}, | ||
}, | ||
{ | ||
name: "report Jira commit evidence with reference as branch name works", | ||
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation | ||
--jira-base-url https://kosli-test.atlassian.net/ --jira-username [email protected] | ||
--repo-root %s | ||
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments), | ||
goldenRegex: "Jira evidence is reported to commit: [0-9a-f]{40}\n.*Issues references reported:.*\n.*EX-1: issue found", | ||
additionalConfig: jiraTestsAdditionalConfig{ | ||
branchName: "EX-1", | ||
commitMessage: "test commit has no reference", | ||
}, | ||
}, | ||
{ | ||
name: "report Jira commit evidence with a slash at the end of --jira-base-url works", | ||
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation | ||
|
@@ -136,7 +149,7 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCm | |
{ | ||
wantError: true, | ||
name: "report Jira commit evidence with --jira-pat and --jira-api-token fails", | ||
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation | ||
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation | ||
--jira-base-url https://kosli-test.atlassian.net --jira-api-token xxx | ||
--jira-pat xxxx --repo-root %s --commit 61ab3ea22bd4264996b35bfb82869c482d9f4a06 | ||
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments), | ||
|
@@ -160,18 +173,54 @@ func (suite *CommitEvidenceJiraCommandTestSuite) TestCommitEvidenceJiraCommandCm | |
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments), | ||
golden: "Error: required flag(s) \"commit\" not set\n", | ||
}, | ||
{ | ||
wantError: true, | ||
name: "assert for non-existing Jira issue gives an error", | ||
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation | ||
--jira-base-url https://kosli-test.atlassian.net --jira-username [email protected] | ||
--repo-root %s | ||
--assert | ||
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments), | ||
goldenRegex: "Error: missing Jira issues from references found in commit message or branch name.*", | ||
additionalConfig: jiraTestsAdditionalConfig{ | ||
commitMessage: "SAMI-1 test commit", | ||
}, | ||
}, | ||
{ | ||
wantError: true, | ||
name: "assert for no Jira issue reference gives an error", | ||
cmd: fmt.Sprintf(`report evidence commit jira --name jira-validation | ||
--jira-base-url https://kosli-test.atlassian.net --jira-username [email protected] | ||
--repo-root %s | ||
--assert | ||
--build-url example.com %s`, suite.tmpDir, suite.defaultKosliArguments), | ||
goldenRegex: "Error: no Jira references are found in commit message or branch name", | ||
additionalConfig: jiraTestsAdditionalConfig{ | ||
commitMessage: "test commit without reference", | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
if test.additionalConfig != nil { | ||
msg := test.additionalConfig.(jiraTestsAdditionalConfig).commitMessage | ||
commitSha, err := testHelpers.CommitToRepo(suite.workTree, suite.fs, msg) | ||
require.NoError(suite.T(), err) | ||
funcName(test, suite) | ||
} | ||
} | ||
|
||
test.cmd = test.cmd + " --commit " + commitSha | ||
func funcName(test cmdTestCase, suite *CommitEvidenceJiraCommandTestSuite) { | ||
if test.additionalConfig != nil { | ||
branchName := test.additionalConfig.(jiraTestsAdditionalConfig).branchName | ||
if branchName != "" { | ||
err := testHelpers.CheckoutNewBranch(suite.workTree, branchName) | ||
require.NoError(suite.T(), err) | ||
defer testHelpers.CheckoutMaster(suite.workTree, suite.T()) | ||
} | ||
msg := test.additionalConfig.(jiraTestsAdditionalConfig).commitMessage | ||
commitSha, err := testHelpers.CommitToRepo(suite.workTree, suite.fs, msg) | ||
require.NoError(suite.T(), err) | ||
|
||
runTestCmd(suite.T(), []cmdTestCase{test}) | ||
test.cmd = test.cmd + " --commit " + commitSha | ||
} | ||
|
||
runTestCmd(suite.T(), []cmdTestCase{test}) | ||
} | ||
|
||
// In order for 'go test' to run this suite, we need to create | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters