Skip to content

Commit 4049892

Browse files
authored
chore: rename githubToken to githubAuthToken (#1868)
Part of #1854. A "GitHub token" is, I think, a reserved name for a special kind of token. With this change we'd like to express that the code can work with various kinds of token that can be used to call GitHub's API, not necessarily what is available under `GITHUB_TOKEN` and created by GitHub, or not necessarily a Personal Access Token. In particulr, we're working on switching to authenticating with a custom GitHub app where we'll have Applicaiton Installation Token.
1 parent 75c5a3c commit 4049892

File tree

10 files changed

+52
-41
lines changed

10 files changed

+52
-41
lines changed

action-updates-checker/src/main/kotlin/io/github/typesafegithub/workflows/updates/Reporting.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.github.typesafegithub.workflows.updates
22

33
import io.github.typesafegithub.workflows.domain.Workflow
44
import io.github.typesafegithub.workflows.shared.internal.findGitRoot
5-
import io.github.typesafegithub.workflows.shared.internal.getGithubTokenOrNull
5+
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthTokenOrNull
66
import kotlinx.coroutines.flow.onEach
77
import kotlinx.coroutines.flow.onEmpty
88
import kotlinx.coroutines.flow.toList
@@ -14,33 +14,32 @@ import kotlin.io.path.relativeTo
1414

1515
/**
1616
* will report all available updates in the terminal output and the github step summary
17-
* looks up the github token from env `GITHUB_TOKEN` by default
18-
* when no github token is present, reporting will be skipped
17+
* when no github auth token is present, reporting will be skipped
1918
*
2019
* @param reportWhenTokenUnset enable to use github api without a token
21-
* @param githubToken if not set, will try to load from the environment variable `GITHUB_TOKEN`
20+
* @param githubAuthToken if not set, will try to use one configured in the environment
2221
*/
2322
public fun Workflow.reportAvailableUpdates(
2423
reportWhenTokenUnset: Boolean = false,
25-
githubToken: String? = null,
24+
githubAuthToken: String? = null,
2625
): Unit =
2726
runBlocking {
2827
if (System.getenv("GHWKT_RUN_STEP") == null) {
2928
reportAvailableUpdatesInternal(
3029
reportWhenTokenUnset = reportWhenTokenUnset,
31-
githubToken = githubToken,
30+
githubAuthToken = githubAuthToken,
3231
)
3332
}
3433
}
3534

3635
internal suspend fun Workflow.reportAvailableUpdatesInternal(
3736
reportWhenTokenUnset: Boolean = false,
38-
githubToken: String? = null,
37+
githubAuthToken: String? = null,
3938
stepSummary: GithubStepSummary? = GithubStepSummary.fromEnv(),
4039
) {
4140
availableVersionsForEachAction(
4241
reportWhenTokenUnset = reportWhenTokenUnset,
43-
githubToken = githubToken ?: getGithubTokenOrNull(),
42+
githubAuthToken = githubAuthToken ?: getGithubAuthTokenOrNull(),
4443
).onEach { regularActionVersions ->
4544
val usesString =
4645
with(regularActionVersions.action) {

action-updates-checker/src/main/kotlin/io/github/typesafegithub/workflows/updates/Utils.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import io.github.typesafegithub.workflows.domain.ActionStep
55
import io.github.typesafegithub.workflows.domain.Workflow
66
import io.github.typesafegithub.workflows.domain.actions.RegularAction
77
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
8-
import io.github.typesafegithub.workflows.shared.internal.getGithubTokenOrNull
8+
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthTokenOrNull
99
import io.github.typesafegithub.workflows.shared.internal.model.Version
1010
import io.github.typesafegithub.workflows.updates.model.RegularActionVersions
1111
import kotlinx.coroutines.flow.Flow
@@ -17,9 +17,9 @@ import kotlin.io.path.readText
1717

1818
internal suspend fun Workflow.availableVersionsForEachAction(
1919
reportWhenTokenUnset: Boolean = true,
20-
githubToken: String? = getGithubTokenOrNull(),
20+
githubAuthToken: String? = getGithubAuthTokenOrNull(),
2121
): Flow<RegularActionVersions> {
22-
if (githubToken == null && !reportWhenTokenUnset) {
22+
if (githubAuthToken == null && !reportWhenTokenUnset) {
2323
githubWarning("github token is required, but not set, skipping api calls")
2424
return emptyFlow()
2525
}
@@ -28,7 +28,7 @@ internal suspend fun Workflow.availableVersionsForEachAction(
2828
groupedSteps.forEach { (action, steps) ->
2929
val availableVersions =
3030
action.fetchAvailableVersionsOrWarn(
31-
githubToken = githubToken,
31+
githubAuthToken = githubAuthToken,
3232
)
3333
val currentVersion = Version(action.actionVersion)
3434
if (availableVersions != null) {
@@ -49,12 +49,12 @@ internal suspend fun Workflow.availableVersionsForEachAction(
4949
}
5050
}
5151

52-
internal suspend fun RegularAction<*>.fetchAvailableVersionsOrWarn(githubToken: String?): List<Version>? =
52+
internal suspend fun RegularAction<*>.fetchAvailableVersionsOrWarn(githubAuthToken: String?): List<Version>? =
5353
try {
5454
fetchAvailableVersions(
5555
owner = actionOwner,
5656
name = actionName.substringBefore('/'),
57-
githubToken = githubToken,
57+
githubAuthToken = githubAuthToken,
5858
).getOrElse {
5959
throw Exception(it)
6060
}

action-updates-checker/src/test/kotlin/io/github/typesafegithub/workflows/updates/ReportingTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import io.github.typesafegithub.workflows.domain.Workflow
55
import io.github.typesafegithub.workflows.domain.actions.CustomAction
66
import io.github.typesafegithub.workflows.domain.triggers.Push
77
import io.github.typesafegithub.workflows.dsl.workflow
8-
import io.github.typesafegithub.workflows.shared.internal.getGithubTokenOrNull
8+
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthTokenOrNull
99
import io.kotest.common.ExperimentalKotest
1010
import io.kotest.core.spec.style.FunSpec
1111
import io.kotest.engine.spec.tempdir
@@ -19,7 +19,7 @@ import kotlinx.coroutines.runBlocking
1919
class ReportingTest :
2020
FunSpec(
2121
{
22-
val token = getGithubTokenOrNull()
22+
val token = getGithubAuthTokenOrNull()
2323
context("tests using github token").config(enabled = token != null) {
2424
val gitRootDir =
2525
tempdir()
@@ -107,7 +107,7 @@ class ReportingTest :
107107
runBlocking {
108108
it.availableVersionsForEachAction(
109109
reportWhenTokenUnset = false,
110-
githubToken = null,
110+
githubAuthToken = null,
111111
)
112112
}
113113
}.toList()

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/MetadataRoutes.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.github.typesafegithub.workflows.jitbindingserver
22

33
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
4-
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
4+
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
55
import io.ktor.http.HttpStatusCode
66
import io.ktor.server.response.respondText
77
import io.ktor.server.routing.Route
@@ -26,7 +26,7 @@ private fun Route.metadata(refresh: Boolean = false) {
2626
val file = call.parameters["file"] ?: return@get call.respondNotFound()
2727
val actionCoords = call.parameters.extractActionCoords(extractVersion = false)
2828

29-
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubToken = getGithubToken())
29+
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubAuthToken = getGithubAuthToken())
3030
if (file in bindingArtifacts) {
3131
when (val artifact = bindingArtifacts[file]) {
3232
is String -> call.respondText(text = artifact)

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuilding.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import java.time.format.DateTimeFormatter
1212
private val logger = logger { }
1313

1414
internal suspend fun ActionCoords.buildMavenMetadataFile(
15-
githubToken: String,
15+
githubAuthToken: String,
1616
fetchAvailableVersions: suspend (
1717
owner: String,
1818
name: String,
19-
githubToken: String?,
19+
githubAuthToken: String?,
2020
) -> Either<String, List<Version>> = ::fetchAvailableVersions,
2121
): String? {
2222
val availableVersions =
23-
fetchAvailableVersions(owner, name, githubToken)
23+
fetchAvailableVersions(owner, name, githubAuthToken)
2424
.getOrElse {
2525
logger.error { it }
2626
emptyList()

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/PackageArtifactsBuilding.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package io.github.typesafegithub.workflows.mavenbinding
22

33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
44

5-
suspend fun ActionCoords.buildPackageArtifacts(githubToken: String): Map<String, String> {
6-
val mavenMetadata = buildMavenMetadataFile(githubToken = githubToken) ?: return emptyMap()
5+
suspend fun ActionCoords.buildPackageArtifacts(githubAuthToken: String): Map<String, String> {
6+
val mavenMetadata = buildMavenMetadataFile(githubAuthToken = githubAuthToken) ?: return emptyMap()
77
return mapOf(
88
"maven-metadata.xml" to mavenMetadata,
99
"maven-metadata.xml.md5" to mavenMetadata.md5Checksum(),

maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuildingTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class MavenMetadataBuildingTest :
3737

3838
val xml =
3939
actionCoords.buildMavenMetadataFile(
40-
githubToken = "SOME_TOKEN",
40+
githubAuthToken = "SOME_TOKEN",
4141
fetchAvailableVersions = fetchAvailableVersions,
4242
)
4343

@@ -74,7 +74,7 @@ class MavenMetadataBuildingTest :
7474

7575
val xml =
7676
actionCoords.buildMavenMetadataFile(
77-
githubToken = "SOME_TOKEN",
77+
githubAuthToken = "SOME_TOKEN",
7878
fetchAvailableVersions = fetchAvailableVersions,
7979
)
8080

@@ -89,7 +89,7 @@ class MavenMetadataBuildingTest :
8989

9090
val xml =
9191
actionCoords.buildMavenMetadataFile(
92-
githubToken = "SOME_TOKEN",
92+
githubAuthToken = "SOME_TOKEN",
9393
fetchAvailableVersions = fetchAvailableVersions,
9494
)
9595

@@ -114,7 +114,7 @@ class MavenMetadataBuildingTest :
114114

115115
val xml =
116116
actionCoords.copy(significantVersion = significantVersion).buildMavenMetadataFile(
117-
githubToken = "SOME_TOKEN",
117+
githubAuthToken = "SOME_TOKEN",
118118
fetchAvailableVersions = fetchAvailableVersions,
119119
)
120120

shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubApi.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ import java.time.ZonedDateTime
2121
suspend fun fetchAvailableVersions(
2222
owner: String,
2323
name: String,
24-
githubToken: String?,
24+
githubAuthToken: String?,
2525
httpClientEngine: HttpClientEngine = CIO.create(),
2626
): Either<String, List<Version>> =
2727
either {
2828
val httpClient = buildHttpClient(engine = httpClientEngine)
2929
return listOf(
3030
apiTagsUrl(owner = owner, name = name),
3131
apiBranchesUrl(owner = owner, name = name),
32-
).flatMap { url -> fetchGithubRefs(url, githubToken, httpClient).bind() }
33-
.versions(githubToken, httpClient)
32+
).flatMap { url -> fetchGithubRefs(url, githubAuthToken, httpClient).bind() }
33+
.versions(githubAuthToken, httpClient)
3434
}
3535

3636
private fun List<GithubRef>.versions(
37-
githubToken: String?,
37+
githubAuthToken: String?,
3838
httpClient: HttpClient,
3939
): Either<String, List<Version>> =
4040
either {
@@ -44,8 +44,8 @@ private fun List<GithubRef>.versions(
4444
val response =
4545
httpClient
4646
.get(urlString = githubRef.`object`.url) {
47-
if (githubToken != null) {
48-
bearerAuth(githubToken)
47+
if (githubAuthToken != null) {
48+
bearerAuth(githubAuthToken)
4949
}
5050
}
5151
val releaseDate =
@@ -61,15 +61,15 @@ private fun List<GithubRef>.versions(
6161

6262
private suspend fun fetchGithubRefs(
6363
url: String,
64-
githubToken: String?,
64+
githubAuthToken: String?,
6565
httpClient: HttpClient,
6666
): Either<String, List<GithubRef>> =
6767
either {
6868
val response =
6969
httpClient
7070
.get(urlString = url) {
71-
if (githubToken != null) {
72-
bearerAuth(githubToken)
71+
if (githubAuthToken != null) {
72+
bearerAuth(githubAuthToken)
7373
}
7474
}
7575
ensure(response.status.isSuccess()) {

shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubUtils.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.github.typesafegithub.workflows.shared.internal
22

3-
fun getGithubTokenOrNull(): String? =
3+
/**
4+
* Returns a token that should be used to make authorized calls to GitHub,
5+
* or null if no token was configured.
6+
* The token may be of various kind, e.g. a Personal Access Token, or an
7+
* Application Installation Token.
8+
*/
9+
fun getGithubAuthTokenOrNull(): String? =
410
System
511
.getenv("GITHUB_TOKEN")
612
.also {
@@ -15,7 +21,13 @@ fun getGithubTokenOrNull(): String? =
1521
}
1622
}
1723

18-
fun getGithubToken(): String =
24+
/**
25+
* Returns a token that should be used to make authorized calls to GitHub,
26+
* or throws an exception if no token was configured.
27+
* The token may be of various kind, e.g. a Personal Access Token, or an
28+
* Application Installation Token.
29+
*/
30+
fun getGithubAuthToken(): String =
1931
System.getenv("GITHUB_TOKEN")
2032
?: error(
2133
"""

shared-internal/src/test/kotlin/io/github/typesafegithub/workflows/shared/internal/model/GithubApiTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class GithubApiTest :
9898
fetchAvailableVersions(
9999
owner = "some-owner",
100100
name = "some-name",
101-
githubToken = "token",
101+
githubAuthToken = "token",
102102
httpClientEngine = mockEngine,
103103
)
104104

@@ -129,7 +129,7 @@ class GithubApiTest :
129129
fetchAvailableVersions(
130130
owner = "some-owner",
131131
name = "some-name",
132-
githubToken = "token",
132+
githubAuthToken = "token",
133133
httpClientEngine = mockEngine,
134134
)
135135

0 commit comments

Comments
 (0)