From f13a8cfff7bae65c4b9ec3116498602330ee6556 Mon Sep 17 00:00:00 2001 From: Leonardo Colman Lopes Date: Sat, 5 Apr 2025 00:23:10 -0300 Subject: [PATCH 1/2] feat(server): add fallback to GITHUB_TOKEN if installation token fails - Attempts to retrieve a GitHub App Installation token first - Falls back to GITHUB_TOKEN if the installation token fails - Logs a warning on failure of installation token retrieval - Updated KDocs for both getGithubAuthToken and getGithubAuthTokenOrNull --- .../workflows/shared/internal/GithubUtils.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt index fbfb146ce5..90d575fefb 100644 --- a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt +++ b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt @@ -1,7 +1,10 @@ package io.github.typesafegithub.workflows.shared.internal +import io.github.oshai.kotlinlogging.KotlinLogging.logger import kotlinx.coroutines.runBlocking +private val logger = logger { } + /** * Returns a token that should be used to make authorized calls to GitHub, * or null if no token was configured. @@ -10,12 +13,9 @@ import kotlinx.coroutines.runBlocking */ fun getGithubAuthTokenOrNull(): String? = runBlocking { - (System.getenv("GITHUB_TOKEN") ?: getInstallationAccessToken()) - .also { - if (it == null) { - println(ERROR_NO_CONFIGURATION) - } - } + runCatching { getInstallationAccessToken() } + .onFailure { logger.warn(it) { "Failed to get GitHub App Installation token, falling back to GITHUB_TOKEN." } } + .getOrNull() ?: System.getenv("GITHUB_TOKEN") } /** From b4f42ef30f7f30a86323a7768e49a6b7f5d24406 Mon Sep 17 00:00:00 2001 From: Leonardo Colman Lopes Date: Sat, 5 Apr 2025 16:28:36 -0300 Subject: [PATCH 2/2] refactor: simplify GitHub auth token retrieval logic --- .../typesafegithub/workflows/shared/internal/GithubUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt index 90d575fefb..122e59d38c 100644 --- a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt +++ b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt @@ -16,7 +16,7 @@ fun getGithubAuthTokenOrNull(): String? = runCatching { getInstallationAccessToken() } .onFailure { logger.warn(it) { "Failed to get GitHub App Installation token, falling back to GITHUB_TOKEN." } } .getOrNull() ?: System.getenv("GITHUB_TOKEN") - } + }.also { if (it == null) logger.warn { ERROR_NO_CONFIGURATION } } /** * Returns a token that should be used to make authorized calls to GitHub,