Skip to content

Commit

Permalink
Cache only open PRs (#351)
Browse files Browse the repository at this point in the history
Fixes #349 (I hope)
  • Loading branch information
lukaszwawrzyk authored Feb 20, 2024
1 parent f1d84b2 commit 24235c8
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ class GithubPlatform private constructor(
private val ghRepository = createClient(token)
private val ghPatRepository = personalToken?.let { createClient(it) }

private val bazelPRs: List<GHPullRequest> =
ghRepository.queryPullRequests().state(GHIssueState.ALL).list().toList()
private val cachedOpenPRs: List<GHPullRequest> =
ghRepository.queryPullRequests().state(GHIssueState.OPEN).list().toList()

private val branchToGHPR: Map<String, GHPullRequest> = bazelPRs.associateBy { it.head.ref }
private val branchToGHPR: Map<String, GHPullRequest> = cachedOpenPRs.associateBy { it.head.ref }

override fun checkPrStatus(branch: GitBranch): PrStatus {
return checkPrStatus(branchToGHPR[branch.name])
val pr = branchToGHPR[branch.name] ?: ghRepository.queryPullRequests().head(branch.name).list().firstOrNull()
return checkPrStatus(pr)
}

override fun openNewPr(pr: NewPullRequest): PullRequest {
Expand All @@ -54,14 +55,14 @@ class GithubPlatform private constructor(

override fun getOpenPrs(): List<PullRequest> {
val openStatuses = setOf(PrStatus.OPEN_MERGEABLE, PrStatus.OPEN_NOT_MERGEABLE)
return bazelPRs
return cachedOpenPRs
.filter { checkPrStatus(it) in openStatuses }
.map { PullRequest(GitBranch(it.head.ref)) }
}

override fun closePrs(pullRequests: List<PullRequest>) {
val names = pullRequests.map { it.branch.name }
bazelPRs.filter { it.head.ref in names }.forEach { it.close() }
val branchNames = pullRequests.map { it.branch.name }
cachedOpenPRs.filter { it.head.ref in branchNames }.forEach { it.close() }
}

override suspend fun onPrChange(pr: PullRequest, prStatusBefore: PrStatus) {
Expand Down

0 comments on commit 24235c8

Please sign in to comment.