Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix defaulted github values not being processed before use in attest … #66

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/kosli/attestPRGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func newAttestGithubPRCmd(out io.Writer) *cobra.Command {
payload: PRAttestationPayload{
CommonAttestationPayload: &CommonAttestationPayload{},
},
retriever: new(ghUtils.GithubConfig),
}
githubFlagsValues := new(ghUtils.GithubFlagsTempValueHolder)
cmd := &cobra.Command{
Use: "github [IMAGE-NAME | FILE-PATH | DIR-PATH]",
Aliases: []string{"gh"},
Expand Down Expand Up @@ -129,13 +129,15 @@ func newAttestGithubPRCmd(out io.Writer) *cobra.Command {

},
RunE: func(cmd *cobra.Command, args []string) error {
o.retriever = ghUtils.NewGithubConfig(githubFlagsValues.Token, githubFlagsValues.BaseURL,
githubFlagsValues.Org, githubFlagsValues.Repository)
return o.run(args)
},
}

ci := WhichCI()
addAttestationFlags(cmd, o.CommonAttestationOptions, o.payload.CommonAttestationPayload, ci)
addAttestationGithubFlags(cmd, o.getRetriever().(*ghUtils.GithubConfig), ci)
addAttestationGithubFlags(cmd, githubFlagsValues, ci)
cmd.Flags().BoolVar(&o.assert, "assert", false, assertPREvidenceFlag)

err := RequireFlags(cmd, []string{"flow", "trail", "name",
Expand Down
1 change: 0 additions & 1 deletion cmd/kosli/cli_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ func DefaultValue(ci, flag string) string {
return strings.Replace(result, "commit", "commits", 1)
}
}

return result
}
}
Expand Down
20 changes: 20 additions & 0 deletions cmd/kosli/cli_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ func (suite *CliUtilsTestSuite) TestDefaultValue() {
},
want: "some-sha",
},
{
name: "Lookup default org for Github.",
args: args{
ci: github,
flag: "org",
envVars: map[string]string{"GITHUB_REPOSITORY_OWNER": "cyber-dojo"},
unsetTestsEnvVar: true,
},
want: "cyber-dojo",
},
{
name: "Lookup default repository for Github.",
args: args{
ci: github,
flag: "repository",
envVars: map[string]string{"GITHUB_REPOSITORY": "cyber-dojo/dashboard"},
unsetTestsEnvVar: true,
},
want: "cyber-dojo/dashboard",
},
{
name: "Lookup an existing default for Bitbucket.",
args: args{
Expand Down
10 changes: 5 additions & 5 deletions cmd/kosli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func addGitlabFlags(cmd *cobra.Command, gitlabConfig *gitlabUtils.GitlabConfig,
cmd.Flags().StringVar(&gitlabConfig.Repository, "repository", DefaultValue(ci, "repository"), repositoryFlag)
}

func addAttestationGithubFlags(cmd *cobra.Command, githubConfig *ghUtils.GithubConfig, ci string) {
cmd.Flags().StringVar(&githubConfig.Token, "github-token", "", githubTokenFlag)
cmd.Flags().StringVar(&githubConfig.Org, "github-org", DefaultValue(ci, "org"), githubOrgFlag)
cmd.Flags().StringVar(&githubConfig.Repository, "repository", DefaultValue(ci, "repository"), repositoryFlag)
cmd.Flags().StringVar(&githubConfig.BaseURL, "github-base-url", "", githubBaseURLFlag)
func addAttestationGithubFlags(cmd *cobra.Command, githubFlagsValueHolder *ghUtils.GithubFlagsTempValueHolder, ci string) {
cmd.Flags().StringVar(&githubFlagsValueHolder.Token, "github-token", "", githubTokenFlag)
cmd.Flags().StringVar(&githubFlagsValueHolder.Org, "github-org", DefaultValue(ci, "org"), githubOrgFlag)
cmd.Flags().StringVar(&githubFlagsValueHolder.Repository, "repository", DefaultValue(ci, "repository"), repositoryFlag)
cmd.Flags().StringVar(&githubFlagsValueHolder.BaseURL, "github-base-url", "", githubBaseURLFlag)
}

func addAttestationAzureFlags(cmd *cobra.Command, azureConfig *azUtils.AzureConfig, ci string) {
Expand Down
14 changes: 14 additions & 0 deletions cmd/kosli/testHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,17 @@
err := o.run([]string{envName})
require.NoError(t, err, "server env should be reported without error")
}

func SetEnvVars(envVars map[string]string, t *testing.T) {
for key, value := range envVars {
err := os.Setenv(key, value)
require.NoErrorf(t, err, "error setting env variable %s", key)
}

Check warning on line 355 in cmd/kosli/testHelpers.go

View check run for this annotation

Codecov / codecov/patch

cmd/kosli/testHelpers.go#L351-L355

Added lines #L351 - L355 were not covered by tests
}

func UnSetEnvVars(envVars map[string]string, t *testing.T) {
for key := range envVars {
err := os.Unsetenv(key)
require.NoErrorf(t, err, "error unsetting env variable %s", key)
}

Check warning on line 362 in cmd/kosli/testHelpers.go

View check run for this annotation

Codecov / codecov/patch

cmd/kosli/testHelpers.go#L358-L362

Added lines #L358 - L362 were not covered by tests
}
Loading