Skip to content

Commit

Permalink
better
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconZet committed Feb 10, 2023
1 parent c6a044e commit 773db69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
18 changes: 12 additions & 6 deletions app/src/main/kotlin/org/virtuslab/bazelsteward/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.virtuslab.bazelsteward.core.config.ConfigEntry
import org.virtuslab.bazelsteward.core.library.Library
import org.virtuslab.bazelsteward.core.library.LibraryId
import org.virtuslab.bazelsteward.core.library.VersioningSchema
import org.virtuslab.bazelsteward.github.GithubClient
import org.virtuslab.bazelsteward.maven.MavenLibraryId

private val logger = KotlinLogging.logger {}
Expand All @@ -23,7 +24,7 @@ class App(private val ctx: Context) {
suspend fun run() {
ctx.gitOperations.checkoutBaseBranch()
val definitions = ctx.bazelFileSearch.buildDefinitions
logger.debug { definitions.map { it.path } }
logger.debug { "Definitions: " + definitions.map { it.path } }
val mavenData = ctx.mavenDataExtractor.extract()
logger.debug { "Repositories " + mavenData.repositories.toString() }
logger.debug { "Dependencies: " + mavenData.dependencies.map { it.id.name + " " + it.version.value }.toString() }
Expand All @@ -44,7 +45,7 @@ class App(private val ctx: Context) {
val changeSuggestions = ctx.fileUpdateSearch.searchBuildFiles(definitions, updateSuggestions)

val bazelVersion = runCatching { BazelVersion.extractBazelVersion(ctx.config.path) }
.onFailure { logger.error { "Can't extract Bazel version" } }.getOrNull()
.onFailure { logger.error(it) { "Can't extract Bazel version" } }.getOrNull()

val bazelChangeSuggestions = bazelVersion?.let {
val availableBazelVersions = ctx.bazelUpdater.availableVersions(bazelVersion)
Expand All @@ -58,7 +59,7 @@ class App(private val ctx: Context) {
val branch = change.branch
when (val prStatus = ctx.gitHostClient.checkPrStatus(branch)) {
NONE, OPEN_NOT_MERGEABLE -> {
logger.info { "Creating branch ${branch.name}" }
logger.info { "Creating branch ${branch.name}, PR status: ${prStatus.name}" }
runCatching {
ctx.gitOperations.createBranchWithChange(change)
if (ctx.config.pushToRemote) {
Expand All @@ -67,8 +68,11 @@ class App(private val ctx: Context) {
ctx.gitHostClient.openNewPR(branch)
ctx.gitHostClient.closePrs(change.library.id, filterNotVersion = change.library.version)
}
if (ctx.gitHostClient is GithubClient) {
ctx.gitHostClient.reopenPr(branch)
}
}
}.exceptionOrNull()?.let { logger.error("Failed at creating branch ${branch.name}", it) }
}.onFailure { logger.error(it) { "Failed at creating branch ${branch.name}" } }
ctx.gitOperations.checkoutBaseBranch()
}

Expand All @@ -85,8 +89,10 @@ class App(private val ctx: Context) {
private fun <Lib : LibraryId> getConfigurableSetupForLibrary(library: Library<Lib>): Pair<VersioningSchema, BumpingStrategy> {
return when (val libraryId = library.id) {
is MavenLibraryId -> {
val versioningForDependency = getConfigEntryFromConfigs(libraryId, ctx.bazelStewardConfig.maven.configs.filter { it.versioning != null })
val bumpingForDependency = getConfigEntryFromConfigs(libraryId, ctx.bazelStewardConfig.maven.configs.filter { it.bumping != null })
val versioningForDependency =
getConfigEntryFromConfigs(libraryId, ctx.bazelStewardConfig.maven.configs.filter { it.versioning != null })
val bumpingForDependency =
getConfigEntryFromConfigs(libraryId, ctx.bazelStewardConfig.maven.configs.filter { it.bumping != null })
Pair(
versioningForDependency?.versioning ?: VersioningSchema.Loose,
bumpingForDependency?.bumping ?: BumpingStrategy.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GitOperations(private val config: Config) {
val branchName = branch.name
git.checkout(branchName)
try {
git.push(branchName)
git.push(branchName, force = force)
} catch (e: RuntimeException) {
git.push(branchName, force = true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.virtuslab.bazelsteward.core.GitHostClient
import org.virtuslab.bazelsteward.core.GitHostClient.Companion.PrStatus
import org.virtuslab.bazelsteward.core.library.LibraryId
import org.virtuslab.bazelsteward.core.library.Version
import java.lang.RuntimeException
import java.nio.file.Path
import kotlin.io.path.Path

Expand Down Expand Up @@ -39,19 +40,12 @@ class GithubClient private constructor(

override fun openNewPR(branch: GitBranch) {
logger.info { "Creating pull request for ${branch.name}" }
val pr = ghRepository.createPullRequest(
ghRepository.createPullRequest(
"Updated ${branch.libraryId.name} to ${branch.version.value}",
branch.name,
config.baseBranch,
""
)

ghPatRepository?.let {
val pullRequest = it.getPullRequest(pr.number)
pullRequest.close()
Thread.sleep(1000)
pullRequest.reopen()
}
}

override fun closePrs(library: LibraryId, filterNotVersion: Version?) {
Expand All @@ -63,6 +57,17 @@ class GithubClient private constructor(
oldPrs.forEach { it.close() }
}

fun reopenPr(branch: GitBranch) {
ghPatRepository?.let { repository ->
val pr = repository.queryPullRequests().state(GHIssueState.OPEN).head(branch.name).list().firstOrNull()
?: throw RuntimeException("PR ${branch.name} not found")
pr.close()
Thread.sleep(1000)
pr.reopen()

}
}

private fun checkPrStatus(pr: GHPullRequest?): PrStatus {
return if (pr == null)
PrStatus.NONE
Expand Down

0 comments on commit 773db69

Please sign in to comment.