-
Notifications
You must be signed in to change notification settings - Fork 6
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
Create Build Evidence. #16
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-cli-artifactory/evidence" | ||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
) | ||
|
||
type evidenceBuildCommand struct { | ||
ctx *components.Context | ||
execute execCommandFunc | ||
} | ||
|
||
func NewEvidenceBuildCommand(ctx *components.Context, execute execCommandFunc) EvidenceCommands { | ||
return &evidenceBuildCommand{ | ||
ctx: ctx, | ||
execute: execute, | ||
} | ||
} | ||
|
||
func (erc *evidenceBuildCommand) CreateEvidence(serverDetails *coreConfig.ServerDetails) error { | ||
createCmd := evidence.NewCreateEvidenceBuild( | ||
serverDetails, | ||
erc.ctx.GetStringFlagValue(predicate), | ||
erc.ctx.GetStringFlagValue(predicateType), | ||
erc.ctx.GetStringFlagValue(key), | ||
erc.ctx.GetStringFlagValue(keyId), | ||
erc.ctx.GetStringFlagValue(project), | ||
erc.ctx.GetStringFlagValue(build)) | ||
return erc.execute(createCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ func exec(command commands.Command) error { | |
var subjectTypes = []string{ | ||
repoPath, | ||
releaseBundle, | ||
build, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package evidence | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jfrog/jfrog-cli-artifactory/evidence/utils" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
"github.com/jfrog/jfrog-client-go/artifactory" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"github.com/jfrog/jfrog-client-go/utils/log" | ||
"strings" | ||
) | ||
|
||
type createEvidenceBuild struct { | ||
createEvidenceBase | ||
project string | ||
build string | ||
} | ||
|
||
func NewCreateEvidenceBuild(serverDetails *coreConfig.ServerDetails, | ||
predicateFilePath, predicateType, key, keyId, project, build string) Command { | ||
return &createEvidenceBuild{ | ||
createEvidenceBase: createEvidenceBase{ | ||
serverDetails: serverDetails, | ||
predicateFilePath: predicateFilePath, | ||
predicateType: predicateType, | ||
key: key, | ||
keyId: keyId, | ||
}, | ||
project: project, | ||
build: build, | ||
} | ||
} | ||
|
||
func (c *createEvidenceBuild) CommandName() string { | ||
return "create-build-evidence" | ||
} | ||
|
||
func (c *createEvidenceBuild) ServerDetails() (*config.ServerDetails, error) { | ||
return c.serverDetails, nil | ||
} | ||
|
||
func (c *createEvidenceBuild) Run() error { | ||
artifactoryClient, err := c.createArtifactoryClient() | ||
if err != nil { | ||
log.Error("failed to create Artifactory client", err) | ||
return err | ||
} | ||
subject, err := c.buildBuildInfoSubjectPath(artifactoryClient) | ||
if err != nil { | ||
return err | ||
} | ||
envelope, err := c.createEnvelope(subject) | ||
if err != nil { | ||
return err | ||
} | ||
err = c.uploadEvidence(envelope, subject) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *createEvidenceBuild) buildBuildInfoSubjectPath(artifactoryClient artifactory.ArtifactoryServicesManager) (string, error) { | ||
build := strings.Split(c.build, ":") | ||
if len(build) != 2 { | ||
return "", fmt.Errorf("invalid build format. expected format is <name>:<number>") | ||
} | ||
name := build[0] | ||
number := build[1] | ||
|
||
timestamp, err := getBuildLatestTimestamp(name, number, c.project, artifactoryClient) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
repoKey := buildBuildInfoRepoKey(c.project) | ||
buildInfoPath := buildBuildInfoPath(repoKey, name, number, timestamp) | ||
buildInfoChecksum, err := getBuildInfoPathChecksum(buildInfoPath, artifactoryClient) | ||
if err != nil { | ||
return "", err | ||
} | ||
return buildInfoPath + "@" + buildInfoChecksum, nil | ||
} | ||
|
||
func getBuildLatestTimestamp(name string, number string, project string, artifactoryClient artifactory.ArtifactoryServicesManager) (string, error) { | ||
buildInfo := services.BuildInfoParams{ | ||
BuildName: name, | ||
BuildNumber: number, | ||
ProjectKey: project, | ||
} | ||
res, ok, err := artifactoryClient.GetBuildInfo(buildInfo) | ||
if err != nil { | ||
return "", err | ||
} | ||
if !ok { | ||
errorMessage := fmt.Sprintf("failed to find build, name:%s, number:%s, project: %s", name, number, project) | ||
return "", errorutils.CheckErrorf(errorMessage) | ||
} | ||
timestamp, err := utils.ParseIsoTimestamp(res.BuildInfo.Started) | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%d", timestamp.UnixMilli()), nil | ||
} | ||
|
||
func buildBuildInfoRepoKey(project string) string { | ||
if project == "" || project == "default" { | ||
return "artifactory-build-info" | ||
} | ||
return fmt.Sprintf("%s-build-info", project) | ||
} | ||
|
||
func buildBuildInfoPath(repoKey string, name string, number string, timestamp string) string { | ||
jsonFile := fmt.Sprintf("%s-%s.json", number, timestamp) | ||
return fmt.Sprintf("%s/%s/%s", repoKey, name, jsonFile) | ||
} | ||
|
||
func getBuildInfoPathChecksum(buildInfoPath string, artifactoryClient artifactory.ArtifactoryServicesManager) (string, error) { | ||
res, err := artifactoryClient.FileInfo(buildInfoPath) | ||
if err != nil { | ||
log.Warn(fmt.Sprintf("build info json path '%s' does not exist.", buildInfoPath)) | ||
return "", err | ||
} | ||
return res.Checksums.Sha256, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package evidence | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
import ( | ||
buildinfo "github.com/jfrog/build-info-go/entities" | ||
"github.com/jfrog/jfrog-client-go/artifactory" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services" | ||
) | ||
|
||
type mockArtifactoryServicesManagerBuild struct { | ||
artifactory.EmptyArtifactoryServicesManager | ||
} | ||
|
||
func (m *mockArtifactoryServicesManagerBuild) FileInfo(relativePath string) (*utils.FileInfo, error) { | ||
fi := &utils.FileInfo{ | ||
Checksums: struct { | ||
Sha1 string `json:"sha1,omitempty"` | ||
Sha256 string `json:"sha256,omitempty"` | ||
Md5 string `json:"md5,omitempty"` | ||
}{ | ||
Sha256: "dummy_sha256", | ||
}, | ||
} | ||
return fi, nil | ||
} | ||
|
||
func (m *mockArtifactoryServicesManagerBuild) GetBuildInfo(services.BuildInfoParams) (*buildinfo.PublishedBuildInfo, bool, error) { | ||
buildInfo := &buildinfo.PublishedBuildInfo{ | ||
BuildInfo: buildinfo.BuildInfo{ | ||
Started: "2024-01-17T15:04:05.000-0700", | ||
}, | ||
} | ||
return buildInfo, true, nil | ||
} | ||
|
||
func TestBuildInfo(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
project string | ||
build string | ||
expectedPath string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Valid build with project", | ||
project: "myProject", | ||
build: "buildName:1", | ||
expectedPath: "myProject-build-info/buildName/1-1705529045000.json@dummy_sha256", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Valid build default project", | ||
project: "default", | ||
build: "buildName:1", | ||
expectedPath: "artifactory-build-info/buildName/1-1705529045000.json@dummy_sha256", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Invalid build format", | ||
project: "myProject", | ||
build: "buildName-1", | ||
expectedPath: "", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &createEvidenceBuild{ | ||
project: tt.project, | ||
build: tt.build, | ||
} | ||
aa := &mockArtifactoryServicesManagerBuild{} | ||
path, err := c.buildBuildInfoSubjectPath(aa) | ||
if tt.expectError { | ||
assert.Error(t, err) | ||
assert.Empty(t, path) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expectedPath, path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please, raise the question about the input parameters one more time.
This split means that neither Build Name nor Build Number may contain
:
(this is not the case).Hence, it means that we do not support Builds with name/number having
:
.Needs to be confirmed explicitly.
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.
I talked with Noam, there is a chance to change all other operations to be splitter to --name --version.
will take it in a separate Jira ticket.