Skip to content
Open
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
4 changes: 3 additions & 1 deletion cmd/kosli/attestArtifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type attestArtifactOptions struct {
repoName string
repoURL string
repoProvider string
repoNameExplicit bool
}

type AttestArtifactPayload struct {
Expand Down Expand Up @@ -133,6 +134,7 @@ func newAttestArtifactCmd(out io.Writer) *cobra.Command {
return ValidateRegistryFlags(cmd, o.fingerprintOptions)
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down Expand Up @@ -210,7 +212,7 @@ func (o *attestArtifactOptions) run(args []string) error {
if err != nil {
logger.Warn("failed to get git repo info. %s", err.Error())
}
o.payload.GitRepoInfo = mergeGitRepoInfo(o.payload.GitRepoInfo, o.repoID, o.repoName, o.repoURL, o.repoProvider)
o.payload.GitRepoInfo = mergeGitRepoInfo(o.payload.GitRepoInfo, o.repoID, o.repoName, o.repoURL, o.repoProvider, o.repoNameExplicit)
o.payload.GitCommit = commitInfo.Sha1
o.payload.GitCommitInfo = &commitInfo.BasicCommitInfo

Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestCustom.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func newAttestCustomCmd(out io.Writer) *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestDecision.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func newAttestDecisionCmd(out io.Writer) *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestGeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func newAttestGenericCmd(out io.Writer) *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestJira.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func newAttestJiraCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestJunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func newAttestJunitCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestOverride.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func newAttestOverrideCmd(out io.Writer) *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestPRAzure.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func newAttestAzurePRCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
o.retriever = azUtils.NewAzureConfig(azureFlagsValues.Token,
azureFlagsValues.OrgUrl, azureFlagsValues.Project, o.repoName)
return o.run(args)
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestPRBitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func newAttestBitbucketPRCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
o.getRetriever().(*bbUtils.Config).Repository = o.repoName
return o.run(args)
},
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestPRGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func newAttestGithubPRCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
o.retriever = ghUtils.NewGithubRetrieverFunc(githubFlagsValues.Token, githubFlagsValues.BaseURL,
githubFlagsValues.Org, o.repoName, global.Debug)
return o.run(args)
Expand Down
4 changes: 4 additions & 0 deletions cmd/kosli/attestPRGitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func newAttestGitlabPRCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
// GitlabConfig.Repository is the short project name (CI_PROJECT_NAME);
// combined with Org (CI_PROJECT_NAMESPACE) it forms the API ProjectID.
// This is separate from repo_info.name, which uses the full CI_PROJECT_PATH.
o.getRetriever().(*gitlabUtils.GitlabConfig).Repository = o.repoName
return o.run(args)
},
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestSnyk.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func newAttestSnykCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/attestSonar.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func newAttestSonarCmd(out io.Writer) *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoURLExplicit = cmd.Flags().Changed("repo-url")
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/kosli/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type CommonAttestationOptions struct {
repoURL string
repoProvider string
repoURLExplicit bool
repoNameExplicit bool
}

func (o *CommonAttestationOptions) run(args []string, payload *CommonAttestationPayload) error {
Expand Down Expand Up @@ -98,7 +99,7 @@ func (o *CommonAttestationOptions) run(args []string, payload *CommonAttestation
if err := validateRepoFlags(o.repoURL, o.repoProvider, o.repoURLExplicit); err != nil {
return err
}
payload.GitRepoInfo = mergeGitRepoInfo(payload.GitRepoInfo, o.repoID, o.repoName, o.repoURL, o.repoProvider)
payload.GitRepoInfo = mergeGitRepoInfo(payload.GitRepoInfo, o.repoID, o.repoName, o.repoURL, o.repoProvider, o.repoNameExplicit)
Comment thread
FayeSGW marked this conversation as resolved.

payload.UserData, err = LoadJsonData(o.userDataFilePath)
if err != nil {
Expand All @@ -119,14 +120,18 @@ func (o *CommonAttestationOptions) run(args []string, payload *CommonAttestation
// mergeGitRepoInfo applies flag overrides onto base (which may be nil) and
// returns nil if ID, Name, or URL is still empty after merging, so that the
// field is omitted from the JSON payload.
func mergeGitRepoInfo(base *gitview.GitRepoInfo, repoID, repoName, repoURL, repoProvider string) *gitview.GitRepoInfo {
//
// --repository only overrides the CI-detected name when set explicitly (or when
// base has none), so its short default doesn't clobber the fuller CI value
// (e.g. GitLab's CI_PROJECT_PATH).
func mergeGitRepoInfo(base *gitview.GitRepoInfo, repoID, repoName, repoURL, repoProvider string, repoNameExplicit bool) *gitview.GitRepoInfo {
if base == nil {
base = &gitview.GitRepoInfo{}
}
if repoID != "" {
base.ID = repoID
}
if repoName != "" {
if repoName != "" && (repoNameExplicit || base.Name == "") {
base.Name = repoName
}
if repoURL != "" {
Expand Down
86 changes: 66 additions & 20 deletions cmd/kosli/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (

func TestMergeGitRepoInfo(t *testing.T) {
tests := []struct {
name string
base *gitview.GitRepoInfo
repoID string
repoName string
repoURL string
repoProvider string
wantNil bool
wantID string
wantName string
wantURL string
wantProvider string
name string
base *gitview.GitRepoInfo
repoID string
repoName string
repoURL string
repoProvider string
repoNameExplicit bool
wantNil bool
wantID string
wantName string
wantURL string
wantProvider string
}{
{
name: "nil when both ID and Name are empty",
Expand Down Expand Up @@ -64,14 +65,59 @@ func TestMergeGitRepoInfo(t *testing.T) {
wantProvider: "github",
},
{
name: "flag values override base values",
base: &gitview.GitRepoInfo{ID: "base-id", Name: "base-name", URL: "https://base.example.com"},
repoID: "override-id",
repoName: "override-name",
wantNil: false,
wantID: "override-id",
wantName: "override-name",
wantURL: "https://base.example.com",
name: "explicit flag values override base values",
base: &gitview.GitRepoInfo{ID: "base-id", Name: "base-name", URL: "https://base.example.com"},
repoID: "override-id",
repoName: "override-name",
repoNameExplicit: true,
wantNil: false,
wantID: "override-id",
wantName: "override-name",
wantURL: "https://base.example.com",
},
{
name: "fully-populated base is returned unchanged when no flags are passed",
base: &gitview.GitRepoInfo{ID: "53419335", Name: "cyber-dojo/creator", URL: "https://gitlab.com/cyber-dojo/creator", Provider: "gitlab"},
wantNil: false,
wantID: "53419335",
wantName: "cyber-dojo/creator",
wantURL: "https://gitlab.com/cyber-dojo/creator",
wantProvider: "gitlab",
},
{
name: "CI-detected full-path name is preserved when --repository is not set explicitly",
base: &gitview.GitRepoInfo{ID: "53419335", Name: "cyber-dojo/creator", URL: "https://gitlab.com/cyber-dojo/creator"},
repoID: "53419335",
repoName: "creator", // short CI default from --repository (e.g. GitLab's CI_PROJECT_NAME)
repoURL: "https://gitlab.com/cyber-dojo/creator",
repoNameExplicit: false,
wantNil: false,
wantID: "53419335",
wantName: "cyber-dojo/creator", // full CI_PROJECT_PATH preserved
wantURL: "https://gitlab.com/cyber-dojo/creator",
},
{
name: "explicit --repository overrides CI-detected full-path name",
base: &gitview.GitRepoInfo{ID: "53419335", Name: "cyber-dojo/creator", URL: "https://gitlab.com/cyber-dojo/creator"},
repoID: "53419335",
repoName: "my/custom-name",
repoURL: "https://gitlab.com/cyber-dojo/creator",
repoNameExplicit: true,
wantNil: false,
wantID: "53419335",
wantName: "my/custom-name",
wantURL: "https://gitlab.com/cyber-dojo/creator",
},
{
name: "flag name applied when base has no name even if not explicit",
repoID: "flag-id",
repoName: "flag-name",
repoURL: "https://github.com/org/repo",
repoNameExplicit: false,
wantNil: false,
wantID: "flag-id",
wantName: "flag-name",
wantURL: "https://github.com/org/repo",
},
{
name: "nil when base has ID but no Name and no flags",
Expand Down Expand Up @@ -103,7 +149,7 @@ func TestMergeGitRepoInfo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := mergeGitRepoInfo(tt.base, tt.repoID, tt.repoName, tt.repoURL, tt.repoProvider)
result := mergeGitRepoInfo(tt.base, tt.repoID, tt.repoName, tt.repoURL, tt.repoProvider, tt.repoNameExplicit)
if tt.wantNil {
assert.Nil(t, result)
return
Expand Down
4 changes: 3 additions & 1 deletion cmd/kosli/beginTrail.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type beginTrailOptions struct {
repoName string
repoURL string
repoProvider string
repoNameExplicit bool
}

type TrailPayload struct {
Expand Down Expand Up @@ -83,6 +84,7 @@ func newBeginTrailCmd(out io.Writer) *cobra.Command {
return validateRepoFlags(o.repoURL, o.repoProvider, cmd.Flags().Changed("repo-url"))
},
RunE: func(cmd *cobra.Command, args []string) error {
o.repoNameExplicit = cmd.Flags().Changed("repository")
return o.run(args)
},
}
Expand Down Expand Up @@ -141,7 +143,7 @@ func (o *beginTrailOptions) run(args []string) error {
if err != nil {
logger.Warn("failed to get git repo info. %s", err.Error())
}
o.payload.GitRepoInfo = mergeGitRepoInfo(base, o.repoID, o.repoName, o.repoURL, o.repoProvider)
o.payload.GitRepoInfo = mergeGitRepoInfo(base, o.repoID, o.repoName, o.repoURL, o.repoProvider, o.repoNameExplicit)

// process external urls
o.payload.ExternalURLs, err = processExternalURLs(o.externalURLs, o.externalFingerprints)
Expand Down
Loading
Loading