Skip to content

Commit

Permalink
rename --git-commit to --commit and default it to HEAD. Stop sending … (
Browse files Browse the repository at this point in the history
#63)

* rename --git-commit to --commit and default it to HEAD. Stop sending commits lists for artifacts

* fix test to match updated server error message
  • Loading branch information
sami-alajrami authored Dec 15, 2023
1 parent 7a7e061 commit 366092d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 63 deletions.
69 changes: 14 additions & 55 deletions cmd/kosli/attestArtifact.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -22,15 +21,15 @@ type attestArtifactOptions struct {
}

type AttestArtifactPayload struct {
Fingerprint string `json:"fingerprint"`
Filename string `json:"filename"`
GitCommit string `json:"git_commit"`
BuildUrl string `json:"build_url"`
CommitUrl string `json:"commit_url"`
RepoUrl string `json:"repo_url"`
CommitsList []*gitview.CommitInfo `json:"commits_list"`
Name string `json:"step_name"`
TrailName string `json:"trail_name"`
Fingerprint string `json:"fingerprint"`
Filename string `json:"filename"`
GitCommit string `json:"git_commit"`
GitCommitInfo *gitview.BasicCommitInfo `json:"git_commit_info"`
BuildUrl string `json:"build_url"`
CommitUrl string `json:"commit_url"`
RepoUrl string `json:"repo_url"`
Name string `json:"step_name"`
TrailName string `json:"trail_name"`
}

const attestArtifactShortDesc = `Attest an artifact creation to a Kosli flow. `
Expand Down Expand Up @@ -95,7 +94,7 @@ func newAttestArtifactCmd(out io.Writer) *cobra.Command {
ci := WhichCI()
cmd.Flags().StringVarP(&o.payload.Fingerprint, "fingerprint", "F", "", fingerprintFlag)
cmd.Flags().StringVarP(&o.flowName, "flow", "f", "", flowNameFlag)
cmd.Flags().StringVarP(&o.gitReference, "git-commit", "g", DefaultValue(ci, "git-commit"), gitCommitFlag)
cmd.Flags().StringVarP(&o.gitReference, "commit", "g", DefaultValue(ci, "git-commit"), gitCommitFlag)
cmd.Flags().StringVarP(&o.payload.BuildUrl, "build-url", "b", DefaultValue(ci, "build-url"), buildUrlFlag)
cmd.Flags().StringVarP(&o.payload.CommitUrl, "commit-url", "u", DefaultValue(ci, "commit-url"), commitUrlFlag)
cmd.Flags().StringVar(&o.srcRepoRoot, "repo-root", ".", repoRootFlag)
Expand All @@ -106,7 +105,7 @@ func newAttestArtifactCmd(out io.Writer) *cobra.Command {

addDryRunFlag(cmd)

err := RequireFlags(cmd, []string{"trail", "flow", "name", "git-commit", "build-url", "commit-url"})
err := RequireFlags(cmd, []string{"trail", "flow", "name", "build-url", "commit-url"})
if err != nil {
logger.Error("failed to configure required flags: %v", err)
}
Expand Down Expand Up @@ -139,21 +138,12 @@ func (o *attestArtifactOptions) run(args []string) error {
return err
}

commitObject, err := gitView.GetCommitInfoFromCommitSHA(o.gitReference)
commitInfo, err := gitView.GetCommitInfoFromCommitSHA(o.gitReference)
if err != nil {
return err
}
o.payload.GitCommit = commitObject.Sha1

previousCommit, err := o.latestCommit(currentBranch(gitView))
if err == nil {
o.payload.CommitsList, err = gitView.ChangeLog(o.payload.GitCommit, previousCommit, logger)
if err != nil && !global.DryRun {
return err
}
} else if !global.DryRun {
return err
}
o.payload.GitCommit = commitInfo.Sha1
o.payload.GitCommitInfo = &commitInfo.BasicCommitInfo

o.payload.RepoUrl, err = gitView.RepoUrl()
if err != nil {
Expand All @@ -175,34 +165,3 @@ func (o *attestArtifactOptions) run(args []string) error {
}
return err
}

// latestCommit retrieves the git commit of the latest artifact for a flow in Kosli
func (o *attestArtifactOptions) latestCommit(branchName string) (string, error) {
latestCommitUrl := fmt.Sprintf(
"%s/api/v2/artifacts/%s/%s/%s/latest_commit%s",
global.Host, global.Org, o.flowName, o.payload.Fingerprint, asBranchParameter(branchName))

reqParams := &requests.RequestParams{
Method: http.MethodGet,
URL: latestCommitUrl,
Password: global.ApiToken,
}
response, err := kosliClient.Do(reqParams)
if err != nil {
return "", err
}

var latestCommitResponse map[string]interface{}
err = json.Unmarshal([]byte(response.Body), &latestCommitResponse)
if err != nil {
return "", err
}
latestCommit := latestCommitResponse["latest_commit"]
if latestCommit == nil {
logger.Debug("no previous artifacts were found for flow: %s", o.flowName)
return "", nil
} else {
logger.Debug("latest artifact for flow: %s has the git commit: %s", o.flowName, latestCommit.(string))
return latestCommit.(string), nil
}
}
10 changes: 5 additions & 5 deletions cmd/kosli/attestArtifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,29 @@ func (suite *AttestArtifactCommandTestSuite) TestAttestArtifactCmd() {
{
wantError: true,
name: "fails when missing a required flag",
cmd: fmt.Sprintf("attest artifact foo --artifact-type file --name bar --git-commit HEAD --build-url example.com %s", suite.defaultKosliArguments),
cmd: fmt.Sprintf("attest artifact foo --artifact-type file --name bar --commit HEAD --build-url example.com %s", suite.defaultKosliArguments),
golden: "Error: required flag(s) \"commit-url\" not set\n",
},
{
wantError: true,
name: "fails when --fingerprint is invalid sha256 digest",
cmd: fmt.Sprintf("attest artifact foo --fingerprint xxxx --name bar --git-commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
cmd: fmt.Sprintf("attest artifact foo --fingerprint xxxx --name bar --commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
golden: "Error: xxxx is not a valid SHA256 fingerprint. It should match the pattern ^([a-f0-9]{64})$\nUsage: kosli attest artifact {IMAGE-NAME | FILE-PATH | DIR-PATH} [flags]\n",
},
{
wantError: true,
name: "fails when --name does not match artifact name in the template",
cmd: fmt.Sprintf("attest artifact testdata/file1 --artifact-type file --name bar --git-commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
cmd: fmt.Sprintf("attest artifact testdata/file1 --artifact-type file --name bar --commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
golden: "Error: Artifact 'bar' does not exist in trail template 'test-123'.\nAvailable artifacts: cli\n",
},
{
name: "can attest a file artifact",
cmd: fmt.Sprintf("attest artifact testdata/file1 --artifact-type file --name cli --git-commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
cmd: fmt.Sprintf("attest artifact testdata/file1 --artifact-type file --name cli --commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
golden: "artifact file1 was attested with fingerprint: 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9\n",
},
{
name: "can attest an artifact with --fingerprint",
cmd: fmt.Sprintf("attest artifact testdata/file1 --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name cli --git-commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
cmd: fmt.Sprintf("attest artifact testdata/file1 --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name cli --commit HEAD --build-url example.com --commit-url example.com %s", suite.defaultKosliArguments),
golden: "artifact testdata/file1 was attested with fingerprint: 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9\n",
},
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/kosli/cli_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func DefaultValue(ci, flag string) string {

return result
}
// when not in a known CI, default some values
if flag == "git-commit" {
return "HEAD"
}
}
return ""
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kosli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func addListFlags(cmd *cobra.Command, o *listOptions) {

func addAttestationFlags(cmd *cobra.Command, o *CommonAttestationOptions, payload *CommonAttestationPayload, ci string) {
cmd.Flags().StringVarP(&payload.ArtifactFingerprint, "fingerprint", "F", "", attestationFingerprintFlag)
cmd.Flags().StringVar(&o.commitSHA, "commit", DefaultValue(ci, "git-commit"), attestationCommitFlag)
cmd.Flags().StringVarP(&o.commitSHA, "commit", "g", DefaultValue(ci, "git-commit"), attestationCommitFlag)
cmd.Flags().StringVarP(&payload.Url, "url", "b", DefaultValue(ci, "build-url"), attestationUrlFlag)
cmd.Flags().StringVarP(&o.attestationNameTemplate, "name", "n", "", attestationNameFlag)
cmd.Flags().StringVar(&payload.EvidenceFingerprint, "evidence-fingerprint", "", evidenceFingerprintFlag)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ The service principal needs to have the following permissions:
attestationUserDataFlag = "[optional] The path to a JSON file containing additional data you would like to attach to the attestation."
deploymentUserDataFlag = "[optional] The path to a JSON file containing additional data you would like to attach to the deployment."
trailUserDataFlag = "[optional] The path to a JSON file containing additional data you would like to attach to the flow trail."
gitCommitFlag = "The git commit from which the artifact was created. (defaulted in some CIs: https://docs.kosli.com/ci-defaults )."
gitCommitFlag = "[defaulted] The git commit from which the artifact was created. (defaulted in some CIs: https://docs.kosli.com/ci-defaults, otherwise defaults to HEAD )."
evidenceBuildUrlFlag = "The url of CI pipeline that generated the evidence. (defaulted in some CIs: https://docs.kosli.com/ci-defaults )."
buildUrlFlag = "The url of CI pipeline that built the artifact. (defaulted in some CIs: https://docs.kosli.com/ci-defaults )."
commitUrlFlag = "The url for the git commit that created the artifact. (defaulted in some CIs: https://docs.kosli.com/ci-defaults )."
Expand Down
2 changes: 1 addition & 1 deletion cmd/kosli/testdata/output/docs/artifact.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ artifact {IMAGE-NAME | FILE-PATH | DIR-PATH} [flags]
| -x, --exclude strings | [optional] The comma separated list of directories and files to exclude from fingerprinting. Only applicable for --artifact-type dir. |
| -F, --fingerprint string | [conditional] The SHA256 fingerprint of the artifact. Only required if you don't specify '--artifact-type'. |
| -f, --flow string | The Kosli flow name. |
| -g, --git-commit string | The git commit from which the artifact was created. (defaulted in some CIs: https://docs.kosli.com/ci-defaults ). |
| -g, --git-commit string | [defaulted] The git commit from which the artifact was created. (defaulted in some CIs: https://docs.kosli.com/ci-defaults, otherwise defaults to HEAD ). |
| -h, --help | help for artifact |
| -n, --name string | [optional] Artifact display name, if different from file, image or directory name. |
| --registry-password string | [conditional] The docker registry password or access token. Only required if you want to read docker image SHA256 digest from a remote docker registry. |
Expand Down

0 comments on commit 366092d

Please sign in to comment.