Skip to content

Commit 147a172

Browse files
authored
Fix encoding GCS objects (#90)
1 parent 7d239f8 commit 147a172

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

librarian-gradle-plugin/src/main/kotlin/com/gradleup/librarian/gradle/internal/task/generateMarkerFiles.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import gratatouille.tasks.GLogger
1616
import gratatouille.tasks.GOutputDirectory
1717
import gratatouille.tasks.GTask
1818
import java.io.File
19+
import java.time.Instant
1920
import java.util.zip.ZipInputStream
2021

2122

@@ -100,7 +101,13 @@ fun generateMarkerFiles(
100101
)
101102
)
102103

103-
val file = resolve("$artifactId-$mainVersion.pom")
104+
val v = if (mainVersion.endsWith("-SNAPSHOT")) {
105+
// Do like Gradle and replace -SNAPSHOT by a timestamp and a buildNumber of 1
106+
"${mainVersion.substringBefore("-SNAPSHOT")}-${timestampNow()}-1"
107+
} else {
108+
mainVersion
109+
}
110+
val file = resolve("$artifactId-$v.pom")
104111
file.writeText(project.serialize())
105112
file.writeChecksums()
106113
if (privateKey != null) {
@@ -136,3 +143,8 @@ private fun File.writeChecksums() {
136143
}
137144
}
138145

146+
internal fun timestampNow(): String {
147+
val now = Instant.now().atZone(java.time.ZoneOffset.UTC)
148+
149+
return String.format("%04d%02d%02d.%02d%02d%02d", now.year, now.monthValue, now.dayOfMonth, now.hour, now.minute, now.second)
150+
}

librarian-gradle-plugin/src/main/kotlin/com/gradleup/librarian/gradle/internal/task/publishToGcs.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import okhttp3.HttpUrl.Companion.toHttpUrl
1212
import okhttp3.OkHttpClient
1313
import okhttp3.Request
1414
import okio.BufferedSource
15+
import java.net.URLEncoder
1516
import java.time.Duration
1617
import kotlin.math.pow
1718
import kotlin.time.Duration.Companion.seconds
@@ -57,9 +58,14 @@ internal class GcsTransport(
5758

5859
val name = "${prefix}${path}"
5960
logger.info("Librarian: gcs-get $name")
61+
6062
val url = getBaseUrl
6163
.newBuilder()
62-
.addPathSegments(name)
64+
/**
65+
* Send the object name as a single segment as this is required by GCP:
66+
* https://cloud.google.com/storage/docs/request-endpoints#encoding
67+
*/
68+
.addPathSegment(name)
6369
.addQueryParameter("alt", "media")
6470
.build()
6571
val request = Request.Builder()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import okhttp3.HttpUrl.Companion.toHttpUrl
2+
import java.net.URLEncoder
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class UrlTest {
7+
@Test
8+
fun test() {
9+
val name = "m2/com/gradleup/compat/patrouille/compat-patrouille-gradle-plugin/0.0.1-SNAPSHOT/maven-metadata.xml"
10+
11+
val httpUrl = "https://example.com".toHttpUrl().newBuilder()
12+
.addPathSegment(name)
13+
.build()
14+
.toString()
15+
16+
val usingUrlEncode = "https://example.com/" + URLEncoder.encode(name, "UTF-8")
17+
assertEquals(httpUrl, usingUrlEncode)
18+
}
19+
}

0 commit comments

Comments
 (0)