Skip to content

Commit 24f0437

Browse files
committed
Dissolve and recreate GitHub stack on every push
The updatePullRequest GraphQL mutation includes baseRefName, and GitHub rejects that field on PRs that are part of a stack, even when the value hasn't changed. Rather than conditionally omitting the field, dissolve the stack before updating PRs and recreate it after. This is simpler and handles all cases (reordering, appending, and no-change pushes) uniformly. Also fix findExistingStack to search all PR numbers instead of just the first, since the API response order is not guaranteed and the first PR might not be in the existing stack. commit-id: I00743382
1 parent a9f640f commit 24f0437

2 files changed

Lines changed: 18 additions & 96 deletions

File tree

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/GitJaspr.kt‎

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -136,40 +136,19 @@ class GitJaspr(
136136
}
137137
}
138138

139-
private suspend fun findExistingStack(prNumbers: List<Int>): StackInfo? {
140-
if (prNumbers.isEmpty()) return null
141-
return stacksClient.findStackByPr(prNumbers.first())?.takeIf { it.open }
142-
}
139+
private suspend fun findExistingStack(prNumbers: List<Int>): StackInfo? =
140+
prNumbers.firstNotNullOfOrNull {
141+
stacksClient.findStackByPr(it)?.takeIf(StackInfo::open)
142+
}
143143

144144
private suspend fun dissolveStack(stack: StackInfo) {
145145
logger.debug("Dissolving existing stack {}", stack.number)
146146
stacksClient.unstack(stack.number)
147147
}
148148

