Skip to content

Commit

Permalink
Add a rendered list of the full stack to PR descriptions
Browse files Browse the repository at this point in the history
Requires a change to the main update logic: we now need to
always update every PR, since we need to update the messages
of lower PRs when an upper PR is changed.

Also, when creating new PRs, the list of stacked PRs would
be incomplete. Queue all mutations and execute them at the
end to fix this.

Fixes #139

commit-id:28b937ff
  • Loading branch information
leoluk authored and Eitan Joffe committed Dec 6, 2021
1 parent 3439ad3 commit 9a0e69a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
21 changes: 19 additions & 2 deletions github/githubclient/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package githubclient

import (
"bytes"
"context"
"fmt"
"net/url"
Expand Down Expand Up @@ -181,7 +182,7 @@ func (c *client) CreatePullRequest(ctx context.Context,
}
} `graphql:"createPullRequest(input: $input)"`
}
commitBody := githubv4.String(commit.Body)
commitBody := githubv4.String(formatBody(commit, info.PullRequests))
input := githubv4.CreatePullRequestInput{
RepositoryID: info.RepositoryID,
BaseRefName: githubv4.String(baseRefName),
Expand Down Expand Up @@ -214,6 +215,22 @@ func (c *client) CreatePullRequest(ctx context.Context,
return pr
}

func formatStackMarkdown(stack []*github.PullRequest) string {
var buf bytes.Buffer
for i := len(stack) - 1; i >= 0; i-- {
buf.WriteString(fmt.Sprintf("- #%d\n", stack[i].Number))
}

return buf.String()
}

func formatBody(commit git.Commit, stack []*github.PullRequest) string {
if len(stack) <= 1 {
return commit.Body
}
return fmt.Sprintf("**Stack**:\n%s\n\n%s", formatStackMarkdown(stack), commit.Body)
}

func (c *client) UpdatePullRequest(ctx context.Context,
info *github.GitHubInfo, pr *github.PullRequest, commit git.Commit, prevCommit *git.Commit) {

Expand All @@ -239,7 +256,7 @@ func (c *client) UpdatePullRequest(ctx context.Context,
}
baseRefNameStr := githubv4.String(baseRefName)
subject := githubv4.String(commit.Subject)
body := githubv4.String(commit.Body)
body := githubv4.String(formatBody(commit, info.PullRequests))
input := githubv4.UpdatePullRequestInput{
PullRequestID: pr.ID,
BaseRefName: &baseRefNameStr,
Expand Down
33 changes: 20 additions & 13 deletions spr/spr.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context) {
}
githubInfo.PullRequests = validPullRequests

reorder := false
if commitsReordered(localCommits, githubInfo.PullRequests) {
reorder = true
// if commits have been reordered :
// first - rebase all pull requests to target branch
// then - update all pull requests
Expand All @@ -124,6 +122,14 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context) {
sd.syncCommitStackToGitHub(ctx, localCommits, githubInfo)
sd.profiletimer.Step("UpdatePullRequests::SyncCommitStackToGithub")

type prUpdate struct {
pr *github.PullRequest
commit git.Commit
prevCommit *git.Commit
}

updateQueue := make([]prUpdate, 0)

// iterate through local_commits and update pull_requests
for commitIndex, c := range localCommits {
if c.WIP {
Expand All @@ -133,18 +139,12 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context) {
for _, pr := range githubInfo.PullRequests {
if c.CommitID == pr.Commit.CommitID {
prFound = true
if c.CommitHash != pr.Commit.CommitHash || reorder {
// if commit id is same but commit hash changed it means the commit
// has been amended and we need to update the pull request
// in the reorder case we also want to update the pull request

var prevCommit *git.Commit
if commitIndex > 0 {
prevCommit = &localCommits[commitIndex-1]
}
sd.github.UpdatePullRequest(ctx, githubInfo, pr, c, prevCommit)
pr.Commit = c
var prevCommit *git.Commit
if commitIndex > 0 {
prevCommit = &localCommits[commitIndex-1]
}
updateQueue = append(updateQueue, prUpdate{pr, c, prevCommit})
pr.Commit = c
break
}
}
Expand All @@ -157,10 +157,17 @@ func (sd *stackediff) UpdatePullRequests(ctx context.Context) {
}
pr := sd.github.CreatePullRequest(ctx, githubInfo, c, prevCommit)
githubInfo.PullRequests = append(githubInfo.PullRequests, pr)
updateQueue = append(updateQueue, prUpdate{pr, c, prevCommit})
}
}
sd.profiletimer.Step("UpdatePullRequests::updatePullRequests")

for _, pr := range updateQueue {
sd.github.UpdatePullRequest(ctx, githubInfo, pr.pr, pr.commit, pr.prevCommit)
}

sd.profiletimer.Step("UpdatePullRequests::commitUpdateQueue")

sd.StatusPullRequests(ctx)
}

Expand Down

0 comments on commit 9a0e69a

Please sign in to comment.