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 ...
140175func 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