Skip to content

Commit

Permalink
report evidence/attestation before failing when --assert is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sami-alajrami committed Jan 2, 2024
1 parent e4edc94 commit 812fad1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 44 deletions.
8 changes: 6 additions & 2 deletions cmd/kosli/assertPRAzure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"

azUtils "github.com/kosli-dev/cli/internal/azure"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion cmd/kosli/assertPRBitbucket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"

bbUtils "github.com/kosli-dev/cli/internal/bitbucket"
Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion cmd/kosli/assertPRGithub.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"

ghUtils "github.com/kosli-dev/cli/internal/github"
Expand Down Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions cmd/kosli/assertPRGitlab.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"

gitlabUtils "github.com/kosli-dev/cli/internal/gitlab"
Expand Down Expand Up @@ -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
}
15 changes: 8 additions & 7 deletions cmd/kosli/attestJira.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
41 changes: 17 additions & 24 deletions cmd/kosli/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions cmd/kosli/reportEvidenceCommitJira.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 812fad1

Please sign in to comment.