Files

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

package ci
import (
"fmt"
2019-07-16 20:42:40 +02:00
"os"
"strings"
2019-09-15 20:13:17 +02:00
"github.com/Nightapes/go-semantic-release/internal/gitutil"
log "github.com/sirupsen/logrus"
)
//ProviderConfig struct
type ProviderConfig struct {
IsPR bool
PR string
PRBranch string
Branch string
Tag string
Commit string
BuildURL string
Service string
Name string
}
//Service interface
type Service interface {
2019-07-16 20:42:40 +02:00
detect(envs map[string]string) (*ProviderConfig, error)
}
//ReadAllEnvs as a map
func ReadAllEnvs() map[string]string {
envs := map[string]string{}
for _, pair := range os.Environ() {
splitted := strings.SplitN(pair, "=", 2)
envs[splitted[0]] = splitted[1]
}
return envs
}
//GetCIProvider get provider
2019-07-16 20:42:40 +02:00
func GetCIProvider(gitUtil *gitutil.GitUtil, envs map[string]string) (*ProviderConfig, error) {
services := []Service{
Travis{},
2019-08-21 21:39:05 +02:00
GithubActions{},
2019-09-15 20:13:17 +02:00
GitlabCI{},
Git{gitUtil: gitUtil}, // GIt must be the last option to check
}
for _, service := range services {
2019-07-16 20:42:40 +02:00
config, err := service.detect(envs)
if err == nil {
log.Infof("Found CI: %s", config.Name)
2019-09-03 22:18:13 +02:00
log.Tracef("Found CI config: %+v", config)
return config, nil
}
log.Debugf("%s", err.Error())
}
return nil, fmt.Errorf("could not find any CI, if running locally set env CI=true")
}