From 99e7321add84e8d0e7eebbc57858dec72b5a0775 Mon Sep 17 00:00:00 2001 From: Gregory Mitchell <54124162+gmitch215@users.noreply.github.com> Date: Thu, 25 Jul 2024 20:14:32 -0400 Subject: [PATCH] Add Server Credentials to User Configuration --- .../kotlin/io/codemc/api/jenkins/jenkins.kt | 9 ++++--- .../templates/jenkins/job-freestyle.xml | 12 ++++++++- .../resources/templates/jenkins/job-maven.xml | 12 ++++++++- .../templates/jenkins/user-config.xml | 26 +++++++++++++++---- src/test/docker/jenkins/plugins.yml | 3 ++- .../io/codemc/api/jenkins/TestJenkins.kt | 6 ++--- 6 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/io/codemc/api/jenkins/jenkins.kt b/src/main/kotlin/io/codemc/api/jenkins/jenkins.kt index 7cdae22..b8d2b86 100644 --- a/src/main/kotlin/io/codemc/api/jenkins/jenkins.kt +++ b/src/main/kotlin/io/codemc/api/jenkins/jenkins.kt @@ -8,8 +8,6 @@ import io.codemc.api.JOB_MAVEN import io.codemc.api.RESOURCE_CACHE import io.codemc.api.USER_CONFIG import org.jetbrains.annotations.VisibleForTesting -import java.net.URI -import java.net.URL var jenkinsConfig: JenkinsConfig = JenkinsConfig("", "", "") set(value) { @@ -35,8 +33,11 @@ data class JenkinsConfig( fun ping(): Boolean = client.api().systemApi().systemInfo().jenkinsVersion() != null -fun createJenkinsUser(username: String): Boolean { - val config = RESOURCE_CACHE[USER_CONFIG]?.replace("{USERNAME}", username) ?: return false +fun createJenkinsUser(username: String, password: String): Boolean { + val config0 = RESOURCE_CACHE[USER_CONFIG] ?: return false + val config = config0 + .replace("{USERNAME}", username) + .replace("{PASSWORD}", password) val status = client.api().jobsApi().create("/", username, config) return status.value() diff --git a/src/main/resources/templates/jenkins/job-freestyle.xml b/src/main/resources/templates/jenkins/job-freestyle.xml index f656d64..0793767 100644 --- a/src/main/resources/templates/jenkins/job-freestyle.xml +++ b/src/main/resources/templates/jenkins/job-freestyle.xml @@ -54,5 +54,15 @@ false - + + + + + nexus-repository + JENKINS_USERNAME + JENKINS_PASSWORD + + + + \ No newline at end of file diff --git a/src/main/resources/templates/jenkins/job-maven.xml b/src/main/resources/templates/jenkins/job-maven.xml index 5048baa..75ec3d8 100644 --- a/src/main/resources/templates/jenkins/job-maven.xml +++ b/src/main/resources/templates/jenkins/job-maven.xml @@ -80,7 +80,17 @@ false - + + + + + nexus-repository + JENKINS_USERNAME + JENKINS_PASSWORD + + + + diff --git a/src/main/resources/templates/jenkins/user-config.xml b/src/main/resources/templates/jenkins/user-config.xml index 37a8133..2a680af 100644 --- a/src/main/resources/templates/jenkins/user-config.xml +++ b/src/main/resources/templates/jenkins/user-config.xml @@ -1,9 +1,9 @@ - + - + - + @@ -43,13 +43,29 @@ + + + Server Credentials + Your credentials for your Nexus Repository + + + + + nexus-repository + Your Nexus Login Details + {USERNAME} + {PASSWORD} + true + + + - + - + false diff --git a/src/test/docker/jenkins/plugins.yml b/src/test/docker/jenkins/plugins.yml index 8b17c76..549d9c4 100644 --- a/src/test/docker/jenkins/plugins.yml +++ b/src/test/docker/jenkins/plugins.yml @@ -6,4 +6,5 @@ plugins: - artifactId: configuration-as-code - artifactId: workflow-aggregator - artifactId: badge - - artifactId: github-oauth \ No newline at end of file + - artifactId: github-oauth + - artifactId: credentials \ No newline at end of file diff --git a/src/test/kotlin/io/codemc/api/jenkins/TestJenkins.kt b/src/test/kotlin/io/codemc/api/jenkins/TestJenkins.kt index fad562f..d6e3fc2 100644 --- a/src/test/kotlin/io/codemc/api/jenkins/TestJenkins.kt +++ b/src/test/kotlin/io/codemc/api/jenkins/TestJenkins.kt @@ -22,10 +22,10 @@ object TestJenkins { @Test fun testCreateJenkinsUser() { - assert(createJenkinsUser("test")) + assert(createJenkinsUser("test", "test_password")) assert(getJenkinsUser("test").isNotEmpty()) - assert(createJenkinsUser("MyPlayer123")) + assert(createJenkinsUser("MyPlayer123", "MyPassword456")) assert(getJenkinsUser("MyPlayer123").isNotEmpty()) } @@ -34,7 +34,7 @@ object TestJenkins { val name = "TestUsername" val url = "https://github.com/TestUsername/TestRepo" - assert(createJenkinsUser(name)) + assert(createJenkinsUser(name, "TestPassword")) assert(getJenkinsUser(name).isNotEmpty()) assert(createJenkinsJob(name, "TestJob_Freestyle", url, true))