Skip to content

Commit d6a4267

Browse files
committed
close #203 bootstrap ini file if it doesn t exist
1 parent 7975c02 commit d6a4267

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

.phlow

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
service = http://jira.teamsinspace.com:8080
1313
issue_url = jira
1414
pipeline_url = http://concourse.bosh.praqma.cloud/teams/main/pipelines/git-phlow
15-
delivery_branch_prefix = ready
15+
delivery_branch_prefix = ready

plugins/gh.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ func AuthenticateGitHub(githubBaseURL string, user, token string) (bool, error)
111111
return true, nil
112112
}
113113

114+
//DefaultBranchGitHub ...
115+
//return the default branch of the repository
116+
func DefaultBranchGitHub(URL, org, repo, token string) (defaultBranch string, err error) {
117+
118+
req, _ := http.NewRequest("GET", URL+fmt.Sprintf("/repos/%s/%s", org, repo), nil)
119+
q := req.URL.Query()
120+
q.Add("access_token", token)
121+
req.URL.RawQuery = q.Encode()
122+
123+
client := http.DefaultClient
124+
125+
res, err := client.Do(req)
126+
if err != nil {
127+
return "", err
128+
}
129+
defer res.Body.Close()
130+
131+
re := Repo{}
132+
err = json.NewDecoder(res.Body).Decode(&re)
133+
if err != nil {
134+
return "", err
135+
}
136+
return re.DefaultBranch, nil
137+
}
138+
114139
//GetIssues ...
115140
func (g *GitHubImpl) GetIssues() (issues []Issue, err error) {
116141
URL := fmt.Sprintf(g.URLNoEsc(urls.issueURL), g.org, g.repo)
@@ -154,6 +179,7 @@ func (g *GitHubImpl) SetLabel(label string, issue int) (labels []Label, err erro
154179
return re, nil
155180
}
156181

182+
//Deprecated
157183
//Default ...
158184
//Get default branch of a GitHub issue
159185
func (g *GitHubImpl) Default() (defaultBranch string, err error) {

plugins/plugin.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ type Authorization func(URL, user, pass string) (token string, err error)
1515
//interface for authentication with external service
1616
type Authentication func(URL, user, pass string) (authenticated bool, err error)
1717

18-
19-
20-
21-
type DefaultBranch func() (defaultBranch string, err error)
18+
//DefaultBranch ...
19+
//interface for getting the default branch of the external service
20+
type DefaultBranch func(URL, org, repo, token string) (defaultBranch string, err error)
2221

2322
//PhlowLabels ...
2423
//Map of labels in the phlow

setting/setting.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"fmt"
99
"reflect"
1010
"github.com/go-errors/errors"
11+
"github.com/praqma/git-phlow/plugins"
12+
"github.com/praqma/git-phlow/githandler"
1113
)
1214

1315
//Load internals
@@ -107,10 +109,23 @@ func LoadProjectSettings(local, global string, INIBlock string) *ProjectSetting
107109
}
108110
os.Exit(1)
109111
}
112+
113+
//Try to get default branch otherwise just create the default
114+
defaultBranch, err := GetDefaultBranchFromInternalDefault()
115+
if err != nil {
116+
defaultBranch = internal_default_integration_branch
117+
}
118+
119+
err = BootstrapPhlowConfig(local, defaultBranch)
120+
if err != nil {
121+
fmt.Println("Could not create a local .phlow config file")
122+
os.Exit(1)
123+
}
124+
110125
//return internal default because no other configuration exist and no other is specified by params
111126
return &ProjectSetting{
112127
Service: internal_default_service,
113-
IntegrationBranch: internal_default_integration_branch,
128+
IntegrationBranch: defaultBranch,
114129
Remote: internal_default_remote,
115130
IssueURL: internal_default_issue_url,
116131
DeliveryBranchPrefix: internal_default_delivery_branch_prefix,
@@ -136,6 +151,26 @@ func LoadProjectSettings(local, global string, INIBlock string) *ProjectSetting
136151
return conf
137152
}
138153

154+
//BootstrapPhlowConfig ...
155+
//Creates a new .phlow ini file on given location
156+
func BootstrapPhlowConfig(local, integrationBranch string) error {
157+
fmt.Println("No .phlow config found")
158+
cfg := ini.Empty()
159+
sec, _ := cfg.NewSection("default")
160+
sec.Key("remote").SetValue(internal_default_remote)
161+
sec.Key("service").SetValue(internal_default_service)
162+
sec.Key("integration_branch").SetValue(integrationBranch)
163+
sec.Key("issue_url").SetValue(internal_default_issue_url)
164+
sec.Key("delivery_branch_prefix").SetValue(internal_default_delivery_branch_prefix)
165+
166+
err := cfg.SaveTo(local + "/" + ".phlow")
167+
if err != nil {
168+
return err
169+
}
170+
fmt.Println("Bootstrapping new .phlow file")
171+
return nil
172+
}
173+
139174
//ValidateLoadedSetting ...
140175
func ValidateLoadedSetting(setting *ProjectSetting) (error) {
141176
r := reflect.ValueOf(setting).Elem()
@@ -203,3 +238,22 @@ func GetLocal() string {
203238
}
204239
return strings.TrimSpace(absoluteRepoPath)
205240
}
241+
242+
//GetDefaultBranchFromInternalDefault ...
243+
//Trying to retrieve the default branch from github
244+
func GetDefaultBranchFromInternalDefault() (string, error) {
245+
git := githandler.Git{Run: executor.RunGit}
246+
247+
remote, err := git.LSRemote("--get-url", internal_default_remote)
248+
if err != nil {
249+
return "", err
250+
}
251+
orgAndRepo := githandler.OrgAndRepo(remote)
252+
token, err := git.Config("--get", "phlow.token")
253+
254+
branch, err := plugins.DefaultBranchGitHub(internal_default_issue_url, orgAndRepo.Organisation, orgAndRepo.Repository, token)
255+
if err != nil {
256+
return "", err
257+
}
258+
return branch, nil
259+
}

0 commit comments

Comments
 (0)