Skip to content

Commit

Permalink
set User-Agent header for requests to sonatype, second attempt (#479)
Browse files Browse the repository at this point in the history
* set User-Agent header for requests to sonatype, second attempt

This reverts commit 343304b.

* fix classpath issue

* Update gradle/libs.versions.toml

Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>

Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>
  • Loading branch information
gabrielittner and vanniktech committed Dec 26, 2022
1 parent 6d4f65a commit db9392a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions build-logic/build.gradle.kts
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation(libs.kotlin.plugin)
implementation(libs.ktlint.plugin)
implementation(libs.maven.publish.plugin)
implementation(libs.buildconfig.plugin)
// https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Expand Up @@ -11,6 +11,7 @@ dokka = "org.jetbrains.dokka:dokka-gradle-plugin:1.7.20"
android-plugin = "com.android.tools.build:gradle:7.3.1"
ktlint-plugin = "org.jlleitschuh.gradle:ktlint-gradle:11.0.0"
maven-publish-plugin = "com.vanniktech:gradle-maven-publish-plugin:0.22.0"
buildconfig-plugin = "com.github.gmazzo:gradle-buildconfig-plugin:3.1.0"

kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
Expand All @@ -32,3 +33,6 @@ truth = { module = "com.google.truth:truth", version.ref = "truth" }
truth-java8 = { module = "com.google.truth.extensions:truth-java8-extension", version.ref = "truth" }
truth-testKit = "com.autonomousapps:testkit-truth:1.1"
maven-model = "org.apache.maven:maven-model:3.8.6"

[plugins]
buildconfig = { id = "com.github.gmazzo.buildconfig", version = "3.1.0" }
Expand Up @@ -12,10 +12,12 @@ class Nexus(
private val baseUrl: String,
private val username: String,
password: String,
userAgentName: String,
userAgentVersion: String,
) {
private val service by lazy {
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(NexusOkHttpInterceptor(username, password))
.addInterceptor(NexusOkHttpInterceptor(username, password, userAgentName, userAgentVersion))
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
Expand Down
Expand Up @@ -4,12 +4,18 @@ import okhttp3.Credentials
import okhttp3.Interceptor
import okhttp3.Response

internal class NexusOkHttpInterceptor(private val username: String, private val password: String) : Interceptor {
internal class NexusOkHttpInterceptor(
private val username: String,
private val password: String,
private val userAgentName: String,
private val userAgentVersion: String,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()

requestBuilder.addHeader("Accept", "application/json") // request json by default, XML is returned else
requestBuilder.addHeader("Authorization", Credentials.basic(username, password))
requestBuilder.addHeader("User-Agent", "$userAgentName/$userAgentVersion")

return chain.proceed(requestBuilder.build())
}
Expand Down
7 changes: 7 additions & 0 deletions plugin/build.gradle.kts
@@ -1,6 +1,7 @@
plugins {
id("shared")
id("java-gradle-plugin")
alias(libs.plugins.buildconfig)
}

gradlePlugin {
Expand All @@ -20,6 +21,12 @@ gradlePlugin {
}
}

buildConfig {
packageName("com.vanniktech.maven.publish")
buildConfigField("String", "NAME", "\"com.vanniktech.maven.publish\"")
buildConfigField("String", "VERSION", "\"${project.property("VERSION_NAME")}\"")
}

val integrationTestSourceSet = sourceSets.create("integrationTest") {
compileClasspath += sourceSets["main"].output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
Expand Down
@@ -1,5 +1,6 @@
package com.vanniktech.maven.publish.sonatype

import com.vanniktech.maven.publish.BuildConfig
import com.vanniktech.maven.publish.SonatypeHost
import com.vanniktech.maven.publish.nexus.Nexus
import java.io.IOException
Expand Down Expand Up @@ -30,6 +31,8 @@ internal abstract class SonatypeRepositoryBuildService : BuildService<SonatypeRe
baseUrl = parameters.sonatypeHost.get().apiBaseUrl(),
username = parameters.repositoryUsername.get(),
password = parameters.repositoryPassword.get(),
userAgentName = BuildConfig.NAME,
userAgentVersion = BuildConfig.VERSION,
)
}

Expand Down

0 comments on commit db9392a

Please sign in to comment.