mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-18 05:18:24 +02:00
feat: Add environment information to orchestrator package (#2942)
* Fix Orchestrator detection * Add unit tests * Add environment info to orchestrator package
This commit is contained in:
parent
a48b8afc31
commit
b5357f9437
@ -439,11 +439,10 @@ func detectParametersFromCI(options *sonarExecuteScanOptions) {
|
||||
options.ChangeID = config.Key
|
||||
}
|
||||
} else {
|
||||
config := provider.GetBranchBuildConfig()
|
||||
|
||||
branch := provider.GetBranch()
|
||||
if options.InferBranchName && len(options.BranchName) == 0 {
|
||||
log.Entry().Info("Inferring parameter branchName from environment: " + config.Branch)
|
||||
options.BranchName = config.Branch
|
||||
log.Entry().Info("Inferring parameter branchName from environment: " + branch)
|
||||
options.BranchName = branch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,23 @@ import (
|
||||
|
||||
type AzureDevOpsConfigProvider struct{}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) GetBranch() string {
|
||||
tmp := os.Getenv("BUILD_SOURCEBRANCH")
|
||||
return strings.TrimPrefix(tmp, "refs/heads/")
|
||||
}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) GetBuildUrl() string {
|
||||
return os.Getenv("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI") + os.Getenv("SYSTEM_TEAMPROJECT") + "/_build/results?buildId=" + os.Getenv("BUILD_BUILDID")
|
||||
}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) GetCommit() string {
|
||||
return os.Getenv("BUILD_SOURCEVERSION")
|
||||
}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) GetRepoUrl() string {
|
||||
return os.Getenv("BUILD_REPOSITORY_URI")
|
||||
}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
return PullRequestConfig{
|
||||
Branch: os.Getenv("SYSTEM_PULLREQUEST_SOURCEBRANCH"),
|
||||
@ -15,12 +32,6 @@ func (a *AzureDevOpsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) GetBranchBuildConfig() BranchBuildConfig {
|
||||
tmp := os.Getenv("BUILD_SOURCEBRANCH")
|
||||
trimmed := strings.TrimPrefix(tmp, "refs/heads/")
|
||||
return BranchBuildConfig{Branch: trimmed}
|
||||
}
|
||||
|
||||
func (a *AzureDevOpsConfigProvider) IsPullRequest() bool {
|
||||
return os.Getenv("BUILD_REASON") == "PullRequest"
|
||||
}
|
||||
|
@ -11,15 +11,21 @@ func TestAzure(t *testing.T) {
|
||||
t.Run("Azure - BranchBuild", func(t *testing.T) {
|
||||
defer resetEnv(os.Environ())
|
||||
os.Clearenv()
|
||||
os.Setenv("BUILD_SOURCEBRANCH", "refs/heads/feat/test-azure")
|
||||
os.Setenv("AZURE_HTTP_USER_AGENT", "FOO BAR BAZ")
|
||||
os.Setenv("BUILD_REASON", "pogo")
|
||||
os.Setenv("BUILD_SOURCEBRANCH", "refs/heads/feat/test-azure")
|
||||
os.Setenv("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", "https://pogo.foo")
|
||||
os.Setenv("SYSTEM_TEAMPROJECT", "bar")
|
||||
os.Setenv("BUILD_BUILDID", "42")
|
||||
os.Setenv("BUILD_SOURCEVERSION", "abcdef42713")
|
||||
os.Setenv("BUILD_REPOSITORY_URI", "github.com/foo/bar")
|
||||
|
||||
p, _ := NewOrchestratorSpecificConfigProvider()
|
||||
c := p.GetBranchBuildConfig()
|
||||
|
||||
assert.False(t, p.IsPullRequest())
|
||||
assert.Equal(t, "feat/test-azure", c.Branch)
|
||||
assert.Equal(t, "feat/test-azure", p.GetBranch())
|
||||
assert.Equal(t, "https://pogo.foobar/_build/results?buildId=42", p.GetBuildUrl())
|
||||
assert.Equal(t, "abcdef42713", p.GetCommit())
|
||||
assert.Equal(t, "github.com/foo/bar", p.GetRepoUrl())
|
||||
})
|
||||
|
||||
t.Run("PR", func(t *testing.T) {
|
||||
|
@ -1,14 +1,29 @@
|
||||
package orchestrator
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GitHubActionsConfigProvider struct{}
|
||||
|
||||
func (a *GitHubActionsConfigProvider) GetBranchBuildConfig() BranchBuildConfig {
|
||||
return BranchBuildConfig{Branch: os.Getenv("GITHUB_REF")}
|
||||
func (g *GitHubActionsConfigProvider) GetBranch() string {
|
||||
return strings.TrimPrefix(os.Getenv("GITHUB_REF"), "refs/heads/")
|
||||
}
|
||||
|
||||
func (a *GitHubActionsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
func (g *GitHubActionsConfigProvider) GetBuildUrl() string {
|
||||
return g.GetRepoUrl() + "/actions/runs/" + os.Getenv("GITHUB_RUN_ID")
|
||||
}
|
||||
|
||||
func (g *GitHubActionsConfigProvider) GetCommit() string {
|
||||
return os.Getenv("GITHUB_SHA")
|
||||
}
|
||||
|
||||
func (g *GitHubActionsConfigProvider) GetRepoUrl() string {
|
||||
return os.Getenv("GITHUB_SERVER_URL") + os.Getenv("GITHUB_REPOSITORY")
|
||||
}
|
||||
|
||||
func (g *GitHubActionsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
return PullRequestConfig{
|
||||
Branch: os.Getenv("GITHUB_HEAD_REF"),
|
||||
Base: os.Getenv("GITHUB_BASE_REF"),
|
||||
@ -16,7 +31,7 @@ func (a *GitHubActionsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *GitHubActionsConfigProvider) IsPullRequest() bool {
|
||||
func (g *GitHubActionsConfigProvider) IsPullRequest() bool {
|
||||
_, exists := os.LookupEnv("GITHUB_HEAD_REF")
|
||||
return exists
|
||||
}
|
||||
|
@ -11,15 +11,21 @@ func TestGitHubActions(t *testing.T) {
|
||||
t.Run("BranchBuild", func(t *testing.T) {
|
||||
defer resetEnv(os.Environ())
|
||||
os.Clearenv()
|
||||
os.Setenv("GITHUB_ACTIONS", "true")
|
||||
os.Setenv("GITHUB_REF", "main")
|
||||
os.Unsetenv("GITHUB_HEAD_REF")
|
||||
os.Setenv("GITHUB_ACTIONS", "true")
|
||||
os.Setenv("GITHUB_REF", "refs/heads/feat/test-gh-actions")
|
||||
os.Setenv("GITHUB_RUN_ID", "42")
|
||||
os.Setenv("GITHUB_SHA", "abcdef42713")
|
||||
os.Setenv("GITHUB_SERVER_URL", "github.com/")
|
||||
os.Setenv("GITHUB_REPOSITORY", "foo/bar")
|
||||
|
||||
p, _ := NewOrchestratorSpecificConfigProvider()
|
||||
c := p.GetBranchBuildConfig()
|
||||
|
||||
assert.False(t, p.IsPullRequest())
|
||||
assert.Equal(t, "main", c.Branch)
|
||||
assert.Equal(t, "github.com/foo/bar/actions/runs/42", p.GetBuildUrl())
|
||||
assert.Equal(t, "feat/test-gh-actions", p.GetBranch())
|
||||
assert.Equal(t, "abcdef42713", p.GetCommit())
|
||||
assert.Equal(t, "github.com/foo/bar", p.GetRepoUrl())
|
||||
})
|
||||
|
||||
t.Run("PR", func(t *testing.T) {
|
||||
|
@ -6,11 +6,23 @@ import (
|
||||
|
||||
type JenkinsConfigProvider struct{}
|
||||
|
||||
func (a *JenkinsConfigProvider) GetBranchBuildConfig() BranchBuildConfig {
|
||||
return BranchBuildConfig{Branch: os.Getenv("BRANCH_NAME")}
|
||||
func (j *JenkinsConfigProvider) GetBranch() string {
|
||||
return os.Getenv("GIT_BRANCH")
|
||||
}
|
||||
|
||||
func (a *JenkinsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
func (j *JenkinsConfigProvider) GetBuildUrl() string {
|
||||
return os.Getenv("BUILD_URL")
|
||||
}
|
||||
|
||||
func (j *JenkinsConfigProvider) GetCommit() string {
|
||||
return os.Getenv("GIT_COMMIT")
|
||||
}
|
||||
|
||||
func (j *JenkinsConfigProvider) GetRepoUrl() string {
|
||||
return os.Getenv("GIT_URL")
|
||||
}
|
||||
|
||||
func (j *JenkinsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
return PullRequestConfig{
|
||||
Branch: os.Getenv("CHANGE_BRANCH"),
|
||||
Base: os.Getenv("CHANGE_TARGET"),
|
||||
@ -18,7 +30,7 @@ func (a *JenkinsConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *JenkinsConfigProvider) IsPullRequest() bool {
|
||||
func (j *JenkinsConfigProvider) IsPullRequest() bool {
|
||||
return truthy("CHANGE_ID")
|
||||
}
|
||||
|
||||
|
@ -11,14 +11,19 @@ func TestJenkins(t *testing.T) {
|
||||
t.Run("BranchBuild", func(t *testing.T) {
|
||||
defer resetEnv(os.Environ())
|
||||
os.Clearenv()
|
||||
os.Setenv("JENKINS_URL", "https://foo.bar/baz")
|
||||
os.Setenv("BRANCH_NAME", "feat/test-jenkins")
|
||||
os.Setenv("JENKINS_URL", "FOO BAR BAZ")
|
||||
os.Setenv("BUILD_URL", "jaas.com/foo/bar/main/42")
|
||||
os.Setenv("GIT_BRANCH", "main")
|
||||
os.Setenv("GIT_COMMIT", "abcdef42713")
|
||||
os.Setenv("GIT_URL", "github.com/foo/bar")
|
||||
|
||||
p, _ := NewOrchestratorSpecificConfigProvider()
|
||||
c := p.GetBranchBuildConfig()
|
||||
|
||||
assert.False(t, p.IsPullRequest())
|
||||
assert.Equal(t, "feat/test-jenkins", c.Branch)
|
||||
assert.Equal(t, "jaas.com/foo/bar/main/42", p.GetBuildUrl())
|
||||
assert.Equal(t, "main", p.GetBranch())
|
||||
assert.Equal(t, "abcdef42713", p.GetCommit())
|
||||
assert.Equal(t, "github.com/foo/bar", p.GetRepoUrl())
|
||||
})
|
||||
|
||||
t.Run("PR", func(t *testing.T) {
|
||||
|
@ -12,12 +12,14 @@ const (
|
||||
AzureDevOps
|
||||
GitHubActions
|
||||
Jenkins
|
||||
Travis
|
||||
)
|
||||
|
||||
type OrchestratorSpecificConfigProviding interface {
|
||||
GetBranchBuildConfig() BranchBuildConfig
|
||||
GetBranch() string
|
||||
GetBuildUrl() string
|
||||
GetCommit() string
|
||||
GetPullRequestConfig() PullRequestConfig
|
||||
GetRepoUrl() string
|
||||
IsPullRequest() bool
|
||||
}
|
||||
|
||||
@ -27,10 +29,6 @@ type PullRequestConfig struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
type BranchBuildConfig struct {
|
||||
Branch string
|
||||
}
|
||||
|
||||
func NewOrchestratorSpecificConfigProvider() (OrchestratorSpecificConfigProviding, error) {
|
||||
switch DetectOrchestrator() {
|
||||
case AzureDevOps:
|
||||
@ -39,12 +37,10 @@ func NewOrchestratorSpecificConfigProvider() (OrchestratorSpecificConfigProvidin
|
||||
return &GitHubActionsConfigProvider{}, nil
|
||||
case Jenkins:
|
||||
return &JenkinsConfigProvider{}, nil
|
||||
case Travis:
|
||||
return &TravisConfigProvider{}, nil
|
||||
case Unknown:
|
||||
fallthrough
|
||||
default:
|
||||
return nil, errors.New("unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins, Travis)")
|
||||
return nil, errors.New("unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins)")
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,15 +51,13 @@ func DetectOrchestrator() Orchestrator {
|
||||
return Orchestrator(GitHubActions)
|
||||
} else if isJenkins() {
|
||||
return Orchestrator(Jenkins)
|
||||
} else if isTravis() {
|
||||
return Orchestrator(Travis)
|
||||
} else {
|
||||
return Orchestrator(Unknown)
|
||||
}
|
||||
}
|
||||
|
||||
func (o Orchestrator) String() string {
|
||||
return [...]string{"Unknown", "AzureDevOps", "GitHubActions", "Travis", "Jenkins"}[o]
|
||||
return [...]string{"Unknown", "AzureDevOps", "GitHubActions", "Jenkins"}[o]
|
||||
}
|
||||
|
||||
func areIndicatingEnvVarsSet(envVars []string) bool {
|
||||
|
@ -14,7 +14,7 @@ func TestOrchestrator(t *testing.T) {
|
||||
|
||||
_, err := NewOrchestratorSpecificConfigProvider()
|
||||
|
||||
assert.EqualError(t, err, "unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins, Travis)")
|
||||
assert.EqualError(t, err, "unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins)")
|
||||
})
|
||||
|
||||
t.Run("Test orchestrator.toString()", func(t *testing.T) {
|
||||
|
@ -1,26 +0,0 @@
|
||||
package orchestrator
|
||||
|
||||
import "os"
|
||||
|
||||
type TravisConfigProvider struct{}
|
||||
|
||||
func (a *TravisConfigProvider) GetBranchBuildConfig() BranchBuildConfig {
|
||||
return BranchBuildConfig{Branch: os.Getenv("TRAVIS_BRANCH")}
|
||||
}
|
||||
|
||||
func (a *TravisConfigProvider) GetPullRequestConfig() PullRequestConfig {
|
||||
return PullRequestConfig{
|
||||
Branch: os.Getenv("TRAVIS_PULL_REQUEST_BRANCH"),
|
||||
Base: os.Getenv("TRAVIS_BRANCH"),
|
||||
Key: os.Getenv("TRAVIS_PULL_REQUEST"),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *TravisConfigProvider) IsPullRequest() bool {
|
||||
return truthy("TRAVIS_PULL_REQUEST")
|
||||
}
|
||||
|
||||
func isTravis() bool {
|
||||
envVars := []string{"TRAVIS"}
|
||||
return areIndicatingEnvVarsSet(envVars)
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package orchestrator
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTravis(t *testing.T) {
|
||||
t.Run("BranchBuild", func(t *testing.T) {
|
||||
defer resetEnv(os.Environ())
|
||||
os.Clearenv()
|
||||
os.Setenv("TRAVIS", "true")
|
||||
os.Setenv("TRAVIS_BRANCH", "feat/test-travis")
|
||||
os.Setenv("TRAVIS_PULL_REQUEST", "false")
|
||||
|
||||
p, _ := NewOrchestratorSpecificConfigProvider()
|
||||
c := p.GetBranchBuildConfig()
|
||||
|
||||
assert.False(t, p.IsPullRequest())
|
||||
assert.Equal(t, "feat/test-travis", c.Branch)
|
||||
})
|
||||
|
||||
t.Run("PR", func(t *testing.T) {
|
||||
defer resetEnv(os.Environ())
|
||||
os.Clearenv()
|
||||
os.Setenv("TRAVIS_PULL_REQUEST_BRANCH", "feat/test-travis")
|
||||
os.Setenv("TRAVIS_BRANCH", "main")
|
||||
os.Setenv("TRAVIS_PULL_REQUEST", "42")
|
||||
|
||||
p := TravisConfigProvider{}
|
||||
c := p.GetPullRequestConfig()
|
||||
|
||||
assert.True(t, p.IsPullRequest())
|
||||
assert.Equal(t, "feat/test-travis", c.Branch)
|
||||
assert.Equal(t, "main", c.Base)
|
||||
assert.Equal(t, "42", c.Key)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user