Skip to content

Commit

Permalink
Merge branch 'master' into #848-auto-update-dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
piotradamczyk5 authored Sep 23, 2020
2 parents 33b9d16 + 4550fb9 commit 005f884
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/macos_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
submodules: true
- uses: actions/setup-java@v1
with:
java-version: 11
java-version: 15
- uses: actions/cache@v2
with:
path: ~/.gradle/caches
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ See [docs/error_monitoring.md](./docs/error_monitoring.md) to disable Bugsnag er

### Contributing

- Install JDK 11 (it works also correctly on the previous version, a newer version is not guaranteed to work properly):
- [Oracle](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html)
- [OpenJDK](https://jdk.java.net/11/)
- Install JDK 15 (it works also correctly on the previous version, a newer version is not guaranteed to work properly):
- [Oracle](https://www.oracle.com/java/technologies/javase-downloads.html#JDK15)
- [OpenJDK](https://openjdk.java.net/projects/jdk/15/)
- [AdoptJDK](https://adoptopenjdk.net/?variant=openjdk15&jvmVariant=hotspot)
- Use [JetBrains Toolbox](https://www.jetbrains.com/toolbox/app/) to install `IntelliJ IDEA Community`
- Clone the repo `git clone --recursive https://github.com/Flank/flank.git`
- `git submodule update --init --recursive` updates the submodules
Expand Down
2 changes: 1 addition & 1 deletion flank-scripts/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-rc-1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ data class GitHubRelease(
)

object GithubReleaseDeserializable : ResponseDeserializable<GitHubRelease> {
override fun deserialize(content: String) = content.toObject(GitHubRelease.serializer())
override fun deserialize(content: String): GitHubRelease = content.toObject()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package flank.scripts.ci.releasenotes

import com.github.kittinunf.fuel.core.ResponseDeserializable
import flank.scripts.utils.toObject
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

@Serializable
data class GithubPullRequest(
Expand All @@ -21,7 +20,5 @@ data class GithubUser(
)

object GithubPullRequestDeserializer : ResponseDeserializable<List<GithubPullRequest>> {
override fun deserialize(content: String): List<GithubPullRequest> {
return Json.decodeFromString(content)
}
override fun deserialize(content: String): List<GithubPullRequest> = content.toObject()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package flank.scripts.exceptions
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.isClientError
import com.github.kittinunf.result.Result
import flank.scripts.github.GitHubErrorResponse
import flank.scripts.release.updatebugsnag.BugSnagResponse
import flank.scripts.utils.toObject

fun <V : Any, E : FuelError, E2 : Exception> Result<V, E>.mapClientError(transform: (E) -> E2) = when (this) {
is Result.Success -> this
is Result.Failure -> if (error.response.isClientError) Result.Failure(transform(error)) else this
}
fun FuelError.toGithubException() =
GitHubException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject(GitHubErrorResponse.serializer()))

fun FuelError.toGithubException() = GitHubException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject())

fun FuelError.toBugsnagException() =
BugsnagException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject(BugSnagResponse.serializer()))
BugsnagException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject())

private const val APPLICATION_JSON_CONTENT_TYPE = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class BugSnagResponse(
)

object BugSnagResponseDeserializer : ResponseDeserializable<BugSnagResponse> {
override fun deserialize(content: String) = content.toObject(BugSnagResponse.serializer())
override fun deserialize(content: String): BugSnagResponse = content.toObject()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private fun createRequestBody(bugsnagApiKey: String, appVersion: String, githubW
builderName = "github-actions",
sourceControl = githubActionsSourceControl(appVersion),
metadata = mapOf("github_actions_build_url" to githubWorkflowUrl)
).toJson(BugSnagRequest.serializer())
).toJson()

private fun githubActionsSourceControl(appVersion: String) = SourceControl(
"github",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package flank.scripts.utils

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

private val json by lazy {
Expand All @@ -10,8 +10,6 @@ private val json by lazy {
}
}

fun <T> T.toJson(serializationStrategy: SerializationStrategy<T>) =
json.encodeToString(serializationStrategy, this)
internal inline fun <reified T> T.toJson() = json.encodeToString(this)

fun <T> String.toObject(deserializationStrategy: DeserializationStrategy<T>) =
json.decodeFromString(deserializationStrategy, this)
internal inline fun <reified T> String.toObject() = json.decodeFromString<T>(this)
11 changes: 6 additions & 5 deletions flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) {
val url = request.url.toString()
return when {
url == "https://api.github.com/repos/Flank/flank/git/refs/tags/success" -> request.buildResponse("", 200)
url == "https://api.github.com/repos/flank/flank/releases/latest" && request.headers["Authorization"].contains("token success") -> request.buildResponse(GitHubRelease("v20.08.0").toJson(GitHubRelease.serializer()), 200)
url == "https://api.github.com/repos/flank/flank/releases/latest" && request.headers["Authorization"].contains("token success") -> request.buildResponse(GitHubRelease("v20.08.0").toJson(), 200)
url == "https://api.github.com/repos/flank/flank/commits/success/pulls" -> request.buildResponse(
Json.encodeToString(githubPullRequestTest), 200)
request.isFailedGithubRequest() -> request.buildResponse(githubErrorBody, 422)
Expand All @@ -52,17 +52,18 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) {
))

private fun Request.handleBugsnagResponse() =
if (body.asString("application/json").toObject(BugSnagRequest.serializer()).apiKey == "success") {
if (body.asString("application/json").toObject<BugSnagRequest>().apiKey == "success") {
buildResponse(
body = BugSnagResponse("success")
.toJson(BugSnagResponse.serializer()), statusCode = 200
body = BugSnagResponse("success").toJson(),
statusCode = 200
)
} else {
buildResponse(
body = BugSnagResponse(
status = "failure",
errors = listOf("errors")
).toJson(BugSnagResponse.serializer()), statusCode = 422
).toJson(),
statusCode = 422
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SerializationTest {
)

// when
val actual = testJson.toObject(GitHubErrorResponse.serializer())
val actual = testJson.toObject<GitHubErrorResponse>()

// then
assertThat(actual).isEqualTo(expected)
Expand All @@ -35,7 +35,7 @@ class SerializationTest {
val expected = "{\"provider\":\"a\",\"repository\":\"b\",\"revision\":\"c\"}"

// when
val actual = testObject.toJson(SourceControl.serializer())
val actual = testObject.toJson()

// then
assertThat(actual).isEqualTo(expected)
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-rc-1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion test_runner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ tasks.withType<io.gitlab.arturbosch.detekt.Detekt> {

// http://www.eclemma.org/jacoco/
jacoco {
toolVersion = "0.8.5"
toolVersion = "0.8.6"
}

tasks.jacocoTestReport {
Expand Down
2 changes: 1 addition & 1 deletion test_runner/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-rc-1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/ArgsHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ object ArgsHelper {
}

fun createJunitBucket(projectId: String, junitGcsPath: String) {
if (useMock || junitGcsPath.isEmpty()) return
if (useMock || junitGcsPath.isBlank()) return
val bucket = junitGcsPath.drop(GCS_PREFIX.length).substringBefore('/')
createGcsBucket(projectId, bucket)
}

// Make best effort to list/create the bucket.
// Due to permission issues, the user may not be able to list or create buckets.
fun createGcsBucket(projectId: String, bucket: String): String {
if (bucket.isEmpty()) return GcToolResults.getDefaultBucket(projectId)
if (bucket.isBlank()) return GcToolResults.getDefaultBucket(projectId)
?: throw FlankGeneralError("Failed to make bucket for $projectId")
if (useMock) return bucket

Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/ValidateCommonArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private fun List<Device>.throwIfAnyMisspelt() =
else Unit

private fun CommonArgs.assertProjectId() {
if (project.isEmpty()) throw FlankConfigurationError(
if (project.isBlank()) throw FlankConfigurationError(
"The project is not set. Define GOOGLE_CLOUD_PROJECT, set project in flank.yml\n" +
"or save service account credential to ${FtlConstants.defaultCredentialPath}\n" +
" See https://github.com/GoogleCloudPlatform/google-cloud-java#specifying-a-project-id"
Expand All @@ -48,7 +48,7 @@ private fun CommonArgs.assertRepeatTests() {

private fun CommonArgs.assertSmartFlankGcsPath() = with(smartFlankGcsPath) {
when {
isEmpty() -> Unit
isBlank() -> Unit

startsWith(FtlConstants.GCS_PREFIX).not() -> throw FlankConfigurationError(
"smart-flank-gcs-path must start with gs://"
Expand Down
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/gc/GcStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object GcStorage {
}

fun uploadJunitXml(testResult: JUnitTestResult, args: IArgs) {
if (args.smartFlankGcsPath.isEmpty() || args.smartFlankDisableUpload) return
if (args.smartFlankGcsPath.isBlank() || args.smartFlankDisableUpload) return

// bucket/path/to/object
val rawPath = args.smartFlankGcsPath.drop(GCS_PREFIX.length)
Expand Down
17 changes: 11 additions & 6 deletions test_runner/src/main/kotlin/ftl/util/BugsnagInitHelper.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ftl.util

import com.bugsnag.Bugsnag
import java.nio.file.Paths
import java.io.File

internal object BugsnagInitHelper {

Expand All @@ -10,10 +10,15 @@ internal object BugsnagInitHelper {
private const val DISABLED = "DISABLED"
private const val FLANK_API_KEY = "3d5f8ba4ee847d6bb51cb9c347eda74f"

internal fun initBugsnag(useMock: Boolean) =
Paths.get(System.getProperty("user.home"), "$GSUTIL_FOLDER/$ANALYTICS_FILE").toFile().run {
if (useMock) null
else if (exists() && readText().trim() == DISABLED) null
else Bugsnag(FLANK_API_KEY)
internal fun initBugsnag(
useMock: Boolean,
rootPath: String = System.getProperty("user.home")
) = when {
useMock -> null
analyticsFileExistAndIsDisabled(rootPath) -> null
else -> Bugsnag(FLANK_API_KEY)
}

private fun analyticsFileExistAndIsDisabled(rootPath: String) =
File(rootPath, "$GSUTIL_FOLDER/$ANALYTICS_FILE").run { exists() && readText().trim() == DISABLED }
}
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/util/Obfuscation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private fun ObfuscationContext.obfuscateAndroidPackageAndClass(packageNameWithCl
.fold("") { previous, next ->
val classChunk = getOrPut(previous) { linkedMapOf() }
val obfuscatedPart = classChunk.getOrPut(next) { nextSymbol(next, classChunk) }
if (previous.isEmpty()) obfuscatedPart else "$previous$ANDROID_PACKAGE_SEPARATOR$obfuscatedPart"
if (previous.isBlank()) obfuscatedPart else "$previous$ANDROID_PACKAGE_SEPARATOR$obfuscatedPart"
}

private fun ObfuscationContext.obfuscateAndroidMethodIfPresent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun TestMatrix.getClientDetails(): Map<String, String>? =

fun TestMatrix.webLinkWithoutExecutionDetails(): String {
val webLink = webLink()
return if (webLink.isEmpty()) {
return if (webLink.isBlank()) {
webLink
} else {
val executionsRegex = "/executions/.+".toRegex()
Expand Down
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun join(first: String, vararg more: String): String {
}

fun assertNotEmpty(str: String, e: String) {
if (str.isEmpty()) {
if (str.isBlank()) {
throw FlankGeneralError(e)
}
}
Expand Down
18 changes: 3 additions & 15 deletions test_runner/src/test/kotlin/ftl/util/FlankBugsnagInitHelperTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ftl.util

import ftl.log.LogbackLogger
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert.assertNotNull
Expand Down Expand Up @@ -37,34 +35,24 @@ class FlankBugsnagInitHelperTest {

@Test
fun `should not create Bugsnag object if user has analytics disabled`() {
mockkStatic(System::class)
every { System.getProperty("user.home") } returns folder.root.absolutePath

val subfolder = folder.newFolder(GSUTIL_FOLDER)
File(subfolder, ANALYTICS_FILE).also { it.writeText(DISABLED) }

assertNull(helper.initBugsnag(useMock = false))
assertNull(helper.initBugsnag(useMock = false, folder.root.absolutePath))
}

@Test
fun `should not create Bugsnag object if user provided analytics-uuid`() {
mockkStatic(System::class)
every { System.getProperty("user.home") } returns folder.root.absolutePath

val subfolder = folder.newFolder(GSUTIL_FOLDER)
File(subfolder, ANALYTICS_FILE).also { it.writeText(UUID.randomUUID().toString()) }

assertNotNull(helper.initBugsnag(useMock = false))
assertNotNull(helper.initBugsnag(useMock = false, folder.root.absolutePath))
}

@Test
fun `should create Bugsnag object if mock server used`() {
mockkStatic(System::class)
every { System.getProperty("user.home") } returns folder.root.absolutePath

val subfolder = folder.newFolder(GSUTIL_FOLDER)
File(subfolder, ANALYTICS_FILE).also { it.writeText(UUID.randomUUID().toString()) }

assertNull(helper.initBugsnag(useMock = true))
assertNull(helper.initBugsnag(useMock = true, folder.root.absolutePath))
}
}

0 comments on commit 005f884

Please sign in to comment.