-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add Release Bundle Evidence Using Cli Client #14
Changes from 7 commits
5f91b34
52e8507
9fee114
a4c7294
f03461d
5191921
775c46e
41944d8
d2d24c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cli | ||
|
||
import ( | ||
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
) | ||
|
||
type EvidenceCommands interface { | ||
CreateEvidence(*coreConfig.ServerDetails) error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,14 @@ package cli | |
|
||
import ( | ||
"errors" | ||
"github.com/jfrog/jfrog-cli-artifactory/evidence" | ||
"github.com/jfrog/jfrog-cli-artifactory/evidence/cli/docs/create" | ||
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils" | ||
"github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" | ||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
"github.com/jfrog/jfrog-client-go/utils" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"strings" | ||
) | ||
|
||
func GetCommands() []components.Command { | ||
|
@@ -26,41 +25,26 @@ func GetCommands() []components.Command { | |
} | ||
} | ||
|
||
func platformToEvidenceUrls(rtDetails *coreConfig.ServerDetails) { | ||
rtDetails.ArtifactoryUrl = utils.AddTrailingSlashIfNeeded(rtDetails.Url) + "artifactory/" | ||
rtDetails.EvidenceUrl = utils.AddTrailingSlashIfNeeded(rtDetails.Url) + "evidence/" | ||
} | ||
|
||
func createEvidence(c *components.Context) error { | ||
if err := validateCreateEvidenceContext(c); err != nil { | ||
return err | ||
} | ||
|
||
artifactoryClient, err := evidenceDetailsByFlags(c) | ||
subject, err := getAndValidateSubject(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
createCmd := evidence.NewEvidenceCreateCommand(). | ||
SetServerDetails(artifactoryClient). | ||
SetPredicateFilePath(c.GetStringFlagValue(EvdPredicate)). | ||
SetPredicateType(c.GetStringFlagValue(EvdPredicateType)). | ||
SetRepoPath(c.GetStringFlagValue(EvdRepoPath)). | ||
SetKey(c.GetStringFlagValue(EvdKey)). | ||
SetKeyId(c.GetStringFlagValue(EvdKeyId)) | ||
return commands.Exec(createCmd) | ||
} | ||
|
||
func evidenceDetailsByFlags(c *components.Context) (*coreConfig.ServerDetails, error) { | ||
artifactoryClient, err := pluginsCommon.CreateServerDetailsWithConfigOffer(c, true, commonCliUtils.Platform) | ||
artifactoryClient, err := evidenceDetailsByFlags(c) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
if artifactoryClient.Url == "" { | ||
return nil, errors.New("platform URL is mandatory for evidence commands") | ||
var command EvidenceCommands | ||
if subject == EvdRepoPath { | ||
command = NewEvidenceCustomCommand(c) | ||
} | ||
platformToEvidenceUrls(artifactoryClient) | ||
return artifactoryClient, nil | ||
if subject == releaseBundle { | ||
command = NewEvidenceReleaseBundleCommand(c) | ||
} | ||
return command.CreateEvidence(artifactoryClient) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsafe code, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
func validateCreateEvidenceContext(c *components.Context) error { | ||
|
@@ -78,16 +62,51 @@ func validateCreateEvidenceContext(c *components.Context) error { | |
if !c.IsFlagSet(EvdPredicateType) || assertValueProvided(c, EvdPredicateType) != nil { | ||
return errorutils.CheckErrorf("'predicate' is a mandatory field for creating a custom evidence: --%s", EvdPredicateType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
if !c.IsFlagSet(EvdRepoPath) || assertValueProvided(c, EvdRepoPath) != nil { | ||
return errorutils.CheckErrorf("'repo-path' is a mandatory field for creating a custom evidence: --%s", EvdRepoPath) | ||
} | ||
if !c.IsFlagSet(EvdKey) || assertValueProvided(c, EvdKey) != nil { | ||
return errorutils.CheckErrorf("'key' is a mandatory field for creating a custom evidence: --%s", EvdKey) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getAndValidateSubject(c *components.Context) (string, error) { | ||
subjects := []string{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be moved outside of the function (no need to re-allocate each time)., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
EvdRepoPath, | ||
releaseBundle, | ||
} | ||
var foundSubjects []string | ||
for _, key := range subjects { | ||
if c.GetStringFlagValue(key) != "" { | ||
foundSubjects = append(foundSubjects, key) | ||
} | ||
} | ||
|
||
if len(foundSubjects) == 0 { | ||
return "", errorutils.CheckErrorf("Subject must be one of the fields: [%s]", strings.Join(subjects, ", ")) | ||
} | ||
if len(foundSubjects) > 1 { | ||
return "", errorutils.CheckErrorf("multiple subjects found: [%s]", strings.Join(foundSubjects, ", ")) | ||
} | ||
return foundSubjects[0], nil | ||
} | ||
|
||
func evidenceDetailsByFlags(c *components.Context) (*coreConfig.ServerDetails, error) { | ||
artifactoryClient, err := pluginsCommon.CreateServerDetailsWithConfigOffer(c, true, commonCliUtils.Platform) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if artifactoryClient.Url == "" { | ||
return nil, errors.New("platform URL is mandatory for evidence commands") | ||
} | ||
platformToEvidenceUrls(artifactoryClient) | ||
return artifactoryClient, nil | ||
} | ||
|
||
func platformToEvidenceUrls(rtDetails *coreConfig.ServerDetails) { | ||
rtDetails.ArtifactoryUrl = utils.AddTrailingSlashIfNeeded(rtDetails.Url) + "artifactory/" | ||
rtDetails.EvidenceUrl = utils.AddTrailingSlashIfNeeded(rtDetails.Url) + "evidence/" | ||
} | ||
|
||
func assertValueProvided(c *components.Context, fieldName string) error { | ||
if c.GetStringFlagValue(fieldName) == "" { | ||
return errorutils.CheckErrorf("the --%s option is mandatory", fieldName) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cli | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateEvidence_Context(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
context *components.Context | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "InvalidContext - Missing Subject", | ||
context: createContext("somePredicate", "InToto", "PGP", "", ""), | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "InvalidContext - Missing Predicate", | ||
context: createContext("", "InToto", "PGP", "someBundle", ""), | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "InvalidContext - Subject Duplication", | ||
context: createContext("somePredicate", "InToto", "PGP", "someBundle", "path"), | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := createEvidence(tt.context) | ||
if tt.expectErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
}) | ||
} | ||
} | ||
|
||
func createContext(predicate string, predicateType string, key string, rb string, repoPath string) *components.Context { | ||
ctx := &components.Context{ | ||
Arguments: []string{}, | ||
} | ||
setStringFlagValue(ctx, EvdPredicate, predicate) | ||
setStringFlagValue(ctx, EvdPredicateType, predicateType) | ||
setStringFlagValue(ctx, EvdKey, key) | ||
setStringFlagValue(ctx, EvdRepoPath, repoPath) | ||
setStringFlagValue(ctx, releaseBundle, rb) | ||
return ctx | ||
} | ||
|
||
func setStringFlagValue(ctx *components.Context, flagName, value string) { | ||
val := reflect.ValueOf(ctx).Elem() | ||
stringFlags := val.FieldByName("stringFlags") | ||
|
||
// If the field is not settable, we need to make it settable | ||
if !stringFlags.CanSet() { | ||
stringFlags = reflect.NewAt(stringFlags.Type(), unsafe.Pointer(stringFlags.UnsafeAddr())).Elem() | ||
} | ||
|
||
if stringFlags.IsNil() { | ||
stringFlags.Set(reflect.MakeMap(stringFlags.Type())) | ||
} | ||
stringFlags.SetMapIndex(reflect.ValueOf(flagName), reflect.ValueOf(value)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-cli-artifactory/evidence" | ||
"github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
) | ||
|
||
type EvidenceCustomCommand struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it an exported struct and not an interface? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
c *components.Context | ||
} | ||
|
||
func NewEvidenceCustomCommand(ctx *components.Context) EvidenceCommands { | ||
return &EvidenceCustomCommand{ | ||
c: ctx, | ||
} | ||
} | ||
|
||
func (ecc *EvidenceCustomCommand) CreateEvidence(artifactoryClient *coreConfig.ServerDetails) error { | ||
createCmd := evidence.NewCreateEvidenceCustom(). | ||
SetServerDetails(artifactoryClient). | ||
SetPredicateFilePath(ecc.c.GetStringFlagValue(EvdPredicate)). | ||
SetPredicateType(ecc.c.GetStringFlagValue(EvdPredicateType)). | ||
SetRepoPath(ecc.c.GetStringFlagValue(EvdRepoPath)). | ||
SetKey(ecc.c.GetStringFlagValue(EvdKey)). | ||
SetKeyId(ecc.c.GetStringFlagValue(EvdKeyId)) | ||
return commands.Exec(createCmd) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-cli-artifactory/evidence" | ||
"github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
) | ||
|
||
type EvidenceReleaseBundleCommand struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it an exported struct and not an interface? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
c *components.Context | ||
} | ||
|
||
func NewEvidenceReleaseBundleCommand(ctx *components.Context) EvidenceCommands { | ||
return &EvidenceReleaseBundleCommand{ | ||
c: ctx, | ||
} | ||
} | ||
|
||
func (erc *EvidenceReleaseBundleCommand) CreateEvidence(artifactoryClient *coreConfig.ServerDetails) error { | ||
createCmd := evidence.NewCreateEvidenceReleaseBundle(). | ||
SetServerDetails(artifactoryClient). | ||
SetPredicateFilePath(erc.c.GetStringFlagValue(EvdPredicate)). | ||
SetPredicateType(erc.c.GetStringFlagValue(EvdPredicateType)). | ||
SetProject(erc.c.GetStringFlagValue(project)). | ||
SetReleaseBundle(erc.c.GetStringFlagValue(releaseBundle)). | ||
SetKey(erc.c.GetStringFlagValue(EvdKey)). | ||
SetKeyId(erc.c.GetStringFlagValue(EvdKeyId)) | ||
return commands.Exec(createCmd) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be either a
switch
orif/else
(no need to check forif
after the match).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done