11import de.undercouch.gradle.tasks.download.Download
22import de.undercouch.gradle.tasks.download.DownloadExtension
3- import groovy.json.JsonSlurper
3+ import java.net.HttpURLConnection
4+ import java.net.URL
45
56plugins {
67 id(" otel.java-conventions" )
@@ -16,26 +17,9 @@ dependencies {
1617 compileOnly(" com.google.auto.value:auto-value-annotations" )
1718}
1819
19- val opampReleaseInfo = tasks.register<Download >(" opampLastReleaseInfo" ) {
20- group = " opamp"
21- src(" https://api.github.com/repos/open-telemetry/opamp-spec/releases/latest" )
22- val token = System .getenv(" GH_TOKEN" )
23- if (token.isNullOrBlank()) {
24- logger.warn(" No GitHub token found in environment variable GH_TOKEN. Rate limits may apply." )
25- } else {
26- header(" Authorization" , " Bearer $token " )
27- header(" X-GitHub-Api-Version" , " 2022-11-28" )
28- }
29- dest(project.layout.buildDirectory.file(" opamp/release.json" ))
30- }
31-
3220val opampProtos = tasks.register<DownloadOpampProtos >(" opampProtoDownload" , download)
3321opampProtos.configure {
3422 group = " opamp"
35- dependsOn(opampReleaseInfo)
36- lastReleaseInfoJson.set {
37- opampReleaseInfo.get().dest
38- }
3923 outputProtosDir.set(project.layout.buildDirectory.dir(" opamp/protos" ))
4024 downloadedZipFile.set(project.layout.buildDirectory.file(" intermediate/$name /release.zip" ))
4125}
@@ -53,20 +37,32 @@ abstract class DownloadOpampProtos @Inject constructor(
5337 private val fileOps : FileSystemOperations ,
5438) : DefaultTask() {
5539
56- @get:InputFile
57- abstract val lastReleaseInfoJson: RegularFileProperty
58-
5940 @get:OutputDirectory
6041 abstract val outputProtosDir: DirectoryProperty
6142
6243 @get:Internal
6344 abstract val downloadedZipFile: RegularFileProperty
6445
65- @Suppress(" UNCHECKED_CAST" )
6646 @TaskAction
6747 fun execute () {
68- val releaseInfo = JsonSlurper ().parse(lastReleaseInfoJson.get().asFile) as Map <String , String >
69- val zipUrl = releaseInfo[" zipball_url" ]
48+ // Get the latest release tag by following the redirect from GitHub's latest release URL
49+ val latestReleaseUrl = " https://github.com/open-telemetry/opamp-spec/releases/latest"
50+ val connection = URL (latestReleaseUrl).openConnection() as HttpURLConnection
51+ connection.instanceFollowRedirects = false
52+ connection.requestMethod = " HEAD"
53+
54+ val redirectLocation = connection.getHeaderField(" Location" )
55+ connection.disconnect()
56+
57+ val latestTag = if (redirectLocation != null && redirectLocation.contains(" /releases/tag/" )) {
58+ // Extract tag from URL like: https://github.com/open-telemetry/opamp-spec/releases/tag/v0.12.0
59+ redirectLocation.substringAfterLast(" /" )
60+ } else {
61+ throw RuntimeException (" Could not determine latest release tag from redirect. Redirect location: $redirectLocation " )
62+ }
63+ // Download the source code for the latest release
64+ val zipUrl = " https://github.com/open-telemetry/opamp-spec/zipball/$latestTag "
65+
7066 download.run {
7167 src(zipUrl)
7268 dest(downloadedZipFile)
0 commit comments