Skip to content

Commit cc16875

Browse files
committed
Add Exists Methods
- `JenkinsAPI#existsUser` - `JenkinsAPI#existsJob` - `NexusAPI#exists`
1 parent fb633ff commit cc16875

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ fun getJenkinsUser(username: String): String = runBlocking(Dispatchers.IO) {
212212
return@runBlocking res.body() ?: ""
213213
}
214214

215+
/**
216+
* Checks if a Jenkins user exists.
217+
* @param username The username of the user.
218+
* @return `true` if the user exists, `false` otherwise.
219+
*/
220+
fun existsUser(username: String): Boolean = getJenkinsUser(username).isNotEmpty()
221+
222+
/**
223+
* Checks if a Jenkins job exists.
224+
* @param username The username of the user.
225+
* @param jobName The name of the job.
226+
* @return `true` if the job exists, `false` otherwise.
227+
*/
228+
fun existsJob(username: String, jobName: String): Boolean = getJenkinsJob(username, jobName).isNotEmpty()
229+
215230
/**
216231
* Gets all Jenkins users.
217232
* @return A list of all Jenkins users mapped by their username.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ fun getNexusUser(name: String): JsonObject? = runBlocking {
205205
return@runBlocking (json.decodeFromString(res.body()) as? JsonArray)?.firstOrNull() as? JsonObject
206206
}
207207

208+
/**
209+
* Checks if a Nexus User exists by its case-sensitive name.
210+
* @param name The name of the nexus user.
211+
* @return `true` if the user exists, `false` otherwise
212+
*/
213+
fun exists(name: String) = getNexusUser(name) != null
214+
208215
@VisibleForTesting
209216
internal suspend fun getNexusRole(name: String): JsonObject? {
210217
val res = nexus("$API_URL/security/roles/$name")

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class TestJenkins {
3838
checkUserConfig(u1)
3939

4040
assertTrue(getJenkinsUser(u1).isNotEmpty())
41+
assertTrue(existsUser(u1))
4142
assertTrue(deleteUser(u1))
4243
assertTrue(getJenkinsUser(u1).isEmpty())
44+
assertFalse(existsUser(u1))
4345

4446
val u2 = "MyPlayer123"
4547
assertTrue(createJenkinsUser(u2, "MyPassword456"))
@@ -84,6 +86,7 @@ class TestJenkins {
8486
val j1 = "TestJob_Freestyle"
8587
assertTrue(createJenkinsJob(name, j1, url, true))
8688
assertTrue(getJenkinsJob(name, j1).isNotEmpty())
89+
assertTrue(existsJob(name, j1))
8790

8891
val i1 = getJobInfo(name, j1)
8992
assertNotNull(i1)
@@ -92,8 +95,10 @@ class TestJenkins {
9295
val j2 = "TestJob2_Freestyle"
9396
assertTrue(createJenkinsJob(name, j2, url, true))
9497
assertTrue(getJenkinsJob(name, j2).isNotEmpty())
98+
assertTrue(existsJob(name, j2))
9599
assertTrue(deleteJob(name, j2))
96100
assertTrue(getJenkinsJob(name, j2).isEmpty())
101+
assertFalse(existsJob(name, j2))
97102

98103
val j3 = "TestJob_Maven"
99104
assertTrue(createJenkinsJob(name, j3, url, false))

src/test/kotlin/io/codemc/api/nexus/TestNexus.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestNexus {
4343

4444
assertTrue(createNexus(name, UUID.randomUUID().toString()))
4545
assertFalse(getNexusUser(name).isNullOrEmpty())
46+
assertTrue(exists(name))
4647
assertTrue(getNexusUser("OtherName").isNullOrEmpty())
4748
assertTrue(getRepositories().isNotEmpty())
4849
assertFalse(getNexusRepository(repoName).isNullOrEmpty())

0 commit comments

Comments
 (0)