Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin committed Jun 18, 2024
1 parent 175bc83 commit fa02d7a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/pga/common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ data class ActionUsage(

val cache = mutableMapOf<ActionUsage, ActionUsage?>()

internal val REGEX = Regex("([ -]*uses: *)([A-Za-z0-9_.-]*)/([A-Za-z0-9_.-/]*)@([^\\s#]+)(.*)")
internal val REGEX = Regex("([ -]*uses: *)([A-Za-z0-9_.-]*)/([A-Za-z0-9_./-]*)@([^\\s#]+)(.*)")
internal fun processLine(line: String, index: Int, block: (ActionUsage) -> ActionUsage?): String {
val matchResult = REGEX.matchEntire(line)
if (matchResult == null) {
Expand Down
77 changes: 51 additions & 26 deletions src/commonMain/kotlin/pga/github.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pga

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
Expand All @@ -26,8 +25,9 @@ fun getSha(owner: String, name: String, tagOrBranch: String): String? {
*
* @throws Exception if there is a network error or if the response cannot be handled
*/
fun getLatestTag(owner: String, name: String): Tag? {
val json = getJson("https://api.github.com/repos/$owner/$name/tags")
fun getLatestTag(owner: String, path: String): Tag? {
val repoName = path.substringBefore('/')
val json = getJson("https://api.github.com/repos/$owner/$repoName/tags").getOrThrow()
if (json !is List<*>) {
error("Unexpected response from the GitHub API: $json")
}
Expand All @@ -49,22 +49,42 @@ data class Tag(val name: String, val sha: String)
*
* @throws Exception if there is a network error or if the response cannot be handled
*/
internal fun getShaFromRef(owner: String, name: String, ref: String): String? {
internal fun getShaFromRef(owner: String, path: String, ref: String): String? {
//println("getShaFromRef($owner, $name, $ref)")
/**
* Some repositories contain several actions
* See https://github.com/orgs/community/discussions/24990
* See https://github.com/gradle/actions
*/
val repoName = name.substringBefore('/')
val json = getJson("https://api.github.com/repos/$owner/$repoName/git/ref/$ref")
if (json !is Map<*, *>) {
error("Unexpected response from the GitHub API: $json")
val repoName = path.substringBefore('/')
val result = getJson("https://api.github.com/repos/$owner/$repoName/git/ref/$ref")
return when (result) {
JsonNotFound -> {
null
}
is JsonSuccess -> {
val json = result.data
if (json !is Map<*, *>) {
error("Unexpected response from the GitHub API: $json")
}
json.get("object")?.asMap?.get("sha")?.asString
}
}
return json.get("object")?.asMap?.get("sha")?.asString
}

private fun getJson(url: String): Any? {
sealed interface JsonResult {
fun getOrThrow(): Any? {
if (this !is JsonSuccess) {
error("Json not found")
}
return data
}
}

class JsonSuccess(val data: Any?): JsonResult
object JsonNotFound: JsonResult

private fun getJson(url: String): JsonResult {
return runBlocking {
val response = httpClient.get(url) {
val token = readConfig()?.token
Expand All @@ -74,25 +94,30 @@ private fun getJson(url: String): Any? {
}

if (response.status.value / 100 != 2) {
if (response.status.value == 403) {
if (response.headers.get("x-ratelimit-limit") == response.headers.get("x-ratelimit-used")) {
val expires = response.headers.get("x-ratelimit-reset")?.toLongOrNull()?.let {
Instant.fromEpochSeconds(it)
}?.let {
"until '$it'"
} ?: ""
println(
"""You have reached the GitHub unauthenticated rate limit, please either:
|- wait $expires
|- or authenticate with `pin-github-actions --login`
""".trimMargin()
)
exitProcess(1)
}
when (response.status.value) {
403 -> {
if (response.headers.get("x-ratelimit-limit") == response.headers.get("x-ratelimit-used")) {
val expires = response.headers.get("x-ratelimit-reset")?.toLongOrNull()?.let {
Instant.fromEpochSeconds(it)
}?.let {
"until '$it'"
} ?: ""
println(
"""You have reached the GitHub unauthenticated rate limit, please either:
|- wait $expires
|- or authenticate with `pin-github-actions --login`
""".trimMargin()
)
exitProcess(1)
}
}
404 -> {
return@runBlocking JsonNotFound
}
}
error("Received HTTP ${response.status.value} from ${response.request.url}")
}

Json.parseToJsonElement(response.body()).toAny()
return@runBlocking JsonSuccess(Json.parseToJsonElement(response.body()).toAny())
}
}
14 changes: 12 additions & 2 deletions src/commonTest/kotlin/pga/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pga

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class MainTest {
@Test
Expand All @@ -25,7 +26,16 @@ class MainTest {

@Test
fun regex() {
val result = REGEX.matchEntire(" uses: martinbonnin/run-benchmarks@main")
check(result != null)
REGEX.matchEntire(" uses: martinbonnin/run-benchmarks@main").let {
assertNotNull(it)
}
REGEX.matchEntire(" uses: gradle/actions/setup-gradle@v3").let {
assertNotNull(it)
}
}

@Test
fun integrationTest() {
MainCommand().main(listOf("/Users/mbonnin/git/apollo-kotlin/.github/workflows"))
}
}

0 comments on commit fa02d7a

Please sign in to comment.