Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
fkorotkov committed Aug 18, 2022
1 parent c30856e commit 3ed3280
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/org/cirruslabs/sq/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fun main() {
configureRouting()
}.start(wait = true)
}

fun Application.configureRouting() {
val githubSecrets: GithubAppSecrets = GithubAppSecretsImpl.initialize()
val gitHubScheme: String = System.getenv().getOrDefault("GITHUB_API_SCHEME", "https")
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/org/cirruslabs/sq/SubmitQueueLogic.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.cirruslabs.sq

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.first
import org.cirruslabs.sq.github.GitHubAPI
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/org/cirruslabs/sq/github/GitHubAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import org.cirruslabs.sq.github.api.Status
interface GitHubAPI {
suspend fun listCheckSuites(installationId: Long, owner: String, repo: String, ref: String): Flow<CheckSuite>
suspend fun listCheckRuns(installationId: Long, owner: String, repo: String, checkSuiteId: Long): Flow<CheckRun>
suspend fun listPullRequests(installationId: Long, owner: String, repo: String, params: Map<String, String> = emptyMap()): Flow<PullRequest>
suspend fun listPullRequests(
installationId: Long,
owner: String,
repo: String,
params: Map<String, String> = emptyMap()
): Flow<PullRequest>

suspend fun setStatus(installationId: Long, owner: String, repo: String, sha: String, status: Status): Status
}
32 changes: 25 additions & 7 deletions src/main/kotlin/org/cirruslabs/sq/github/impl/GitHubAPIImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import io.ktor.client.statement.*
import io.ktor.client.utils.*
import io.ktor.http.*
import io.ktor.serialization.gson.*
import io.ktor.util.*
import io.ktor.utils.io.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.emitAll
Expand Down Expand Up @@ -61,7 +58,12 @@ class GitHubAPIImpl constructor(
}
}

