Skip to content

Commit 7ed55a4

Browse files
committed
Add remote target branch to pr : no more config
commit-id:afb94b00
1 parent 5c50650 commit 7ed55a4

File tree

8 files changed

+52
-90
lines changed

8 files changed

+52
-90
lines changed

.spr.yml

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ githubRepoName: spr
33
githubHost: github.com
44
requireChecks: true
55
requireApproval: false
6-
githubRemote: origin
7-
githubBranch: master
8-
remoteBranches: []
96
mergeMethod: rebase
107
mergeQueue: false
118
forceFetchTags: false

config/config.go

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ type RepoConfig struct {
3434
MergeCheck string `yaml:"mergeCheck,omitempty"`
3535

3636
ForceFetchTags bool `default:"false" yaml:"forceFetchTags"`
37-
38-
BranchNameIncludeTarget bool `default:"false" yaml:"branchNameIncludeTarget"`
3937
}
4038

4139
type InternalConfig struct {

git/helpers.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,12 @@ func GetLocalBranchName(gitcmd GitInterface) string {
2424
panic("cannot determine local git branch name")
2525
}
2626

27-
func BranchNameFromCommit(cfg *config.Config, gitcmd GitInterface, commit Commit) string {
28-
if cfg.Repo.BranchNameIncludeTarget {
29-
remoteBranchName := cfg.Internal.GitHubBranch
30-
return "spr/" + remoteBranchName + "/" + commit.CommitID
31-
}
32-
33-
return "spr/" + commit.CommitID
27+
func BranchNameFromCommit(cfg *config.Config, commit Commit) string {
28+
remoteBranchName := cfg.Internal.GitHubBranch
29+
return "spr/" + remoteBranchName + "/" + commit.CommitID
3430
}
3531

36-
var _branchNameRegex = regexp.MustCompile(`spr/([a-f0-9]{8})$`)
37-
var _branchNameWithTargetRegex = regexp.MustCompile(`spr/([a-zA-Z0-9_\-/\.]+)/([a-f0-9]{8})$`)
38-
39-
func BranchNameRegex(repoConfig *config.RepoConfig) *regexp.Regexp {
40-
if repoConfig.BranchNameIncludeTarget {
41-
return _branchNameWithTargetRegex
42-
}
43-
44-
return _branchNameRegex
45-
}
32+
var BranchNameRegex = regexp.MustCompile(`spr/([a-zA-Z0-9_\-/\.]+)/([a-f0-9]{8})$`)
4633

4734
// GetLocalTopCommit returns the top unmerged commit in the stack
4835
//

git/helpers_test.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,6 @@ package git
33
import "testing"
44

55
func TestBranchNameRegex(t *testing.T) {
6-
tests := []struct {
7-
input string
8-
commit string
9-
}{
10-
{input: "spr/deadbeef", commit: "deadbeef"},
11-
}
12-
13-
for _, tc := range tests {
14-
matches := _branchNameRegex.FindStringSubmatch(tc.input)
15-
if tc.commit != matches[1] {
16-
t.Fatalf("expected: '%v', actual: '%v'", tc.commit, matches[1])
17-
}
18-
}
19-
}
20-
21-
func TestBranchNameWithTargetRegex(t *testing.T) {
226
tests := []struct {
237
input string
248
branch string
@@ -28,7 +12,7 @@ func TestBranchNameWithTargetRegex(t *testing.T) {
2812
}
2913

3014
for _, tc := range tests {
31-
matches := _branchNameWithTargetRegex.FindStringSubmatch(tc.input)
15+
matches := BranchNameRegex.FindStringSubmatch(tc.input)
3216
if tc.branch != matches[1] {
3317
t.Fatalf("expected: '%v', actual: '%v'", tc.branch, matches[1])
3418
}

git/mockgit/mockgit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (m *Mock) ExpectPushCommits(commits []*git.Commit) {
7878

7979
var refNames []string
8080
for _, c := range commits {
81-
branchName := "spr/" + c.CommitID
81+
branchName := "spr/master/" + c.CommitID
8282
refNames = append(refNames, c.CommitHash+":refs/heads/"+branchName)
8383
}
8484
m.expect("git push --force --atomic origin " + strings.Join(refNames, " "))

github/githubclient/client.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ func matchPullRequestStack(
215215
return []*github.PullRequest{}
216216
}
217217

218-
branchNameRegex := git.BranchNameRegex(repoConfig)
219-
220218
// pullRequestMap is a map from commit-id to pull request
221219
pullRequestMap := make(map[string]*github.PullRequest)
222220
for _, node := range *allPullRequests.Nodes {
@@ -229,11 +227,11 @@ func matchPullRequestStack(
229227
ToBranch: node.BaseRefName,
230228
}
231229

232-
matches := branchNameRegex.FindStringSubmatch(node.HeadRefName)
230+
matches := git.BranchNameRegex.FindStringSubmatch(node.HeadRefName)
233231
if matches != nil {
234232
commit := (*node.Commits.Nodes)[0].Commit
235233
pullRequest.Commit = git.Commit{
236-
CommitID: matches[1],
234+
CommitID: matches[2],
237235
CommitHash: commit.Oid,
238236
Subject: commit.MessageHeadline,
239237
Body: commit.MessageBody,
@@ -288,11 +286,11 @@ func matchPullRequestStack(
288286
break
289287
}
290288

291-
matches := branchNameRegex.FindStringSubmatch(currpr.ToBranch)
289+
matches := git.BranchNameRegex.FindStringSubmatch(currpr.ToBranch)
292290
if matches == nil {
293291
panic(fmt.Errorf("invalid base branch for pull request:%s", currpr.ToBranch))
294292
}
295-
nextCommitID := matches[1]
293+
nextCommitID := matches[2]
296294

297295
currpr = pullRequestMap[nextCommitID]
298296
}
@@ -342,9 +340,9 @@ func (c *client) CreatePullRequest(ctx context.Context, gitcmd git.GitInterface,
342340

343341
baseRefName := c.config.Internal.GitHubBranch
344342
if prevCommit != nil {
345-
baseRefName = git.BranchNameFromCommit(c.config, gitcmd, *prevCommit)
343+
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
346344
}
347-
headRefName := git.BranchNameFromCommit(c.config, gitcmd, commit)
345+
headRefName := git.BranchNameFromCommit(c.config, commit)
348346

349347
log.Debug().Interface("Commit", commit).
350348
Str("FromBranch", headRefName).Str("ToBranch", baseRefName).
@@ -500,7 +498,7 @@ func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface,
500498

501499
baseRefName := c.config.Internal.GitHubBranch
502500
if prevCommit != nil {
503-
baseRefName = git.BranchNameFromCommit(c.config, gitcmd, *prevCommit)
501+
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
504502
}
505503

506504
log.Debug().Interface("Commit", commit).

github/githubclient/client_test.go

+37-37
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestMatchPullRequestStack(t *testing.T) {
4040
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
4141
{
4242
Id: "1",
43-
HeadRefName: "spr/00000001",
43+
HeadRefName: "spr/master/00000001",
4444
BaseRefName: "master",
4545
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
4646
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
@@ -55,7 +55,7 @@ func TestMatchPullRequestStack(t *testing.T) {
5555
expect: []*github.PullRequest{
5656
{
5757
ID: "1",
58-
FromBranch: "spr/00000001",
58+
FromBranch: "spr/master/00000001",
5959
ToBranch: "master",
6060
Commit: git.Commit{
6161
CommitID: "00000001",
@@ -78,7 +78,7 @@ func TestMatchPullRequestStack(t *testing.T) {
7878
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
7979
{
8080
Id: "1",
81-
HeadRefName: "spr/00000001",
81+
HeadRefName: "spr/master/00000001",
8282
BaseRefName: "master",
8383
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
8484
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
@@ -90,8 +90,8 @@ func TestMatchPullRequestStack(t *testing.T) {
9090
},
9191
{
9292
Id: "2",
93-
HeadRefName: "spr/00000002",
94-
BaseRefName: "spr/00000001",
93+
HeadRefName: "spr/master/00000002",
94+
BaseRefName: "spr/master/00000001",
9595
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
9696
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
9797
{
@@ -105,7 +105,7 @@ func TestMatchPullRequestStack(t *testing.T) {
105105
expect: []*github.PullRequest{
106106
{
107107
ID: "1",
108-
FromBranch: "spr/00000001",
108+
FromBranch: "spr/master/00000001",
109109
ToBranch: "master",
110110
Commit: git.Commit{
111111
CommitID: "00000001",
@@ -117,8 +117,8 @@ func TestMatchPullRequestStack(t *testing.T) {
117117
},
118118
{
119119
ID: "2",
120-
FromBranch: "spr/00000002",
121-
ToBranch: "spr/00000001",
120+
FromBranch: "spr/master/00000002",
121+
ToBranch: "spr/master/00000001",
122122
Commit: git.Commit{
123123
CommitID: "00000002",
124124
CommitHash: "2",
@@ -136,7 +136,7 @@ func TestMatchPullRequestStack(t *testing.T) {
136136
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
137137
{
138138
Id: "1",
139-
HeadRefName: "spr/00000001",
139+
HeadRefName: "spr/master/00000001",
140140
BaseRefName: "master",
141141
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
142142
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
@@ -160,7 +160,7 @@ func TestMatchPullRequestStack(t *testing.T) {
160160
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
161161
{
162162
Id: "1",
163-
HeadRefName: "spr/00000001",
163+
HeadRefName: "spr/master/00000001",
164164
BaseRefName: "master",
165165
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
166166
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
@@ -172,8 +172,8 @@ func TestMatchPullRequestStack(t *testing.T) {
172172
},
173173
{
174174
Id: "3",
175-
HeadRefName: "spr/00000003",
176-
BaseRefName: "spr/00000002",
175+
HeadRefName: "spr/master/00000003",
176+
BaseRefName: "spr/master/00000002",
177177
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
178178
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
179179
{
@@ -184,8 +184,8 @@ func TestMatchPullRequestStack(t *testing.T) {
184184
},
185185
{
186186
Id: "2",
187-
HeadRefName: "spr/00000002",
188-
BaseRefName: "spr/00000001",
187+
HeadRefName: "spr/master/00000002",
188+
BaseRefName: "spr/master/00000001",
189189
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
190190
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
191191
{
@@ -199,7 +199,7 @@ func TestMatchPullRequestStack(t *testing.T) {
199199
expect: []*github.PullRequest{
200200
{
201201
ID: "1",
202-
FromBranch: "spr/00000001",
202+
FromBranch: "spr/master/00000001",
203203
ToBranch: "master",
204204
Commit: git.Commit{
205205
CommitID: "00000001",
@@ -211,8 +211,8 @@ func TestMatchPullRequestStack(t *testing.T) {
211211
},
212212
{
213213
ID: "2",
214-
FromBranch: "spr/00000002",
215-
ToBranch: "spr/00000001",
214+
FromBranch: "spr/master/00000002",
215+
ToBranch: "spr/master/00000001",
216216
Commit: git.Commit{
217217
CommitID: "00000002",
218218
CommitHash: "2",
@@ -233,7 +233,7 @@ func TestMatchPullRequestStack(t *testing.T) {
233233
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
234234
{
235235
Id: "1",
236-
HeadRefName: "spr/00000001",
236+
HeadRefName: "spr/master/00000001",
237237
BaseRefName: "master",
238238
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
239239
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
@@ -245,8 +245,8 @@ func TestMatchPullRequestStack(t *testing.T) {
245245
},
246246
{
247247
Id: "2",
248-
HeadRefName: "spr/00000002",
249-
BaseRefName: "spr/00000001",
248+
HeadRefName: "spr/master/00000002",
249+
BaseRefName: "spr/master/00000001",
250250
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
251251
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
252252
{
@@ -257,8 +257,8 @@ func TestMatchPullRequestStack(t *testing.T) {
257257
},
258258
{
259259
Id: "3",
260-
HeadRefName: "spr/00000003",
261-
BaseRefName: "spr/00000002",
260+
HeadRefName: "spr/master/00000003",
261+
BaseRefName: "spr/master/00000002",
262262
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
263263
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
264264
{
@@ -272,7 +272,7 @@ func TestMatchPullRequestStack(t *testing.T) {
272272
expect: []*github.PullRequest{
273273
{
274274
ID: "1",
275-
FromBranch: "spr/00000001",
275+
FromBranch: "spr/master/00000001",
276276
ToBranch: "master",
277277
Commit: git.Commit{
278278
CommitID: "00000001",
@@ -284,8 +284,8 @@ func TestMatchPullRequestStack(t *testing.T) {
284284
},
285285
{
286286
ID: "2",
287-
FromBranch: "spr/00000002",
288-
ToBranch: "spr/00000001",
287+
FromBranch: "spr/master/00000002",
288+
ToBranch: "spr/master/00000001",
289289
Commit: git.Commit{
290290
CommitID: "00000002",
291291
CommitHash: "2",
@@ -296,8 +296,8 @@ func TestMatchPullRequestStack(t *testing.T) {
296296
},
297297
{
298298
ID: "3",
299-
FromBranch: "spr/00000003",
300-
ToBranch: "spr/00000002",
299+
FromBranch: "spr/master/00000003",
300+
ToBranch: "spr/master/00000002",
301301
Commit: git.Commit{
302302
CommitID: "00000003",
303303
CommitHash: "3",
@@ -318,7 +318,7 @@ func TestMatchPullRequestStack(t *testing.T) {
318318
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
319319
{
320320
Id: "1",
321-
HeadRefName: "spr/00000001",
321+
HeadRefName: "spr/master/00000001",
322322
BaseRefName: "master",
323323
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
324324
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
@@ -330,8 +330,8 @@ func TestMatchPullRequestStack(t *testing.T) {
330330
},
331331
{
332332
Id: "2",
333-
HeadRefName: "spr/00000002",
334-
BaseRefName: "spr/00000001",
333+
HeadRefName: "spr/master/00000002",
334+
BaseRefName: "spr/master/00000001",
335335
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
336336
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
337337
{
@@ -342,8 +342,8 @@ func TestMatchPullRequestStack(t *testing.T) {
342342
},
343343
{
344344
Id: "3",
345-
HeadRefName: "spr/00000003",
346-
BaseRefName: "spr/00000002",
345+
HeadRefName: "spr/master/00000003",
346+
BaseRefName: "spr/master/00000002",
347347
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
348348
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
349349
{
@@ -357,7 +357,7 @@ func TestMatchPullRequestStack(t *testing.T) {
357357
expect: []*github.PullRequest{
358358
{
359359
ID: "1",
360-
FromBranch: "spr/00000001",
360+
FromBranch: "spr/master/00000001",
361361
ToBranch: "master",
362362
Commit: git.Commit{
363363
CommitID: "00000001",
@@ -370,8 +370,8 @@ func TestMatchPullRequestStack(t *testing.T) {
370370

371371
{
372372
ID: "2",
373-
FromBranch: "spr/00000002",
374-
ToBranch: "spr/00000001",
373+
FromBranch: "spr/master/00000002",
374+
ToBranch: "spr/master/00000001",
375375
Commit: git.Commit{
376376
CommitID: "00000002",
377377
CommitHash: "2",
@@ -382,8 +382,8 @@ func TestMatchPullRequestStack(t *testing.T) {
382382
},
383383
{
384384
ID: "3",
385-
FromBranch: "spr/00000003",
386-
ToBranch: "spr/00000002",
385+
FromBranch: "spr/master/00000003",
386+
ToBranch: "spr/master/00000002",
387387
Commit: git.Commit{
388388
CommitID: "00000003",
389389
CommitHash: "3",

0 commit comments

Comments
 (0)