Skip to content

Commit

Permalink
⚡ Set up QODANA_PROJECT_ID_HASH if token is provided (QD-8125 )
Browse files Browse the repository at this point in the history
  • Loading branch information
avafanasiev authored and tiulpin committed Jan 22, 2024
1 parent b8f91dd commit c6398e9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions cloud/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type LicenseData struct {
LicenseID string `json:"licenseId"`
LicenseKey string `json:"licenseKey"`
ExpirationDate string `json:"expirationDate"`
ProjectIdHash string `json:"projectIdHash"`
LicensePlan string `json:"licensePlan"`
}

Expand Down
14 changes: 12 additions & 2 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,9 @@ func Test_ideaExitCode(t *testing.T) {
func TestSetupLicense(t *testing.T) {
Prod.Code = "QDJVM"
Prod.EAP = false
license := `{"licenseId":"VA5HGQWQH6","licenseKey":"VA5HGQWQH6","expirationDate":"2023-07-31","licensePlan":"EAP_ULTIMATE_PLUS"}`
license := `{"licenseId":"VA5HGQWQH6","licenseKey":"VA5HGQWQH6","expirationDate":"2023-07-31","licensePlan":"EAP_ULTIMATE_PLUS","projectIdHash":"hash"}`
expectedKey := "VA5HGQWQH6"
expectedHash := "hash"

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, license)
Expand All @@ -1085,12 +1086,16 @@ func TestSetupLicense(t *testing.T) {
if err != nil {
t.Fatal(err)
}
SetupLicense("token")
SetupLicenseAndProjectHash("token")

licenseKey := os.Getenv(QodanaLicense)
if licenseKey != expectedKey {
t.Errorf("expected key to be '%s' got '%s'", expectedKey, licenseKey)
}
projectIdHash := os.Getenv(QodanaProjectIdHash)
if projectIdHash != expectedHash {
t.Errorf("expected projectIdHash to be '%s' got '%s'", expectedHash, projectIdHash)
}

err = os.Unsetenv(QodanaLicenseEndpoint)
if err != nil {
Expand All @@ -1101,6 +1106,11 @@ func TestSetupLicense(t *testing.T) {
if err != nil {
t.Fatal(err)
}

err = os.Unsetenv(QodanaProjectIdHash)
if err != nil {
t.Fatal(err)
}
}

func TestSetupLicenseToken(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions core/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
androidSdkRoot = "ANDROID_SDK_ROOT"
QodanaLicenseEndpoint = "LICENSE_ENDPOINT"
QodanaLicense = "QODANA_LICENSE"
QodanaProjectIdHash = "QODANA_PROJECT_ID_HASH"
QodanaTreatAsRelease = "QODANA_TREAT_AS_RELEASE"
qodanaClearKeyring = "QODANA_CLEAR_KEYRING"
qodanaNugetUrl = "QODANA_NUGET_URL"
Expand Down
2 changes: 1 addition & 1 deletion core/ide.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func prepareLocalIdeSettings(opts *QodanaOptions) {

ExtractQodanaEnvironment(setEnv)
SetupLicenseToken(opts)
SetupLicense(cloud.Token.Token)
SetupLicenseAndProjectHash(cloud.Token.Token)
prepareDirectories(
opts.CacheDir,
opts.logDirPath(),
Expand Down
41 changes: 27 additions & 14 deletions core/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,30 @@ import (
"strings"
)

func SetupLicense(token string) {
func requestLicenseData(token string) cloud.LicenseData {
licenseEndpoint := cloud.GetEnvWithDefault(QodanaLicenseEndpoint, "https://linters.qodana.cloud")

licenseDataResponse, err := cloud.RequestLicenseData(licenseEndpoint, token)
if errors.Is(err, cloud.TokenDeclinedError) {
log.Fatalf("License request: %v\n%s", err, cloud.DeclinedTokenErrorMessage)
}
if err != nil {
log.Fatalf("License request: %v\n%s", err, cloud.GeneralLicenseErrorMessage)
}
return cloud.DeserializeLicenseData(licenseDataResponse)
}

func SetupLicenseAndProjectHash(token string) {
var licenseData cloud.LicenseData
if token != "" {
licenseData = requestLicenseData(token)
if licenseData.ProjectIdHash != "" {
err := os.Setenv(QodanaProjectIdHash, licenseData.ProjectIdHash)
if err != nil {
log.Fatal(err)
}
}
}
_, exists := os.LookupEnv(QodanaLicense)
if exists {
return
Expand All @@ -45,21 +68,11 @@ func SetupLicense(token string) {
return
}

// usual builds should have token for execution
// usual builds should have token and LicenseData for execution
if token == "" {
log.Fatal(cloud.EmptyTokenMessage)
}

licenseEndpoint := cloud.GetEnvWithDefault(QodanaLicenseEndpoint, "https://linters.qodana.cloud")

licenseDataResponse, err := cloud.RequestLicenseData(licenseEndpoint, token)
if errors.Is(err, cloud.TokenDeclinedError) {
log.Fatalf("License request: %v\n%s", err, cloud.DeclinedTokenErrorMessage)
}
if err != nil {
log.Fatalf("License request: %v\n%s", err, cloud.GeneralLicenseErrorMessage)
}
licenseData := cloud.DeserializeLicenseData(licenseDataResponse)
if strings.ToLower(licenseData.LicensePlan) == "community" {
log.Fatalf("Your Qodana Cloud organization has Community license that doesn’t support \"%s\" linter, "+
"please try one of the community linters instead: %s or obtain Ultimate "+
Expand All @@ -70,9 +83,9 @@ func SetupLicense(token string) {
)
}
if licenseData.LicenseKey == "" {
log.Fatalf("Response for license request should contain license key\n%s", string(licenseDataResponse))
log.Fatalf("License key should not be empty\n")
}
err = os.Setenv(QodanaLicense, licenseData.LicenseKey)
err := os.Setenv(QodanaLicense, licenseData.LicenseKey)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit c6398e9

Please sign in to comment.