Skip to content

Commit bffcafd

Browse files
derokemeinto
authored andcommitted
Auto detect cicdOrigin (#12)
Auto detect cicdOrigin
1 parent 0b7ebe4 commit bffcafd

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

gitprovider/github_adapter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,7 @@ func (a *githubAdapter) createPullRequest(source glow.Branch, target glow.Branch
9292
func (a *githubAdapter) GetCIBranch() (glow.Branch, error) {
9393
return nil, errors.New("not implemented")
9494
}
95+
96+
func (a *githubAdapter) DetectCICDOrigin() (string, error) {
97+
return "", errors.New("not implemented")
98+
}

gitprovider/gitlab_adapter.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,26 @@ func (a *gitlabAdapter) GetCIBranch() (glow.Branch, error) {
9898
branch, err := glow.NewBranch(branchName)
9999
return branch, errors.Wrap(err, "error get ci branch")
100100
}
101+
102+
func (a *gitlabAdapter) DetectCICDOrigin() (string, error) {
103+
gitProviderURL := a.endpoint
104+
gitUser := os.Getenv("CI_GIT_USER")
105+
gitToken := os.Getenv("CI_GIT_TOKEN")
106+
107+
if gitUser != "" && gitToken != "" {
108+
endpointURL, err := url.Parse(a.endpoint)
109+
if err != nil {
110+
return "", errors.Wrap(err, "couldn't parse gitLab endpoint")
111+
}
112+
113+
gitProviderURL = fmt.Sprintf(
114+
"%s://%s:%s@%s",
115+
endpointURL.Scheme, gitUser, gitToken, endpointURL.Host,
116+
)
117+
}
118+
119+
return fmt.Sprintf(
120+
"%s/%s/%s.git",
121+
gitProviderURL, a.namespace, a.project,
122+
), nil
123+
}

gitprovider/logging.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ func (s *loggingService) GetCIBranch() (branch glow.Branch, err error) {
4343
}()
4444
return s.next.GetCIBranch()
4545
}
46+
47+
func (s *loggingService) DetectCICDOrigin() (cicdOrigin string, err error) {
48+
defer func() {
49+
l.Log().WithFields(logrus.Fields{
50+
"cicdOrigin": cicdOrigin,
51+
"error": err,
52+
}).Info()
53+
}()
54+
return s.next.DetectCICDOrigin()
55+
}

gitprovider/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type Service interface {
1212
Close(b glow.Branch) error
1313
Publish(b glow.Branch) error
14+
DetectCICDOrigin() (string, error)
1415
GetCIBranch() (glow.Branch, error)
1516
}
1617

pkg/cli/cmd/root.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
)
1414

1515
var rootCmdOptions struct {
16-
Author string
17-
GitPath string
18-
CICDOrigin string
19-
CI bool
20-
SkipChecks bool
16+
Author string
17+
GitPath string
18+
CICDOrigin string
19+
DetectCICDOrigin bool
20+
CI bool
21+
SkipChecks bool
2122
}
2223

2324
var logger kitlog.Logger
@@ -32,6 +33,17 @@ var rootCmd = &cobra.Command{
3233
util.ExitOnError(err)
3334

3435
util.ExitOnError(g.SetCICDOrigin(rootCmdOptions.CICDOrigin))
36+
} else if rootCmdOptions.DetectCICDOrigin {
37+
g, err := util.GetGitClient()
38+
util.ExitOnError(err)
39+
40+
gp, err := util.GetGitProvider()
41+
util.ExitOnError(err)
42+
43+
cicdOrigin, err := gp.DetectCICDOrigin()
44+
util.ExitOnError(err)
45+
46+
util.ExitOnError(g.SetCICDOrigin(cicdOrigin))
3547
}
3648
},
3749
}
@@ -48,6 +60,7 @@ func init() {
4860
rootCmd.PersistentFlags().StringVarP(&rootCmdOptions.Author, "author", "a", "", "name of the author")
4961
rootCmd.PersistentFlags().StringVar(&rootCmdOptions.GitPath, "gitPath", "/usr/local/bin/git", "path to native git installation")
5062
rootCmd.PersistentFlags().StringVar(&rootCmdOptions.CICDOrigin, "cicdOrigin", "", "provide a git origin url where a pipeline can push things via token")
63+
rootCmd.PersistentFlags().BoolVar(&rootCmdOptions.DetectCICDOrigin, "detectCicdOrigin", false, "auto detect a git origin url where a pipeline can push things via token")
5164
rootCmd.PersistentFlags().BoolVar(&rootCmdOptions.CI, "ci", false, "detects if command is running in a ci")
5265
rootCmd.PersistentFlags().BoolVar(&rootCmdOptions.SkipChecks, "skipChecks", false, "skip checks like accidentally creating git-flow branches from wrong source branch")
5366
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))

0 commit comments

Comments
 (0)