Skip to content

Commit 744f3f7

Browse files
authored
Support multi user profile build-info collection (#1512)
1 parent 78d708a commit 744f3f7

File tree

4 files changed

+113
-9
lines changed

4 files changed

+113
-9
lines changed

common/build/buildutils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ import (
2525

2626
const (
2727
BuildInfoDetails = "details"
28-
BuildTempPath = "jfrog/builds/"
2928
ProjectConfigBuildNameKey = "name"
3029
)
3130

3231
func CreateBuildInfoService() *build.BuildInfoService {
3332
buildInfoService := build.NewBuildInfoService()
34-
buildInfoService.SetTempDirPath(filepath.Join(coreutils.GetCliPersistentTempDirPath(), BuildTempPath))
33+
buildInfoService.SetTempDirPath(filepath.Join(coreutils.GetCliPersistentTempDirPath(), buildInfoService.GetUserSpecificBuildDirName()))
3534
buildInfoService.SetLogger(log.Logger)
3635
return buildInfoService
3736
}
@@ -66,7 +65,8 @@ func PrepareBuildPrerequisites(buildConfiguration *BuildConfiguration) (build *b
6665

6766
func GetBuildDir(buildName, buildNumber, projectKey string) (string, error) {
6867
hash := sha256.Sum256([]byte(buildName + "_" + buildNumber + "_" + projectKey))
69-
buildsDir := filepath.Join(coreutils.GetCliPersistentTempDirPath(), BuildTempPath, hex.EncodeToString(hash[:]))
68+
buildsDir := filepath.Join(coreutils.GetCliPersistentTempDirPath(),
69+
build.NewBuildInfoService().GetUserSpecificBuildDirName(), hex.EncodeToString(hash[:]))
7070
err := os.MkdirAll(buildsDir, 0777)
7171
if errorutils.CheckError(err) != nil {
7272
return "", err

common/build/buildutils_test.go

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package build
22

33
import (
4-
biutils "github.com/jfrog/build-info-go/utils"
54
"os"
5+
"os/user"
66
"path/filepath"
77
"strconv"
8+
"strings"
89
"testing"
910
"time"
1011

12+
biutils "github.com/jfrog/build-info-go/utils"
13+
14+
"github.com/jfrog/build-info-go/build"
1115
"github.com/jfrog/jfrog-cli-core/v2/utils/tests"
1216
testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
1317

1418
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
1519
artclientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
1620
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
1722
)
1823

1924
var timestamp = strconv.FormatInt(time.Now().Unix(), 10)
@@ -245,3 +250,102 @@ func TestIsLoadedFromConfigFile(t *testing.T) {
245250
assert.Equal(t, buildName, buildNameFile)
246251
assert.Equal(t, buildNumber, artclientutils.LatestBuildNumberKey)
247252
}
253+
254+
func TestCreateBuildInfoService(t *testing.T) {
255+
service := CreateBuildInfoService()
256+
257+
// Service should not be nil
258+
require.NotNil(t, service, "CreateBuildInfoService should not return nil")
259+
260+
// Get the user-specific build directory name
261+
userSpecificDir := service.GetUserSpecificBuildDirName()
262+
assert.NotEmpty(t, userSpecificDir, "User-specific build directory should not be empty")
263+
264+
// Should contain "jfrog-" prefix (user-specific path)
265+
assert.True(t, strings.Contains(userSpecificDir, build.BuildsJfrogPath),
266+
"Build directory should contain '%s', got: %s", build.BuildsJfrogPath, userSpecificDir)
267+
268+
// Should contain "builds" directory
269+
assert.True(t, strings.Contains(userSpecificDir, build.BuildsDirPath),
270+
"Build directory should contain '%s', got: %s", build.BuildsDirPath, userSpecificDir)
271+
}
272+
273+
func TestCreateBuildInfoServiceContainsUsername(t *testing.T) {
274+
currentUser, err := user.Current()
275+
require.NoError(t, err, "Failed to get current user")
276+
277+
service := CreateBuildInfoService()
278+
userSpecificDir := service.GetUserSpecificBuildDirName()
279+
280+
// The path should contain the current username
281+
expectedPrefix := build.BuildsJfrogPath + currentUser.Username
282+
assert.True(t, strings.Contains(userSpecificDir, expectedPrefix),
283+
"Build directory should contain '%s', got: %s", expectedPrefix, userSpecificDir)
284+
}
285+
286+
func TestGetBuildDir(t *testing.T) {
287+
const testBuildName = "test-build-dir"
288+
const testBuildNumber = "1"
289+
const testProjectKey = ""
290+
291+
// Get the build directory
292+
buildDir, err := GetBuildDir(testBuildName, testBuildNumber, testProjectKey)
293+
require.NoError(t, err, "GetBuildDir should not return error")
294+
295+
// Cleanup after test
296+
defer func() {
297+
// Remove the created directory
298+
_ = os.RemoveAll(buildDir)
299+
}()
300+
301+
// Directory should be created
302+
assert.DirExists(t, buildDir, "Build directory should be created")
303+
304+
// Path should contain user-specific directory
305+
assert.True(t, strings.Contains(buildDir, build.BuildsJfrogPath),
306+
"Build directory path should contain '%s', got: %s", build.BuildsJfrogPath, buildDir)
307+
308+
// Path should start with CLI persistent temp dir
309+
assert.True(t, strings.HasPrefix(buildDir, coreutils.GetCliPersistentTempDirPath()),
310+
"Build directory should be under CLI temp dir. Got: %s, Expected prefix: %s",
311+
buildDir, coreutils.GetCliPersistentTempDirPath())
312+
}
313+
314+
func TestGetBuildDirUserIsolation(t *testing.T) {
315+
currentUser, err := user.Current()
316+
require.NoError(t, err, "Failed to get current user")
317+
318+
buildDir, err := GetBuildDir("isolation-test", "1", "")
319+
require.NoError(t, err)
320+
321+
// Cleanup after test
322+
defer func() {
323+
_ = os.RemoveAll(buildDir)
324+
}()
325+
326+
// Path should contain user-specific directory with username
327+
expectedUserDir := build.BuildsJfrogPath + currentUser.Username
328+
assert.True(t, strings.Contains(buildDir, expectedUserDir),
329+
"Build directory should contain user-specific path '%s', got: %s", expectedUserDir, buildDir)
330+
}
331+
332+
func TestGetBuildDirDifferentBuildsGetDifferentPaths(t *testing.T) {
333+
// Create two different builds
334+
buildDir1, err := GetBuildDir("build-a", "1", "")
335+
require.NoError(t, err)
336+
defer func() { _ = os.RemoveAll(buildDir1) }()
337+
338+
buildDir2, err := GetBuildDir("build-b", "1", "")
339+
require.NoError(t, err)
340+
defer func() { _ = os.RemoveAll(buildDir2) }()
341+
342+
// Different builds should get different directories (different hash)
343+
assert.NotEqual(t, buildDir1, buildDir2,
344+
"Different builds should have different directories")
345+
346+
// But both should be under the same user-specific parent
347+
parent1 := filepath.Dir(buildDir1)
348+
parent2 := filepath.Dir(buildDir2)
349+
assert.Equal(t, parent1, parent2,
350+
"Different builds should share the same user-specific parent directory")
351+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/jfrog/jfrog-cli-core/v2
22

3-
go 1.25.4
3+
go 1.25.5
44

55
require github.com/c-bata/go-prompt v0.2.6 // Should not be updated to 0.2.6 due to a bug (https://github.com/jfrog/jfrog-cli-core/pull/372)
66

@@ -13,7 +13,7 @@ require (
1313
github.com/google/uuid v1.6.0
1414
github.com/gookit/color v1.6.0
1515
github.com/jedib0t/go-pretty/v6 v6.6.8
16-
github.com/jfrog/build-info-go v1.8.9-0.20251223092904-9e9460642431
16+
github.com/jfrog/build-info-go v1.13.1-0.20260106203543-03b99793ca5a
1717
github.com/jfrog/gofrog v1.7.6
1818
github.com/jfrog/jfrog-client-go v1.55.1-0.20251223101502-1a13a993b0c7
1919
github.com/magiconair/properties v1.8.10
@@ -94,6 +94,6 @@ require (
9494

9595
// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go master
9696

97-
// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go dev
97+
//replace github.com/jfrog/build-info-go => github.com/fluxxBot/build-info-go v1.10.10-0.20260105064157-73c3f6f22ba2
9898

9999
// replace github.com/jfrog/gofrog => github.com/jfrog/gofrog v1.3.3-0.20231223133729-ef57bd08cedc

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ github.com/jedib0t/go-pretty/v6 v6.6.8 h1:JnnzQeRz2bACBobIaa/r+nqjvws4yEhcmaZ4n1
9494
github.com/jedib0t/go-pretty/v6 v6.6.8/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
9595
github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI=
9696
github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw=
97-
github.com/jfrog/build-info-go v1.8.9-0.20251223092904-9e9460642431 h1:HyGqdD957CrW6T1Xst0CxTR0XqJ0baIo/uL4I5lhyuo=
98-
github.com/jfrog/build-info-go v1.8.9-0.20251223092904-9e9460642431/go.mod h1:9W4U440fdTHwW1HiB/R0VQvz/5q8ZHsms9MWcq+JrdY=
97+
github.com/jfrog/build-info-go v1.13.1-0.20260106203543-03b99793ca5a h1:vFjLpuSwRCQCPGjGoZcC3zn+cAGYmsuKV4mSXHq94LU=
98+
github.com/jfrog/build-info-go v1.13.1-0.20260106203543-03b99793ca5a/go.mod h1:+OCtMb22/D+u7Wne5lzkjJjaWr0LRZcHlDwTH86Mpwo=
9999
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
100100
github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4=
101101
github.com/jfrog/jfrog-client-go v1.55.1-0.20251223101502-1a13a993b0c7 h1:5JUiqmBV9ikFOZEH+ZgvJLHshT1aAuw08bfdJOLHbzQ=

0 commit comments

Comments
 (0)