From cff82cd07ec26e250ebb1bedfe5edde78e03bf67 Mon Sep 17 00:00:00 2001 From: Ewelina Wilkosz Date: Fri, 1 Sep 2023 10:34:16 +0200 Subject: [PATCH] Solutions for kosli-dev/server#881 and kosli-dev/server#885 Add exclude to every command when kosli can automatically calculate sha Always resolve given revision to git commit - accepts shas, tags, branch names, HEAD... --- charts/k8s-reporter/README.md | 4 ++-- cmd/kosli/fingerprint.go | 10 +++++++++- cmd/kosli/fingerprint_test.go | 5 +++++ cmd/kosli/flags.go | 1 + cmd/kosli/reportArtifact.go | 12 +++++++++++- cmd/kosli/root.go | 2 +- cmd/kosli/snapshotServer.go | 12 ++++++++++-- cmd/kosli/testHelpers.go | 5 +++-- cmd/kosli/testdata/folder1/folder2/hello2.txt | 1 + cmd/kosli/testdata/folder1/folder2/hello3.txt | 1 + cmd/kosli/testdata/output/docs/artifact.md | 1 + docs.kosli.com/content/helm/_index.md | 4 ++-- 12 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 cmd/kosli/testdata/folder1/folder2/hello2.txt create mode 100644 cmd/kosli/testdata/folder1/folder2/hello3.txt diff --git a/charts/k8s-reporter/README.md b/charts/k8s-reporter/README.md index a67e3c0a6..579937da7 100644 --- a/charts/k8s-reporter/README.md +++ b/charts/k8s-reporter/README.md @@ -61,7 +61,7 @@ helm upgrade [RELEASE-NAME] kosli/k8s-reporter | kosliApiToken.secretKey | string | `""` | the name of the key in the secret data which contains the kosli API token | | kosliApiToken.secretName | string | `""` | the name of the secret containing the kosli API token | | nameOverride | string | `""` | overrides the name used for the created k8s resources. If `fullnameOverride` is provided, it has higher precedence than this one | -| podAnnotations | object | `{}` | | +| podAnnotations | object | `{}` | any custom annotations to be added to the cronjob | | reporterConfig.dryRun | bool | `false` | whether the dry run mode is enabled or not. In dry run mode, the reporter logs the reports to stdout and does not send them to kosli. | | reporterConfig.kosliEnvironmentName | string | `""` | the name of kosli environment that the k8s cluster/namespace correlates to | | reporterConfig.kosliOrg | string | `""` | the name of the kosli org | @@ -74,5 +74,5 @@ helm upgrade [RELEASE-NAME] kosli/k8s-reporter | serviceAccount.name | string | `""` | the name of the service account to use. If not set and create is true, a name is generated using the fullname template | ---------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.5.0](https://github.com/norwoodj/helm-docs/releases/v1.5.0) +Autogenerated from chart metadata using [helm-docs v1.11.0](https://github.com/norwoodj/helm-docs/releases/v1.11.0) diff --git a/cmd/kosli/fingerprint.go b/cmd/kosli/fingerprint.go index 87c99cbb9..4267d7175 100644 --- a/cmd/kosli/fingerprint.go +++ b/cmd/kosli/fingerprint.go @@ -47,11 +47,19 @@ func newFingerprintCmd(out io.Writer) *cobra.Command { } addFingerprintFlags(cmd, o) - cmd.Flags().StringSliceVarP(&o.excludePaths, "exclude", "e", []string{}, excludePathsFlag) + cmd.Flags().StringSliceVarP(&o.excludePaths, "e", "e", []string{}, excludePathsFlag) err := RequireFlags(cmd, []string{"artifact-type"}) if err != nil { logger.Error("failed to configure required flags: %v", err) } + + err = DeprecateFlags(cmd, map[string]string{ + "e": "use -x instead", + }) + if err != nil { + logger.Error("failed to configure deprecated flags: %v", err) + } + return cmd } diff --git a/cmd/kosli/fingerprint_test.go b/cmd/kosli/fingerprint_test.go index fc8709989..d52ac83b0 100644 --- a/cmd/kosli/fingerprint_test.go +++ b/cmd/kosli/fingerprint_test.go @@ -32,6 +32,11 @@ func (suite *FingerprintTestSuite) TestFingerprintCmd() { { name: "dir fingerprint", cmd: "fingerprint --artifact-type dir testdata/folder1", + golden: "c43808cb04c6e66c4c6fc1f972dd67c3b9b71c81e0a0c78730da3699922d17be\n", + }, + { + name: "dir fingerprint with exclude", + cmd: "fingerprint --artifact-type dir testdata/folder1 -x folder2", golden: "773fd3300860454a2b065c5912c03008adb11e6a6dcf7c1c64c094ceab8f430a\n", }, { diff --git a/cmd/kosli/flags.go b/cmd/kosli/flags.go index c527f631b..ae7b9b617 100644 --- a/cmd/kosli/flags.go +++ b/cmd/kosli/flags.go @@ -14,6 +14,7 @@ func addFingerprintFlags(cmd *cobra.Command, o *fingerprintOptions) { cmd.Flags().StringVar(&o.registryProvider, "registry-provider", "", registryProviderFlag) cmd.Flags().StringVar(&o.registryUsername, "registry-username", "", registryUsernameFlag) cmd.Flags().StringVar(&o.registryPassword, "registry-password", "", registryPasswordFlag) + cmd.Flags().StringSliceVarP(&o.excludePaths, "exclude", "x", []string{}, excludePathsFlag) } func addAWSAuthFlags(cmd *cobra.Command, o *aws.AWSStaticCreds) { diff --git a/cmd/kosli/reportArtifact.go b/cmd/kosli/reportArtifact.go index 38751deea..e4093f195 100644 --- a/cmd/kosli/reportArtifact.go +++ b/cmd/kosli/reportArtifact.go @@ -15,6 +15,7 @@ import ( type reportArtifactOptions struct { fingerprintOptions *fingerprintOptions flowName string + gitReference string srcRepoRoot string payload ArtifactPayload } @@ -84,11 +85,13 @@ func newReportArtifactCmd(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.payload.GitCommit, "git-commit", "g", DefaultValue(ci, "git-commit"), gitCommitFlag) + cmd.Flags().StringVarP(&o.gitReference, "git-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) addFingerprintFlags(cmd, o.fingerprintOptions) + // cmd.Flags().StringSliceVarP(&o.fingerprintOptions.excludePaths, "exclude", "e", []string{}, excludePathsFlag) + addDryRunFlag(cmd) err := RequireFlags(cmd, []string{"flow", "git-commit", "build-url", "commit-url"}) @@ -125,6 +128,13 @@ func (o *reportArtifactOptions) run(args []string) error { return err } + commitObject, err := gitView.GetCommitInfoFromCommitSHA(o.gitReference) + if err != nil { + return err + } + + o.payload.GitCommit = commitObject.Sha1 + o.payload.CommitsList, err = gitView.ChangeLog(o.payload.GitCommit, previousCommit, logger) if err != nil { return err diff --git a/cmd/kosli/root.go b/cmd/kosli/root.go index 1ea153cb6..450f5a79a 100644 --- a/cmd/kosli/root.go +++ b/cmd/kosli/root.go @@ -128,7 +128,7 @@ More details can be found here: https://aws.github.io/aws-sdk-go-v2/docs/configu awsRegionFlag = "The AWS region." bucketNameFlag = "The name of the S3 bucket." pathsFlag = "The comma separated list of artifact directories." - excludePathsFlag = "[optional] The comma separated list of directories and files to exclude from fingerprinting." + excludePathsFlag = "[optional] The comma separated list of directories and files to exclude from fingerprinting. Only applicable for --artifact-type dir." shortFlag = "[optional] Print only the Kosli CLI version number." longFlag = "[optional] Print detailed output." reverseFlag = "[defaulted] Reverse the order of output list." diff --git a/cmd/kosli/snapshotServer.go b/cmd/kosli/snapshotServer.go index 887bb7423..28df08b30 100644 --- a/cmd/kosli/snapshotServer.go +++ b/cmd/kosli/snapshotServer.go @@ -66,10 +66,18 @@ func newSnapshotServerCmd(out io.Writer) *cobra.Command { } cmd.Flags().StringSliceVarP(&o.paths, "paths", "p", []string{}, pathsFlag) - cmd.Flags().StringSliceVarP(&o.excludePaths, "exclude", "e", []string{}, excludePathsFlag) + cmd.Flags().StringSliceVarP(&o.excludePaths, "exclude", "x", []string{}, excludePathsFlag) + cmd.Flags().StringSliceVarP(&o.excludePaths, "e", "e", []string{}, excludePathsFlag) addDryRunFlag(cmd) - err := RequireFlags(cmd, []string{"paths"}) + err := DeprecateFlags(cmd, map[string]string{ + "e": "use -x instead", + }) + if err != nil { + logger.Error("failed to configure deprecated flags: %v", err) + } + + err = RequireFlags(cmd, []string{"paths"}) if err != nil { logger.Error("failed to configure required flags: %v", err) } diff --git a/cmd/kosli/testHelpers.go b/cmd/kosli/testHelpers.go index 7d47f0a53..bae831a77 100644 --- a/cmd/kosli/testHelpers.go +++ b/cmd/kosli/testHelpers.go @@ -185,8 +185,9 @@ func CreateWorkflowEvidence(auditTrailName, externalId string, t *testing.T) { func CreateArtifact(flowName, artifactFingerprint, artifactName string, t *testing.T) { t.Helper() o := &reportArtifactOptions{ - srcRepoRoot: "../..", - flowName: flowName, + srcRepoRoot: "../..", + flowName: flowName, + gitReference: "0fc1ba9876f91b215679f3649b8668085d820ab5", payload: ArtifactPayload{ Fingerprint: artifactFingerprint, GitCommit: "0fc1ba9876f91b215679f3649b8668085d820ab5", diff --git a/cmd/kosli/testdata/folder1/folder2/hello2.txt b/cmd/kosli/testdata/folder1/folder2/hello2.txt new file mode 100644 index 000000000..23294b061 --- /dev/null +++ b/cmd/kosli/testdata/folder1/folder2/hello2.txt @@ -0,0 +1 @@ +hello2 \ No newline at end of file diff --git a/cmd/kosli/testdata/folder1/folder2/hello3.txt b/cmd/kosli/testdata/folder1/folder2/hello3.txt new file mode 100644 index 000000000..96803d198 --- /dev/null +++ b/cmd/kosli/testdata/folder1/folder2/hello3.txt @@ -0,0 +1 @@ +hello3 \ No newline at end of file diff --git a/cmd/kosli/testdata/output/docs/artifact.md b/cmd/kosli/testdata/output/docs/artifact.md index d0d52254a..0e7017dd5 100644 --- a/cmd/kosli/testdata/output/docs/artifact.md +++ b/cmd/kosli/testdata/output/docs/artifact.md @@ -21,6 +21,7 @@ artifact {IMAGE-NAME | FILE-PATH | DIR-PATH} [flags] | -b, --build-url string | The url of CI pipeline that built the artifact. (defaulted in some CIs: https://docs.kosli.com/ci-defaults ). | | -u, --commit-url string | The url for the git commit that created the artifact. (defaulted in some CIs: https://docs.kosli.com/ci-defaults ). | | -D, --dry-run | [optional] Run in dry-run mode. When enabled, no data is sent to Kosli and the CLI exits with 0 exit code regardless of any errors. | +| -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 ). | diff --git a/docs.kosli.com/content/helm/_index.md b/docs.kosli.com/content/helm/_index.md index a67e3c0a6..579937da7 100644 --- a/docs.kosli.com/content/helm/_index.md +++ b/docs.kosli.com/content/helm/_index.md @@ -61,7 +61,7 @@ helm upgrade [RELEASE-NAME] kosli/k8s-reporter | kosliApiToken.secretKey | string | `""` | the name of the key in the secret data which contains the kosli API token | | kosliApiToken.secretName | string | `""` | the name of the secret containing the kosli API token | | nameOverride | string | `""` | overrides the name used for the created k8s resources. If `fullnameOverride` is provided, it has higher precedence than this one | -| podAnnotations | object | `{}` | | +| podAnnotations | object | `{}` | any custom annotations to be added to the cronjob | | reporterConfig.dryRun | bool | `false` | whether the dry run mode is enabled or not. In dry run mode, the reporter logs the reports to stdout and does not send them to kosli. | | reporterConfig.kosliEnvironmentName | string | `""` | the name of kosli environment that the k8s cluster/namespace correlates to | | reporterConfig.kosliOrg | string | `""` | the name of the kosli org | @@ -74,5 +74,5 @@ helm upgrade [RELEASE-NAME] kosli/k8s-reporter | serviceAccount.name | string | `""` | the name of the service account to use. If not set and create is true, a name is generated using the fullname template | ---------------------------------------------- -Autogenerated from chart metadata using [helm-docs v1.5.0](https://github.com/norwoodj/helm-docs/releases/v1.5.0) +Autogenerated from chart metadata using [helm-docs v1.11.0](https://github.com/norwoodj/helm-docs/releases/v1.11.0)