1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/orchestrator/orchestrator.go
Marc Bormeth 911d4bc770
feat(sonar): make step orchestrator-agnostic (#2874)
* Make sonarExecuteScan orchestrator-agnostic

* Increase coverage + support empty or false env vars

* Use cleared env for unit tests

* Refactor to standalone package

* Fix review findings

* Fix review findings

* Fix unit test

* Add logging

* Refactor

* Add to codeowners 😎

* Apply suggestions from code review

* Remove unreachable code

* no message

* fix typos

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2021-06-09 09:38:52 +02:00

89 lines
1.8 KiB
Go

package orchestrator
import (
"errors"
"os"
)
type Orchestrator int
const (
Unknown Orchestrator = iota
AzureDevOps
GitHubActions
Jenkins
Travis
)
type OrchestratorSpecificConfigProviding interface {
GetBranchBuildConfig() BranchBuildConfig
GetPullRequestConfig() PullRequestConfig
IsPullRequest() bool
}
type PullRequestConfig struct {
Branch string
Base string
Key string
}
type BranchBuildConfig struct {
Branch string
}
func NewOrchestratorSpecificConfigProvider() (OrchestratorSpecificConfigProviding, error) {
switch DetectOrchestrator() {
case AzureDevOps:
return &AzureDevOpsConfigProvider{}, nil
case GitHubActions:
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)")
}
}
func DetectOrchestrator() Orchestrator {
if isAzure() {
return Orchestrator(AzureDevOps)
} else if isGitHubActions() {
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]
}
func areIndicatingEnvVarsSet(envVars []string) bool {
found := false
for _, v := range envVars {
found = truthy(v)
}
return found
}
// Checks if var is set and neither empty nor false
func truthy(key string) bool {
val, exists := os.LookupEnv(key)
if !exists {
return false
}
if len(val) == 0 || val == "no" || val == "false" || val == "off" || val == "0" {
return false
}
return true
}