Skip to content

Commit 94272c8

Browse files
committed
Fix JenkinsAPI#changeJenkinsPassword, Add Maven settings.xml Configuration
1 parent b1b5139 commit 94272c8

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

src/main/kotlin/io/codemc/api/jenkins/jenkins.kt

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ package io.codemc.api.jenkins
44

55
import com.cdancy.jenkins.rest.JenkinsClient
66
import io.codemc.api.*
7+
import io.codemc.api.nexus.isSuccess
78
import kotlinx.coroutines.Dispatchers
89
import kotlinx.coroutines.runBlocking
910
import org.jetbrains.annotations.VisibleForTesting
1011
import java.net.http.HttpRequest
12+
import javax.xml.parsers.DocumentBuilderFactory
1113

1214
/**
1315
* The [JenkinsConfig] instance.
@@ -59,7 +61,7 @@ const val NEXUS_CREDENTIALS_ID = "nexus-repository"
5961
*/
6062
const val NEXUS_CREDENTIALS_DESCRIPTION = "Your Nexus Login Details"
6163

62-
internal suspend fun createCredentials(username: String, password: String): Boolean {
64+
internal suspend fun setCredentials(username: String, password: String): Boolean {
6365
// Create Credentials Domain
6466
val checkDomain = req("${jenkinsConfig.url}/job/$username/credentials/store/folder/domain/Services/config.xml") {
6567
GET()
@@ -76,7 +78,7 @@ internal suspend fun createCredentials(username: String, password: String): Bool
7678
header("Content-Type", "application/xml")
7779
}
7880

79-
if (domain.statusCode() != 200) return false
81+
if (!domain.statusCode().isSuccess) return false
8082
}
8183

8284
// Create Credentials Store
@@ -93,8 +95,18 @@ internal suspend fun createCredentials(username: String, password: String): Bool
9395
header("Content-Type", "application/xml")
9496
}
9597

96-
// Either successful or already exists
97-
return store.statusCode() == 200 || store.statusCode() == 409
98+
// Update if Already Exists
99+
if (store.statusCode() == 409) {
100+
val update = req("${jenkinsConfig.url}/job/$username/credentials/store/folder/domain/Services/credential/$NEXUS_CREDENTIALS_ID/config.xml") {
101+
POST(HttpRequest.BodyPublishers.ofString(storeConfig))
102+
103+
header("Authorization", "Basic ${client.authValue()}")
104+
header("Content-Type", "application/xml")
105+
}
106+
107+
return update.statusCode().isSuccess
108+
} else
109+
return store.statusCode().isSuccess
98110
}
99111

100112
/**
@@ -118,18 +130,44 @@ fun checkCredentials(username: String, password: String) = runBlocking(Dispatche
118130
}
119131

120132
if (checkDomain.statusCode() == 404 || checkStore.statusCode() == 404) {
121-
createCredentials(username, password)
133+
setCredentials(username, password)
122134
}
123135
}
124136

137+
/**
138+
* Checks the user `config.xml` present on the Jenkins CI.
139+
* @param username The username of the user.
140+
* @return `true` if the user configuration was changed, `false` otherwise.
141+
*/
142+
fun checkUserConfig(username: String) = runBlocking(Dispatchers.IO) {
143+
val xml = client.api().jobsApi().config("/", username)
144+
var changed = false
145+
146+
val factory = DocumentBuilderFactory.newInstance()
147+
val builder = factory.newDocumentBuilder()
148+
val doc = builder.parse(xml.byteInputStream())
149+
150+
// Check Maven Settings/
151+
if (!xml.contains("<id>nexus-login</id>")) {
152+
val settings = RESOURCE_CACHE[MAVEN_SETTINGS_XML] ?: return@runBlocking
153+
val configs = doc.getElementsByTagName("configs").item(0)
154+
155+
configs.appendChild(builder.parse(settings.byteInputStream()).documentElement)
156+
changed = true
157+
}
158+
159+
if (changed)
160+
client.api().jobsApi().config("/", username, doc.textContent)
161+
}
162+
125163
/**
126164
* Changes the Jenkins password for a user.
127165
* @param username The username of the user.
128166
* @param newPassword The new password.
129167
* @return `true` if the password was changed, `false` otherwise.
130168
*/
131169
fun changeJenkinsPassword(username: String, newPassword: String): Boolean = runBlocking(Dispatchers.IO) {
132-
return@runBlocking createCredentials(username, newPassword)
170+
return@runBlocking setCredentials(username, newPassword)
133171
}
134172

135173
/**
@@ -152,7 +190,7 @@ fun createJenkinsUser(username: String, password: String): Boolean = runBlocking
152190

153191
if (!status.value()) return@runBlocking false
154192

155-
return@runBlocking createCredentials(username, password)
193+
return@runBlocking setCredentials(username, password)
156194
}
157195

158196
/**

src/main/kotlin/io/codemc/api/resources.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ const val CREDENTIALS = "credentials"
3131
*/
3232
const val CREDENTIALS_DOMAIN = "credentials-domain"
3333

34+
/**
35+
* The ID of the maven settings template for jenkins credentials.
36+
*/
37+
const val MAVEN_SETTINGS_XML = "maven-settings"
38+
3439
// Fields
3540

3641
/**
@@ -42,7 +47,8 @@ private val resources = mapOf(
4247
"/templates/jenkins/job-maven.xml" to JOB_MAVEN,
4348
"/templates/jenkins/user-config.xml" to USER_CONFIG,
4449
"/templates/jenkins/credentials.xml" to CREDENTIALS,
45-
"/templates/jenkins/credentials-domain.xml" to CREDENTIALS_DOMAIN
50+
"/templates/jenkins/credentials-domain.xml" to CREDENTIALS_DOMAIN,
51+
"/templates/jenkins/maven/settings.xml" to MAVEN_SETTINGS_XML
4652
)
4753

4854
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig>
2+
<id>nexus-login</id>
3+
<name>Nexus Login Details</name>
4+
<comment>Maven Credentials for your Nexus Repository</comment>
5+
<content>&lt;settings&gt;
6+
&lt;servers&gt;
7+
&lt;server&gt;
8+
&lt;id&gt;${env.JENKINS_USERNAME}&lt;/id&gt;
9+
&lt;username&gt;${env.JENKINS_USERNAME}&lt;/username&gt;
10+
&lt;password&gt;${env.JENKINS_PASSWORD}&lt;/password&gt;
11+
&lt;/server&gt;
12+
&lt;/servers&gt;
13+
&lt;/settings&gt;</content>
14+
<providerId>org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig</providerId>
15+
<serverCredentialMappings/>
16+
<isReplaceAll>true</isReplaceAll>
17+
</org.jenkinsci.plugins.configfiles.maven.MavenSettingsConfig>

src/test/kotlin/io/codemc/api/jenkins/TestJenkins.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ class TestJenkins {
2828
fun testCreateJenkinsUser() {
2929
val u1 = "test"
3030
assertTrue(createJenkinsUser(u1, "test_password"))
31+
checkUserConfig(u1)
32+
3133
assertTrue(getJenkinsUser(u1).isNotEmpty())
3234
assertTrue(deleteUser(u1))
3335
assertTrue(getJenkinsUser(u1).isEmpty())
3436

3537
val u2 = "MyPlayer123"
3638
assertTrue(createJenkinsUser(u2, "MyPassword456"))
39+
checkUserConfig(u2)
40+
3741
assertTrue(getJenkinsUser(u2).isNotEmpty())
3842
assertTrue(deleteUser(u2))
3943
assertTrue(getJenkinsUser(u2).isEmpty())

0 commit comments

Comments
 (0)