Skip to content

chore: return token type for github auth token #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal suspend fun Workflow.reportAvailableUpdatesInternal(
) {
availableVersionsForEachAction(
reportWhenTokenUnset = reportWhenTokenUnset,
githubAuthToken = githubAuthToken ?: getGithubAuthTokenOrNull(),
githubAuthToken = githubAuthToken ?: getGithubAuthTokenOrNull()?.first,
).onEach { regularActionVersions ->
val usesString =
with(regularActionVersions.action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.io.path.readText

internal suspend fun Workflow.availableVersionsForEachAction(
reportWhenTokenUnset: Boolean = true,
githubAuthToken: String? = getGithubAuthTokenOrNull(),
githubAuthToken: String? = getGithubAuthTokenOrNull()?.first,
): Flow<RegularActionVersions> {
if (githubAuthToken == null && !reportWhenTokenUnset) {
githubWarning("github auth token is required, but not set, skipping api calls")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ private fun Route.metadata(refresh: Boolean = false) {
val file = call.parameters["file"] ?: return@get call.respondNotFound()
val actionCoords = call.parameters.extractActionCoords(extractVersion = false)

val bindingArtifacts = actionCoords.buildPackageArtifacts(githubAuthToken = getGithubAuthToken())
val (githubAuthToken, tokenType) = getGithubAuthToken()
// TODO: use tokenType to register a metric on the fallback
// TODO: what if getGithubAuthToken() returns null? Looks like destructuring asserts non-null?
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubAuthToken = githubAuthToken)
if (file in bindingArtifacts) {
when (val artifact = bindingArtifacts[file]) {
is String -> call.respondText(text = artifact)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ private val logger = logger { }
* The token may be of various kind, e.g. a Personal Access Token, or an
* Application Installation Token.
*/
fun getGithubAuthTokenOrNull(): String? =
fun getGithubAuthTokenOrNull(): Pair<String, TokenType>? =
runBlocking {
runCatching { getInstallationAccessToken() }
.onFailure { logger.warn(it) { "Failed to get GitHub App Installation token, falling back to GITHUB_TOKEN." } }
.getOrNull() ?: System.getenv("GITHUB_TOKEN")
.getOrNull()
?.let { Pair(it, TokenType.InstallationAccessToken) }
?: System.getenv("GITHUB_TOKEN")?.let { Pair(it, TokenType.PersonalAccessToken) }
}.also { if (it == null) logger.warn { ERROR_NO_CONFIGURATION } }

/**
Expand All @@ -24,7 +26,12 @@ fun getGithubAuthTokenOrNull(): String? =
* The token may be of various kind, e.g. a Personal Access Token, or an
* Application Installation Token.
*/
fun getGithubAuthToken(): String = getGithubAuthTokenOrNull() ?: error(ERROR_NO_CONFIGURATION)
fun getGithubAuthToken(): Pair<String, TokenType> = getGithubAuthTokenOrNull() ?: error(ERROR_NO_CONFIGURATION)

enum class TokenType {
InstallationAccessToken,
PersonalAccessToken,
}

private val ERROR_NO_CONFIGURATION =
"""
Expand Down
Loading