Skip to content

Commit 69d7964

Browse files
committed
QD-13135 Detect and set VCS information on CLI level
1 parent 37a740c commit 69d7964

File tree

5 files changed

+113
-11
lines changed

5 files changed

+113
-11
lines changed

internal/core/container.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"strings"
3131

3232
"github.com/JetBrains/qodana-cli/internal/cloud"
33+
"github.com/JetBrains/qodana-cli/internal/platform/git"
3334
"github.com/docker/docker/api/types/image"
3435
"github.com/docker/docker/api/types/network"
3536

@@ -285,7 +286,7 @@ func getDockerOptions(c corescan.Context, image string) *backend.ContainerCreate
285286
cmdOpts := GetIdeArgs(c)
286287

287288
updateScanContextEnv := func(key string, value string) { c = c.WithEnvExtractedFromOsEnv(key, value) }
288-
qdenv.ExtractQodanaEnvironment(updateScanContextEnv)
289+
qdenv.ExtractQodanaEnvironment(git.GetGitEnv(c.RepositoryRoot(), c.LogDir()), updateScanContextEnv)
289290

290291
dockerEnv := c.Env()
291292
qodanaCloudUploadToken := c.QodanaUploadToken()

internal/core/corescan/env_test.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
7070
}
7171

7272
for _, tc := range []struct {
73-
ci string
74-
variables map[string]string
75-
jobUrlExpected string
76-
envExpected string
77-
remoteUrlExpected string
78-
revisionExpected string
79-
branchExpected string
73+
ci string
74+
variables map[string]string
75+
jobUrlExpected string
76+
envExpected string
77+
remoteUrlExpected string
78+
repositoryUrlExpected string
79+
revisionExpected string
80+
branchExpected string
81+
gitEnv qdenv.GitEnv
8082
}{
8183
{
8284
ci: "no CI detected",
@@ -213,6 +215,23 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
213215
envExpected: fmt.Sprintf("bitbucket:%s", version.Version),
214216
jobUrlExpected: "https://bitbucket.org/sa/entrypoint/pipelines/results/123456789",
215217
},
218+
{
219+
ci: "local-git",
220+
gitEnv: qdenv.GitEnv{
221+
RemoteUrl: "https://qodana.jetbrains.com/never-gonna-give-you-up",
222+
RepositoryUrl: "https://qodana.jetbrains.com/never-gonna-give-you-up",
223+
Revision: revisionExpected,
224+
Branch: branchExpected,
225+
},
226+
variables: map[string]string{
227+
qdenv.QodanaEnv: "local-git",
228+
},
229+
envExpected: "local-git",
230+
remoteUrlExpected: "https://qodana.jetbrains.com/never-gonna-give-you-up",
231+
repositoryUrlExpected: "https://qodana.jetbrains.com/never-gonna-give-you-up",
232+
revisionExpected: revisionExpected,
233+
branchExpected: branchExpected,
234+
},
216235
} {
217236
t.Run(
218237
tc.ci, func(t *testing.T) {
@@ -246,7 +265,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
246265
environment.name, func(t *testing.T) {
247266
qdenv.InitializeQodanaGlobalEnv(qdenv.EmptyEnvProvider())
248267

249-
qdenv.ExtractQodanaEnvironment(environment.set)
268+
qdenv.ExtractQodanaEnvironment(tc.gitEnv, environment.set)
250269
currentQodanaEnv := environment.get(qdenv.QodanaEnv)
251270
if currentQodanaEnv != tc.envExpected {
252271
t.Errorf("%s: Expected %s, got %s", environment.name, tc.envExpected, currentQodanaEnv)
@@ -267,6 +286,14 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
267286
environment.get(qdenv.QodanaRemoteUrl),
268287
)
269288
}
289+
if environment.get(qdenv.QodanaRepoUrl) != tc.repositoryUrlExpected {
290+
t.Errorf(
291+
"%s: Expected %s, got %s",
292+
environment.name,
293+
tc.repositoryUrlExpected,
294+
environment.get(qdenv.QodanaRepoUrl),
295+
)
296+
}
270297
if environment.get(qdenv.QodanaRevision) != tc.revisionExpected {
271298
t.Errorf(
272299
"%s: Expected %s, got %s",

internal/core/startup/prepare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func prepareLocalIdeSettingsAndGetQodanaCloudUploadToken(
116116
) (product.Product, string) {
117117
prod := product.GuessProduct(ideDir, commonCtx.Analyzer)
118118

119-
qdenv.ExtractQodanaEnvironment(qdenv.SetEnv)
119+
qdenv.ExtractQodanaEnvironment(git.GetGitEnv(commonCtx.RepositoryRoot, commonCtx.LogDir()), qdenv.SetEnv)
120120
isTokenRequired := tokenloader.IsCloudTokenRequired(commonCtx)
121121
token := tokenloader.LoadCloudUploadToken(commonCtx, false, isTokenRequired, true)
122122
cloud.SetupLicenseToken(token)

internal/platform/git/env.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2021-2024 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package git
18+
19+
import (
20+
"strings"
21+
22+
"github.com/JetBrains/qodana-cli/internal/platform/qdenv"
23+
)
24+
25+
func GetGitEnv(gitRoot string, logDir string) qdenv.GitEnv {
26+
var env = &qdenv.GitEnv{}
27+
remUrl, err := RemoteUrl(gitRoot, logDir)
28+
if err == nil {
29+
env.RemoteUrl = remUrl
30+
if strings.HasPrefix(remUrl, "https://") {
31+
env.RepositoryUrl = remUrl
32+
}
33+
}
34+
brnch, err := Branch(gitRoot, logDir)
35+
if err == nil {
36+
env.Branch = brnch
37+
}
38+
revisions := Revisions(gitRoot)
39+
if len(revisions) > 0 {
40+
env.Revision = revisions[len(revisions)-1]
41+
}
42+
return *env
43+
}

internal/platform/qdenv/qdenv.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
// QodanaLicenseOnlyToken !!! QODANA_LICENSE_ONLY_TOKEN is accessed only by env !!!
3333
QodanaLicenseOnlyToken = "QODANA_LICENSE_ONLY_TOKEN"
3434
QodanaRemoteUrl = "QODANA_REMOTE_URL"
35+
QodanaRepoUrl = "QODANA_REPO_URL"
3536
QodanaDockerEnv = "QODANA_DOCKER"
3637
QodanaToolEnv = "QODANA_TOOL"
3738
QodanaConfEnv = "QODANA_CONF"
@@ -64,6 +65,14 @@ const (
6465
QodanaToken = "QODANA_TOKEN"
6566
)
6667

68+
// GitEnv contains information about the current git repository.
69+
type GitEnv struct {
70+
RemoteUrl string
71+
RepositoryUrl string
72+
Branch string
73+
Revision string
74+
}
75+
6776
type qodanaGlobalEnv struct {
6877
env map[string]string
6978
}
@@ -138,7 +147,7 @@ func IsContainer() bool {
138147
}
139148

140149
// ExtractQodanaEnvironment extracts Qodana environment variables from the current environment.
141-
func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
150+
func ExtractQodanaEnvironment(gitEnv GitEnv, setEnvironmentFunc func(string, string)) {
142151
if license := os.Getenv(QodanaLicense); license != "" {
143152
setEnvironmentFunc(QodanaLicense, license)
144153
}
@@ -178,9 +187,31 @@ func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
178187
qEnv = "bitbucket"
179188
setEnvironmentFunc(QodanaJobUrl, GetBitBucketJobUrl())
180189
}
190+
setDefaultQodanaGitEnvironment(gitEnv, setEnvironmentFunc)
181191
setEnvironmentFunc(QodanaEnv, fmt.Sprintf("%s:%s", qEnv, version.Version))
182192
}
183193

194+
func setDefaultQodanaGitEnvironment(gitEnv GitEnv, setEnvironmentFunc func(string, string)) {
195+
if remoteUrl := os.Getenv(QodanaRemoteUrl); remoteUrl == "" {
196+
if gitEnv.RemoteUrl != "" {
197+
setEnvironmentFunc(QodanaRemoteUrl, gitEnv.RemoteUrl)
198+
}
199+
if gitEnv.RepositoryUrl != "" {
200+
setEnvironmentFunc(QodanaRepoUrl, gitEnv.RepositoryUrl)
201+
}
202+
}
203+
if branch := os.Getenv(QodanaBranch); branch == "" {
204+
if gitEnv.Branch != "" {
205+
setEnvironmentFunc(QodanaBranch, gitEnv.Branch)
206+
}
207+
}
208+
if revision := os.Getenv(QodanaRevision); revision == "" {
209+
if gitEnv.Revision != "" {
210+
setEnvironmentFunc(QodanaRevision, gitEnv.Revision)
211+
}
212+
}
213+
}
214+
184215
func GetCIName(ci *cienvironment.CiEnvironment) string {
185216
return strings.ReplaceAll(strings.ToLower(ci.Name), " ", "-")
186217
}

0 commit comments

Comments
 (0)