Skip to content

Commit

Permalink
fix defaulted github values not being processed before use in attest …
Browse files Browse the repository at this point in the history
…github pr command
  • Loading branch information
sami-alajrami committed Dec 15, 2023
1 parent 6dc4e4f commit a5b43a7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
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 @@ func ReportServerArtifactToEnv(paths []string, envName string, t *testing.T) {
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
}

0 comments on commit a5b43a7

Please sign in to comment.