Skip to content

Commit 05ca182

Browse files
committed
Update Usage
1 parent 7217a02 commit 05ca182

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

general/token/oidctokenexchange.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"fmt"
55
rtUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
66
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
7+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
78
"github.com/jfrog/jfrog-client-go/access/services"
89
"github.com/jfrog/jfrog-client-go/auth"
910
"github.com/jfrog/jfrog-client-go/utils/log"
11+
"os"
1012
"strings"
1113
)
1214

@@ -30,20 +32,27 @@ func (p OidcProviderType) String() string {
3032
}
3133

3234
func OidcProviderTypeFromString(providerType string) (OidcProviderType, error) {
35+
var oidcProviderType OidcProviderType
3336
if providerType == "" {
3437
// If no provider type is provided, return 0 (GitHub) as default
35-
return 0, nil
38+
oidcProviderType = 0
39+
} else {
40+
switch strings.ToLower(providerType) {
41+
case strings.ToLower(GitHub.String()):
42+
oidcProviderType = GitHub
43+
case strings.ToLower(Azure.String()):
44+
oidcProviderType = Azure
45+
case strings.ToLower(GenericOidc.String()):
46+
oidcProviderType = GenericOidc
47+
default:
48+
return 0, fmt.Errorf("unsupported oidc provider type: %s", providerType)
49+
}
3650
}
37-
switch strings.ToLower(providerType) {
38-
case strings.ToLower(GitHub.String()):
39-
return GitHub, nil
40-
case strings.ToLower(Azure.String()):
41-
return Azure, nil
42-
case strings.ToLower(GenericOidc.String()):
43-
return GenericOidc, nil
44-
default:
45-
return 0, fmt.Errorf("unsupported oidc provider type: %s", providerType)
51+
// This is used for usage reporting
52+
if err := os.Setenv(coreutils.OidcProviderType, oidcProviderType.String()); err != nil {
53+
log.Warn("Failed to set JFROG_CLI_OIDC_PROVIDER_TYPE environment variable")
4654
}
55+
return oidcProviderType, nil
4756
}
4857

4958
type OidcTokenExchangeCommand struct {

utils/usage/visibility/commands_count_metric.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type commandsCountLabels struct {
1010
ProductID string `json:"product_id"`
1111
ProductVersion string `json:"product_version"`
1212
FeatureID string `json:"feature_id"`
13+
ProviderType string `json:"provider_type"`
1314
JobID string `json:"job_id"`
1415
RunID string `json:"run_id"`
1516
GitRepo string `json:"git_repo"`
@@ -24,9 +25,10 @@ func NewCommandsCountMetric(commandName string) services.VisibilityMetric {
2425
ProductID: coreutils.GetCliUserAgentName(),
2526
ProductVersion: coreutils.GetCliUserAgentVersion(),
2627
FeatureID: commandName,
27-
JobID: os.Getenv("JFROG_CLI_CI_JOB_ID"),
28-
RunID: os.Getenv("JFROG_CLI_CI_RUN_ID"),
29-
GitRepo: os.Getenv("JFROG_CLI_USAGE_GIT_REPO"),
28+
ProviderType: os.Getenv(coreutils.OidcProviderType),
29+
JobID: os.Getenv(coreutils.CIJobID),
30+
RunID: os.Getenv(coreutils.CIRunID),
31+
GitRepo: os.Getenv(coreutils.SourceCodeRepository),
3032
GhTokenForCodeScanningAlertsProvided: os.Getenv("JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED"),
3133
},
3234
}

utils/usage/visibility/commands_count_metric_test.go

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

33
import (
44
"encoding/json"
5+
"github.com/jfrog/jfrog-cli-core/v2/general/token"
56
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
67
testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
78
"github.com/stretchr/testify/assert"
@@ -11,9 +12,10 @@ import (
1112
func TestCreateCommandsCountMetric(t *testing.T) {
1213
// Set environment variables for the test using SetEnvWithCallbackAndAssert
1314
envVars := map[string]string{
14-
"JFROG_CLI_CI_JOB_ID": "job123",
15-
"JFROG_CLI_CI_RUN_ID": "run456",
16-
"JFROG_CLI_USAGE_GIT_REPO": "test-repo",
15+
coreutils.CIJobID: "job123",
16+
coreutils.CIRunID: "run456",
17+
coreutils.SourceCodeRepository: "test-repo",
18+
coreutils.OidcProviderType: token.GitHub.String(),
1719
"JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED": "TRUE",
1820
}
1921
cleanupFuncs := []func(){}
@@ -40,6 +42,7 @@ func TestCreateCommandsCountMetric(t *testing.T) {
4042
"product_id": "` + coreutils.GetCliUserAgentName() + `",
4143
"product_version": "` + coreutils.GetCliUserAgentVersion() + `",
4244
"feature_id": "testCommand",
45+
"provider_type": "GitHub",
4346
"job_id": "job123",
4447
"run_id": "run456",
4548
"git_repo": "test-repo",

0 commit comments

Comments
 (0)