Skip to content

Commit

Permalink
🐛 QD-10955 Add QODANA_TOKEN to docker env, QODANA_LICENSE_ONLY_TOKEN …
Browse files Browse the repository at this point in the history
…accessed only by os.Getenv
  • Loading branch information
MekhailS committed Feb 21, 2025
1 parent 48a4c87 commit fad8dca
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 123 deletions.
6 changes: 3 additions & 3 deletions cloud/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ func GetEnvWithDefaultInt(env string, defaultValue int) int {
return result
}

func SetupLicenseToken(token string) {
func SetupLicenseToken(cloudUploadToken string) {
licenseOnlyToken := os.Getenv(qdenv.QodanaLicenseOnlyToken)
if token == "" && licenseOnlyToken != "" {
if cloudUploadToken == "" && licenseOnlyToken != "" {
Token = LicenseToken{
Token: licenseOnlyToken,
LicenseOnly: true,
}
} else {
Token = LicenseToken{
Token: token,
Token: cloudUploadToken,
LicenseOnly: false,
}
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/JetBrains/qodana-cli/v2024/core"
"github.com/JetBrains/qodana-cli/v2024/platform/msg"
"github.com/JetBrains/qodana-cli/v2024/platform/product"
"github.com/JetBrains/qodana-cli/v2024/platform/qdenv"
"github.com/JetBrains/qodana-cli/v2024/platform/qdyaml"
"github.com/JetBrains/qodana-cli/v2024/platform/version"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -230,7 +229,7 @@ func TestAllCommandsWithContainer(t *testing.T) {
token := os.Getenv("QODANA_LICENSE_ONLY_TOKEN")
if //goland:noinspection GoBoolExpressions
token == "" {
t.Skip("set your token here to run the test")
t.Skip("set your QODANA_LICENSE_ONLY_TOKEN as env variable to run the test")
}

if os.Getenv("GITHUB_ACTIONS") == "true" {
Expand Down Expand Up @@ -273,7 +272,6 @@ func TestAllCommandsWithContainer(t *testing.T) {
"-o", resultsPath,
"--cache-dir", cachePath,
"-v", filepath.Join(projectPath, ".idea") + ":/data/some",
"-e", qdenv.QodanaLicenseOnlyToken + "=" + os.Getenv("QODANA_LICENSE_ONLY_TOKEN"),
"--fail-threshold", "5",
"--print-problems",
"--apply-fixes",
Expand Down
3 changes: 1 addition & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,12 @@ func newInitCommand() *cobra.Command {
"",
"",
os.Getenv(qdenv.QodanaToken),
os.Getenv(qdenv.QodanaLicenseOnlyToken),
false,
cliOptions.ProjectDir,
cliOptions.ConfigName,
)
if tokenloader.IsCloudTokenRequired(commonCtx, false) {
tokenloader.ValidateToken(commonCtx, cliOptions.Force)
tokenloader.ValidateCloudToken(commonCtx, cliOptions.Force)
}
},
}
Expand Down
1 change: 0 additions & 1 deletion cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func newPullCommand() *cobra.Command {
"",
"",
os.Getenv(qdenv.QodanaToken),
os.Getenv(qdenv.QodanaLicenseOnlyToken),
false,
cliOptions.ProjectDir,
cliOptions.ConfigName,
Expand Down
1 change: 0 additions & 1 deletion cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ But you can always override qodana.yaml options with the following command-line
cliOptions.ResultsDir,
cliOptions.ReportDir,
platform.GetEnvWithOsEnv(cliOptions, qdenv.QodanaToken),
platform.GetEnvWithOsEnv(cliOptions, qdenv.QodanaLicenseOnlyToken),
cliOptions.ClearCache,
cliOptions.ProjectDir,
cliOptions.ConfigName,
Expand Down
3 changes: 1 addition & 2 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ If you are using other Qodana Cloud instance than https://qodana.cloud/, overrid
cliOptions.ResultsDir,
cliOptions.ReportDir,
os.Getenv(qdenv.QodanaToken),
os.Getenv(qdenv.QodanaLicenseOnlyToken),
false,
cliOptions.ProjectDir,
cliOptions.ConfigName,
Expand All @@ -74,7 +73,7 @@ If you are using other Qodana Cloud instance than https://qodana.cloud/, overrid
}
platform.SendReport(
publisher,
tokenloader.ValidateToken(commonCtx, false),
tokenloader.ValidateCloudToken(commonCtx, false),
publisherPath,
java,
)
Expand Down
1 change: 0 additions & 1 deletion cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ This command serves the Qodana report locally and opens a browser to it.`,
cliOptions.ResultsDir,
cliOptions.ReportDir,
os.Getenv(qdenv.QodanaToken),
os.Getenv(qdenv.QodanaLicenseOnlyToken),
false,
cliOptions.ProjectDir,
cliOptions.ConfigName,
Expand Down
12 changes: 11 additions & 1 deletion core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ func getDockerOptions(c corescan.Context) *backend.ContainerCreateConfig {
updateScanContextEnv := func(key string, value string) { c = c.WithEnvExtractedFromOsEnv(key, value) }
qdenv.ExtractQodanaEnvironment(updateScanContextEnv)

dockerEnv := c.Env()
qodanaCloudUploadToken := c.QodanaUploadToken()
if qodanaCloudUploadToken != "" {
dockerEnv = append(dockerEnv, fmt.Sprintf("%s=%s", qdenv.QodanaToken, qodanaCloudUploadToken))
}
qodanaLicenseOnlyToken := os.Getenv(qdenv.QodanaLicenseOnlyToken)
if qodanaLicenseOnlyToken != "" && qodanaCloudUploadToken == "" {
dockerEnv = append(dockerEnv, fmt.Sprintf("%s=%s", qdenv.QodanaLicenseOnlyToken, qodanaLicenseOnlyToken))
}

cachePath, err := filepath.Abs(c.CacheDir())
if err != nil {
log.Fatal("couldn't get abs path for cache", err)
Expand Down Expand Up @@ -370,7 +380,7 @@ func getDockerOptions(c corescan.Context) *backend.ContainerCreateConfig {
Tty: msg.IsInteractive(),
AttachStdout: true,
AttachStderr: true,
Env: c.Env(),
Env: dockerEnv,
User: c.User(),
ExposedPorts: exposedPorts,
},
Expand Down
26 changes: 12 additions & 14 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,17 +942,16 @@ func TestQodanaOptions_RequiresToken(t *testing.T) {
t.Run(
tt.name, func(t *testing.T) {
initArgs := commoncontext.Context{
Linter: tt.linter,
Ide: tt.ide,
IsClearCache: false,
CacheDir: "",
ProjectDir: "",
ResultsDir: "",
ReportDir: "",
QodanaSystemDir: "",
Id: "",
QodanaToken: "",
QodanaLicenseOnlyToken: "",
Linter: tt.linter,
Ide: tt.ide,
IsClearCache: false,
CacheDir: "",
ProjectDir: "",
ResultsDir: "",
ReportDir: "",
QodanaSystemDir: "",
Id: "",
QodanaToken: "",
}

if tt.name == qdenv.QodanaToken {
Expand Down Expand Up @@ -1099,7 +1098,6 @@ func Test_Properties(t *testing.T) {
resultsDir,
"",
"",
"",
false,
projectDir,
"",
Expand All @@ -1119,8 +1117,8 @@ func Test_Properties(t *testing.T) {
},
commonCtx,
startup.PreparedHost{
IdeDir: "",
QodanaToken: "",
IdeDir: "",
QodanaUploadToken: "",
Prod: product.Product{
BaseScriptName: "rider",
Code: "QDNET",
Expand Down
12 changes: 4 additions & 8 deletions core/corescan/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ type Context struct {
ideDir string
qodanaYaml qdyaml.QodanaYaml
prod product.Product
qodanaToken string
qodanaLicenseOnlyToken string
qodanaUploadToken string
projectDir string
resultsDir string
configDir string
Expand Down Expand Up @@ -122,8 +121,7 @@ func (c Context) Id() string { return c.id }
func (c Context) IdeDir() string { return c.ideDir }
func (c Context) QodanaYaml() qdyaml.QodanaYaml { return c.qodanaYaml }
func (c Context) Prod() product.Product { return c.prod }
func (c Context) QodanaToken() string { return c.qodanaToken }
func (c Context) QodanaLicenseOnlyToken() string { return c.qodanaLicenseOnlyToken }
func (c Context) QodanaUploadToken() string { return c.qodanaUploadToken }
func (c Context) ProjectDir() string { return c.projectDir }
func (c Context) ResultsDir() string { return c.resultsDir }
func (c Context) ConfigDir() string { return c.configDir }
Expand Down Expand Up @@ -183,8 +181,7 @@ type ContextBuilder struct {
IdeDir string
QodanaYaml qdyaml.QodanaYaml
Prod product.Product
QodanaToken string
QodanaLicenseOnlyToken string
QodanaUploadToken string
ProjectDir string
ResultsDir string
ConfigDir string
Expand Down Expand Up @@ -246,8 +243,7 @@ func (b ContextBuilder) Build() Context {
ideDir: b.IdeDir,
qodanaYaml: b.QodanaYaml,
prod: b.Prod,
qodanaToken: b.QodanaToken,
qodanaLicenseOnlyToken: b.QodanaLicenseOnlyToken,
qodanaUploadToken: b.QodanaUploadToken,
projectDir: b.ProjectDir,
resultsDir: b.ResultsDir,
configDir: b.ConfigDir,
Expand Down
3 changes: 1 addition & 2 deletions core/corescan/create_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func CreateContext(
IdeDir: preparedHost.IdeDir,
QodanaYaml: qodanaYaml,
Prod: preparedHost.Prod,
QodanaToken: preparedHost.QodanaToken,
QodanaLicenseOnlyToken: commonCtx.QodanaLicenseOnlyToken,
QodanaUploadToken: preparedHost.QodanaUploadToken,
ProjectDir: commonCtx.ProjectDir,
ResultsDir: commonCtx.ResultsDir,
ConfigDir: commonCtx.ConfDirPath(),
Expand Down
23 changes: 11 additions & 12 deletions core/startup/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ const (
)

type PreparedHost struct {
IdeDir string
QodanaToken string
Prod product.Product
IdeDir string
QodanaUploadToken string
Prod product.Product
}

// PrepareHost gets the current user, creates the necessary folders for the analysis.
func PrepareHost(commonCtx commoncontext.Context) PreparedHost {
prod := product.Product{}
token := commonCtx.QodanaToken
cloudUploadToken := commonCtx.QodanaToken
ideDir := ""

if commonCtx.IsClearCache {
Expand Down Expand Up @@ -99,31 +99,30 @@ func PrepareHost(commonCtx commoncontext.Context) PreparedHost {
}
ideDir = val
}
prod, token = prepareLocalIdeSettingsAndGetQodanaCloudToken(commonCtx, ideDir)
prod, cloudUploadToken = prepareLocalIdeSettingsAndGetQodanaCloudUploadToken(commonCtx, ideDir)
}
commonCtx.QodanaToken = token

if tokenloader.IsCloudTokenRequired(commonCtx, prod.IsCommunity() || prod.IsEap) {
token = tokenloader.ValidateToken(commonCtx, false)
cloudUploadToken = tokenloader.ValidateCloudToken(commonCtx, false)
}

result := PreparedHost{
IdeDir: ideDir,
QodanaToken: token,
Prod: prod,
IdeDir: ideDir,
QodanaUploadToken: cloudUploadToken,
Prod: prod,
}
return result
}

func prepareLocalIdeSettingsAndGetQodanaCloudToken(
func prepareLocalIdeSettingsAndGetQodanaCloudUploadToken(
commonCtx commoncontext.Context,
ideDir string,
) (product.Product, string) {
prod := product.GuessProduct(ideDir)

qdenv.ExtractQodanaEnvironment(qdenv.SetEnv)
isTokenRequired := tokenloader.IsCloudTokenRequired(commonCtx, prod.IsEap || prod.IsCommunity())
token := tokenloader.LoadCloudToken(commonCtx, false, isTokenRequired, true)
token := tokenloader.LoadCloudUploadToken(commonCtx, false, isTokenRequired, true)
cloud.SetupLicenseToken(token)
SetupLicenseAndProjectHash(prod, cloud.GetCloudApiEndpoints(), cloud.Token.Token)
PrepareDirectories(
Expand Down
22 changes: 10 additions & 12 deletions platform/commoncontext/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func Compute(
resultsDirFromCliOptions string,
reportDirFromCliOptions string,
qodanaCloudToken string,
qodanaLicenseOnlyToken string,
clearCache bool,
projectDir string,
qodanaYamlPath string,
Expand All @@ -55,17 +54,16 @@ func Compute(
reportDir := computeReportDir(reportDirFromCliOptions, resultsDir)

commonCtx := Context{
Linter: linter,
Ide: ide,
IsClearCache: clearCache,
CacheDir: cacheDir,
ProjectDir: projectDir,
ResultsDir: resultsDir,
QodanaSystemDir: systemDir,
ReportDir: reportDir,
Id: qodanaId,
QodanaToken: qodanaCloudToken,
QodanaLicenseOnlyToken: qodanaLicenseOnlyToken,
Linter: linter,
Ide: ide,
IsClearCache: clearCache,
CacheDir: cacheDir,
ProjectDir: projectDir,
ResultsDir: resultsDir,
QodanaSystemDir: systemDir,
ReportDir: reportDir,
Id: qodanaId,
QodanaToken: qodanaCloudToken,
}
return commonCtx
}
Expand Down
34 changes: 16 additions & 18 deletions platform/commoncontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ import (
)

type Context struct {
Linter string
Ide string
IsClearCache bool
CacheDir string
ProjectDir string
ResultsDir string
ReportDir string
QodanaSystemDir string
Id string
QodanaToken string
QodanaLicenseOnlyToken string
Linter string
Ide string
IsClearCache bool
CacheDir string
ProjectDir string
ResultsDir string
ReportDir string
QodanaSystemDir string
Id string
QodanaToken string
}

func (c Context) LogDir() string {
Expand All @@ -59,10 +58,9 @@ func (c Context) GetLinterDir() string {
CloudTokenLoader
*/

func (c Context) GetQodanaToken() string { return c.QodanaToken }
func (c Context) GetQodanaLicenseOnlyToken() string { return c.QodanaLicenseOnlyToken }
func (c Context) GetId() string { return c.Id }
func (c Context) GetIde() string { return c.Ide }
func (c Context) GetLinter() string { return c.Linter }
func (c Context) GetProjectDir() string { return c.ProjectDir }
func (c Context) GetLogDir() string { return c.LogDir() }
func (c Context) GetQodanaToken() string { return c.QodanaToken }
func (c Context) GetId() string { return c.Id }
func (c Context) GetIde() string { return c.Ide }
func (c Context) GetLinter() string { return c.Linter }
func (c Context) GetProjectDir() string { return c.ProjectDir }
func (c Context) GetLogDir() string { return c.LogDir() }
Loading

0 comments on commit fad8dca

Please sign in to comment.