2021-06-09 09:38:52 +02:00
|
|
|
package orchestrator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Orchestrator int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown Orchestrator = iota
|
|
|
|
AzureDevOps
|
|
|
|
GitHubActions
|
|
|
|
Jenkins
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrchestratorSpecificConfigProviding interface {
|
2021-06-25 10:50:56 +02:00
|
|
|
GetBranch() string
|
|
|
|
GetBuildUrl() string
|
|
|
|
GetCommit() string
|
2021-06-09 09:38:52 +02:00
|
|
|
GetPullRequestConfig() PullRequestConfig
|
2021-06-25 10:50:56 +02:00
|
|
|
GetRepoUrl() string
|
2021-06-09 09:38:52 +02:00
|
|
|
IsPullRequest() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type PullRequestConfig struct {
|
|
|
|
Branch string
|
|
|
|
Base string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrchestratorSpecificConfigProvider() (OrchestratorSpecificConfigProviding, error) {
|
|
|
|
switch DetectOrchestrator() {
|
|
|
|
case AzureDevOps:
|
|
|
|
return &AzureDevOpsConfigProvider{}, nil
|
|
|
|
case GitHubActions:
|
|
|
|
return &GitHubActionsConfigProvider{}, nil
|
|
|
|
case Jenkins:
|
|
|
|
return &JenkinsConfigProvider{}, nil
|
|
|
|
case Unknown:
|
|
|
|
fallthrough
|
|
|
|
default:
|
2021-06-25 10:50:56 +02:00
|
|
|
return nil, errors.New("unable to detect a supported orchestrator (Azure DevOps, GitHub Actions, Jenkins)")
|
2021-06-09 09:38:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DetectOrchestrator() Orchestrator {
|
|
|
|
if isAzure() {
|
|
|
|
return Orchestrator(AzureDevOps)
|
|
|
|
} else if isGitHubActions() {
|
|
|
|
return Orchestrator(GitHubActions)
|
|
|
|
} else if isJenkins() {
|
|
|
|
return Orchestrator(Jenkins)
|
|
|
|
} else {
|
|
|
|
return Orchestrator(Unknown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o Orchestrator) String() string {
|
2021-06-25 10:50:56 +02:00
|
|
|
return [...]string{"Unknown", "AzureDevOps", "GitHubActions", "Jenkins"}[o]
|
2021-06-09 09:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func areIndicatingEnvVarsSet(envVars []string) bool {
|
|
|
|
for _, v := range envVars {
|
2021-06-10 23:47:38 +02:00
|
|
|
if truthy(v) {
|
|
|
|
return true
|
|
|
|
}
|
2021-06-09 09:38:52 +02:00
|
|
|
}
|
2021-06-10 23:47:38 +02:00
|
|
|
return false
|
2021-06-09 09:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|