private suspend fun post(installationId: Long, path: String, body: Any = EmptyContent, block: HttpRequestBuilder.() -> Unit = {}): HttpResponse {
private suspend fun post(
installationId: Long,
path: String,
body: Any = EmptyContent,
block: HttpRequestBuilder.() -> Unit = {}
): HttpResponse {
return httpClient.post(
"$baseAPIScheme://$baseAPIHost/${path.removePrefix("/")}"
) {
Expand All @@ -84,7 +86,12 @@ class GitHubAPIImpl constructor(
private fun noMorePages(response: HttpResponse) =
response.headers.getAll(HttpHeaders.Link)?.size != 2

override suspend fun listCheckSuites(installationId: Long, owner: String, repo: String, ref: String): Flow<CheckSuite> {
override suspend fun listCheckSuites(
installationId: Long,
owner: String,
repo: String,
ref: String
): Flow<CheckSuite> {
return flow {
val infinitePagesSequence = generateSequence(1) { it + 1 }
for (page in infinitePagesSequence) {
Expand All @@ -103,7 +110,12 @@ class GitHubAPIImpl constructor(
}
}

override suspend fun listCheckRuns(installationId: Long, owner: String, repo: String, checkSuiteId: Long): Flow<CheckRun> {
override suspend fun listCheckRuns(
installationId: Long,
owner: String,
repo: String,
checkSuiteId: Long
): Flow<CheckRun> {
return flow {
val infinitePagesSequence = generateSequence(1) { it + 1 }
for (page in infinitePagesSequence) {
Expand Down Expand Up @@ -149,7 +161,13 @@ class GitHubAPIImpl constructor(
}
}

override suspend fun setStatus(installationId: Long, owner: String, repo: String, sha: String, status: Status): Status {
override suspend fun setStatus(
installationId: Long,
owner: String,
repo: String,
sha: String,
status: Status
): Status {
val response = post(
installationId = installationId,
path = "/repos/$owner/$repo/statuses/$sha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ class GitHubAccessTokenManagerImpl constructor(

private val accessTokenCache: Cache<Long, AccessTokenResponse> = Caffeine.newBuilder()
.expireAfter(object : Expiry<Long, AccessTokenResponse> {
override fun expireAfterUpdate(key: Long, value: AccessTokenResponse, currentTime: Long, currentDuration: Long): Long {
override fun expireAfterUpdate(
key: Long,
value: AccessTokenResponse,
currentTime: Long,
currentDuration: Long
): Long {
return value.expiresIn.toNanos()
}

override fun expireAfterCreate(key: Long, value: AccessTokenResponse, currentTime: Long): Long {
return value.expiresIn.toNanos()
}

override fun expireAfterRead(key: Long, value: AccessTokenResponse, currentTime: Long, currentDuration: Long): Long {
override fun expireAfterRead(
key: Long,
value: AccessTokenResponse,
currentTime: Long,
currentDuration: Long
): Long {
return value.expiresIn.toNanos()
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/cirruslabs/sq/model/Conclusion.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.cirruslabs.sq.model

data class Conclusion(
val completed: Boolean,
val failureDetails: ConclusionDetails? = null
val completed: Boolean,
val failureDetails: ConclusionDetails? = null
)

data class ConclusionDetails(
Expand Down
23 changes: 17 additions & 6 deletions src/test/kotlin/org/cirruslabs/sq/AppKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package org.cirruslabs.sq

import com.google.common.io.Resources
import com.google.gson.Gson
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.*
import io.ktor.server.testing.*
import io.mockk.*
import kotlinx.coroutines.flow.asFlow
Expand Down Expand Up @@ -55,8 +53,17 @@ class AppKtTest {
coVerifyOrder {
mockAPI.listCheckSuites(102236L, "cirruslabs", "sandbox", "master")
mockAPI.listCheckRuns(102236L, "cirruslabs", "sandbox", 517820163)
mockAPI.listPullRequests(102236L, "cirruslabs", "sandbox", mapOf("base" to "master", "state" to "open", "sort" to "updated", "direction" to "desc"))
val expectedStatus = Status(StatusState.failure, "Cirrus CI failure on master", target_url = "https://github.com/cirruslabs/sandbox/runs/504360682")
mockAPI.listPullRequests(
102236L,
"cirruslabs",
"sandbox",
mapOf("base" to "master", "state" to "open", "sort" to "updated", "direction" to "desc")
)
val expectedStatus = Status(
StatusState.failure,
"Cirrus CI failure on master",
target_url = "https://github.com/cirruslabs/sandbox/runs/504360682"
)
mockAPI.setStatus(102236L, "cirruslabs", "sandbox", "990e3dc578b8b1607e28dfa2d5353a276741d77c", expectedStatus)
}

Expand Down Expand Up @@ -91,7 +98,11 @@ class AppKtTest {
coVerifyOrder {
mockAPI.listCheckSuites(102236L, "cirruslabs", "sandbox", "master")
mockAPI.listCheckRuns(102236L, "cirruslabs", "sandbox", 517820163)
val expectedStatus = Status(StatusState.failure, "Cirrus CI failure on master", target_url = "https://github.com/cirruslabs/sandbox/runs/504360682")
val expectedStatus = Status(
StatusState.failure,
"Cirrus CI failure on master",
target_url = "https://github.com/cirruslabs/sandbox/runs/504360682"
)
mockAPI.setStatus(102236L, "cirruslabs", "sandbox", "5687afbadb49ea7fd8fca74efbf88e7bb48e123a", expectedStatus)
}

Expand Down
6 changes: 4 additions & 2 deletions src/test/kotlin/org/cirruslabs/sq/SubmitQueueLogicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class SubmitQueueLogicTest {
@Test
fun overallConclusion() {
runBlocking {
val conclusion = logic.overallConclusion(installationId = 0L,
val conclusion = logic.overallConclusion(
installationId = 0L,
owner = "flutter",
repo = "flutter",
ref = "acd51a726e7c2eeb0e077890cd7b2f4f3bbc4931")
ref = "acd51a726e7c2eeb0e077890cd7b2f4f3bbc4931"
)
assertEquals(Conclusion(true), conclusion)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.cirruslabs.sq.github.impl

import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.serialization.gson.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cirruslabs.sq.github.impl

import com.auth0.jwt.algorithms.Algorithm
import com.google.common.io.Resources
import org.junit.Test
import kotlin.test.assertEquals

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"before": "a3c6374298c09dbc35357695eb1a12cec498a48f",
"after": "e8ea21f5940a9ddd74b566138753435a17579530",
"pull_requests": [

],
"app": {
"id": 3232,
Expand Down
4 changes: 0 additions & 4 deletions src/test/resources/pull_request/opened.hook.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@
"merge_commit_sha": null,
"assignee": null,
"assignees": [

],
"requested_reviewers": [

],
"requested_teams": [

],
"labels": [

],
"milestone": null,
"draft": false,
Expand Down

0 comments on commit 3ed3280

Please sign in to comment.