Skip to content

Commit

Permalink
Check orphaned commits only on pr merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
CamaradeRoman committed May 6, 2024
1 parent 1015e67 commit 867f46b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface OpenSearchService {
fun saveCommits(commits: List<CommitDocument>)
fun deleteCommitsByIds(commitsIds: List<String>)
fun deleteCommitsByRepositoryId(repositoryId: String)
fun findPullRequestById(pullRequestId: String): PullRequestDocument?
fun findPullRequestsByRepositoryId(repositoryId: String): Set<PullRequestDocument>
fun savePullRequests(pullRequests: List<PullRequestDocument>)
fun deletePullRequestsByIds(pullRequestsIds: List<String>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.octopusden.octopus.infrastructure.gitea.client.dto.GiteaRepository
import org.octopusden.octopus.infrastructure.gitea.client.dto.GiteaShortCommit
import org.octopusden.octopus.infrastructure.gitea.client.dto.GiteaTag
import org.octopusden.octopus.infrastructure.gitea.client.exception.NotFoundException
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequestStatus
import org.octopusden.octopus.vcsfacade.client.common.dto.RefType
import org.octopusden.octopus.vcsfacade.document.RepositoryDocument
import org.octopusden.octopus.vcsfacade.dto.GiteaCreateRefEvent
Expand Down Expand Up @@ -80,18 +81,24 @@ class GiteaIndexerServiceImpl(
override fun registerGiteaPullRequestEvent(giteaPullRequestEvent: GiteaPullRequestEvent) {
log.trace("=> registerGiteaPullRequestEvent({})", giteaPullRequestEvent)
val indexPullRequest = giteaPullRequestEvent.toPullRequestDocument()
val removeOrphanedCommits = if (indexPullRequest.status == PullRequestStatus.MERGED)
openSearchService.findPullRequestById(indexPullRequest.id)?.status != PullRequestStatus.MERGED
else false
openSearchService.savePullRequests(listOf(indexPullRequest))
val indexCommitsIds = openSearchService.findCommitsByRepositoryId(indexPullRequest.repository.id).map { it.id }
val commitsIds = giteaService.getBranchesCommitGraph(
indexPullRequest.repository.group,
indexPullRequest.repository.name
).map { it.toDocument(indexPullRequest.repository).id }.toSet()
val orphanedCommitsIds = indexCommitsIds - commitsIds
logIndexActionMessage(
"Remove ${orphanedCommitsIds.size} commit(s) from index for `${indexPullRequest.repository.fullName}` $GITEA repository",
orphanedCommitsIds
)
openSearchService.deleteCommitsByIds(orphanedCommitsIds)
if (removeOrphanedCommits) {
val indexCommitsIds =
openSearchService.findCommitsByRepositoryId(indexPullRequest.repository.id).map { it.id }
val commitsIds = giteaService.getBranchesCommitGraph(
indexPullRequest.repository.group,
indexPullRequest.repository.name
).map { it.toDocument(indexPullRequest.repository).id }.toSet()
val orphanedCommitsIds = indexCommitsIds - commitsIds
logIndexActionMessage(
"Remove ${orphanedCommitsIds.size} commit(s) from index for `${indexPullRequest.repository.fullName}` $GITEA repository",
orphanedCommitsIds
)
openSearchService.deleteCommitsByIds(orphanedCommitsIds)
}
log.trace("<= registerGiteaPullRequestEvent({})", giteaPullRequestEvent)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class OpenSearchServiceImpl(
log.trace("<= deleteCommitsByRepositoryId({})", repositoryId)
}

override fun findPullRequestById(pullRequestId: String): PullRequestDocument? {
log.trace("=> findPullRequestById({})", pullRequestId)
return pullRequestRepository.findById(pullRequestId).getOrNull().also {
log.trace("<= findPullRequestById({}): {}", pullRequestId, it)
}
}

override fun findPullRequestsByRepositoryId(repositoryId: String): Set<PullRequestDocument> {
log.trace("=> findPullRequestsByRepositoryId({})", repositoryId)
return fetchAll { id ->
Expand Down

0 comments on commit 867f46b

Please sign in to comment.