Skip to content

Commit 64ae048

Browse files
authored
Merge pull request #27 from gomicro/custom-release-branch
Allow setting base branch from config file
2 parents 00f0256 + 31f5ada commit 64ae048

File tree

10 files changed

+36
-20
lines changed

10 files changed

+36
-20
lines changed

cmd/org/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func orgCreateFunc(cmd *cobra.Command, args []string) {
6161
owner = repo.GetOwner().GetLogin()
6262
appendStr = fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)
6363

64-
url, err := repositories.Process(ctx, client, repo, dryRun)
64+
url, err := repositories.Process(ctx, client, repo, base, dryRun)
6565
if err != nil {
6666
if strings.HasPrefix(err.Error(), "get branch: ") || strings.HasPrefix(err.Error(), "no commits") {
6767
bar.Incr()

cmd/org/org.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
var (
15+
base string
1516
dryRun bool
1617
client *github.Client
1718
)
@@ -24,12 +25,22 @@ var OrgCmd = &cobra.Command{
2425
}
2526

2627
func setupCommand(cmd *cobra.Command, args []string) {
27-
var err error
28-
client, err = config.GetClient()
28+
c, err := config.ParseFromFile()
2929
if err != nil {
3030
fmt.Printf("Error: %v", err.Error())
3131
os.Exit(1)
3232
}
3333

34+
client, err = c.GetClient()
35+
if err != nil {
36+
fmt.Printf("Error: %v", err.Error())
37+
os.Exit(1)
38+
}
39+
40+
base = "release"
41+
if c.ReleaseBranch != "" {
42+
base = c.ReleaseBranch
43+
}
44+
3445
dryRun = viper.GetBool("dryRun")
3546
}

cmd/org/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func orgReleaseFunc(cmd *cobra.Command, args []string) {
3636
return
3737
}
3838

39-
releases, err := repositories.GetReleases(ctx, client, repos)
39+
releases, err := repositories.GetReleases(ctx, client, repos, base)
4040
if err != nil {
4141
fmt.Printf("releases: %v\n", err.Error())
4242
os.Exit(1)

cmd/user/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func userCreateFunc(cmd *cobra.Command, args []string) {
6161
owner = repo.GetOwner().GetLogin()
6262
appendStr = fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)
6363

64-
url, err := repositories.Process(ctx, client, repo, dryRun)
64+
url, err := repositories.Process(ctx, client, repo, base, dryRun)
6565
if err != nil {
6666
if strings.HasPrefix(err.Error(), "get branch: ") || strings.HasPrefix(err.Error(), "no commits") {
6767
bar.Incr()

cmd/user/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func userReleaseFunc(cmd *cobra.Command, args []string) {
3636
return
3737
}
3838

39-
releases, err := repositories.GetReleases(ctx, client, repos)
39+
releases, err := repositories.GetReleases(ctx, client, repos, base)
4040
if err != nil {
4141
fmt.Printf("releases: %v\n", err.Error())
4242
os.Exit(1)

cmd/user/user.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
var (
15+
base string
1516
dryRun bool
1617
client *github.Client
1718
)
@@ -25,12 +26,22 @@ var UserCmd = &cobra.Command{
2526
}
2627

2728
func setupCommand(cmd *cobra.Command, args []string) {
28-
var err error
29-
client, err = config.GetClient()
29+
c, err := config.ParseFromFile()
3030
if err != nil {
3131
fmt.Printf("Error: %v", err.Error())
3232
os.Exit(1)
3333
}
3434

35+
client, err = c.GetClient()
36+
if err != nil {
37+
fmt.Printf("Error: %v", err.Error())
38+
os.Exit(1)
39+
}
40+
41+
base = "release"
42+
if c.ReleaseBranch != "" {
43+
base = c.ReleaseBranch
44+
}
45+
3546
dryRun = viper.GetBool("dryRun")
3647
}

config/client.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"golang.org/x/oauth2"
1212
)
1313

14-
func GetClient() (*github.Client, error) {
14+
func (c *Config) GetClient() (*github.Client, error) {
1515
pool := trust.New()
1616

1717
certs, err := pool.CACerts()
@@ -25,17 +25,12 @@ func GetClient() (*github.Client, error) {
2525
},
2626
}
2727

28-
config, err := ParseFromFile()
29-
if err != nil {
30-
return nil, err
31-
}
32-
3328
clientCtx := context.Background()
3429
clientCtx = context.WithValue(clientCtx, oauth2.HTTPClient, httpClient)
3530

3631
ts := oauth2.StaticTokenSource(
3732
&oauth2.Token{
38-
AccessToken: config.Github.Token,
33+
AccessToken: c.Github.Token,
3934
},
4035
)
4136

config/file.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const (
1515

1616
// Config represents the config file for train
1717
type Config struct {
18-
Github Host `yaml:"github.com"`
18+
ReleaseBranch string `yaml:"release_branck"`
19+
Github Host `yaml:"github.com"`
1920
}
2021

2122
// Host represents a single host that train has a configuration for

repositories/process.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ Release PR created with ` + "`train`"
1414

1515
// Process takes a github client, context for the client, and a repo to process.
1616
// It returns the resulting pull request URL or an error if any are encountered.
17-
func Process(ctx context.Context, client *github.Client, repo *github.Repository, dryRun bool) (string, error) {
17+
func Process(ctx context.Context, client *github.Client, repo *github.Repository, base string, dryRun bool) (string, error) {
1818
name := repo.GetName()
1919
owner := repo.GetOwner().GetLogin()
2020
head := repo.GetDefaultBranch()
21-
base := "release"
2221

2322
_, _, err := client.Repositories.GetBranch(ctx, owner, name, base)
2423
if err != nil {

repositories/releases.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// GetReleases takes a context, github client, and respos to iterate and collects
1313
// release pull requests for the repos.
14-
func GetReleases(ctx context.Context, client *github.Client, repos []*github.Repository) ([]*github.PullRequest, error) {
14+
func GetReleases(ctx context.Context, client *github.Client, repos []*github.Repository, base string) ([]*github.PullRequest, error) {
1515
var releases []*github.PullRequest
1616

1717
count := len(repos)
@@ -34,7 +34,6 @@ func GetReleases(ctx context.Context, client *github.Client, repos []*github.Rep
3434
name = repo.GetName()
3535
appendStr = fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)
3636
head := repo.GetDefaultBranch()
37-
base := "release"
3837

3938
opts := &github.PullRequestListOptions{
4039
Head: head,

0 commit comments

Comments
 (0)