Skip to content

Commit

Permalink
Read default ENV variables for unknown CI (#48)
Browse files Browse the repository at this point in the history
Use the values of the recommended ENV variables for non-supported CIs
  • Loading branch information
mokkun authored Jun 22, 2021
1 parent 964cd3c commit fe9db8b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/kotlin/ServiceInfoParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ class ServiceInfoParser(val envGetter: EnvGetter) {
branch = envGetter("BITRISE_GIT_BRANCH"),
buildUrl = envGetter("BITRISE_BUILD_URL")
)
else -> ServiceInfo("other")
else -> ServiceInfo(
name = envGetter("CI_NAME") ?: "other",
number = envGetter("CI_BUILD_NUMBER"),
pr = envGetter("CI_PULL_REQUEST"),
branch = envGetter("CI_BRANCH"),
buildUrl = envGetter("CI_BUILD_URL")
)
}
}
}
42 changes: 41 additions & 1 deletion src/test/kotlin/ServiceInfoParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,54 @@ internal class ServiceInfoParserTest {
}

@Test
fun `ServiceInfoParser parses unidentifiable ci as other`() {
fun `ServiceInfoParser parses unidentifiable ci as other when no env is set`() {
val envGetter = createEnvGetter(emptyMap())

val actual = ServiceInfoParser(envGetter).parse()
val expected = ServiceInfo("other")
assertEquals(expected, actual)
}

@Test
fun `ServiceInfoParser parses unidentifiable ci with default env on master`() {
val envGetter = createEnvGetter(mapOf(
"CI_NAME" to "teamcity",
"CI_BUILD_NUMBER" to "123123",
"CI_BUILD_URL" to "https://localhost:8111/viewLog.html?buildId=123123",
"CI_BRANCH" to "master",
))

val actual = ServiceInfoParser(envGetter).parse()
val expected = ServiceInfo(
name = "teamcity",
number = "123123",
buildUrl = "https://localhost:8111/viewLog.html?buildId=123123",
branch = "master",
)
assertEquals(expected, actual)
}

@Test
fun `ServiceInfoParser parses unidentifiable ci with default env on pr`() {
val envGetter = createEnvGetter(mapOf(
"CI_NAME" to "teamcity",
"CI_BUILD_NUMBER" to "123123",
"CI_BUILD_URL" to "https://localhost:8111/viewLog.html?buildId=123123",
"CI_BRANCH" to "foobar",
"CI_PULL_REQUEST" to "11",
))

val actual = ServiceInfoParser(envGetter).parse()
val expected = ServiceInfo(
name = "teamcity",
number = "123123",
buildUrl = "https://localhost:8111/viewLog.html?buildId=123123",
branch = "foobar",
pr = "11"
)
assertEquals(expected, actual)
}

private fun createEnvGetter(entries: Map<String, String>): EnvGetter {
return { k: String -> entries[k] }
}
Expand Down

0 comments on commit fe9db8b

Please sign in to comment.