Skip to content

Commit ff738ba

Browse files
authored
fix(server): respond 'not found' if no commit hash given but required (#2191)
Fixes #2190.
1 parent 3238989 commit ff738ba

4 files changed

Lines changed: 49 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private suspend fun ApplicationCall.toBindingArtifacts(
125125
refresh: Boolean,
126126
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
127127
): VersionArtifacts? {
128-
val parsedRequest = parameters.parseRequest(extractVersion = true)
128+
val parsedRequest = parameters.parseRequest(extractVersion = true) ?: return null
129129

130130
logger.info { "➡️ Requesting ${parsedRequest.actionCoords.prettyPrint}" }
131131
if (refresh) {

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
@@ -42,7 +42,7 @@ private fun Route.headMetadata(
4242
refresh: Boolean = false,
4343
) {
4444
head {
45-
val request = call.parameters.parseRequest(extractVersion = false)
45+
val request = call.parameters.parseRequest(extractVersion = false) ?: return@head call.respondNotFound()
4646

4747
if (refresh) {
4848
metadataCache.invalidate(request)
@@ -66,7 +66,7 @@ private fun Route.getMetadata(
6666
refresh: Boolean = false,
6767
) {
6868
get {
69-
val request = call.parameters.parseRequest(extractVersion = false)
69+
val request = call.parameters.parseRequest(extractVersion = false) ?: return@get call.respondNotFound()
7070

7171
logger.info { "➡️ Requesting metadata for ${request.actionCoords.prettyPrintWithoutVersion}" }
7272

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.Signific
66
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
77
import io.ktor.http.Parameters
88

9-
fun Parameters.parseRequest(extractVersion: Boolean): BindingsServerRequest {
9+
/**
10+
* Returns `null` if the request doesn't make sense and the service should return no resource.
11+
*/
12+
fun Parameters.parseRequest(extractVersion: Boolean): BindingsServerRequest? {
1013
val owner = this["owner"]!!
1114
val nameAndPathAndSignificantVersionParts = this["name"]!!.split("___", limit = 2)
1215
val nameAndPath = nameAndPathAndSignificantVersionParts.first()
@@ -36,7 +39,11 @@ fun Parameters.parseRequest(extractVersion: Boolean): BindingsServerRequest {
3639
if (extractVersion) {
3740
val versionPart = this["version"]!!
3841
if (pinToCommit) {
39-
versionPart.split("__")[1]
42+
val versionParts = versionPart.split("__")
43+
if (versionParts.size < 2) {
44+
return null
45+
}
46+
versionParts[1]
4047
} else {
4148
versionPart
4249
}

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/RequestParsingTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.Signific
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.MINOR
77
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
88
import io.kotest.core.spec.style.FunSpec
9+
import io.kotest.matchers.nulls.shouldBeNull
910
import io.kotest.matchers.shouldBe
1011
import io.ktor.http.Parameters
1112
import io.ktor.http.ParametersBuilder
@@ -104,6 +105,42 @@ class RequestParsingTest :
104105
),
105106
)
106107
}
108+
109+
test("parses commit_lenient syntax") {
110+
val parameters =
111+
createParameters(
112+
owner = "o",
113+
name = "act___commit_lenient",
114+
version = "v1.2.3__323898970401d85df44b3324a610af9a862d54b3",
115+
)
116+
117+
parameters.parseRequest(extractVersion = true) shouldBe
118+
BindingsServerRequest(
119+
rawName = "act___commit_lenient",
120+
rawVersion = "v1.2.3__323898970401d85df44b3324a610af9a862d54b3",
121+
actionCoords =
122+
ActionCoords(
123+
owner = "o",
124+
name = "act",
125+
version = "323898970401d85df44b3324a610af9a862d54b3",
126+
significantVersion = FULL,
127+
comment = "v1.2.3",
128+
versionForTypings = "v1.2.3",
129+
path = null,
130+
),
131+
)
132+
}
133+
134+
test("parses commit_lenient syntax and no commit hash given") {
135+
val parameters =
136+
createParameters(
137+
owner = "o",
138+
name = "act___commit_lenient",
139+
version = "v1.2.3",
140+
)
141+
142+
parameters.parseRequest(extractVersion = true).shouldBeNull()
143+
}
107144
}
108145
},
109146
)

0 commit comments

Comments
 (0)