@@ -61,15 +61,23 @@ const val NEXUS_CREDENTIALS_DESCRIPTION = "Your Nexus Login Details"
61
61
62
62
internal suspend fun createCredentials (username : String , password : String ): Boolean {
63
63
// Create Credentials Domain
64
- val domainConfig = RESOURCE_CACHE [CREDENTIALS_DOMAIN ] ? : return false
65
- val domain = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/createDomain" ) {
66
- POST (HttpRequest .BodyPublishers .ofString(domainConfig))
64
+ val checkDomain = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/domain/Services/config.xml" ) {
65
+ GET ()
67
66
68
67
header(" Authorization" , " Basic ${client.authValue()} " )
69
- header(" Content-Type" , " application/xml" )
70
68
}
71
69
72
- if (domain.statusCode() != 200 ) return false
70
+ if (checkDomain.statusCode() == 404 ) {
71
+ val domainConfig = RESOURCE_CACHE [CREDENTIALS_DOMAIN ] ? : return false
72
+ val domain = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/createDomain" ) {
73
+ POST (HttpRequest .BodyPublishers .ofString(domainConfig))
74
+
75
+ header(" Authorization" , " Basic ${client.authValue()} " )
76
+ header(" Content-Type" , " application/xml" )
77
+ }
78
+
79
+ if (domain.statusCode() != 200 ) return false
80
+ }
73
81
74
82
// Create Credentials Store
75
83
val storeConfig = (RESOURCE_CACHE [CREDENTIALS ] ? : return false )
@@ -85,49 +93,43 @@ internal suspend fun createCredentials(username: String, password: String): Bool
85
93
header(" Content-Type" , " application/xml" )
86
94
}
87
95
88
- return store.statusCode() == 200
96
+ // Either successful or already exists
97
+ return store.statusCode() == 200 || store.statusCode() == 409
89
98
}
90
99
91
100
/* *
92
- * Changes the Jenkins password for a user .
101
+ * Checks if the Jenkins credentials exist, and creates them if they don't .
93
102
* @param username The username of the user.
94
- * @param newPassword The new password.
95
- * @return `true` if the password was changed, `false` otherwise.
103
+ * @param password The password of the user.
96
104
*/
97
- fun changeJenkinsPassword (username : String , newPassword : String ): Boolean = runBlocking(Dispatchers .IO ) {
98
- // Create Credentials Domain
105
+ fun checkCredentials (username : String , password : String ) = runBlocking(Dispatchers .IO ) {
106
+ // Check Domain
99
107
val checkDomain = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/domain/Services/config.xml" ) {
100
108
GET ()
101
109
102
110
header(" Authorization" , " Basic ${client.authValue()} " )
103
111
}
104
112
105
- if (checkDomain.statusCode() == 404 ) {
106
- val domainConfig = RESOURCE_CACHE [CREDENTIALS_DOMAIN ] ? : return @runBlocking false
107
- val domain = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/createDomain" ) {
108
- POST (HttpRequest .BodyPublishers .ofString(domainConfig))
109
-
110
- header(" Authorization" , " Basic ${client.authValue()} " )
111
- header(" Content-Type" , " application/xml" )
112
- }
113
+ // Check Credential Store
114
+ val checkStore = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/domain/Services/credential/$NEXUS_CREDENTIALS_ID /config.xml" ) {
115
+ GET ()
113
116
114
- if (domain.statusCode() != 200 ) return @runBlocking false
117
+ header( " Authorization " , " Basic ${client.authValue()} " )
115
118
}
116
119
117
- val config = (RESOURCE_CACHE [CREDENTIALS ] ? : return @runBlocking false )
118
- .replace(" {ID}" , NEXUS_CREDENTIALS_ID )
119
- .replace(" {DESCRIPTION}" , NEXUS_CREDENTIALS_DESCRIPTION )
120
- .replace(" {USERNAME}" , username.lowercase())
121
- .replace(" {PASSWORD}" , newPassword)
122
-
123
- val res = req(" ${jenkinsConfig.url} /job/$username /credentials/store/folder/domain/Services/credential/$NEXUS_CREDENTIALS_ID /config.xml" ) {
124
- POST (HttpRequest .BodyPublishers .ofString(config))
125
-
126
- header(" Authorization" , " Basic ${client.authValue()} " )
127
- header(" Content-Type" , " application/xml" )
120
+ if (checkDomain.statusCode() == 404 || checkStore.statusCode() == 404 ) {
121
+ createCredentials(username, password)
128
122
}
123
+ }
129
124
130
- return @runBlocking res.statusCode() == 200
125
+ /* *
126
+ * Changes the Jenkins password for a user.
127
+ * @param username The username of the user.
128
+ * @param newPassword The new password.
129
+ * @return `true` if the password was changed, `false` otherwise.
130
+ */
131
+ fun changeJenkinsPassword (username : String , newPassword : String ): Boolean = runBlocking(Dispatchers .IO ) {
132
+ return @runBlocking createCredentials(username, newPassword)
131
133
}
132
134
133
135
/* *
@@ -137,6 +139,8 @@ fun changeJenkinsPassword(username: String, newPassword: String): Boolean = runB
137
139
* @return `true` if the user was created, `false` otherwise.
138
140
*/
139
141
fun createJenkinsUser (username : String , password : String ): Boolean = runBlocking(Dispatchers .IO ) {
142
+ if (getJenkinsUser(username).isNotEmpty()) return @runBlocking false
143
+
140
144
val config0 = RESOURCE_CACHE [USER_CONFIG ] ? : return @runBlocking false
141
145
142
146
val config = config0
@@ -184,6 +188,8 @@ fun createJenkinsJob(
184
188
isFreestyle : Boolean ,
185
189
config : (String ) -> String = { it }
186
190
): Boolean {
191
+ if (getJenkinsJob(username, jobName).isNotEmpty()) return false
192
+
187
193
val template = (if (isFreestyle) RESOURCE_CACHE [JOB_FREESTYLE ] else RESOURCE_CACHE [JOB_MAVEN ])
188
194
?.replace(" {PROJECT_URL}" , repoLink) ? : return false
189
195
0 commit comments