Skip to content

Commit fd83fda

Browse files
authored
Fix: plugin github-repo-scaffolding-golang branch doesn't take effect. (#239)
Signed-off-by: Daniel Hu <[email protected]>
1 parent cacf5ad commit fd83fda

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

internal/pkg/plugin/reposcaffolding/github/golang/golang.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919
DefaultTemplateRepo = "dtm-scaffolding-golang"
2020
DefaultTemplateOwner = "merico-dev"
2121
TransitBranch = "init-with-devstream"
22-
MainBranch = "main"
22+
DefaultMainBranch = "main"
2323
)
2424

2525
type Config struct {
@@ -44,11 +44,14 @@ func InitRepoLocalAndPushToRemote(repoPath string, param *Param, ghClient *githu
4444
return err
4545
}
4646

47-
return MergeCommits(ghClient)
47+
mainBranch := getMainBranchName(param)
48+
return MergeCommits(ghClient, mainBranch)
4849
}
4950

5051
func WalkLocalRepoPath(repoPath string, param *Param, ghClient *github.Client) error {
5152
appName := param.Repo
53+
mainBranch := getMainBranchName(param)
54+
5255
if err := filepath.Walk(repoPath, func(path string, info fs.FileInfo, err error) error {
5356
if err != nil {
5457
log.Debugf("Walk error: %s.", err)
@@ -95,13 +98,13 @@ func WalkLocalRepoPath(repoPath string, param *Param, ghClient *github.Client) e
9598
} else {
9699
// the main branch needs a initial commit
97100
if strings.Contains(newPathForGithub, "gitignore") {
98-
err := ghClient.CreateFile(content, strings.TrimSuffix(newPathForGithub, ".tpl"), MainBranch)
101+
err := ghClient.CreateFile(content, strings.TrimSuffix(newPathForGithub, ".tpl"), mainBranch)
99102
if err != nil {
100103
log.Debugf("Failed to add the .gitignore file.")
101104
return err
102105
}
103106
log.Debugf("Added the .gitignore file.")
104-
return ghClient.NewBranch(MainBranch, TransitBranch)
107+
return ghClient.NewBranch(mainBranch, TransitBranch)
105108
}
106109
return ghClient.CreateFile(content, strings.TrimSuffix(newPathForGithub, ".tpl"), TransitBranch)
107110
}
@@ -112,8 +115,8 @@ func WalkLocalRepoPath(repoPath string, param *Param, ghClient *github.Client) e
112115
return nil
113116
}
114117

115-
func MergeCommits(ghClient *github.Client) error {
116-
number, err := ghClient.NewPullRequest(TransitBranch, MainBranch)
118+
func MergeCommits(ghClient *github.Client, mainBranch string) error {
119+
number, err := ghClient.NewPullRequest(TransitBranch, mainBranch)
117120
if err != nil {
118121
return err
119122
}
@@ -186,3 +189,10 @@ func buildState(param *Param) map[string]interface{} {
186189
res["repoName"] = param.Repo
187190
return res
188191
}
192+
193+
func getMainBranchName(param *Param) string {
194+
if param.Branch == "" {
195+
return DefaultMainBranch
196+
}
197+
return param.Branch
198+
}

pkg/util/github/github.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ func NewClient(option *Option) (*Client, error) {
4141
return client, nil
4242
}
4343

44+
var retErr error
4445
defer func() {
46+
if retErr != nil {
47+
return
48+
}
4549
if client.Option.WorkPath == "" {
4650
client.Option.WorkPath = DefaultWorkPath
4751
}
@@ -71,8 +75,9 @@ func NewClient(option *Option) (*Client, error) {
7175
token = os.Getenv("github_token")
7276
}
7377
if token == "" {
74-
return nil, fmt.Errorf("failed to initialize GitHub token. More info - " +
78+
retErr = fmt.Errorf("failed to initialize GitHub token. More info - " +
7579
"https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token")
80+
return nil, retErr
7681
}
7782
log.Debugf("Token: %s.", token)
7883

0 commit comments

Comments
 (0)