From 812fad1eb4787e8c8df1a72c1645df2bb847ceb4 Mon Sep 17 00:00:00 2001 From: Sami Alajrami Date: Tue, 2 Jan 2024 13:11:45 +0100 Subject: [PATCH] report evidence/attestation before failing when --assert is used --- cmd/kosli/assertPRAzure.go | 8 ++++-- cmd/kosli/assertPRBitbucket.go | 6 +++- cmd/kosli/assertPRGithub.go | 6 +++- cmd/kosli/assertPRGitlab.go | 8 ++++-- cmd/kosli/attestJira.go | 15 +++++----- cmd/kosli/pullrequest.go | 41 +++++++++++---------------- cmd/kosli/reportEvidenceCommitJira.go | 15 +++++----- 7 files changed, 55 insertions(+), 44 deletions(-) diff --git a/cmd/kosli/assertPRAzure.go b/cmd/kosli/assertPRAzure.go index 1a1431a26..0695cfcb9 100644 --- a/cmd/kosli/assertPRAzure.go +++ b/cmd/kosli/assertPRAzure.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" azUtils "github.com/kosli-dev/cli/internal/azure" @@ -12,7 +13,7 @@ type assertPullRequestAzureOptions struct { commit string } -const assertPRAzureShortDesc = `Assert a Azure DevOps pull request for a git commit exists. ` +const assertPRAzureShortDesc = `Assert an Azure DevOps pull request for a git commit exists. ` const assertPRAzureLongDesc = assertPRAzureShortDesc + ` The command exits with non-zero exit code @@ -58,10 +59,13 @@ func newAssertPullRequestAzureCmd(out io.Writer) *cobra.Command { } func (o *assertPullRequestAzureOptions) run(args []string) error { - pullRequestsEvidence, err := getPullRequestsEvidence(o.azureConfig, o.commit, true) + pullRequestsEvidence, err := o.azureConfig.PREvidenceForCommit(o.commit) if err != nil { return err } + if len(pullRequestsEvidence) == 0 { + return fmt.Errorf("assert failed: found no pull request(s) in Azure DevOps for commit: %s", o.commit) + } logger.Info("found [%d] pull request(s) in Azure DevOps for commit: %s", len(pullRequestsEvidence), o.commit) return nil } diff --git a/cmd/kosli/assertPRBitbucket.go b/cmd/kosli/assertPRBitbucket.go index f3c3ae859..43282d895 100644 --- a/cmd/kosli/assertPRBitbucket.go +++ b/cmd/kosli/assertPRBitbucket.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" bbUtils "github.com/kosli-dev/cli/internal/bitbucket" @@ -59,10 +60,13 @@ func newAssertPullRequestBitbucketCmd(out io.Writer) *cobra.Command { } func (o *assertPullRequestBitbucketOptions) run(args []string) error { - pullRequestsEvidence, err := getPullRequestsEvidence(o.bbConfig, o.commit, true) + pullRequestsEvidence, err := o.bbConfig.PREvidenceForCommit(o.commit) if err != nil { return err } + if len(pullRequestsEvidence) == 0 { + return fmt.Errorf("assert failed: found no pull request(s) in Bitbucket for commit: %s", o.commit) + } logger.Info("found [%d] pull request(s) in Bitbucket for commit: %s", len(pullRequestsEvidence), o.commit) return nil } diff --git a/cmd/kosli/assertPRGithub.go b/cmd/kosli/assertPRGithub.go index 8eea78739..9b6e81fcf 100644 --- a/cmd/kosli/assertPRGithub.go +++ b/cmd/kosli/assertPRGithub.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" ghUtils "github.com/kosli-dev/cli/internal/github" @@ -57,10 +58,13 @@ func newAssertPullRequestGithubCmd(out io.Writer) *cobra.Command { } func (o *assertPullRequestGithubOptions) run(args []string) error { - pullRequestsEvidence, err := getPullRequestsEvidence(o.githubConfig, o.commit, true) + pullRequestsEvidence, err := o.githubConfig.PREvidenceForCommit(o.commit) if err != nil { return err } + if len(pullRequestsEvidence) == 0 { + return fmt.Errorf("assert failed: found no pull request(s) in Github for commit: %s", o.commit) + } logger.Info("found [%d] pull request(s) in Github for commit: %s", len(pullRequestsEvidence), o.commit) return nil } diff --git a/cmd/kosli/assertPRGitlab.go b/cmd/kosli/assertPRGitlab.go index 981b37716..3c114691c 100644 --- a/cmd/kosli/assertPRGitlab.go +++ b/cmd/kosli/assertPRGitlab.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io" gitlabUtils "github.com/kosli-dev/cli/internal/gitlab" @@ -57,10 +58,13 @@ func newAssertPullRequestGitlabCmd(out io.Writer) *cobra.Command { } func (o *assertPullRequestGitlabOptions) run(args []string) error { - pullRequestsEvidence, err := getPullRequestsEvidence(o.gitlabConfig, o.commit, true) + pullRequestsEvidence, err := o.gitlabConfig.PREvidenceForCommit(o.commit) if err != nil { return err } - logger.Info("found [%d] pull request(s) in Gitlab for commit: %s", len(pullRequestsEvidence), o.commit) + if len(pullRequestsEvidence) == 0 { + return fmt.Errorf("assert failed: found no merge request(s) in Gitlab for commit: %s", o.commit) + } + logger.Info("found [%d] merge request(s) in Gitlab for commit: %s", len(pullRequestsEvidence), o.commit) return nil } diff --git a/cmd/kosli/attestJira.go b/cmd/kosli/attestJira.go index 9c570cd39..89bbf51a2 100644 --- a/cmd/kosli/attestJira.go +++ b/cmd/kosli/attestJira.go @@ -211,10 +211,6 @@ func (o *attestJiraOptions) run(args []string) error { 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 { @@ -230,9 +226,6 @@ func (o *attestJiraOptions) run(args []string) error { } 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 := prepareAttestationForm(o.payload, o.evidencePaths) if err != nil { @@ -254,5 +247,13 @@ func (o *attestJiraOptions) run(args []string) error { if err == nil && !global.DryRun { logger.Info("jira attestation '%s' is reported to trail: %s", o.payload.AttestationName, o.trailName) } + + if len(issueIDs) == 0 && o.assert { + return fmt.Errorf("no Jira references are found in commit message or branch name") + } + + if issueFoundCount != len(issueIDs) && o.assert { + return fmt.Errorf("missing Jira issues from references found in commit message or branch name%s", issueLog) + } return err } diff --git a/cmd/kosli/pullrequest.go b/cmd/kosli/pullrequest.go index 4b9f8e121..acababecb 100644 --- a/cmd/kosli/pullrequest.go +++ b/cmd/kosli/pullrequest.go @@ -49,7 +49,7 @@ func (o *pullRequestArtifactOptions) run(out io.Writer, args []string) error { } url := fmt.Sprintf("%s/api/v2/evidence/%s/artifact/%s/pull_request", global.Host, global.Org, o.flowName) - pullRequestsEvidence, err := getPullRequestsEvidence(o.getRetriever(), o.commit, o.assert) + pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommit(o.commit) if err != nil { return err } @@ -74,7 +74,7 @@ func (o *pullRequestArtifactOptions) run(out io.Writer, args []string) error { return err } - logger.Debug("found %d %s(s) for commit: %s\n", len(pullRequestsEvidence), label, o.commit) + logger.Info("found %d %s(s) for commit: %s\n", len(pullRequestsEvidence), label, o.commit) reqParams := &requests.RequestParams{ Method: http.MethodPost, @@ -87,6 +87,10 @@ func (o *pullRequestArtifactOptions) run(out io.Writer, args []string) error { if err == nil && !global.DryRun { logger.Info("%s %s evidence is reported to artifact: %s", o.payload.GitProvider, label, o.payload.ArtifactFingerprint) } + + if o.pullRequestOptions.assert && !global.DryRun { + return fmt.Errorf("assert failed: no %s found for the given commit: %s", label, o.commit) + } return err } @@ -115,7 +119,7 @@ func (o *attestPROptions) run(args []string) error { return err } - pullRequestsEvidence, err := getPullRequestsEvidence(o.getRetriever(), o.payload.Commit.Sha1, o.assert) + pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommit(o.payload.Commit.Sha1) if err != nil { return err } @@ -134,7 +138,7 @@ func (o *attestPROptions) run(args []string) error { defer os.Remove(evidencePath) } - logger.Debug("found %d %s(s) for commit: %s\n", len(pullRequestsEvidence), label, o.payload.Commit.Sha1) + logger.Info("found %d %s(s) for commit: %s\n", len(pullRequestsEvidence), label, o.payload.Commit.Sha1) reqParams := &requests.RequestParams{ Method: http.MethodPost, @@ -147,6 +151,10 @@ func (o *attestPROptions) run(args []string) error { if err == nil && !global.DryRun { logger.Info("%s %s attestation '%s' is reported to trail: %s", o.payload.GitProvider, label, o.payload.AttestationName, o.trailName) } + + if o.assert && !global.DryRun { + return fmt.Errorf("assert failed: no %s found for the given commit: %s", label, o.payload.Commit.Sha1) + } return err } @@ -167,7 +175,7 @@ func (o *pullRequestCommitOptions) run(args []string) error { return err } - pullRequestsEvidence, err := getPullRequestsEvidence(o.getRetriever(), o.payload.CommitSHA, o.assert) + pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommit(o.payload.CommitSHA) if err != nil { return err } @@ -186,7 +194,7 @@ func (o *pullRequestCommitOptions) run(args []string) error { if err != nil { return err } - logger.Debug("found %d %s(s) for commit: %s\n", len(pullRequestsEvidence), label, o.payload.CommitSHA) + logger.Info("found %d %s(s) for commit: %s\n", len(pullRequestsEvidence), label, o.payload.CommitSHA) reqParams := &requests.RequestParams{ Method: http.MethodPost, @@ -199,26 +207,11 @@ func (o *pullRequestCommitOptions) run(args []string) error { if err == nil && !global.DryRun { logger.Info("%s %s evidence is reported to commit: %s", o.payload.GitProvider, label, o.payload.CommitSHA) } - return err -} - -func getPullRequestsEvidence(retriever types.PRRetriever, commit string, assert bool) ([]*types.PREvidence, error) { - pullRequestsEvidence, err := retriever.PREvidenceForCommit(commit) - if err != nil { - return pullRequestsEvidence, err - } - if len(pullRequestsEvidence) == 0 { - name := "pull requests" - if reflect.TypeOf(retriever) == reflect.TypeOf(&gitlabUtils.GitlabConfig{}) { - name = "merge requests" - } - if assert { - return pullRequestsEvidence, fmt.Errorf("no %s found for the given commit: %s", name, commit) - } - logger.Info("no %s found for given commit: %s", name, commit) + if o.pullRequestOptions.assert && !global.DryRun { + return fmt.Errorf("assert failed: no %s found for the given commit: %s", label, o.payload.CommitSHA) } - return pullRequestsEvidence, nil + return err } func getGitProviderAndLabel(retriever interface{}) (string, string) { diff --git a/cmd/kosli/reportEvidenceCommitJira.go b/cmd/kosli/reportEvidenceCommitJira.go index 749ea4227..954faf793 100644 --- a/cmd/kosli/reportEvidenceCommitJira.go +++ b/cmd/kosli/reportEvidenceCommitJira.go @@ -181,10 +181,6 @@ func (o *reportEvidenceCommitJiraOptions) run(args []string) error { 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 { @@ -200,9 +196,6 @@ func (o *reportEvidenceCommitJiraOptions) run(args []string) error { } 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 @@ -227,5 +220,13 @@ func (o *reportEvidenceCommitJiraOptions) run(args []string) error { logger.Info("Jira evidence is reported to commit: %s", o.payload.CommitSHA) logger.Info(" Issues references reported: %s", issueLog) } + + if len(issueIDs) == 0 && o.assert { + return fmt.Errorf("no Jira references are found in commit message or branch name") + } + if issueFoundCount != len(issueIDs) && o.assert { + return fmt.Errorf("missing Jira issues from references found in commit message or branch name%s", issueLog) + } + return err }