-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from ryandens/getStagingProfileTask
✨ Get staging profile task
- Loading branch information
Showing
5 changed files
with
250 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/compatTest/kotlin/io/github/gradlenexus/publishplugin/MethodScopeWiremockResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.gradlenexus.publishplugin | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import org.junit.jupiter.api.extension.AfterEachCallback | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.api.extension.ParameterContext | ||
import org.junit.jupiter.api.extension.ParameterResolver | ||
import ru.lanwen.wiremock.ext.WiremockResolver | ||
import java.lang.IllegalArgumentException | ||
|
||
/** | ||
* Composes a [WiremockResolver] and uses that by default. But, if a parameter is annotated | ||
* with [MethodScopeWiremockResolver] it creates a new instance of the [WiremockResolver] extension and | ||
* manages its lifecycle in the scope of that [ExtensionContext] | ||
*/ | ||
class MethodScopeWiremockResolver( | ||
private val inner: WiremockResolver = WiremockResolver() | ||
) : ParameterResolver by inner, AfterEachCallback { | ||
|
||
/** | ||
* Checks to see if a method the parameter is annotated with [MethodScopedWiremockServer]. If it is, we first verify | ||
*/ | ||
override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any { | ||
return if (parameterContext.isAnnotated(MethodScopedWiremockServer::class.java)) { | ||
if (!parameterContext.parameter.type.isAssignableFrom(WireMockServer::class.java)) { | ||
throw IllegalArgumentException("Annotated type must be a WireMockServer") | ||
} | ||
return getStore(extensionContext) | ||
.getOrComputeIfAbsent(Keys.LOCAL_RESOLVER, { WiremockResolver() }, WiremockResolver::class.java) | ||
.resolveParameter(parameterContext, extensionContext) | ||
} else { | ||
inner.resolveParameter(parameterContext, extensionContext) | ||
} | ||
} | ||
|
||
override fun afterEach(context: ExtensionContext) { | ||
getStore(context).get(Keys.LOCAL_RESOLVER, WiremockResolver::class.java)?.afterEach(context) | ||
inner.afterEach(context) | ||
} | ||
|
||
/** | ||
* helper method for get getting a [ExtensionContext.Store] specific to a test method | ||
*/ | ||
private fun getStore(context: ExtensionContext): ExtensionContext.Store { | ||
return context.getStore(ExtensionContext.Namespace.create(javaClass)) | ||
} | ||
|
||
/** | ||
* Keys for storing and accessing [MethodScopeWiremockResolver]s | ||
*/ | ||
enum class Keys { | ||
LOCAL_RESOLVER | ||
} | ||
|
||
/** | ||
* Decorates a parameter also annotated with [WiremockResolver.Wiremock] | ||
*/ | ||
@Target(allowedTargets = [AnnotationTarget.VALUE_PARAMETER]) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class MethodScopedWiremockServer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/io/github/gradlenexus/publishplugin/RetrieveStagingProfile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.github.gradlenexus.publishplugin | ||
|
||
import io.github.gradlenexus.publishplugin.internal.NexusClient | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.Incubating | ||
import org.gradle.api.model.ObjectFactory | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.kotlin.dsl.property | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Diagnostic task for retrieving the [NexusRepository.stagingProfileId] for the [packageGroup] from the provided [NexusRepository] and logging it | ||
*/ | ||
@Incubating | ||
open class RetrieveStagingProfile @Inject constructor( | ||
objects: ObjectFactory, | ||
extension: NexusPublishExtension, | ||
repository: NexusRepository | ||
) : AbstractNexusStagingRepositoryTask(objects, extension, repository) { | ||
|
||
@Input | ||
val packageGroup = objects.property<String>().apply { | ||
set(extension.packageGroup) | ||
} | ||
|
||
@TaskAction | ||
fun retrieveStagingProfile() { | ||
val repository = repository.get() | ||
|
||
val client = NexusClient( | ||
repository.nexusUrl.get(), | ||
repository.username.orNull, | ||
repository.password.orNull, | ||
clientTimeout.orNull, | ||
connectTimeout.orNull | ||
) | ||
|
||
val packageGroup = packageGroup.get() | ||
val stagingProfileId = client.findStagingProfileId(packageGroup) | ||
?: throw GradleException("Failed to find staging profile for package group: $packageGroup") | ||
logger.lifecycle("Received staging profile id: '{}' for package {}", stagingProfileId, packageGroup) | ||
} | ||
} |