Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename CI tags #984

Merged
merged 10 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 100 additions & 105 deletions internal/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
)

type CI struct {
Provider Provider
OriginURL string
Repo string
RefName string // branch or tag
SHA string
User string
Provider Provider
URL string
Repo string
Ref string // branch or tag
SHA string
}

// Provider represents a CI Provider.
Expand All @@ -29,9 +28,9 @@ var (
// AWS represents https://aws.amazon.com/codebuild/
AWS = Provider{Name: "AWS CodeBuild", Envar: "CODEBUILD_INITIATOR"}
// Azure represents https://azure.microsoft.com/en-us/services/devops/
Azure = Provider{Name: "Azure DevOps", Envar: "Agent_BuildDirectory"}
Azure = Provider{Name: "Azure DevOps", Envar: "TF_BUILD"}
// Bamboo represents https://www.atlassian.com/software/bamboo
Bamboo = Provider{Name: "Bamboo", Envar: "bamboo_buildNumber"}
Bamboo = Provider{Name: "Bamboo", Envar: "bamboo_agentId"}
// Bitbucket represents https://bitbucket.org/product/features/pipelines
Bitbucket = Provider{Name: "Bitbucket", Envar: "BITBUCKET_BUILD_NUMBER"}
// Buildkite represents https://buildkite.com/
Expand Down Expand Up @@ -101,138 +100,127 @@ func GetCI(provider Provider) CI {
switch provider {
case AppVeyor:
return CI{
Provider: provider,
OriginURL: fmt.Sprintf("%s/project/%s/%s/builds/%s", os.Getenv("APPVEYOR_URL"), os.Getenv("APPVEYOR_ACCOUNT_NAME"), os.Getenv("APPVEYOR_PROJECT_NAME"), os.Getenv("APPVEYOR_BUILD_ID")),
Repo: os.Getenv("APPVEYOR_REPO_NAME"),
RefName: os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH"),
SHA: os.Getenv("APPVEYOR_REPO_COMMIT"),
Provider: provider,
URL: fmt.Sprintf("%s/project/%s/%s/builds/%s", os.Getenv("APPVEYOR_URL"), os.Getenv("APPVEYOR_ACCOUNT_NAME"), os.Getenv("APPVEYOR_PROJECT_NAME"), os.Getenv("APPVEYOR_BUILD_ID")),
Repo: os.Getenv("APPVEYOR_REPO_NAME"),
Ref: os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH"),
SHA: os.Getenv("APPVEYOR_REPO_COMMIT"),
}
case AWS:
return CI{
Provider: provider,
OriginURL: os.Getenv("CODEBUILD_PUBLIC_BUILD_URL"),
Repo: os.Getenv("CODEBUILD_SOURCE_REPO_URL"),
RefName: os.Getenv("CODEBUILD_SOURCE_VERSION"),
SHA: os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION"),
User: os.Getenv("CODEBUILD_WEBHOOK_ACTOR_ACCOUNT_ID"),
Provider: provider,
URL: os.Getenv("CODEBUILD_PUBLIC_BUILD_URL"),
Repo: os.Getenv("CODEBUILD_SOURCE_REPO_URL"),
Ref: os.Getenv("CODEBUILD_SOURCE_VERSION"),
SHA: os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION"),
}
case Azure:
return CI{
Provider: provider,
OriginURL: os.Getenv("Build_BuildUri"),
Repo: os.Getenv("System_PullRequest_SourceRepositoryURI"),
RefName: os.Getenv("Build_SourceBranchName"),
SHA: os.Getenv("Build_SourceVersion"),
User: os.Getenv("Build_RequestedFor"),
Provider: provider,
URL: os.Getenv("BUILD_REPOSITORY_URI"),
Repo: os.Getenv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI"),
Ref: os.Getenv("BUILD_SOURCEBRANCHNAME"),
SHA: os.Getenv("BUILD_SOURCEVERSION"),
}
case Bamboo:
return CI{
Provider: provider,
OriginURL: os.Getenv("bamboo_buildResultsUrl"),
Repo: os.Getenv("bamboo_planRepository_repositoryUrl"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terminologies: In Bamboo CI, a plan organizes a series of jobs and their associated configurations. position is the index of a repository in the plan's configuration. The default position is 0.

The variable bamboo_planRepository_repositoryUrl is now replaced with bamboo_planRepository_<position>_repositoryUrl. There’s no way to dynamically retrieve position.
For commit SHA information, the only available variable is bamboo_planRepository_<position>_revision, which also depends on <position>. The value cannot be accurately determined.

Based on these, I removed the repo url and revision.

Reference: https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html

Note: How can these variables be accessed as environment variables?

Bamboo variables are exported as bash shell variables. All full stops (periods) are converted to underscores. For example, the variable bamboo.my.variable is $bamboo_my_variable in bash. This is related to File Script and Inline Script tasks.

RefName: os.Getenv("bamboo_planRepository_branch"),
SHA: os.Getenv("bamboo_planRepository_revision"),
User: os.Getenv("bamboo_ManualBuildTriggerReason_userName"),
Provider: provider,
URL: os.Getenv("bamboo_resultsUrl"),
Ref: os.Getenv("bamboo_planRepository_branchDisplayName"),
}
case Bitbucket:
return CI{
Provider: provider,
OriginURL: fmt.Sprintf("https://bitbucket.org/%s/addon/pipelines/home#!/results/%s", os.Getenv("BITBUCKET_REPO_FULL_NAME"), os.Getenv("BITBUCKET_BUILD_NUMBER")),
Repo: os.Getenv("BITBUCKET_REPO_FULL_NAME"),
RefName: os.Getenv("BITBUCKET_BRANCH"),
SHA: os.Getenv("BITBUCKET_COMMIT"),
User: os.Getenv("BITBUCKET_STEP_TRIGGERER_UUID"),
Provider: provider,
URL: fmt.Sprintf("https://bitbucket.org/%s/addon/pipelines/home#!/results/%s", os.Getenv("BITBUCKET_REPO_FULL_NAME"), os.Getenv("BITBUCKET_BUILD_NUMBER")),
Repo: os.Getenv("BITBUCKET_REPO_FULL_NAME"),
Ref: os.Getenv("BITBUCKET_BRANCH"),
SHA: os.Getenv("BITBUCKET_COMMIT"),
}
case Buildkite:
return CI{
Provider: provider,
OriginURL: os.Getenv("BUILDKITE_BUILD_URL"),
Repo: os.Getenv("BUILDKITE_REPO"),
RefName: os.Getenv("BUILDKITE_BRANCH"),
SHA: os.Getenv("BUILDKITE_COMMIT"),
User: os.Getenv("BUILDKITE_BUILD_CREATOR"),
Provider: provider,
URL: os.Getenv("BUILDKITE_BUILD_URL"),
Repo: os.Getenv("BUILDKITE_REPO"),
Ref: os.Getenv("BUILDKITE_BRANCH"),
SHA: os.Getenv("BUILDKITE_COMMIT"),
}
case Buddy:
return CI{
Provider: provider,
OriginURL: os.Getenv("BUDDY_PIPELINE_URL"),
Repo: os.Getenv("BUDDY_PROJECT_URL"),
RefName: os.Getenv("BUDDY_EXECUTION_BRANCH"),
SHA: os.Getenv("BUDDY_EXECUTION_REVISION"),
User: os.Getenv("BUDDY_INVOKER_NAME"),
Provider: provider,
URL: os.Getenv("BUDDY_PIPELINE_URL"),
Repo: os.Getenv("BUDDY_PROJECT_URL"),
Ref: os.Getenv("BUDDY_RUN_BRANCH"),
SHA: os.Getenv("BUDDY_RUN_COMMIT"),
}
case Circle:
return CI{
Provider: provider,
OriginURL: os.Getenv("CIRCLE_BUILD_URL"),
Repo: os.Getenv("CIRCLE_REPOSITORY_URL"),
RefName: os.Getenv("CIRCLE_BRANCH"),
SHA: os.Getenv("CIRCLE_SHA1"),
User: os.Getenv("CIRCLE_USERNAME"),
Provider: provider,
URL: os.Getenv("CIRCLE_BUILD_URL"),
Repo: os.Getenv("CIRCLE_REPOSITORY_URL"),
Ref: os.Getenv("CIRCLE_BRANCH"),
SHA: os.Getenv("CIRCLE_SHA1"),
}
case CodeShip:
return CI{
Provider: provider,
OriginURL: os.Getenv("CI_BUILD_URL"),
Repo: os.Getenv("CI_REPO_NAME"),
RefName: os.Getenv("CI_BRANCH"),
SHA: os.Getenv("CI_COMMIT_ID"),
Provider: provider,
URL: os.Getenv("CI_BUILD_URL"),
Repo: os.Getenv("CI_REPO_NAME"),
Ref: os.Getenv("CI_BRANCH"),
SHA: os.Getenv("CI_COMMIT_ID"),
}
case Drone:
return CI{
Provider: provider,
OriginURL: os.Getenv("DRONE_BUILD_LINK"),
Repo: os.Getenv("DRONE_REPO"),
RefName: os.Getenv("DRONE_BRANCH"),
SHA: os.Getenv("DRONE_COMMIT_SHA"),
Provider: provider,
URL: os.Getenv("DRONE_BUILD_LINK"),
Repo: os.Getenv("DRONE_REPO"),
Ref: os.Getenv("DRONE_BRANCH"),
SHA: os.Getenv("DRONE_COMMIT_SHA"),
}
case GitHub:
return CI{
Provider: provider,
OriginURL: fmt.Sprintf("%s/%s/actions/runs/%s", os.Getenv("GITHUB_SERVER_URL"), os.Getenv("GITHUB_REPOSITORY"), os.Getenv("GITHUB_RUN_ID")),
Repo: os.Getenv("GITHUB_REPOSITORY"),
RefName: os.Getenv("GITHUB_REF_NAME"),
SHA: os.Getenv("GITHUB_SHA"),
User: os.Getenv("GITHUB_ACTOR"),
Provider: provider,
URL: fmt.Sprintf("%s/%s/actions/runs/%s", os.Getenv("GITHUB_SERVER_URL"), os.Getenv("GITHUB_REPOSITORY"), os.Getenv("GITHUB_RUN_ID")),
Repo: os.Getenv("GITHUB_REPOSITORY"),
Ref: os.Getenv("GITHUB_REF_NAME"),
SHA: os.Getenv("GITHUB_SHA"),
}
case GitLab:
return CI{
Provider: provider,
OriginURL: os.Getenv("CI_JOB_URL"),
Repo: os.Getenv("CI_PROJECT_PATH"),
RefName: os.Getenv("CI_COMMIT_REF_NAME"),
SHA: os.Getenv("CI_COMMIT_SHA"),
User: os.Getenv("GITLAB_USER_LOGIN"),
Provider: provider,
URL: os.Getenv("CI_JOB_URL"),
Repo: os.Getenv("CI_PROJECT_PATH"),
Ref: os.Getenv("CI_COMMIT_REF_NAME"),
SHA: os.Getenv("CI_COMMIT_SHA"),
}
case Gitpod:
return CI{
Provider: provider,
OriginURL: os.Getenv("GITPOD_WORKSPACE_URL"),
Repo: os.Getenv("GITPOD_REPO_ROOT"),
Provider: provider,
URL: os.Getenv("GITPOD_WORKSPACE_URL"),
Repo: os.Getenv("GITPOD_REPO_ROOT"),
}
case Jenkins:
return CI{
Provider: provider,
OriginURL: os.Getenv("JOB_URL"),
Repo: os.Getenv("GIT_URL"),
RefName: os.Getenv("GIT_BRANCH"),
SHA: os.Getenv("GIT_COMMIT"),
Provider: provider,
URL: os.Getenv("JOB_URL"),
Repo: os.Getenv("GIT_URL"),
Ref: os.Getenv("GIT_BRANCH"),
SHA: os.Getenv("GIT_COMMIT"),
}
case Semaphore:
return CI{
Provider: provider,
OriginURL: fmt.Sprintf("%s/workflows/%s?pipeline_id=%s", os.Getenv("SEMAPHORE_ORGANIZATION_URL"), os.Getenv("SEMAPHORE_PROJECT_ID"), os.Getenv("SEMAPHORE_JOB_ID")),
Repo: os.Getenv("SEMAPHORE_GIT_URL"),
RefName: os.Getenv("SEMAPHORE_GIT_BRANCH"),
SHA: os.Getenv("SEMAPHORE_GIT_SHA"),
Provider: provider,
URL: fmt.Sprintf("%s/workflows/%s?pipeline_id=%s", os.Getenv("SEMAPHORE_ORGANIZATION_URL"), os.Getenv("SEMAPHORE_PROJECT_ID"), os.Getenv("SEMAPHORE_JOB_ID")),
Repo: os.Getenv("SEMAPHORE_GIT_URL"),
Ref: os.Getenv("SEMAPHORE_GIT_WORKING_BRANCH"),
SHA: os.Getenv("SEMAPHORE_GIT_SHA"),
}
case Travis:
return CI{
Provider: provider,
OriginURL: os.Getenv("TRAVIS_BUILD_WEB_URL"),
Repo: os.Getenv("TRAVIS_REPO_SLUG"),
RefName: os.Getenv("TRAVIS_BRANCH"),
SHA: os.Getenv("TRAVIS_COMMIT"),
Provider: provider,
URL: os.Getenv("TRAVIS_BUILD_WEB_URL"),
Repo: os.Getenv("TRAVIS_REPO_SLUG"),
Ref: os.Getenv("TRAVIS_BRANCH"),
SHA: os.Getenv("TRAVIS_COMMIT"),
}
case TeamCity:
return CI{
Expand All @@ -243,7 +231,7 @@ func GetCI(provider Provider) CI {
return CI{}
}

// GetTags returns tag list containing CI info
// GetTags returns tag list containing CI info.
func GetTags() []string {
var tags []string
provider := GetProvider()
Expand All @@ -253,21 +241,28 @@ func GetTags() []string {

ci := GetCI(provider)
tags = append(tags, ci.Provider.Name)
if ci.OriginURL != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "originURL", ci.OriginURL))
if ci.URL != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "ci:url", ci.URL))
}
if ci.Repo != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "repo", ci.Repo))
tags = append(tags, fmt.Sprintf("%s:%s", "ci:repo", ci.Repo))
}
if ci.RefName != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "refName", ci.RefName))
if ci.Ref != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "ci:ref", ci.Ref))
}
if ci.SHA != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "SHA", ci.SHA))
}
if ci.User != "" {
tags = append(tags, fmt.Sprintf("%s:%s", "user", ci.User))
tags = append(tags, fmt.Sprintf("%s:%s", "ci:ssha", shortenSHA(ci.SHA)))
}

return tags
}

// shortenSHA truncates a given SHA string to 7 characters.
// If the input SHA is already shorter than 8 characters, it is returned as-is.
func shortenSHA(sha string) string {
if len(sha) < 8 {
return sha
}

return sha[:7]
}
4 changes: 2 additions & 2 deletions internal/insights/testrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ func enrichInsightTestRun(runs []TestRun, jobID string, jobName string, details

if provider != ci.None {
runs[idx].CI = &CI{
Branch: ciData.RefName,
RefName: ciData.RefName,
Branch: ciData.Ref,
RefName: ciData.Ref,
Repository: ciData.Repo,
CommitSha: ciData.SHA,
}
Expand Down