Skip to content

Commit 2defcef

Browse files
author
Devon Stewart
committed
Before pushing updates or branches, ensure we haven't already made these changes
1 parent 0b02090 commit 2defcef

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ final class NurtureAlg[F[_]](config: Config)(implicit
8484
update => {
8585
val updateData =
8686
UpdateData(data, fork, update, baseBranch, baseSha1, git.branchFor(update))
87-
processUpdate(updateData).flatMap {
87+
processUpdate(updateData, seenBranches).flatMap {
8888
case result @ Created(newPrNumber) =>
8989
(for {
9090
_ <- closeObsoletePullRequests(updateData, newPrNumber)
@@ -99,7 +99,7 @@ final class NurtureAlg[F[_]](config: Config)(implicit
9999
)
100100
} yield ()
101101

102-
def processUpdate(data: UpdateData): F[ProcessResult] =
102+
def processUpdate(data: UpdateData, seenBranches: Ref[F, List[Branch]]): F[ProcessResult] =
103103
for {
104104
_ <- logger.info(s"Process update ${data.update.show}")
105105
head = vcs.listingBranch(config.vcsType, data.fork, data.update)
@@ -109,9 +109,9 @@ final class NurtureAlg[F[_]](config: Config)(implicit
109109
logger.info(s"PR ${pr.html_url} is closed") >>
110110
removeRemoteBranch(data.repo, data.updateBranch).as(Ignored)
111111
case Some(pr) =>
112-
logger.info(s"Found PR ${pr.html_url}") >> updatePullRequest(data)
112+
logger.info(s"Found PR ${pr.html_url}") >> updatePullRequest(data, seenBranches)
113113
case None =>
114-
applyNewUpdate(data)
114+
applyNewUpdate(data, seenBranches)
115115
}
116116
_ <- pullRequests.headOption.traverse_ { pr =>
117117
pullRequestRepository.createOrUpdate(
@@ -155,13 +155,30 @@ final class NurtureAlg[F[_]](config: Config)(implicit
155155
gitAlg.removeBranch(repo, branch)
156156
}
157157

158-
def applyNewUpdate(data: UpdateData): F[ProcessResult] =
158+
def ensureDistinctBranch(
159+
data: UpdateData,
160+
seenBranches: Ref[F, List[Branch]],
161+
whenDistinct: F[ProcessResult]
162+
): F[ProcessResult] =
163+
seenBranches.get
164+
.flatMap(_.forallM(gitAlg.diff(data.repo, _).map(_.nonEmpty)))
165+
.ifM(
166+
whenDistinct,
167+
logger.warn("Discovered a duplicate branch, not pushing").as(Ignored)
168+
)
169+
170+
def applyNewUpdate(data: UpdateData, seenBranches: Ref[F, List[Branch]]): F[ProcessResult] =
159171
gitAlg.returnToCurrentBranch(data.repo) {
160172
val createBranch = logger.info(s"Create branch ${data.updateBranch.name}") >>
161173
gitAlg.createBranch(data.repo, data.updateBranch)
162174
editAlg.applyUpdate(data.repoData, data.update, createBranch).flatMap { editCommits =>
163175
if (editCommits.isEmpty) logger.warn("No commits created").as(Ignored)
164-
else pushCommits(data, editCommits) >> createPullRequest(data)
176+
else
177+
ensureDistinctBranch(
178+
data,
179+
seenBranches,
180+
pushCommits(data, editCommits) >> createPullRequest(data)
181+
)
165182
}
166183
}
167184

@@ -210,13 +227,14 @@ final class NurtureAlg[F[_]](config: Config)(implicit
210227
_ <- logger.info(s"Created PR ${pr.html_url}")
211228
} yield Created(pr.number)
212229

213-
def updatePullRequest(data: UpdateData): F[ProcessResult] =
230+
def updatePullRequest(data: UpdateData, seenBranches: Ref[F, List[Branch]]): F[ProcessResult] =
214231
if (data.repoConfig.updatePullRequestsOrDefault =!= PullRequestUpdateStrategy.Never)
215232
gitAlg.returnToCurrentBranch(data.repo) {
216233
for {
217234
_ <- gitAlg.checkoutBranch(data.repo, data.updateBranch)
218235
update <- shouldBeUpdated(data)
219-
result <- if (update) mergeAndApplyAgain(data) else F.pure[ProcessResult](Ignored)
236+
result <-
237+
if (update) mergeAndApplyAgain(data, seenBranches) else F.pure[ProcessResult](Ignored)
220238
} yield result
221239
}
222240
else
@@ -242,14 +260,18 @@ final class NurtureAlg[F[_]](config: Config)(implicit
242260
result.flatMap { case (update, msg) => logger.info(msg).as(update) }
243261
}
244262

245-
def mergeAndApplyAgain(data: UpdateData): F[ProcessResult] =
263+
def mergeAndApplyAgain(data: UpdateData, seenBranches: Ref[F, List[Branch]]): F[ProcessResult] =
246264
for {
247265
_ <- logger.info(
248266
s"Merge branch ${data.baseBranch.name} into ${data.updateBranch.name} and apply again"
249267
)
250268
maybeMergeCommit <- gitAlg.mergeTheirs(data.repo, data.baseBranch)
251269
editCommits <- editAlg.applyUpdate(data.repoData, data.update)
252-
result <- pushCommits(data, maybeMergeCommit.toList ++ editCommits)
270+
result <- ensureDistinctBranch(
271+
data,
272+
seenBranches,
273+
pushCommits(data, maybeMergeCommit.toList ++ editCommits)
274+
)
253275
} yield result
254276
}
255277

0 commit comments

Comments
 (0)