@@ -57,8 +57,7 @@ final class NurtureAlg[F[_]](config: Config)(implicit
57
57
for {
58
58
_ <- logger.info(s " Nurture ${data.repo.show}" )
59
59
baseBranch <- cloneAndSync(data.repo, fork)
60
- seenBranches <- Ref [F ].of(List .empty[Branch ])
61
- _ <- updateDependencies(data, fork.repo, baseBranch, updates, seenBranches)
60
+ _ <- updateDependencies(data, fork.repo, baseBranch, updates)
62
61
} yield ()
63
62
64
63
def cloneAndSync (repo : Repo , fork : RepoOut ): F [Branch ] =
@@ -71,35 +70,40 @@ final class NurtureAlg[F[_]](config: Config)(implicit
71
70
data : RepoData ,
72
71
fork : Repo ,
73
72
baseBranch : Branch ,
74
- updates : List [Update .Single ],
75
- seenBranches : Ref [F , List [Branch ]]
73
+ updates : List [Update .Single ]
76
74
): F [Unit ] =
77
75
for {
78
76
_ <- F .unit
79
77
grouped = Update .groupByGroupId(updates)
80
78
_ <- logger.info(util.logger.showUpdates(grouped))
81
79
baseSha1 <- gitAlg.latestSha1(data.repo, baseBranch)
80
+ seenBranches <- Ref [F ].of(List .empty[Branch ])
82
81
_ <- NurtureAlg .processUpdates(
83
82
grouped,
84
83
update => {
85
84
val updateData =
86
85
UpdateData (data, fork, update, baseBranch, baseSha1, git.branchFor(update))
87
- processUpdate(updateData, seenBranches).flatMap {
88
- case result @ Created (newPrNumber) =>
89
- (for {
90
- _ <- closeObsoletePullRequests(updateData, newPrNumber)
91
- _ <- seenBranches.update(updateData.updateBranch :: _)
92
- } yield ()).as[ProcessResult ](result)
93
- case result @ Updated =>
94
- seenBranches.update(updateData.updateBranch :: _).as[ProcessResult ](result)
95
- case result @ Ignored => F .pure(result)
96
- }
86
+ seenBranches
87
+ .getAndUpdate(
88
+ identity
89
+ ) // Suppress Codacity's faulty `.get` detection, https://twitter.com/blast_hardchese/status/1373376444827508737
90
+ .flatMap(processUpdate(updateData, _))
91
+ .flatMap {
92
+ case result @ Created (newPrNumber) =>
93
+ (for {
94
+ _ <- closeObsoletePullRequests(updateData, newPrNumber)
95
+ _ <- seenBranches.update(updateData.updateBranch :: _)
96
+ } yield ()).as[ProcessResult ](result)
97
+ case result @ Updated =>
98
+ seenBranches.update(updateData.updateBranch :: _).as[ProcessResult ](result)
99
+ case result @ Ignored => F .pure(result)
100
+ }
97
101
},
98
102
data.config.updates.limit
99
103
)
100
104
} yield ()
101
105
102
- def processUpdate (data : UpdateData , seenBranches : Ref [ F , List [Branch ] ]): F [ProcessResult ] =
106
+ def processUpdate (data : UpdateData , seenBranches : List [Branch ]): F [ProcessResult ] =
103
107
for {
104
108
_ <- logger.info(s " Process update ${data.update.show}" )
105
109
head = vcs.listingBranch(config.vcsType, data.fork, data.update)
@@ -155,30 +159,19 @@ final class NurtureAlg[F[_]](config: Config)(implicit
155
159
gitAlg.removeBranch(repo, branch)
156
160
}
157
161
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 ] =
162
+ def applyNewUpdate (data : UpdateData , seenBranches : List [Branch ]): F [ProcessResult ] =
171
163
gitAlg.returnToCurrentBranch(data.repo) {
172
164
val createBranch = logger.info(s " Create branch ${data.updateBranch.name}" ) >>
173
165
gitAlg.createBranch(data.repo, data.updateBranch)
174
166
editAlg.applyUpdate(data.repoData, data.update, createBranch).flatMap { editCommits =>
175
167
if (editCommits.isEmpty) logger.warn(" No commits created" ).as(Ignored )
176
168
else
177
- ensureDistinctBranch(
178
- data,
179
- seenBranches,
180
- pushCommits(data, editCommits) >> createPullRequest(data)
181
- )
169
+ seenBranches
170
+ .forallM(gitAlg.diff(data.repo, _).map(_.nonEmpty))
171
+ .ifM(
172
+ pushCommits(data, editCommits) >> createPullRequest(data),
173
+ logger.warn(" Discovered a duplicate branch, not pushing" ).as[ProcessResult ](Ignored )
174
+ )
182
175
}
183
176
}
184
177
@@ -227,7 +220,7 @@ final class NurtureAlg[F[_]](config: Config)(implicit
227
220
_ <- logger.info(s " Created PR ${pr.html_url}" )
228
221
} yield Created (pr.number)
229
222
230
- def updatePullRequest (data : UpdateData , seenBranches : Ref [ F , List [Branch ] ]): F [ProcessResult ] =
223
+ def updatePullRequest (data : UpdateData , seenBranches : List [Branch ]): F [ProcessResult ] =
231
224
if (data.repoConfig.updatePullRequestsOrDefault =!= PullRequestUpdateStrategy .Never )
232
225
gitAlg.returnToCurrentBranch(data.repo) {
233
226
for {
@@ -260,18 +253,20 @@ final class NurtureAlg[F[_]](config: Config)(implicit
260
253
result.flatMap { case (update, msg) => logger.info(msg).as(update) }
261
254
}
262
255
263
- def mergeAndApplyAgain (data : UpdateData , seenBranches : Ref [ F , List [Branch ] ]): F [ProcessResult ] =
256
+ def mergeAndApplyAgain (data : UpdateData , seenBranches : List [Branch ]): F [ProcessResult ] =
264
257
for {
265
258
_ <- logger.info(
266
259
s " Merge branch ${data.baseBranch.name} into ${data.updateBranch.name} and apply again "
267
260
)
268
261
maybeMergeCommit <- gitAlg.mergeTheirs(data.repo, data.baseBranch)
269
262
editCommits <- editAlg.applyUpdate(data.repoData, data.update)
270
- result <- ensureDistinctBranch(
271
- data,
272
- seenBranches,
273
- pushCommits(data, maybeMergeCommit.toList ++ editCommits)
274
- )
263
+ result <-
264
+ seenBranches
265
+ .forallM(gitAlg.diff(data.repo, _).map(_.nonEmpty))
266
+ .ifM(
267
+ pushCommits(data, maybeMergeCommit.toList ++ editCommits),
268
+ logger.warn(" Discovered a duplicate branch, not pushing" ).as[ProcessResult ](Ignored )
269
+ )
275
270
} yield result
276
271
}
277
272
0 commit comments