Skip to content

Commit 9b4efda

Browse files
PR attestation enhancement (#514)
* Implement optional flag and loop to poll SonarQube and wait for scan to complete * Add missing argument to NewSonarConfig * Add 'IN_PROGRESS' status to sleep loop * Add more thorough error message to attest sonar command * Change --allow-wait flag to take number of seconds to wait as parameter * Clean up leftover comment * Add more details on new flag to attest sonar description * Change --allow-wait flag to --max-retries * Change --allow-wait flag in example commands for docs * Fix typo * Start implementing GraphQL for Github PRs * Update variable names for PR structs to match API * Update PR struct to work with both old and new versions of PR attestation * Fix payload for non-github PR attestations and tidy up comments * Fix payload issues for non-Github-attest commands * Update report evidence command payloads * Get git provider for reporting commit evidence properly * add author_username to github pr attestation commits --------- Co-authored-by: Faye <[email protected]>
1 parent a7b6274 commit 9b4efda

File tree

18 files changed

+285
-46
lines changed

18 files changed

+285
-46
lines changed

cmd/kosli/assertPRAzure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func newAssertPullRequestAzureCmd(out io.Writer) *cobra.Command {
5959
}
6060

6161
func (o *assertPullRequestAzureOptions) run(args []string) error {
62-
pullRequestsEvidence, err := o.azureConfig.PREvidenceForCommit(o.commit)
62+
pullRequestsEvidence, err := o.azureConfig.PREvidenceForCommitV2(o.commit)
6363
if err != nil {
6464
return err
6565
}

cmd/kosli/assertPRBitbucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func newAssertPullRequestBitbucketCmd(out io.Writer) *cobra.Command {
8181
}
8282

8383
func (o *assertPullRequestBitbucketOptions) run(args []string) error {
84-
pullRequestsEvidence, err := o.bbConfig.PREvidenceForCommit(o.commit)
84+
pullRequestsEvidence, err := o.bbConfig.PREvidenceForCommitV2(o.commit)
8585
if err != nil {
8686
return err
8787
}

cmd/kosli/assertPRGithub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newAssertPullRequestGithubCmd(out io.Writer) *cobra.Command {
5858
}
5959

6060
func (o *assertPullRequestGithubOptions) run(args []string) error {
61-
pullRequestsEvidence, err := o.githubConfig.PREvidenceForCommit(o.commit)
61+
pullRequestsEvidence, err := o.githubConfig.PREvidenceForCommitV2(o.commit)
6262
if err != nil {
6363
return err
6464
}

cmd/kosli/assertPRGithub_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (suite *AssertPRGithubCommandTestSuite) TestAssertPRGithubCmd() {
4747
name: "assert Github PR evidence fails when commit does not exist",
4848
cmd: `assert pullrequest github --github-org kosli-dev --repository cli
4949
--commit 19aab7f063147614451c88969602a10afba123ab` + suite.defaultKosliArguments,
50-
golden: "Error: GET https://api.github.com/repos/kosli-dev/cli/commits/19aab7f063147614451c88969602a10afba123ab/pulls: 422 No commit found for SHA: 19aab7f063147614451c88969602a10afba123ab []\n",
50+
golden: "Error: assert failed: found no pull request(s) in Github for commit: 19aab7f063147614451c88969602a10afba123ab\n",
5151
},
5252
}
5353

cmd/kosli/assertPRGitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newAssertPullRequestGitlabCmd(out io.Writer) *cobra.Command {
5858
}
5959

6060
func (o *assertPullRequestGitlabOptions) run(args []string) error {
61-
pullRequestsEvidence, err := o.gitlabConfig.PREvidenceForCommit(o.commit)
61+
pullRequestsEvidence, err := o.gitlabConfig.PREvidenceForCommitV2(o.commit)
6262
if err != nil {
6363
return err
6464
}

cmd/kosli/docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,6 @@ var liveCliMap = map[string]string{
340340
"kosli list flows": "kosli list flows --output=json",
341341
"kosli get flow": "kosli get flow dashboard-ci --output=json",
342342
//"kosli list trails": "kosli list trails dashboard-ci --output=json", // Produces too much output
343-
"kosli get trail": "kosli get trail dashboard-ci 1159a6f1193150681b8484545150334e89de6c1c --output=json",
344-
"kosli get attestation": "kosli get attestation snyk-container-scan --flow=differ-ci --fingerprint=0cbbe3a6e73e733e8ca4b8813738d68e824badad0508ff20842832b5143b48c0 --output=json",
343+
"kosli get trail": "kosli get trail dashboard-ci 1159a6f1193150681b8484545150334e89de6c1c --output=json",
344+
"kosli get attestation": "kosli get attestation snyk-container-scan --flow=differ-ci --fingerprint=0cbbe3a6e73e733e8ca4b8813738d68e824badad0508ff20842832b5143b48c0 --output=json",
345345
}

cmd/kosli/pullrequest.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ func (o *pullRequestArtifactOptions) run(out io.Writer, args []string) error {
4848
}
4949
}
5050

51+
label := ""
52+
o.payload.GitProvider, label = getGitProviderAndLabel(o.retriever)
53+
5154
url := fmt.Sprintf("%s/api/v2/evidence/%s/artifact/%s/pull_request", global.Host, global.Org, o.flowName)
52-
pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommit(o.commit)
55+
56+
// TODO: after the PR payload is enhanced for all git providers they will all use the same method
57+
var pullRequestsEvidence []*types.PREvidence
58+
if o.payload.GitProvider == "github" {
59+
pullRequestsEvidence, err = o.getRetriever().PREvidenceForCommitV1(o.commit)
60+
} else {
61+
pullRequestsEvidence, err = o.getRetriever().PREvidenceForCommitV2(o.commit)
62+
}
63+
5364
if err != nil {
5465
return err
5566
}
@@ -60,9 +71,6 @@ func (o *pullRequestArtifactOptions) run(out io.Writer, args []string) error {
6071
return err
6172
}
6273

63-
label := ""
64-
o.payload.GitProvider, label = getGitProviderAndLabel(o.retriever)
65-
6674
// PR evidence does not have files to upload
6775
form, cleanupNeeded, evidencePath, err := newEvidenceForm(o.payload, []string{})
6876
// if we created a tar package, remove it after uploading it
@@ -119,7 +127,7 @@ func (o *attestPROptions) run(args []string) error {
119127
return err
120128
}
121129

122-
pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommit(o.payload.Commit.Sha1)
130+
pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommitV2(o.payload.Commit.Sha1)
123131
if err != nil {
124132
return err
125133
}
@@ -175,14 +183,21 @@ func (o *pullRequestCommitOptions) run(args []string) error {
175183
return err
176184
}
177185

178-
pullRequestsEvidence, err := o.getRetriever().PREvidenceForCommit(o.payload.CommitSHA)
186+
label := ""
187+
o.payload.GitProvider, label = getGitProviderAndLabel(o.retriever)
188+
189+
// TODO: after the PR payload is enhanced for all git providers they will all use the same method
190+
var pullRequestsEvidence []*types.PREvidence
191+
if o.payload.GitProvider == "github" {
192+
pullRequestsEvidence, err = o.getRetriever().PREvidenceForCommitV1(o.payload.CommitSHA)
193+
} else {
194+
pullRequestsEvidence, err = o.getRetriever().PREvidenceForCommitV2(o.payload.CommitSHA)
195+
}
179196
if err != nil {
180197
return err
181198
}
182199

183200
o.payload.PullRequests = pullRequestsEvidence
184-
label := ""
185-
o.payload.GitProvider, label = getGitProviderAndLabel(o.retriever)
186201

187202
// PR evidence does not have files to upload
188203
form, cleanupNeeded, evidencePath, err := newEvidenceForm(o.payload, []string{})

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
github.com/owenrumney/go-sarif/v2 v2.3.3
3333
github.com/pkg/errors v0.9.1
3434
github.com/rjeczalik/notify v0.9.3
35+
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466
3536
github.com/spf13/cobra v1.9.1
3637
github.com/spf13/pflag v1.0.6
3738
github.com/spf13/viper v1.20.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ github.com/sagikazarmark/locafero v0.9.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPr
611611
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
612612
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
613613
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
614+
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 h1:17JxqqJY66GmZVHkmAsGEkcIu0oCe3AM420QDgGwZx0=
615+
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466/go.mod h1:9dIRpgIY7hVhoqfe0/FcYp0bpInZaT7dc3BYOprrIUE=
614616
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
615617
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
616618
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=

internal/azure/azure.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/kosli-dev/cli/internal/types"
10+
"github.com/kosli-dev/cli/internal/utils"
1011
"github.com/microsoft/azure-devops-go-api/azuredevops"
1112
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
1213
)
@@ -54,7 +55,8 @@ func NewAzureClientFromToken(ctx context.Context, azToken, orgURL string) (git.C
5455
return gitClient, nil
5556
}
5657

57-
func (c *AzureConfig) PREvidenceForCommit(commit string) ([]*types.PREvidence, error) {
58+
// This is the old implementation, it will be removed after the PR payload is enhanced for Azure
59+
func (c *AzureConfig) PREvidenceForCommitV2(commit string) ([]*types.PREvidence, error) {
5860
pullRequestsEvidence := []*types.PREvidence{}
5961
prs, err := c.PullRequestsForCommit(commit)
6062
if err != nil {
@@ -70,6 +72,11 @@ func (c *AzureConfig) PREvidenceForCommit(commit string) ([]*types.PREvidence, e
7072
return pullRequestsEvidence, nil
7173
}
7274

75+
// This is the new implementation, it will be used for Azure
76+
func (c *AzureConfig) PREvidenceForCommitV1(commit string) ([]*types.PREvidence, error) {
77+
return []*types.PREvidence{}, nil
78+
}
79+
7380
func (c *AzureConfig) newPRAzureEvidence(pr git.GitPullRequest) (*types.PREvidence, error) {
7481
prID := strconv.Itoa(*pr.PullRequestId)
7582
url, err := url.JoinPath(c.OrgURL, c.Project, "_git", c.Repository, "pullrequest", prID)
@@ -85,7 +92,7 @@ func (c *AzureConfig) newPRAzureEvidence(pr git.GitPullRequest) (*types.PREviden
8592
if err != nil {
8693
return evidence, err
8794
}
88-
evidence.Approvers = approvers
95+
evidence.Approvers = utils.ConvertStringListToInterfaceList(approvers)
8996
return evidence, nil
9097
}
9198

0 commit comments

Comments
 (0)