Skip to content

Commit

Permalink
fix: Removed mandatory GitHub token on artifacts downloading (#1761)
Browse files Browse the repository at this point in the history
Fixes #1695 

## Test Plan
> How do we know the code works?

Flank should download test artifacts without any exceptions when GitHub token is not set in environment variables.

- [X] Unit tested
  • Loading branch information
adamfilipow92 authored Mar 31, 2021
1 parent 238e379 commit af10786
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion flank-scripts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.9.6"
version = "1.9.7"
group = "com.github.flank"

application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,12 @@ fun Request.appendGitHubHeaders(githubToken: String, contentType: String = "appl
fun githubRepo(
token: String,
repoPath: String
): Repo =
RtGithub(token)
.repos()
.get(Coordinates.Simple(repoPath))
): Repo = createGitHubClient(token).repos().get(Coordinates.Simple(repoPath))

private fun createGitHubClient(gitHubToken: String) = when {
gitHubToken.isEmpty() -> RtGithub()
else -> RtGithub(gitHubToken)
}

fun Repo.getRelease(tag: String): Release.Smart? =
Releases.Smart(releases()).run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ val File.testArtifacts: File get() = resolve(TEST_ARTIFACTS_PATH).apply { if (!e

fun File.testArtifacts(branch: String = currentGitBranch()): File = testArtifacts.resolve(branch)

internal fun testArtifactsRepo(): Repo = githubRepo(getEnv(GITHUB_TOKEN_ENV_KEY), flankTestArtifactsRepository)
internal fun testArtifactsRepo(): Repo =
githubRepo(getEnv(GITHUB_TOKEN_ENV_KEY, ""), flankTestArtifactsRepository)
5 changes: 2 additions & 3 deletions flank-scripts/src/main/kotlin/flank/scripts/utils/Env.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package flank.scripts.utils

fun getEnv(key: String): String =
System.getenv(key) ?: throw RuntimeException("Environment variable '$key' not found!")

fun getEnv(key: String, default: String? = null): String =
System.getenv(key) ?: default ?: throw RuntimeException("Environment variable '$key' not found!")
val isWindows: Boolean = System.getProperty("os.name").startsWith("Windows")
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package flank.scripts.ops.testartifacts

import flank.scripts.data.github.getRelease
import flank.scripts.utils.getEnv
import io.mockk.every
import io.mockk.mockkStatic
import org.junit.Test

class TestArtifactsRepoTest {

@Test
fun `should not throw when gh token not set`() {
mockkStatic(::getEnv)
every { getEnv(any(), any()) } returns ""
testArtifactsRepo().getRelease("master")
?.body()?.toLong()
}
}

0 comments on commit af10786

Please sign in to comment.