Skip to content

Commit

Permalink
✨ [QD-8082] Set QODANA_REPO_URL variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jckoenen authored and tiulpin committed Jan 22, 2024
1 parent 9db9f51 commit 6b19967
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
31 changes: 30 additions & 1 deletion core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
jobUrlExpected string
envExpected string
remoteUrlExpected string
repoUrlExpected string
revisionExpected string
branchExpected string
}{
Expand All @@ -145,12 +146,14 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
qodanaEnv: "user-defined",
qodanaJobUrl: "https://qodana.jetbrains.com/never-gonna-give-you-up",
qodanaRemoteUrl: "https://qodana.jetbrains.com/never-gonna-give-you-up",
qodanaRepoUrl: "https://qodana.jetbrains.com/never-gonna-give-you-up",
qodanaBranch: branchExpected,
qodanaRevision: revisionExpected,
},
envExpected: "user-defined",
remoteUrlExpected: "https://qodana.jetbrains.com/never-gonna-give-you-up",
jobUrlExpected: "https://qodana.jetbrains.com/never-gonna-give-you-up",
repoUrlExpected: "https://qodana.jetbrains.com/never-gonna-give-you-up",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -167,6 +170,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
envExpected: fmt.Sprintf("space:%s", Version),
remoteUrlExpected: "ssh://git@git.jetbrains.team/sa/entrypoint.git",
jobUrlExpected: "https://space.jetbrains.com/never-gonna-give-you-up",
repoUrlExpected: "https://jetbrains.team/p/sa/repositories/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -177,10 +181,12 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
"CI_COMMIT_BRANCH": branchExpected,
"CI_COMMIT_SHA": revisionExpected,
"CI_REPOSITORY_URL": "https://gitlab.jetbrains.com/sa/entrypoint.git",
"CI_PROJECT_URL": "https://gitlab.jetbrains.com/sa/entrypoint",
},
envExpected: fmt.Sprintf("gitlab:%s", Version),
remoteUrlExpected: "https://gitlab.jetbrains.com/sa/entrypoint.git",
jobUrlExpected: "https://gitlab.jetbrains.com/never-gonna-give-you-up",
repoUrlExpected: "https://gitlab.jetbrains.com/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -195,6 +201,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
envExpected: fmt.Sprintf("jenkins:%s", Version),
jobUrlExpected: "https://jenkins.jetbrains.com/never-gonna-give-you-up",
remoteUrlExpected: "https://git.jetbrains.com/sa/entrypoint.git",
repoUrlExpected: "https://git.jetbrains.com/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -210,6 +217,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
envExpected: fmt.Sprintf("github-actions:%s", Version),
jobUrlExpected: "https://github.jetbrains.com/sa/entrypoint/actions/runs/123456789",
remoteUrlExpected: "https://github.jetbrains.com/sa/entrypoint.git",
repoUrlExpected: "https://github.jetbrains.com/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -225,6 +233,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
envExpected: fmt.Sprintf("github-actions:%s", Version),
jobUrlExpected: "https://github.jetbrains.com/sa/entrypoint/actions/runs/123456789",
remoteUrlExpected: "https://github.jetbrains.com/sa/entrypoint.git",
repoUrlExpected: "https://github.jetbrains.com/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -239,6 +248,7 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
envExpected: fmt.Sprintf("circleci:%s", Version),
jobUrlExpected: "https://circleci.jetbrains.com/never-gonna-give-you-up",
remoteUrlExpected: "https://circleci.jetbrains.com/sa/entrypoint.git",
repoUrlExpected: "https://circleci.jetbrains.com/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand All @@ -256,6 +266,22 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
envExpected: fmt.Sprintf("azure-pipelines:%s", Version),
jobUrlExpected: "https://dev.azure.com/jetbrains/sa/_build/results?buildId=123456789",
remoteUrlExpected: "https://dev.azure.com/jetbrains/sa/entrypoint.git",
repoUrlExpected: "https://dev.azure.com/jetbrains/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
{
ci: "CircleCI", // includes userinfo in the url
variables: map[string]string{
"CIRCLE_BUILD_URL": "https://circleci.jetbrains.com/never-gonna-give-you-up",
"CIRCLE_SHA1": revisionExpected,
"CIRCLE_BRANCH": branchExpected,
"CIRCLE_REPOSITORY_URL": "https://user:password@circleci.jetbrains.com/sa/entrypoint.git",
},
envExpected: fmt.Sprintf("circleci:%s", Version),
jobUrlExpected: "https://circleci.jetbrains.com/never-gonna-give-you-up",
remoteUrlExpected: "https://circleci.jetbrains.com/sa/entrypoint.git",
repoUrlExpected: "https://circleci.jetbrains.com/sa/entrypoint",
revisionExpected: revisionExpected,
branchExpected: branchExpected,
},
Expand Down Expand Up @@ -305,10 +331,13 @@ func Test_ExtractEnvironmentVariables(t *testing.T) {
if environment.get(qodanaBranch) != tc.branchExpected {
t.Errorf("%s: Expected %s, got %s", environment.name, branchExpected, environment.get(qodanaBranch))
}
if environment.get(qodanaRepoUrl) != tc.repoUrlExpected {
t.Errorf("%s: Expected %s, got %s", environment.name, tc.repoUrlExpected, environment.get(qodanaRepoUrl))
}
})
}

