Skip to content

Commit 37da813

Browse files
authored
V1 fix gradle plugin (#209)
1 parent e5f1fe1 commit 37da813

File tree

12 files changed

+209
-70
lines changed

12 files changed

+209
-70
lines changed

artifactory/commands/gradle/gradle.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ func (gc *GradleCommand) Run() error {
8181
}
8282

8383
func (gc *GradleCommand) unmarshalDeployableArtifacts(filesPath string) error {
84-
serverDetails, err := gc.ServerDetails()
85-
if err != nil {
86-
return err
87-
}
88-
result, err := commandsutils.UnmarshalDeployableArtifacts(filesPath, serverDetails.ArtifactoryUrl)
84+
result, err := commandsutils.UnmarshalDeployableArtifacts(filesPath, gc.configPath)
8985
if err != nil {
9086
return err
9187
}

artifactory/commands/mvn/mvn.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ func (mc *MvnCommand) createMvnRunConfig(dependenciesPath string) (*mvnRunConfig
255255
}
256256

257257
func (mc *MvnCommand) unmarshalDeployableArtifacts(filesPath string) error {
258-
serverDetails, err := mc.ServerDetails()
259-
if err != nil {
260-
return err
261-
}
262-
result, err := commandsutils.UnmarshalDeployableArtifacts(filesPath, serverDetails.ArtifactoryUrl)
258+
result, err := commandsutils.UnmarshalDeployableArtifacts(filesPath, mc.configPath)
263259
if err != nil {
264260
return err
265261
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 1
2+
type: gradle
3+
resolver:
4+
repo: libs-release
5+
serverId: test
6+
deployer:
7+
repo: gradle-local-repo
8+
serverId: test
9+
ivyPattern: '[organization]/[module]/ivy-[revision].xml'
10+
artifactPattern: '[organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
11+
usePlugin: true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"shared":[{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/shared/build/libs/shared-1.0-SNAPSHOT.jar","artifactDest":"org/jfrog/test/gradle/publish/shared/1.0-SNAPSHOT/shared-1.0-SNAPSHOT.jar","sha1":"e471721512461342a0cb230726474c54a3089275","sha256":"3664fa68f0e5ef2c01ccc41715108f8380e336d0cfc82367750f035ec9a494ba","deploySucceeded":true},{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/gradle.properties","artifactDest":"org/jfrog/test/gradle/publish/shared/1.0-SNAPSHOT/shared-1.0-SNAPSHOT.properties","sha1":"06fdab8aa5df49b45b1771cf6ec56d379090a7ba","sha256":"ab17a74ebcbd624ccd245378126bef1fd30ed8d392e3fa2ad3088f7fa8bc4de9","deploySucceeded":true}],"webservice":[{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/services/webservice/build/libs/webservice-1.0-SNAPSHOT.jar","artifactDest":"org/jfrog/test/gradle/publish/webservice/1.0-SNAPSHOT/webservice-1.0-SNAPSHOT.jar","sha1":"73613a903ad45ed335f28aad3a725226a71cd969","sha256":"f8107aa11da0d043e4b5a86827c8ba6945b596486a8c1dbe602e96b6ecb624ad","deploySucceeded":true},{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/gradle.properties","artifactDest":"org/jfrog/test/gradle/publish/webservice/1.0-SNAPSHOT/webservice-1.0-SNAPSHOT.properties","sha1":"06fdab8aa5df49b45b1771cf6ec56d379090a7ba","sha256":"ab17a74ebcbd624ccd245378126bef1fd30ed8d392e3fa2ad3088f7fa8bc4de9","deploySucceeded":true}],"api":[{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/api/build/libs/api-1.0-SNAPSHOT.jar","artifactDest":"org/jfrog/test/gradle/publish/api/1.0-SNAPSHOT/api-1.0-SNAPSHOT.jar","sha1":"0a57f71bbb170f8a1abf8ca48e4d66583ed6c212","sha256":"4e4144738752d89d5f44dc1e1dbadb1a86d07423ff95ed68d993d3d9ee551878","deploySucceeded":true},{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/gradle.properties","artifactDest":"org/jfrog/test/gradle/publish/api/1.0-SNAPSHOT/api-1.0-SNAPSHOT.properties","sha1":"06fdab8aa5df49b45b1771cf6ec56d379090a7ba","sha256":"ab17a74ebcbd624ccd245378126bef1fd30ed8d392e3fa2ad3088f7fa8bc4de9","deploySucceeded":true},{"sourcePath":"/Users/gail/dev/project-examples-master/gradle-examples/gradle-example-publish/settings.gradle","artifactDest":"org/jfrog/test/gradle/publish/api/1.0-SNAPSHOT/gradle-settings-1.0-SNAPSHOT.txt","sha1":"30788efb365346a1bcabfa8b2041cd875fdad213","sha256":"fb08055200862980c6e80a7d862ec306bf4549fcff87f132ab824f38c8a5205c","deploySucceeded":true}]}

artifactory/commands/utils/result.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package utils
22

33
import (
44
"encoding/json"
5+
"github.com/jfrog/jfrog-cli-core/artifactory/utils"
6+
"github.com/jfrog/jfrog-cli-core/utils/config"
57
"io/ioutil"
68
"os"
79

@@ -10,6 +12,12 @@ import (
1012
"github.com/jfrog/jfrog-client-go/utils/io/content"
1113
)
1214

15+
const (
16+
configDeployerPrefix = "deployer"
17+
gradleConfigRepo = "repo"
18+
configServerId = "serverid"
19+
)
20+
1321
type Result struct {
1422
successCount int
1523
failCount int
@@ -40,10 +48,16 @@ func (r *Result) SetReader(reader *content.ContentReader) {
4048
r.reader = reader
4149
}
4250

43-
// UnmarshalDeployableArtifacts Reads and parses the deployed artifacts details from the provided file.
51+
// UnmarshalDeployableArtifacts reads and parses the deployed artifacts details from the provided file.
4452
// The details were written by Buildinfo project while deploying artifacts to maven and gradle repositories.
45-
func UnmarshalDeployableArtifacts(filePath, rtUrl string) (*Result, error) {
46-
modulesMap, err := unmarshalDeployableArtifactsJson(filePath)
53+
// deployableArtifactsFilePath - path to deployableArtifacts file written by buildinfo project.
54+
// ProjectConfigPath - path to gradle/maven config yaml path.
55+
func UnmarshalDeployableArtifacts(deployableArtifactsFilePath, ProjectConfigPath string) (*Result, error) {
56+
modulesMap, err := unmarshalDeployableArtifactsJson(deployableArtifactsFilePath)
57+
if err != nil {
58+
return nil, err
59+
}
60+
url, repo, err := getDeployerUrlAndRepo(modulesMap, ProjectConfigPath)
4761
if err != nil {
4862
return nil, err
4963
}
@@ -54,21 +68,55 @@ func UnmarshalDeployableArtifacts(filePath, rtUrl string) (*Result, error) {
5468
for _, artifact := range module {
5569
if artifact.DeploySucceeded {
5670
succeeded++
57-
artifactsArray = append(artifactsArray, artifact.CreateFileTransferDetails(rtUrl))
71+
artifactsArray = append(artifactsArray, artifact.CreateFileTransferDetails(url, repo))
5872
} else {
5973
failed++
6074
}
6175
}
6276
}
63-
err = clientutils.SaveFileTransferDetailsInFile(filePath, &artifactsArray)
77+
err = clientutils.SaveFileTransferDetailsInFile(deployableArtifactsFilePath, &artifactsArray)
6478
// Return result
6579
result := new(Result)
6680
result.SetSuccessCount(succeeded)
6781
result.SetFailCount(failed)
68-
result.SetReader(content.NewContentReader(filePath, "files"))
82+
result.SetReader(content.NewContentReader(deployableArtifactsFilePath, "files"))
6983
return result, nil
7084
}
7185

86+
// getDeployerUrlAndRepo returns the deployer url and the target repository for maven and gradle.
87+
// Url is being read from the project's local configuration file.
88+
// Repository is being read from the modulesMap.
89+
// modulesMap - map of the DeployableArtifactDetails.
90+
// configPath - path to the project's local configuration file.
91+
func getDeployerUrlAndRepo(modulesMap *map[string][]clientutils.DeployableArtifactDetails, configPath string) (string, string, error) {
92+
repo := getTargetRepoFromMap(modulesMap)
93+
vConfig, err := utils.ReadConfigFile(configPath, utils.YAML)
94+
if err != nil {
95+
return "", "", err
96+
}
97+
// The relevant deployment repository will be written by the buildinfo project to the diplyableArtifacts file starting from version 2.24.12 of build-info-extractor-gradle.
98+
// In case of a gradle project with a configuration of 'usePlugin=true' it's possible that an old build-info-extractor-gradle version is being used.
99+
// In this case, the value of "repo" will be empty, and the deployment repository will be therefore read from the local project configuration file.
100+
if repo == "" {
101+
repo = vConfig.GetString(configDeployerPrefix + "." + gradleConfigRepo)
102+
}
103+
artDetails, err := config.GetSpecificConfig(vConfig.GetString(configDeployerPrefix+"."+configServerId), true, true)
104+
if err != nil {
105+
return "", "", err
106+
}
107+
url := artDetails.ArtifactoryUrl
108+
return url, repo, nil
109+
}
110+
111+
func getTargetRepoFromMap(modulesMap *map[string][]clientutils.DeployableArtifactDetails) string {
112+
for _, module := range *modulesMap {
113+
for _, artifact := range module {
114+
return artifact.TargetRepository
115+
}
116+
}
117+
return ""
118+
}
119+
72120
func unmarshalDeployableArtifactsJson(filesPath string) (*map[string][]clientutils.DeployableArtifactDetails, error) {
73121
// Open the file
74122
jsonFile, err := os.Open(filesPath)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package utils
2+
3+
import (
4+
"github.com/jfrog/jfrog-cli-core/common/tests"
5+
clientutils "github.com/jfrog/jfrog-client-go/utils"
6+
"github.com/jfrog/jfrog-client-go/utils/errorutils"
7+
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
8+
"github.com/stretchr/testify/assert"
9+
"os"
10+
"path"
11+
"path/filepath"
12+
"strings"
13+
"testing"
14+
)
15+
16+
// Checks a case that targetRepository is not written in a deployableArtifacts file and needs to be read from the config file.
17+
func TestUnmarshalDeployableArtifacts(t *testing.T) {
18+
err, cleanUpJfrogHome := tests.ConfigTestServer(t)
19+
assert.NoError(t, err)
20+
defer cleanUpJfrogHome()
21+
// DeployableArtifact file is changed at runtime so a copy needs to be created.
22+
tempDeployableArtifacts, err := createTempDeployableArtifactFile()
23+
// Delete DeployableArtifacts tempDir
24+
defer os.Remove(filepath.Dir(tempDeployableArtifacts))
25+
gradleConfigFile := path.Join(getTestsDataGradlePath(), "config", "gradle.yaml")
26+
result, err := UnmarshalDeployableArtifacts(tempDeployableArtifacts, gradleConfigFile)
27+
assert.NoError(t, err)
28+
for transferDetails := new(clientutils.FileTransferDetails); result.reader.NextRecord(transferDetails) == nil; transferDetails = new(clientutils.FileTransferDetails) {
29+
assert.True(t, strings.HasPrefix(transferDetails.TargetPath, "http://localhost:8080/artifactory/"))
30+
assert.True(t, strings.Contains(transferDetails.TargetPath, "gradle-local-repo"))
31+
}
32+
}
33+
34+
// createTempDeployableArtifactFile copies a deployableArtifacts file from gradle testdata directory to a tempDir
35+
func createTempDeployableArtifactFile() (filePath string, err error) {
36+
filePath = ""
37+
testsDataGradlePath := getTestsDataGradlePath()
38+
summary, err := os.Open(path.Join(testsDataGradlePath, "deployableArtifacts", "artifacts"))
39+
if err != nil {
40+
err = errorutils.CheckError(err)
41+
return
42+
}
43+
defer func() {
44+
e := summary.Close()
45+
if err == nil {
46+
err = e
47+
}
48+
}()
49+
tmpDir, err := fileutils.CreateTempDir()
50+
if err != nil {
51+
return
52+
}
53+
fileutils.CopyFile(tmpDir, summary.Name())
54+
filePath = filepath.Join(tmpDir, "artifacts")
55+
return
56+
}
57+
58+
func getTestsDataGradlePath() string {
59+
return path.Join("..", "testdata", "gradle")
60+
}

common/commands/config_test.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"encoding/json"
5+
"github.com/jfrog/jfrog-cli-core/common/tests"
56
"testing"
67

78
"github.com/jfrog/jfrog-cli-core/utils/config"
@@ -15,7 +16,7 @@ func init() {
1516
}
1617

1718
func TestBasicAuth(t *testing.T) {
18-
inputDetails := createTestServerDetails()
19+
inputDetails := tests.CreateTestServerDetails()
1920
inputDetails.User = "admin"
2021
inputDetails.Password = "password"
2122

@@ -24,7 +25,7 @@ func TestBasicAuth(t *testing.T) {
2425
}
2526

2627
func TestUsernameSavedLowercase(t *testing.T) {
27-
inputDetails := createTestServerDetails()
28+
inputDetails := tests.CreateTestServerDetails()
2829
inputDetails.User = "ADMIN"
2930
inputDetails.Password = "password"
3031

@@ -36,7 +37,7 @@ func TestUsernameSavedLowercase(t *testing.T) {
3637
func TestApiKey(t *testing.T) {
3738
// API key is no longer allowed to be configured without providing a username.
3839
// This test is here to make sure that old configurations (with API key and no username) are still accepted.
39-
inputDetails := createTestServerDetails()
40+
inputDetails := tests.CreateTestServerDetails()
4041
inputDetails.ApiKey = "apiKey"
4142
configAndTest(t, inputDetails, false)
4243

@@ -46,7 +47,7 @@ func TestApiKey(t *testing.T) {
4647
}
4748

4849
func TestArtifactorySshKey(t *testing.T) {
49-
inputDetails := createTestServerDetails()
50+
inputDetails := tests.CreateTestServerDetails()
5051
inputDetails.SshKeyPath = "/tmp/sshKey"
5152
inputDetails.SshPassphrase = "123456"
5253
inputDetails.ArtifactoryUrl = "ssh://localhost:1339/"
@@ -56,7 +57,7 @@ func TestArtifactorySshKey(t *testing.T) {
5657
}
5758

5859
func TestAccessToken(t *testing.T) {
59-
inputDetails := createTestServerDetails()
60+
inputDetails := tests.CreateTestServerDetails()
6061
inputDetails.AccessToken = "accessToken"
6162

6263
configAndTest(t, inputDetails, false)
@@ -65,7 +66,7 @@ func TestAccessToken(t *testing.T) {
6566

6667
func TestRefreshToken(t *testing.T) {
6768
// Import after tokens were generated.
68-
inputDetails := createTestServerDetails()
69+
inputDetails := tests.CreateTestServerDetails()
6970
inputDetails.User = "admin"
7071
inputDetails.Password = "password"
7172
inputDetails.AccessToken = "accessToken"
@@ -82,7 +83,7 @@ func TestRefreshToken(t *testing.T) {
8283
}
8384

8485
func TestEmptyCredentials(t *testing.T) {
85-
configAndTest(t, createTestServerDetails(), false)
86+
configAndTest(t, tests.CreateTestServerDetails(), false)
8687
}
8788

8889
func TestUrls(t *testing.T) {
@@ -123,7 +124,7 @@ func testUrls(t *testing.T, interactive bool) {
123124
}
124125

125126
func TestBasicAuthOnlyOption(t *testing.T) {
126-
inputDetails := createTestServerDetails()
127+
inputDetails := tests.CreateTestServerDetails()
127128
inputDetails.User = "admin"
128129
inputDetails.Password = "password"
129130

@@ -168,17 +169,3 @@ func configStructToString(artConfig *config.ServerDetails) string {
168169
marshaledStruct, _ := json.Marshal(*artConfig)
169170
return string(marshaledStruct)
170171
}
171-
172-
func createTestServerDetails() *config.ServerDetails {
173-
return &config.ServerDetails{
174-
Url: "http://localhost:8080",
175-
ArtifactoryUrl: "http://localhost:8080/artifactory",
176-
DistributionUrl: "http://localhost:8080/distribution",
177-
XrayUrl: "http://localhost:8080/xray",
178-
MissionControlUrl: "http://localhost:8080/mc",
179-
PipelinesUrl: "http://localhost:8080/pipelines",
180-
ServerId: "test",
181-
IsDefault: false,
182-
ClientCertPath: "ClientCertPath", ClientCertKeyPath: "ClientCertKeyPath",
183-
}
184-
}

common/tests/utils.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tests
2+
3+
import (
4+
"github.com/jfrog/jfrog-cli-core/utils/config"
5+
testsutils "github.com/jfrog/jfrog-cli-core/utils/config/tests"
6+
"testing"
7+
)
8+
9+
func ConfigTestServer(t *testing.T) (err error, cleanUp func()) {
10+
cleanUp = testsutils.CreateTempEnv(t, false)
11+
serverDetails := CreateTestServerDetails()
12+
err = config.SaveServersConf([]*config.ServerDetails{serverDetails})
13+
return
14+
}
15+
16+
func CreateTestServerDetails() *config.ServerDetails {
17+
return &config.ServerDetails{
18+
Url: "http://localhost:8080/",
19+
ArtifactoryUrl: "http://localhost:8080/artifactory/",
20+
DistributionUrl: "http://localhost:8080/distribution/",
21+
XrayUrl: "http://localhost:8080/xray/",
22+
MissionControlUrl: "http://localhost:8080/mc/",
23+
PipelinesUrl: "http://localhost:8080/pipelines/",
24+
ServerId: "test",
25+
IsDefault: false,
26+
ClientCertPath: "ClientCertPath", ClientCertKeyPath: "ClientCertKeyPath",
27+
}
28+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ require (
2121
gopkg.in/yaml.v2 v2.3.0
2222
)
2323

24-
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.0.1-0.20210808220504-d4c272a91562
24+
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.0.1-0.20210809143017-dd454849a8ba
2525

2626
// replace github.com/jfrog/gocmd => github.com/jfrog/gocmd v0.3.1-0.20210623152326-422f211f4e7f

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ github.com/jfrog/gocmd v0.3.1 h1:EyOot6llZaHlV9S6We6P0OrYA2pKO8Xifm3yXuUxriM=
151151
github.com/jfrog/gocmd v0.3.1/go.mod h1:spTHT8F2u0DtGdVq3v6frnSiqaKwPdNuMQd856MCHhw=
152152
github.com/jfrog/gofrog v1.0.6 h1:yUDxSCw8gTK6vC4PvtG0HTnEOQJSZ+O4lWGCgkev1nU=
153153
github.com/jfrog/gofrog v1.0.6/go.mod h1:HkDzg+tMNw23UryoOv0+LB94BzYcl6MCIoz8Tmlb+s8=
154-
github.com/jfrog/jfrog-client-go v1.0.1-0.20210808220504-d4c272a91562 h1:5ysqFe2L+MWuRsnGg+9cumjG+s+4IrA+s98mq/2+fOg=
155-
github.com/jfrog/jfrog-client-go v1.0.1-0.20210808220504-d4c272a91562/go.mod h1:dR0Z+zjSAoMWT5phrPIVjj+kjYYHHAEq1JnvA7klwvI=
154+
github.com/jfrog/jfrog-client-go v1.0.1-0.20210809143017-dd454849a8ba h1:k13h1j3mkbnIQiVjqWfrPYw7YAm51M0oPRtmo90y6Oo=
155+
github.com/jfrog/jfrog-client-go v1.0.1-0.20210809143017-dd454849a8ba/go.mod h1:dR0Z+zjSAoMWT5phrPIVjj+kjYYHHAEq1JnvA7klwvI=
156156
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
157157
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
158158
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=

0 commit comments

Comments
 (0)