@@ -4,10 +4,12 @@ package io.codemc.api.jenkins
44
55import com.cdancy.jenkins.rest.JenkinsClient
66import io.codemc.api.*
7+ import io.codemc.api.nexus.isSuccess
78import kotlinx.coroutines.Dispatchers
89import kotlinx.coroutines.runBlocking
910import org.jetbrains.annotations.VisibleForTesting
1011import 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 */
6062const 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 */
131169fun 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/* *
0 commit comments