149-
private suspend fun registerOrAppendStack(
150-
orderedPrNumbers: List<Int>,
151-
existingStack: StackInfo?,
152-
) {
149+
private suspend fun registerStack(orderedPrNumbers: List<Int>) {
153150
if (orderedPrNumbers.size < 2) return
154151
try {
155-
if (existingStack != null) {
156-
val existingPrs = existingStack.pullRequestNumbers
157-
if (orderedPrNumbers == existingPrs) {
158-
logger.debug("GitHub stack #{} already matches, skipping", existingStack.number)
159-
return
160-
}
161-
if (
162-
orderedPrNumbers.size > existingPrs.size &&
163-
orderedPrNumbers.subList(0, existingPrs.size) == existingPrs
164-
) {
165-
val newPrs = orderedPrNumbers.subList(existingPrs.size, orderedPrNumbers.size)
166-
val updated = stacksClient.addToStack(existingStack.number, newPrs)
167-
renderer.info {
168-
"Added ${newPrs.size} PR(s) to GitHub stack #${updated.number}"
169-
}
170-
return
171-
}
172-
}
173152
val stack = stacksClient.createStack(orderedPrNumbers)
174153
renderer.info {
175154
"Registered GitHub stack #${stack.number} (${orderedPrNumbers.size} PRs)"
@@ -1305,22 +1284,16 @@ class GitJaspr(
13051284
ghClient.getPullRequests(stack).filterByMatchingTargetRef()
13061285
)
13071286

1308-
val existingGhStack =
1309-
if (areStacksAvailable()) {
1310-
val existingPrNumbers = pullRequests.mapNotNull(PullRequest::number)
1311-
val existing = findExistingStack(existingPrNumbers)
1312-
if (
1313-
existing != null &&
1314-
pullRequests.willRetargetBases(stack, filteredRefSpec.remoteRef)
1315-
) {
1316-
dissolveStack(existing)
1317-
null
1318-
} else {
1319-
existing
1320-
}
1321-
} else {
1322-
null
1287+
// Dissolve any existing GitHub stack before updating PRs. The updatePullRequest
1288+
// mutation includes baseRefName, and GitHub rejects that field on stacked PRs. Dissolving
1289+
// first avoids the rejection; registerStack re-creates the stack after the push.
1290+
if (areStacksAvailable()) {
1291+
val existingPrNumbers = pullRequests.mapNotNull(PullRequest::number)
1292+
val existing = findExistingStack(existingPrNumbers)
1293+
if (existing != null) {
1294+
dissolveStack(existing)
13231295
}
1296+
}
13241297

13251298
val pullRequestsRebased =
13261299
pullRequests.updateBaseRefForReorderedPrsIfAny(stack, filteredRefSpec.remoteRef)
@@ -1485,7 +1458,7 @@ class GitJaspr(
14851458
stack.indexOfFirst { commit -> commit.id == pr.commitId }
14861459
}
14871460
.mapNotNull(PullRequest::number)
1488-
registerOrAppendStack(orderedPrNumbers, existingGhStack)
1461+
registerStack(orderedPrNumbers)
14891462
}
14901463

14911464
print(getStatusString(refSpec, remoteBranchesAfterPush, theme))
@@ -2667,24 +2640,6 @@ class GitJaspr(
26672640
return true
26682641
}
26692642

2670-
/**
2671-
* Returns true if [updateBaseRefForReorderedPrsIfAny] would change any PR bases. Used to decide
2672-
* whether a GitHub stack must be dissolved before pushing.
2673-
*/
2674-
private fun List<PullRequest>.willRetargetBases(
2675-
commitStack: List<Commit>,
2676-
remoteRef: String,
2677-
): Boolean {
2678-
val commitMap =
2679-
commitStack.windowedPairs().associateBy { (_, commit) -> checkNotNull(commit.id) }
2680-
return any { pr ->
2681-
val commitPair = commitMap[pr.commitId] ?: return@any false
2682-
val (prevCommit, _) = commitPair
2683-
val newBaseRef = prevCommit?.toRemoteRefName() ?: remoteRef
2684-
pr.baseRefName != newBaseRef
2685-
}
2686-
}
2687-
26882643
/**
26892644
* Update any of the given pull requests whose commits have since been reordered so that their
26902645
* [PullRequest.baseRefName] is equal to [remoteRef], and return a potentially updated list.

‎git-jaspr/src/test/kotlin/sims/michael/gitjaspr/GitJasprTest.kt‎

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10976,32 +10976,6 @@ interface GitJasprTest {
1097610976
count: Int? = null,
1097710977
) = gitJasprWithStacks.push(stackName = stackName, count = count)
1097810978

10979-
@GhStacks
10980-
@Test
10981-
fun `repush with no changes does not create a duplicate stack`() {
10982-
withTestSetup(useFakeRemote) {
10983-
createCommitsFrom(
10984-
testCase {
10985-
repository {
10986-
commit { title = "A" }
10987-
commit {
10988-
title = "B"
10989-
localRefs += "main"
10990-
}
10991-
}
10992-
}
10993-
)
10994-
10995-
pushWithStacks()
10996-
val firstStack = stacksStub.allStacks.single { it.open }
10997-
10998-
pushWithStacks()
10999-
val openStacks = stacksStub.allStacks.filter { it.open }
11000-
assertEquals(1, openStacks.size, "No duplicate stack should be created")
11001-
assertEquals(firstStack.number, openStacks.single().number, "Same stack number")
11002-
}
11003-
}
11004-
1100510979
@GhStacks
1100610980
@Test
1100710981
fun `push registers a GitHub stack when stacks are available`() {
@@ -11126,7 +11100,7 @@ interface GitJasprTest {
1112611100

1112711101
@GhStacks
1112811102
@Test
11129-
fun `appending commits to top preserves stack number`() {
11103+
fun `appending commits to top recreates stack with all PRs`() {
1113011104
withTestSetup(useFakeRemote) {
1113111105
createCommitsFrom(
1113211106
testCase {
@@ -11142,8 +11116,7 @@ interface GitJasprTest {
1114211116

1114311117
pushWithStacks()
1114411118

11145-
val firstStack = stacksStub.allStacks.single { it.open }
11146-
assertEquals(2, firstStack.pullRequestNumbers.size)
11119+
assertEquals(2, stacksStub.allStacks.single { it.open }.pullRequestNumbers.size)
1114711120

1114811121
createCommitsFrom(
1114911122
testCase {
@@ -11162,13 +11135,7 @@ interface GitJasprTest {
1116211135

1116311136
val openStacks = stacksStub.allStacks.filter { it.open }
1116411137
assertEquals(1, openStacks.size, "Exactly one open stack")
11165-
val updatedStack = openStacks.single()
11166-
assertEquals(3, updatedStack.pullRequestNumbers.size, "Stack should have 3 PRs")
11167-
assertEquals(
11168-
firstStack.number,
11169-
updatedStack.number,
11170-
"Stack number should be preserved for append-only pushes",
11171-
)
11138+
assertEquals(3, openStacks.single().pullRequestNumbers.size, "Stack should have 3 PRs")
1117211139
}
1117311140
}
1117411141

0 commit comments

Comments
 (0)