for _, k := range append(maps.Keys(tc.variables), []string{qodanaJobUrl, qodanaEnv, qodanaRemoteUrl, qodanaRevision, qodanaBranch}...) {
for _, k := range append(maps.Keys(tc.variables), []string{qodanaRepoUrl, qodanaJobUrl, qodanaEnv, qodanaRemoteUrl, qodanaRevision, qodanaBranch}...) {
err := os.Unsetenv(k)
if err != nil {
t.Fatal(err)
Expand Down
30 changes: 30 additions & 0 deletions core/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
qodanaNugetUser = "QODANA_NUGET_USER"
qodanaNugetPassword = "QODANA_NUGET_PASSWORD"
qodanaNugetName = "QODANA_NUGET_NAME"
qodanaRepoUrl = "QODANA_REPO_URL"
)

// ExtractQodanaEnvironment extracts Qodana environment variables from the current environment.
Expand All @@ -64,6 +65,7 @@ func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
setEnvironmentFunc(qodanaRemoteUrl, validateRemoteUrl(ci.Git.Remote, qEnv))
setEnvironmentFunc(qodanaBranch, validateBranch(ci.Git.Branch, qEnv))
setEnvironmentFunc(qodanaRevision, ci.Git.Revision)
setEnvironmentFunc(qodanaRepoUrl, getRepositoryHttpUrl(qEnv, ci.Git.Remote))
}
setEnvironmentFunc(qodanaNugetUrl, os.Getenv(qodanaNugetUrl))
setEnvironmentFunc(qodanaNugetUser, os.Getenv(qodanaNugetUser))
Expand All @@ -75,6 +77,7 @@ func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
setEnvironmentFunc(qodanaRemoteUrl, getSpaceRemoteUrl())
setEnvironmentFunc(qodanaBranch, os.Getenv("JB_SPACE_GIT_BRANCH"))
setEnvironmentFunc(qodanaRevision, os.Getenv("JB_SPACE_GIT_REVISION"))
setEnvironmentFunc(qodanaRepoUrl, getRepositoryHttpUrl(qEnv, ""))
}
setEnvironmentFunc(qodanaEnv, fmt.Sprintf("%s:%s", qEnv, Version))
}
Expand All @@ -91,6 +94,33 @@ func validateRemoteUrl(remote string, qEnv string) string {
return remote
}

func getRepositoryHttpUrl(qEnv string, remoteUrl string) string {
switch qEnv {
case "gitlab":
return os.Getenv("CI_PROJECT_URL") // gitlab exposes this directly, don't need to mess with the url
case "space":
return strings.Join([]string{
"https://",
os.Getenv("JB_SPACE_API_URL"),
"/p/",
os.Getenv("JB_SPACE_PROJECT_KEY"),
"/repositories/",
os.Getenv("JB_SPACE_GIT_REPOSITORY_NAME"),
}, "")
default:
remoteUrl = strings.TrimSuffix(remoteUrl, ".git")
}
parsed, err := url.ParseRequestURI(remoteUrl)

if remoteUrl == "" || err != nil || !strings.HasPrefix(parsed.Scheme, "http") {
log.Warnf("Unable to parse http(s) remote URL from %s, set %s env variable for proper qodana.cloud reporting", remoteUrl, qodanaRepoUrl)
return ""
}

parsed.User = nil
return parsed.String()
}

func validateBranch(branch string, env string) string {
if branch == "" {
if env == "github-actions" {
Expand Down

0 comments on commit 6b19967

Please sign in to comment.