Skip to content

Commit

Permalink
Add remote target branch to pr : no more config
Browse files Browse the repository at this point in the history
commit-id:afb94b00
  • Loading branch information
ejoffe committed Jun 1, 2023
1 parent 5c50650 commit 7ed55a4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 90 deletions.
3 changes: 0 additions & 3 deletions .spr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ githubRepoName: spr
githubHost: github.com
requireChecks: true
requireApproval: false
githubRemote: origin
githubBranch: master
remoteBranches: []
mergeMethod: rebase
mergeQueue: false
forceFetchTags: false
Expand Down
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ type RepoConfig struct {
MergeCheck string `yaml:"mergeCheck,omitempty"`

ForceFetchTags bool `default:"false" yaml:"forceFetchTags"`

BranchNameIncludeTarget bool `default:"false" yaml:"branchNameIncludeTarget"`
}

type InternalConfig struct {
Expand Down
21 changes: 4 additions & 17 deletions git/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,12 @@ func GetLocalBranchName(gitcmd GitInterface) string {
panic("cannot determine local git branch name")
}

func BranchNameFromCommit(cfg *config.Config, gitcmd GitInterface, commit Commit) string {
if cfg.Repo.BranchNameIncludeTarget {
remoteBranchName := cfg.Internal.GitHubBranch
return "spr/" + remoteBranchName + "/" + commit.CommitID
}

return "spr/" + commit.CommitID
func BranchNameFromCommit(cfg *config.Config, commit Commit) string {
remoteBranchName := cfg.Internal.GitHubBranch
return "spr/" + remoteBranchName + "/" + commit.CommitID
}

var _branchNameRegex = regexp.MustCompile(`spr/([a-f0-9]{8})$`)
var _branchNameWithTargetRegex = regexp.MustCompile(`spr/([a-zA-Z0-9_\-/\.]+)/([a-f0-9]{8})$`)

func BranchNameRegex(repoConfig *config.RepoConfig) *regexp.Regexp {
if repoConfig.BranchNameIncludeTarget {
return _branchNameWithTargetRegex
}

return _branchNameRegex
}
var BranchNameRegex = regexp.MustCompile(`spr/([a-zA-Z0-9_\-/\.]+)/([a-f0-9]{8})$`)

// GetLocalTopCommit returns the top unmerged commit in the stack
//
Expand Down
18 changes: 1 addition & 17 deletions git/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@ package git
import "testing"

func TestBranchNameRegex(t *testing.T) {
tests := []struct {
input string
commit string
}{
{input: "spr/deadbeef", commit: "deadbeef"},
}

for _, tc := range tests {
matches := _branchNameRegex.FindStringSubmatch(tc.input)
if tc.commit != matches[1] {
t.Fatalf("expected: '%v', actual: '%v'", tc.commit, matches[1])
}
}
}

func TestBranchNameWithTargetRegex(t *testing.T) {
tests := []struct {
input string
branch string
Expand All @@ -28,7 +12,7 @@ func TestBranchNameWithTargetRegex(t *testing.T) {
}

for _, tc := range tests {
matches := _branchNameWithTargetRegex.FindStringSubmatch(tc.input)
matches := BranchNameRegex.FindStringSubmatch(tc.input)
if tc.branch != matches[1] {
t.Fatalf("expected: '%v', actual: '%v'", tc.branch, matches[1])
}
Expand Down
2 changes: 1 addition & 1 deletion git/mockgit/mockgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (m *Mock) ExpectPushCommits(commits []*git.Commit) {

var refNames []string
for _, c := range commits {
branchName := "spr/" + c.CommitID
branchName := "spr/master/" + c.CommitID
refNames = append(refNames, c.CommitHash+":refs/heads/"+branchName)
}
m.expect("git push --force --atomic origin " + strings.Join(refNames, " "))
Expand Down
16 changes: 7 additions & 9 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ func matchPullRequestStack(
return []*github.PullRequest{}
}

branchNameRegex := git.BranchNameRegex(repoConfig)

// pullRequestMap is a map from commit-id to pull request
pullRequestMap := make(map[string]*github.PullRequest)
for _, node := range *allPullRequests.Nodes {
Expand All @@ -229,11 +227,11 @@ func matchPullRequestStack(
ToBranch: node.BaseRefName,
}

matches := branchNameRegex.FindStringSubmatch(node.HeadRefName)
matches := git.BranchNameRegex.FindStringSubmatch(node.HeadRefName)
if matches != nil {
commit := (*node.Commits.Nodes)[0].Commit
pullRequest.Commit = git.Commit{
CommitID: matches[1],
CommitID: matches[2],
CommitHash: commit.Oid,
Subject: commit.MessageHeadline,
Body: commit.MessageBody,
Expand Down Expand Up @@ -288,11 +286,11 @@ func matchPullRequestStack(
break
}

matches := branchNameRegex.FindStringSubmatch(currpr.ToBranch)
matches := git.BranchNameRegex.FindStringSubmatch(currpr.ToBranch)
if matches == nil {
panic(fmt.Errorf("invalid base branch for pull request:%s", currpr.ToBranch))
}
nextCommitID := matches[1]
nextCommitID := matches[2]

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

baseRefName := c.config.Internal.GitHubBranch
if prevCommit != nil {
baseRefName = git.BranchNameFromCommit(c.config, gitcmd, *prevCommit)
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
}
headRefName := git.BranchNameFromCommit(c.config, gitcmd, commit)
headRefName := git.BranchNameFromCommit(c.config, commit)

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

baseRefName := c.config.Internal.GitHubBranch
if prevCommit != nil {
baseRefName = git.BranchNameFromCommit(c.config, gitcmd, *prevCommit)
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
}

log.Debug().Interface("Commit", commit).
Expand Down
74 changes: 37 additions & 37 deletions github/githubclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestMatchPullRequestStack(t *testing.T) {
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
{
Id: "1",
HeadRefName: "spr/00000001",
HeadRefName: "spr/master/00000001",
BaseRefName: "master",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
Expand All @@ -55,7 +55,7 @@ func TestMatchPullRequestStack(t *testing.T) {
expect: []*github.PullRequest{
{
ID: "1",
FromBranch: "spr/00000001",
FromBranch: "spr/master/00000001",
ToBranch: "master",
Commit: git.Commit{
CommitID: "00000001",
Expand All @@ -78,7 +78,7 @@ func TestMatchPullRequestStack(t *testing.T) {
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
{
Id: "1",
HeadRefName: "spr/00000001",
HeadRefName: "spr/master/00000001",
BaseRefName: "master",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
Expand All @@ -90,8 +90,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "2",
HeadRefName: "spr/00000002",
BaseRefName: "spr/00000001",
HeadRefName: "spr/master/00000002",
BaseRefName: "spr/master/00000001",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -105,7 +105,7 @@ func TestMatchPullRequestStack(t *testing.T) {
expect: []*github.PullRequest{
{
ID: "1",
FromBranch: "spr/00000001",
FromBranch: "spr/master/00000001",
ToBranch: "master",
Commit: git.Commit{
CommitID: "00000001",
Expand All @@ -117,8 +117,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
ID: "2",
FromBranch: "spr/00000002",
ToBranch: "spr/00000001",
FromBranch: "spr/master/00000002",
ToBranch: "spr/master/00000001",
Commit: git.Commit{
CommitID: "00000002",
CommitHash: "2",
Expand All @@ -136,7 +136,7 @@ func TestMatchPullRequestStack(t *testing.T) {
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
{
Id: "1",
HeadRefName: "spr/00000001",
HeadRefName: "spr/master/00000001",
BaseRefName: "master",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
Expand All @@ -160,7 +160,7 @@ func TestMatchPullRequestStack(t *testing.T) {
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
{
Id: "1",
HeadRefName: "spr/00000001",
HeadRefName: "spr/master/00000001",
BaseRefName: "master",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
Expand All @@ -172,8 +172,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "3",
HeadRefName: "spr/00000003",
BaseRefName: "spr/00000002",
HeadRefName: "spr/master/00000003",
BaseRefName: "spr/master/00000002",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -184,8 +184,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "2",
HeadRefName: "spr/00000002",
BaseRefName: "spr/00000001",
HeadRefName: "spr/master/00000002",
BaseRefName: "spr/master/00000001",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -199,7 +199,7 @@ func TestMatchPullRequestStack(t *testing.T) {
expect: []*github.PullRequest{
{
ID: "1",
FromBranch: "spr/00000001",
FromBranch: "spr/master/00000001",
ToBranch: "master",
Commit: git.Commit{
CommitID: "00000001",
Expand All @@ -211,8 +211,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
ID: "2",
FromBranch: "spr/00000002",
ToBranch: "spr/00000001",
FromBranch: "spr/master/00000002",
ToBranch: "spr/master/00000001",
Commit: git.Commit{
CommitID: "00000002",
CommitHash: "2",
Expand All @@ -233,7 +233,7 @@ func TestMatchPullRequestStack(t *testing.T) {
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
{
Id: "1",
HeadRefName: "spr/00000001",
HeadRefName: "spr/master/00000001",
BaseRefName: "master",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
Expand All @@ -245,8 +245,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "2",
HeadRefName: "spr/00000002",
BaseRefName: "spr/00000001",
HeadRefName: "spr/master/00000002",
BaseRefName: "spr/master/00000001",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -257,8 +257,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "3",
HeadRefName: "spr/00000003",
BaseRefName: "spr/00000002",
HeadRefName: "spr/master/00000003",
BaseRefName: "spr/master/00000002",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -272,7 +272,7 @@ func TestMatchPullRequestStack(t *testing.T) {
expect: []*github.PullRequest{
{
ID: "1",
FromBranch: "spr/00000001",
FromBranch: "spr/master/00000001",
ToBranch: "master",
Commit: git.Commit{
CommitID: "00000001",
Expand All @@ -284,8 +284,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
ID: "2",
FromBranch: "spr/00000002",
ToBranch: "spr/00000001",
FromBranch: "spr/master/00000002",
ToBranch: "spr/master/00000001",
Commit: git.Commit{
CommitID: "00000002",
CommitHash: "2",
Expand All @@ -296,8 +296,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
ID: "3",
FromBranch: "spr/00000003",
ToBranch: "spr/00000002",
FromBranch: "spr/master/00000003",
ToBranch: "spr/master/00000002",
Commit: git.Commit{
CommitID: "00000003",
CommitHash: "3",
Expand All @@ -318,7 +318,7 @@ func TestMatchPullRequestStack(t *testing.T) {
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodes{
{
Id: "1",
HeadRefName: "spr/00000001",
HeadRefName: "spr/master/00000001",
BaseRefName: "master",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
Expand All @@ -330,8 +330,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "2",
HeadRefName: "spr/00000002",
BaseRefName: "spr/00000001",
HeadRefName: "spr/master/00000002",
BaseRefName: "spr/master/00000001",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -342,8 +342,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
Id: "3",
HeadRefName: "spr/00000003",
BaseRefName: "spr/00000002",
HeadRefName: "spr/master/00000003",
BaseRefName: "spr/master/00000002",
Commits: genclient.PullRequestsRepositoryPullRequestsNodesCommits{
Nodes: &genclient.PullRequestsRepositoryPullRequestsNodesCommitsNodes{
{
Expand All @@ -357,7 +357,7 @@ func TestMatchPullRequestStack(t *testing.T) {
expect: []*github.PullRequest{
{
ID: "1",
FromBranch: "spr/00000001",
FromBranch: "spr/master/00000001",
ToBranch: "master",
Commit: git.Commit{
CommitID: "00000001",
Expand All @@ -370,8 +370,8 @@ func TestMatchPullRequestStack(t *testing.T) {

{
ID: "2",
FromBranch: "spr/00000002",
ToBranch: "spr/00000001",
FromBranch: "spr/master/00000002",
ToBranch: "spr/master/00000001",
Commit: git.Commit{
CommitID: "00000002",
CommitHash: "2",
Expand All @@ -382,8 +382,8 @@ func TestMatchPullRequestStack(t *testing.T) {
},
{
ID: "3",
FromBranch: "spr/00000003",
ToBranch: "spr/00000002",
FromBranch: "spr/master/00000003",
ToBranch: "spr/master/00000002",
Commit: git.Commit{
CommitID: "00000003",
CommitHash: "3",
Expand Down
Loading

0 comments on commit 7ed55a4

Please sign in to comment.