From 3f05b0a82b9f9f29d53dd48650520d4632c838dd Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Tue, 26 Nov 2024 15:43:04 -0800 Subject: [PATCH 01/10] feat: rename CI tags --- internal/ci/ci.go | 207 +++++++++++++++++------------------ internal/insights/testrun.go | 6 +- 2 files changed, 106 insertions(+), 107 deletions(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 407f7c2f5..b26b21ed8 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -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 + ShortSHA string } // Provider represents a CI Provider. @@ -101,138 +100,130 @@ 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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(os.Getenv("Build.SourceVersion")), } case Bamboo: return CI{ - Provider: provider, - OriginURL: os.Getenv("bamboo_buildResultsUrl"), - Repo: os.Getenv("bamboo_planRepository_repositoryUrl"), - 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"), + Repo: os.Getenv("bamboo.repository.git.repositoryUrl"), // bamboo.planRepository..name + Ref: os.Getenv("bamboo.planRepository.branchDisplayName"), + ShortSHA: os.Getenv("bamboo_planRepository_revision"), // bamboo.planRepository..revision + // Do we still need Bamboo???? } 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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: os.Getenv("BUDDY_RUN_COMMIT_SHORT"), } 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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: os.Getenv("CI_COMMIT_SHORT_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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(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"), + ShortSHA: ShortenSHA(os.Getenv("TRAVIS_COMMIT")), } case TeamCity: return CI{ @@ -243,7 +234,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() @@ -253,21 +244,29 @@ 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)) + if ci.ShortSHA != "" { + tags = append(tags, fmt.Sprintf("%s:%s", "ci:ssha", ci.ShortSHA)) } return tags } + +// ShortenSHA truncates a given SHA string to 7 characters, the standard length for +// short SHAs used in Git. If the input SHA is already shorter than 8 characters, +// it is returned as-is without modification. +func ShortenSHA(sha string) string { + if len(sha) < 8 { + return sha + } + + return sha[:7] +} diff --git a/internal/insights/testrun.go b/internal/insights/testrun.go index d29eaba70..b796c92a3 100644 --- a/internal/insights/testrun.go +++ b/internal/insights/testrun.go @@ -204,10 +204,10 @@ 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, + CommitSha: ciData.ShortSHA, } } } From 9a746c6da0b1d375cfabafd7891084cf03cfea4a Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Tue, 26 Nov 2024 16:40:24 -0800 Subject: [PATCH 02/10] fix CI env vars --- internal/ci/ci.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index b26b21ed8..63f0e12b1 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -117,19 +117,18 @@ func GetCI(provider Provider) CI { case Azure: return CI{ Provider: provider, - URL: os.Getenv("Build.Repository.Uri"), - Repo: os.Getenv("System.PullRequest.SourceRepositoryURI"), - Ref: os.Getenv("Build.SourceBranchName"), - ShortSHA: ShortenSHA(os.Getenv("Build.SourceVersion")), + URL: os.Getenv("BUILD_REPOSITORY_URI"), + Repo: os.Getenv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI"), + Ref: os.Getenv("BUILD_SOURCEBRANCHNAME"), + ShortSHA: ShortenSHA(os.Getenv("BUILD_SOURCEVERSION")), } case Bamboo: return CI{ Provider: provider, - URL: os.Getenv("bamboo.resultsUrl"), - Repo: os.Getenv("bamboo.repository.git.repositoryUrl"), // bamboo.planRepository..name - Ref: os.Getenv("bamboo.planRepository.branchDisplayName"), - ShortSHA: os.Getenv("bamboo_planRepository_revision"), // bamboo.planRepository..revision - // Do we still need Bamboo???? + URL: os.Getenv("bamboo_resultsUrl"), + //Repo: os.Getenv("bamboo.planRepository.name"), // bamboo.planRepository..name + Ref: os.Getenv("bamboo_planRepository_branchDisplayName"), + //ShortSHA: os.Getenv("bamboo_planRepository_revision"), // bamboo.planRepository..revision } case Bitbucket: return CI{ From 37ba9d1238d1a3ac502088632cc02df3fbc20807 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Tue, 26 Nov 2024 17:01:14 -0800 Subject: [PATCH 03/10] private function is enough --- internal/ci/ci.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 63f0e12b1..2e85f8a4e 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -104,7 +104,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: ShortenSHA(os.Getenv("APPVEYOR_REPO_COMMIT")), + ShortSHA: shortenSHA(os.Getenv("APPVEYOR_REPO_COMMIT")), } case AWS: return CI{ @@ -112,7 +112,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CODEBUILD_PUBLIC_BUILD_URL"), Repo: os.Getenv("CODEBUILD_SOURCE_REPO_URL"), Ref: os.Getenv("CODEBUILD_SOURCE_VERSION"), - ShortSHA: ShortenSHA(os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION")), + ShortSHA: shortenSHA(os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION")), } case Azure: return CI{ @@ -120,15 +120,13 @@ func GetCI(provider Provider) CI { URL: os.Getenv("BUILD_REPOSITORY_URI"), Repo: os.Getenv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI"), Ref: os.Getenv("BUILD_SOURCEBRANCHNAME"), - ShortSHA: ShortenSHA(os.Getenv("BUILD_SOURCEVERSION")), + ShortSHA: shortenSHA(os.Getenv("BUILD_SOURCEVERSION")), } case Bamboo: return CI{ Provider: provider, URL: os.Getenv("bamboo_resultsUrl"), - //Repo: os.Getenv("bamboo.planRepository.name"), // bamboo.planRepository..name - Ref: os.Getenv("bamboo_planRepository_branchDisplayName"), - //ShortSHA: os.Getenv("bamboo_planRepository_revision"), // bamboo.planRepository..revision + Ref: os.Getenv("bamboo_planRepository_branchDisplayName"), } case Bitbucket: return CI{ @@ -136,7 +134,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: ShortenSHA(os.Getenv("BITBUCKET_COMMIT")), + ShortSHA: shortenSHA(os.Getenv("BITBUCKET_COMMIT")), } case Buildkite: return CI{ @@ -144,7 +142,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("BUILDKITE_BUILD_URL"), Repo: os.Getenv("BUILDKITE_REPO"), Ref: os.Getenv("BUILDKITE_BRANCH"), - ShortSHA: ShortenSHA(os.Getenv("BUILDKITE_COMMIT")), + ShortSHA: shortenSHA(os.Getenv("BUILDKITE_COMMIT")), } case Buddy: return CI{ @@ -160,7 +158,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CIRCLE_BUILD_URL"), Repo: os.Getenv("CIRCLE_REPOSITORY_URL"), Ref: os.Getenv("CIRCLE_BRANCH"), - ShortSHA: ShortenSHA(os.Getenv("CIRCLE_SHA1")), + ShortSHA: shortenSHA(os.Getenv("CIRCLE_SHA1")), } case CodeShip: return CI{ @@ -168,7 +166,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CI_BUILD_URL"), Repo: os.Getenv("CI_REPO_NAME"), Ref: os.Getenv("CI_BRANCH"), - ShortSHA: ShortenSHA(os.Getenv("CI_COMMIT_ID")), + ShortSHA: shortenSHA(os.Getenv("CI_COMMIT_ID")), } case Drone: return CI{ @@ -176,7 +174,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("DRONE_BUILD_LINK"), Repo: os.Getenv("DRONE_REPO"), Ref: os.Getenv("DRONE_BRANCH"), - ShortSHA: ShortenSHA(os.Getenv("DRONE_COMMIT_SHA")), + ShortSHA: shortenSHA(os.Getenv("DRONE_COMMIT_SHA")), } case GitHub: return CI{ @@ -184,7 +182,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: ShortenSHA(os.Getenv("GITHUB_SHA")), + ShortSHA: shortenSHA(os.Getenv("GITHUB_SHA")), } case GitLab: return CI{ @@ -206,7 +204,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("JOB_URL"), Repo: os.Getenv("GIT_URL"), Ref: os.Getenv("GIT_BRANCH"), - ShortSHA: ShortenSHA(os.Getenv("GIT_COMMIT")), + ShortSHA: shortenSHA(os.Getenv("GIT_COMMIT")), } case Semaphore: return CI{ @@ -214,7 +212,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: ShortenSHA(os.Getenv("SEMAPHORE_GIT_SHA")), + ShortSHA: shortenSHA(os.Getenv("SEMAPHORE_GIT_SHA")), } case Travis: return CI{ @@ -222,7 +220,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("TRAVIS_BUILD_WEB_URL"), Repo: os.Getenv("TRAVIS_REPO_SLUG"), Ref: os.Getenv("TRAVIS_BRANCH"), - ShortSHA: ShortenSHA(os.Getenv("TRAVIS_COMMIT")), + ShortSHA: shortenSHA(os.Getenv("TRAVIS_COMMIT")), } case TeamCity: return CI{ @@ -259,10 +257,10 @@ func GetTags() []string { return tags } -// ShortenSHA truncates a given SHA string to 7 characters, the standard length for +// shortenSHA truncates a given SHA string to 7 characters, the standard length for // short SHAs used in Git. If the input SHA is already shorter than 8 characters, // it is returned as-is without modification. -func ShortenSHA(sha string) string { +func shortenSHA(sha string) string { if len(sha) < 8 { return sha } From 55eb80c0fbe5032ec288dd4a215f93be6a62af72 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 08:53:53 -0800 Subject: [PATCH 04/10] revise function comment --- internal/ci/ci.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 2e85f8a4e..9dc6bd6a8 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -257,9 +257,8 @@ func GetTags() []string { return tags } -// shortenSHA truncates a given SHA string to 7 characters, the standard length for -// short SHAs used in Git. If the input SHA is already shorter than 8 characters, -// it is returned as-is without modification. +// shortenSHA truncates a given SHA string to 7 characters. +// If the input SHA is already shorter than 8 characters, it is returned as-is without modification. func shortenSHA(sha string) string { if len(sha) < 8 { return sha From 383121c1e9e810652c915854e459098b4d6b8853 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 08:54:34 -0800 Subject: [PATCH 05/10] revise function comment --- internal/ci/ci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 9dc6bd6a8..3bb3cfd6d 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -258,7 +258,7 @@ func GetTags() []string { } // shortenSHA truncates a given SHA string to 7 characters. -// If the input SHA is already shorter than 8 characters, it is returned as-is without modification. +// 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 From 1990a9341974dd65561672091a1a0dcd06e791ac Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 09:30:44 -0800 Subject: [PATCH 06/10] update handling shortsha --- internal/ci/ci.go | 34 +++++++++++++++++----------------- internal/insights/testrun.go | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 3bb3cfd6d..d97f4d5b5 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -10,7 +10,7 @@ type CI struct { URL string Repo string Ref string // branch or tag - ShortSHA string + SHA string } // Provider represents a CI Provider. @@ -104,7 +104,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: shortenSHA(os.Getenv("APPVEYOR_REPO_COMMIT")), + SHA: os.Getenv("APPVEYOR_REPO_COMMIT"), } case AWS: return CI{ @@ -112,7 +112,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CODEBUILD_PUBLIC_BUILD_URL"), Repo: os.Getenv("CODEBUILD_SOURCE_REPO_URL"), Ref: os.Getenv("CODEBUILD_SOURCE_VERSION"), - ShortSHA: shortenSHA(os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION")), + SHA: os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION"), } case Azure: return CI{ @@ -120,7 +120,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("BUILD_REPOSITORY_URI"), Repo: os.Getenv("SYSTEM_PULLREQUEST_SOURCEREPOSITORYURI"), Ref: os.Getenv("BUILD_SOURCEBRANCHNAME"), - ShortSHA: shortenSHA(os.Getenv("BUILD_SOURCEVERSION")), + SHA: os.Getenv("BUILD_SOURCEVERSION"), } case Bamboo: return CI{ @@ -134,7 +134,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: shortenSHA(os.Getenv("BITBUCKET_COMMIT")), + SHA: os.Getenv("BITBUCKET_COMMIT"), } case Buildkite: return CI{ @@ -142,7 +142,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("BUILDKITE_BUILD_URL"), Repo: os.Getenv("BUILDKITE_REPO"), Ref: os.Getenv("BUILDKITE_BRANCH"), - ShortSHA: shortenSHA(os.Getenv("BUILDKITE_COMMIT")), + SHA: os.Getenv("BUILDKITE_COMMIT"), } case Buddy: return CI{ @@ -150,7 +150,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("BUDDY_PIPELINE_URL"), Repo: os.Getenv("BUDDY_PROJECT_URL"), Ref: os.Getenv("BUDDY_RUN_BRANCH"), - ShortSHA: os.Getenv("BUDDY_RUN_COMMIT_SHORT"), + SHA: os.Getenv("BUDDY_RUN_COMMIT"), } case Circle: return CI{ @@ -158,7 +158,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CIRCLE_BUILD_URL"), Repo: os.Getenv("CIRCLE_REPOSITORY_URL"), Ref: os.Getenv("CIRCLE_BRANCH"), - ShortSHA: shortenSHA(os.Getenv("CIRCLE_SHA1")), + SHA: os.Getenv("CIRCLE_SHA1"), } case CodeShip: return CI{ @@ -166,7 +166,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CI_BUILD_URL"), Repo: os.Getenv("CI_REPO_NAME"), Ref: os.Getenv("CI_BRANCH"), - ShortSHA: shortenSHA(os.Getenv("CI_COMMIT_ID")), + SHA: os.Getenv("CI_COMMIT_ID"), } case Drone: return CI{ @@ -174,7 +174,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("DRONE_BUILD_LINK"), Repo: os.Getenv("DRONE_REPO"), Ref: os.Getenv("DRONE_BRANCH"), - ShortSHA: shortenSHA(os.Getenv("DRONE_COMMIT_SHA")), + SHA: (os.Getenv("DRONE_COMMIT_SHA")), } case GitHub: return CI{ @@ -182,7 +182,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: shortenSHA(os.Getenv("GITHUB_SHA")), + SHA: (os.Getenv("GITHUB_SHA")), } case GitLab: return CI{ @@ -190,7 +190,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("CI_JOB_URL"), Repo: os.Getenv("CI_PROJECT_PATH"), Ref: os.Getenv("CI_COMMIT_REF_NAME"), - ShortSHA: os.Getenv("CI_COMMIT_SHORT_SHA"), + SHA: os.Getenv("CI_COMMIT_SHA"), } case Gitpod: return CI{ @@ -204,7 +204,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("JOB_URL"), Repo: os.Getenv("GIT_URL"), Ref: os.Getenv("GIT_BRANCH"), - ShortSHA: shortenSHA(os.Getenv("GIT_COMMIT")), + SHA: os.Getenv("GIT_COMMIT"), } case Semaphore: return CI{ @@ -212,7 +212,7 @@ func GetCI(provider Provider) CI { 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"), - ShortSHA: shortenSHA(os.Getenv("SEMAPHORE_GIT_SHA")), + SHA: os.Getenv("SEMAPHORE_GIT_SHA"), } case Travis: return CI{ @@ -220,7 +220,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("TRAVIS_BUILD_WEB_URL"), Repo: os.Getenv("TRAVIS_REPO_SLUG"), Ref: os.Getenv("TRAVIS_BRANCH"), - ShortSHA: shortenSHA(os.Getenv("TRAVIS_COMMIT")), + SHA: os.Getenv("TRAVIS_COMMIT"), } case TeamCity: return CI{ @@ -250,8 +250,8 @@ func GetTags() []string { if ci.Ref != "" { tags = append(tags, fmt.Sprintf("%s:%s", "ci:ref", ci.Ref)) } - if ci.ShortSHA != "" { - tags = append(tags, fmt.Sprintf("%s:%s", "ci:ssha", ci.ShortSHA)) + if ci.SHA != "" { + tags = append(tags, fmt.Sprintf("%s:%s", "ci:ssha", shortenSHA(ci.SHA))) } return tags diff --git a/internal/insights/testrun.go b/internal/insights/testrun.go index b796c92a3..da7b90295 100644 --- a/internal/insights/testrun.go +++ b/internal/insights/testrun.go @@ -207,7 +207,7 @@ func enrichInsightTestRun(runs []TestRun, jobID string, jobName string, details Branch: ciData.Ref, RefName: ciData.Ref, Repository: ciData.Repo, - CommitSha: ciData.ShortSHA, + CommitSha: ciData.SHA, } } } From 41af2e3aad5ce363c9d122e107d1b1444097c4c8 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 09:32:09 -0800 Subject: [PATCH 07/10] cleanup --- internal/ci/ci.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index d97f4d5b5..8e9838dc1 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -174,7 +174,7 @@ func GetCI(provider Provider) CI { URL: os.Getenv("DRONE_BUILD_LINK"), Repo: os.Getenv("DRONE_REPO"), Ref: os.Getenv("DRONE_BRANCH"), - SHA: (os.Getenv("DRONE_COMMIT_SHA")), + SHA: os.Getenv("DRONE_COMMIT_SHA"), } case GitHub: return CI{ @@ -182,7 +182,7 @@ func GetCI(provider Provider) CI { 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")), + SHA: os.Getenv("GITHUB_SHA"), } case GitLab: return CI{ From cd61977c0c61040961cd856ede834ffc4b6f5fb2 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 12:52:52 -0800 Subject: [PATCH 08/10] update Azure CI identifier --- internal/ci/ci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 8e9838dc1..4c0ffdac9 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -28,7 +28,7 @@ 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: "AGENT_ID"} // Bamboo represents https://www.atlassian.com/software/bamboo Bamboo = Provider{Name: "Bamboo", Envar: "bamboo_buildNumber"} // Bitbucket represents https://bitbucket.org/product/features/pipelines From 10e3e48815d4790a37f495e2b79640ba67c9efa6 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 13:18:16 -0800 Subject: [PATCH 09/10] update Bamboo CI identifier --- internal/ci/ci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 4c0ffdac9..6ce46cd64 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -30,7 +30,7 @@ var ( // Azure represents https://azure.microsoft.com/en-us/services/devops/ Azure = Provider{Name: "Azure DevOps", Envar: "AGENT_ID"} // 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/ From e507976a5fb4831b3e9a362a7cd3b85995ffad76 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 27 Nov 2024 16:46:08 -0800 Subject: [PATCH 10/10] update Azure identifier --- internal/ci/ci.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 6ce46cd64..1195fcae4 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -28,7 +28,7 @@ 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_ID"} + Azure = Provider{Name: "Azure DevOps", Envar: "TF_BUILD"} // Bamboo represents https://www.atlassian.com/software/bamboo Bamboo = Provider{Name: "Bamboo", Envar: "bamboo_agentId"} // Bitbucket represents https://bitbucket.org/product/features/pipelines