Skip to content

Commit

Permalink
PR descriptions are updated after commit reordering
Browse files Browse the repository at this point in the history
Fixes #277
  • Loading branch information
MichaelSims authored and ejoffe committed Apr 24, 2023
1 parent 1046af0 commit 85b71ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
17 changes: 16 additions & 1 deletion spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,12 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context, reviewers []string
wg := new(sync.WaitGroup)
wg.Add(len(updateQueue))

// Sort the PR stack by the local commit order, in case some commits were reordered
sortedPullRequests := sortPullRequestsByLocalCommitOrder(githubInfo.PullRequests, localCommits)
for i := range updateQueue {
go func(i int) {
pr := updateQueue[i]
sd.github.UpdatePullRequest(ctx, sd.gitcmd, githubInfo.PullRequests, pr.pr, pr.commit, pr.prevCommit)
sd.github.UpdatePullRequest(ctx, sd.gitcmd, sortedPullRequests, pr.pr, pr.commit, pr.prevCommit)
wg.Done()
}(i)
}
Expand Down Expand Up @@ -446,6 +448,19 @@ func commitsReordered(localCommits []git.Commit, pullRequests []*github.PullRequ
return false
}

func sortPullRequestsByLocalCommitOrder(pullRequests []*github.PullRequest, localCommits []git.Commit) []*github.PullRequest {
pullRequestMap := map[string]*github.PullRequest{}
for _, pullRequest := range pullRequests {
pullRequestMap[pullRequest.Commit.CommitID] = pullRequest
}

var sortedPullRequests []*github.PullRequest
for _, commit := range localCommits {
sortedPullRequests = append(sortedPullRequests, pullRequestMap[commit.CommitID])
}
return sortedPullRequests
}

func (sd *stackediff) fetchAndGetGitHubInfo(ctx context.Context) *github.GitHubInfo {
if sd.config.Repo.ForceFetchTags {
sd.gitcmd.MustGit("fetch --tags --force", nil)
Expand Down
37 changes: 37 additions & 0 deletions spr/spr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ func TestSPRReorderCommit(t *testing.T) {
CommitHash: "c400000000000000000000000000000000000000",
Subject: "test commit 4",
}
c5 := git.Commit{
CommitID: "00000005",
CommitHash: "c500000000000000000000000000000000000000",
Subject: "test commit 5",
}

// 'git spr status' :: StatusPullRequest
githubmock.ExpectGetInfo()
Expand Down Expand Up @@ -406,6 +411,38 @@ func TestSPRReorderCommit(t *testing.T) {
//assert.Equal("[✔✔✔✔] 1 : test commit 2", lines[3])
output.Reset()

// 'git spr update' :: UpdatePullRequest :: commits=[c5, c1, c2, c3, c4]
githubmock.ExpectGetInfo()
gitmock.ExpectFetch()
gitmock.ExpectLogAndRespond([]*git.Commit{&c1, &c2, &c3, &c4, &c5})
githubmock.ExpectUpdatePullRequest(c1, nil)
githubmock.ExpectUpdatePullRequest(c2, nil)
githubmock.ExpectUpdatePullRequest(c3, nil)
githubmock.ExpectUpdatePullRequest(c4, nil)
// reorder commits
c1.CommitHash = "c102000000000000000000000000000000000000"
c2.CommitHash = "c202000000000000000000000000000000000000"
c3.CommitHash = "c302000000000000000000000000000000000000"
c4.CommitHash = "c402000000000000000000000000000000000000"
gitmock.ExpectPushCommits([]*git.Commit{&c5, &c4, &c3, &c2, &c1})
githubmock.ExpectCreatePullRequest(c5, nil)
githubmock.ExpectUpdatePullRequest(c5, nil)
githubmock.ExpectUpdatePullRequest(c4, &c5)
githubmock.ExpectUpdatePullRequest(c3, &c4)
githubmock.ExpectUpdatePullRequest(c2, &c3)
githubmock.ExpectUpdatePullRequest(c1, &c2)
githubmock.ExpectGetInfo()
s.UpdatePullRequests(ctx, nil, nil)
fmt.Printf("OUT: %s\n", output.String())
// TODO : Need to update pull requests in GetInfo expect to get this check to work
// lines = strings.Split(output.String(), "\n")
//assert.Equal("[✔✔✔✔] 1 : test commit 5", lines[0])
//assert.Equal("[✔✔✔✔] 1 : test commit 4", lines[1])
//assert.Equal("[✔✔✔✔] 1 : test commit 3", lines[2])
//assert.Equal("[✔✔✔✔] 1 : test commit 2", lines[3])
//assert.Equal("[✔✔✔✔] 1 : test commit 1", lines[4])
output.Reset()

// TODO : add a call to merge and check merge order
}

Expand Down

0 comments on commit 85b71ef

Please sign in to comment.