Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed Nov 12, 2020
1 parent fe8d175 commit 0bbe934
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package flank.scripts.ci.releasenotes
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.getOrElse
import com.github.kittinunf.result.map
import flank.scripts.github.GithubPullRequest
import flank.scripts.github.GithubUser
import flank.scripts.github.getLatestReleaseTag
import flank.scripts.github.getPrDetailsByCommit
import flank.scripts.utils.markdownLink
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import com.jcabi.github.Release
import com.jcabi.github.Releases
import com.jcabi.github.Repo
import com.jcabi.github.RtGithub
import flank.scripts.ci.releasenotes.GithubPullRequestDeserializer
import flank.scripts.ci.releasenotes.GithubReleaseDeserializable
import flank.scripts.exceptions.mapClientError
import flank.scripts.exceptions.toGithubException
import flank.scripts.pullrequest.GitHubPullRequestDeserializable

// ============= HTTP GITHUB API =============

Expand All @@ -37,6 +37,13 @@ fun deleteOldTag(tag: String, username: String, password: String) =
.third
.mapClientError { it.toGithubException() }

suspend fun getGitHubPullRequest(githubToken: String, issueNumber: Int) =
Fuel.get("https://api.github.com/repos/Flank/flank/pulls/$issueNumber")
.appendHeader("Accept", "application/vnd.github.v3+json")
.appendHeader("Authorization", "token $githubToken")
.awaitResult(GitHubPullRequestDeserializable)
.mapClientError { it.toGithubException() }

private const val DELETE_ENDPOINT = "https://api.github.com/repos/Flank/flank/git/refs/tags/"

// ============= JCABI GITHUB API =============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class GithubPullRequest(
val assignees: List<GithubUser>,
val labels: List<GitHubLabel> = emptyList(),
val body: String = "",
val head: GitHubHead?
val head: GitHubHead? = null
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package flank.scripts.pullrequest
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.types.int
import com.github.kittinunf.result.onError
import com.github.kittinunf.result.success
import flank.scripts.github.getGitHubPullRequest
import flank.scripts.testartifacts.core.GITHUB_TOKEN_ENV_KEY
import flank.scripts.utils.getEnv
import kotlinx.coroutines.Dispatchers
Expand All @@ -19,15 +19,13 @@ object CopyProperties :
CliktCommand(name = "copyProperties", help = "Copy properties from referanced issue to pull request") {

private val githubToken by option(help = "Git Token").default(getEnv(GITHUB_TOKEN_ENV_KEY))
private val zenhubToken by option(help = "ZenHub api Token").required()
private val prNumber by option(help = "Pull request number").int().required()
private val zenhubToken by option(help = "ZenHub api Token").default("010c7fa7004bbff5057773858c509379ad33530805d4555ebd2c998a7309934dc55ca0afce0bae47")
private val prNumber by option(help = "Pull request number").int().default(1310)

override fun run() {
runBlocking {
getGitHubPullRequest(githubToken, prNumber)
.onError {
println("Could not copy properties, because of ${it.message}")
}
.onError { println("Could not copy properties, because of ${it.message}") }
.success { pullRequest ->
val issueNumber = pullRequest.findReferenceNumber()
checkNotNull(issueNumber) { "Reference issue not found on description and branch" }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.coroutines.awaitResult
import com.github.kittinunf.fuel.coroutines.awaitStringResult
import com.github.kittinunf.result.getOrNull
import com.github.kittinunf.result.onError
import com.github.kittinunf.result.success
import flank.scripts.utils.toJson
Expand All @@ -14,25 +15,22 @@ private const val ZENHUB_BASE_URL = "https://api.zenhub.com/p1/repositories/$FLA

suspend fun copyEstimations(zenhubToken: String, issueNumber: Int, pullRequestNumber: Int) {
getEstimation(zenhubToken, issueNumber)
.run { setEstimation(zenhubToken, pullRequestNumber, estimate.value) }
?.run { setEstimation(zenhubToken, pullRequestNumber, estimate.value) }
}

suspend fun getEstimation(zenhubToken: String, issueNumber: Int) =
Fuel.get("$ZENHUB_BASE_URL/issues/$issueNumber")
.withZenhubHeaders(zenhubToken)
.awaitResult(ZenHubIssueDeserializable)
.onError { println("Could not get estimations because of ${it.message}") }
.get()
.getOrNull()

private suspend fun setEstimation(zenhubToken: String, pullRequestNumber: Int, estimate: Int) {
Fuel.put("$ZENHUB_BASE_URL/issues/$pullRequestNumber/estimate")
.withZenhubHeaders(zenhubToken)
.body(ZenHubEstimateRequest(estimate).toJson())
.awaitStringResult()
.onError {
it.printStackTrace()
println("Could not set estimations because of ${it.message}")
}
.onError { println("Could not set estimations because of ${it.message}") }
.success { println("Estimate $estimate set to pull request #$pullRequestNumber") }
}

Expand Down
27 changes: 17 additions & 10 deletions flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.Response
import com.github.kittinunf.fuel.core.requests.DefaultBody
import flank.scripts.ci.releasenotes.GitHubRelease
import flank.scripts.ci.releasenotes.GithubPullRequest
import flank.scripts.github.GithubPullRequest
import flank.scripts.release.updatebugsnag.BugSnagRequest
import flank.scripts.release.updatebugsnag.BugSnagResponse
import flank.scripts.utils.toJson
Expand All @@ -28,10 +28,16 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) {
override fun executeRequest(request: Request): Response {
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(), 200)
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(), 200)
url == "https://api.github.com/repos/flank/flank/commits/success/pulls" -> request.buildResponse(
Json.encodeToString(githubPullRequestTest), 200)
Json.encodeToString(githubPullRequestTest), 200
)
request.isFailedGithubRequest() -> request.buildResponse(githubErrorBody, 422)
url == "https://build.bugsnag.com/" -> request.handleBugsnagResponse()
else -> Response(request.url)
Expand Down Expand Up @@ -68,12 +74,13 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) {
}
}

private val githubPullRequestTest = listOf(GithubPullRequest(
"www.pull.request",
"feat: new Feature",
5,
listOf()
)
private val githubPullRequestTest = listOf(
GithubPullRequest(
"www.pull.request",
"feat: new Feature",
5,
listOf()
)
)

private val githubErrorBody = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.kittinunf.result.Result
import com.google.common.truth.Truth.assertThat
import flank.scripts.FuelTestRunner
import flank.scripts.ci.releasenotes.GitHubRelease
import flank.scripts.ci.releasenotes.GithubPullRequest
import flank.scripts.exceptions.GitHubException
import kotlinx.coroutines.runBlocking
import org.junit.Test
Expand Down

0 comments on commit 0bbe934

Please sign in to comment.