Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Small changes in flank-scripts module #1432

Merged
merged 2 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions flank-scripts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.jfrog.bintray.gradle.BintrayExtension
import groovy.xml.dom.DOMCategory.attributes
import java.io.ByteArrayOutputStream
import java.nio.file.Paths
import java.util.*
import java.util.Date

plugins {
application
Expand All @@ -29,7 +28,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.2.8"
version = "1.2.9"
group = "com.github.flank"

application {
Expand All @@ -46,17 +45,21 @@ bintray {
publish = true
override = true
setPublications("mavenJava")
pkg(closureOf<BintrayExtension.PackageConfig> {
repo = "maven"
name = artifactID
userOrg = "flank"
setLicenses("Apache-2.0")
vcsUrl = "https://github.com/Flank/flank.git"
version(closureOf<BintrayExtension.VersionConfig> {
name = version.name
released = Date().toString()
})
})
pkg(
closureOf<BintrayExtension.PackageConfig> {
repo = "maven"
name = artifactID
userOrg = "flank"
setLicenses("Apache-2.0")
vcsUrl = "https://github.com/Flank/flank.git"
version(
closureOf<BintrayExtension.VersionConfig> {
name = version.name
released = Date().toString()
}
)
}
)
}

publishing {
Expand Down
49 changes: 24 additions & 25 deletions flank-scripts/src/main/kotlin/flank/scripts/github/GithubApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,80 +32,81 @@ import flank.scripts.github.objects.GithubWorkflowRunsSummaryDeserializer
import flank.scripts.utils.toJson
import java.lang.Exception

private const val URL_BASE = "https://api.github.com/repos/Flank/flank"
private const val FLANK_REPO = "Flank/flank"
private const val URL_BASE = "https://api.github.com/repos"

// ============= HTTP GITHUB API =============
// GET
suspend fun getPrDetailsByCommit(commitSha: String, githubToken: String): Result<List<GithubPullRequest>, Exception> =
Fuel.get("$URL_BASE/commits/$commitSha/pulls")
suspend fun getPrDetailsByCommit(commitSha: String, githubToken: String, repo: String = FLANK_REPO): Result<List<GithubPullRequest>, Exception> =
Fuel.get("$URL_BASE/$repo/commits/$commitSha/pulls")
.appendGitHubHeaders(githubToken, "application/vnd.github.groot-preview+json")
.awaitResult(GithubPullRequestListDeserializer)
.mapClientErrorToGithubException()
.onError { println("Could not download info for commit $commitSha, because of ${it.message}") }

suspend fun getLatestReleaseTag(githubToken: String): Result<GitHubRelease, Exception> =
Fuel.get("$URL_BASE/releases/latest")
suspend fun getLatestReleaseTag(githubToken: String, repo: String = FLANK_REPO): Result<GitHubRelease, Exception> =
Fuel.get("$URL_BASE/$repo/releases/latest")
.appendGitHubHeaders(githubToken)
.awaitResult(GithubReleaseDeserializable)
.mapClientErrorToGithubException()

suspend fun getGitHubPullRequest(githubToken: String, issueNumber: Int): Result<GithubPullRequest, Exception> =
Fuel.get("$URL_BASE/pulls/$issueNumber")
suspend fun getGitHubPullRequest(githubToken: String, issueNumber: Int, repo: String = FLANK_REPO): Result<GithubPullRequest, Exception> =
Fuel.get("$URL_BASE/$repo/pulls/$issueNumber")
.appendGitHubHeaders(githubToken)
.awaitResult(GithubPullRequestDeserializer)
.mapClientErrorToGithubException()

suspend fun getGitHubIssue(githubToken: String, issueNumber: Int): Result<GithubPullRequest, Exception> =
Fuel.get("$URL_BASE/issues/$issueNumber")
suspend fun getGitHubIssue(githubToken: String, issueNumber: Int, repo: String = FLANK_REPO): Result<GithubPullRequest, Exception> =
Fuel.get("$URL_BASE/$repo/issues/$issueNumber")
.appendGitHubHeaders(githubToken)
.awaitResult(GithubPullRequestDeserializer)
.mapClientErrorToGithubException()

suspend fun getGitHubIssueList(githubToken: String, parameters: Parameters = emptyList()): Result<List<GithubPullRequest>, Exception> =
Fuel.get("$URL_BASE/issues", parameters)
suspend fun getGitHubIssueList(githubToken: String, parameters: Parameters = emptyList(), repo: String = FLANK_REPO): Result<List<GithubPullRequest>, Exception> =
Fuel.get("$URL_BASE/$repo/issues", parameters)
.appendGitHubHeaders(githubToken)
.awaitResult(GithubPullRequestListDeserializer)
.mapClientErrorToGithubException()

suspend fun getGitHubCommitList(githubToken: String, parameters: Parameters = emptyList()): Result<List<GitHubCommit>, Exception> =
Fuel.get("$URL_BASE/commits", parameters)
suspend fun getGitHubCommitList(githubToken: String, parameters: Parameters = emptyList(), repo: String = FLANK_REPO): Result<List<GitHubCommit>, Exception> =
Fuel.get("$URL_BASE/$repo/commits", parameters)
.appendGitHubHeaders(githubToken)
.awaitResult(GitHubCommitListDeserializer)
.mapClientErrorToGithubException()

suspend fun getGitHubWorkflowRunsSummary(githubToken: String, workflow: String, parameters: Parameters = emptyList()): Result<GitHubWorkflowRunsSummary, Exception> =
Fuel.get("$URL_BASE/actions/workflows/$workflow/runs", parameters)
suspend fun getGitHubWorkflowRunsSummary(githubToken: String, workflow: String, parameters: Parameters = emptyList(), repo: String = FLANK_REPO): Result<GitHubWorkflowRunsSummary, Exception> =
Fuel.get("$URL_BASE/$repo/actions/workflows/$workflow/runs", parameters)
.appendGitHubHeaders(githubToken)
.awaitResult(GithubWorkflowRunsSummaryDeserializer)
.mapClientErrorToGithubException()

// POST
suspend fun postNewIssueComment(githubToken: String, issueNumber: Int, payload: GitHubCreateIssueCommentRequest): Result<GitHubCreateIssueCommentResponse, Exception> =
Fuel.post("$URL_BASE/issues/$issueNumber/comments")
suspend fun postNewIssueComment(githubToken: String, issueNumber: Int, payload: GitHubCreateIssueCommentRequest, repo: String = FLANK_REPO): Result<GitHubCreateIssueCommentResponse, Exception> =
Fuel.post("$URL_BASE/$repo/issues/$issueNumber/comments")
.appendGitHubHeaders(githubToken)
.body(payload.toJson())
.awaitResult(GitHubCreateIssueCommentResponseDeserializer)
.mapClientErrorToGithubException()

suspend fun postNewIssue(githubToken: String, payload: GitHubCreateIssueRequest): Result<GitHubCreateIssueResponse, Exception> =
Fuel.post("$URL_BASE/issues")
suspend fun postNewIssue(githubToken: String, payload: GitHubCreateIssueRequest, repo: String = FLANK_REPO): Result<GitHubCreateIssueResponse, Exception> =
Fuel.post("$URL_BASE/$repo/issues")
.appendGitHubHeaders(githubToken)
.body(payload.toJson())
.awaitResult(GitHubCreateIssueResponseDeserializer)
.mapClientErrorToGithubException()

// PATCH
fun patchIssue(githubToken: String, issueNumber: Int, payload: GitHubUpdateIssueRequest): Result<ByteArray, Exception> =
Fuel.patch("$URL_BASE/issues/$issueNumber")
fun patchIssue(githubToken: String, issueNumber: Int, payload: GitHubUpdateIssueRequest, repo: String = FLANK_REPO): Result<ByteArray, Exception> =
Fuel.patch("$URL_BASE/$repo/issues/$issueNumber")
.appendGitHubHeaders(githubToken)
.body(payload.toJson())
.response()
.third
.mapClientErrorToGithubException()

// DELETE
fun deleteOldTag(tag: String, username: String, password: String): Result<ByteArray, Exception> =
Fuel.delete(DELETE_ENDPOINT + tag)
fun deleteOldTag(tag: String, username: String, password: String, repo: String = FLANK_REPO): Result<ByteArray, Exception> =
Fuel.delete("$URL_BASE/$repo/git/refs/tags/$tag")
.authentication()
.basic(username, password)
.response()
Expand All @@ -116,8 +117,6 @@ fun Request.appendGitHubHeaders(githubToken: String, contentType: String = "appl
appendHeader("Accept", contentType)
.appendHeader("Authorization", "token $githubToken")

private const val DELETE_ENDPOINT = "$URL_BASE/git/refs/tags/"

// ============= JCABI GITHUB API =============

fun githubRepo(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package flank.scripts.github.commons

import com.github.kittinunf.result.getOrNull
import flank.scripts.github.getGitHubWorkflowRunsSummary
import flank.scripts.github.objects.GitHubWorkflowRun
import java.time.Instant
import java.time.format.DateTimeFormatter

suspend fun getLastWorkflowRunDate(
token: String,
workflowName: String? = null,
workflowFileName: String? = null
): String = getGitHubWorkflowRunsSummary(
githubToken = token,
workflow = requireNotNull(
workflowName ?: workflowFileName
) { "** Missing either workflow name or workflow file name. Both can not be null" },
parameters = listOf(
"per_page" to 20,
"page" to 1
)
).getOrNull()
?.run {
workflowRuns
.filter { it.status != "in_progress" }
.filter { it.conclusion != "cancelled" }
.getOrNull(0)
.logRun()
?.createdAt.run { DateTimeFormatter.ISO_INSTANT.format(Instant.parse(this)) }
} ?: run {
println("** No workflow run found for ${workflowName ?: workflowFileName}")
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
}

private fun GitHubWorkflowRun?.logRun() = this?.also {
println(
"""
** Last workflow run:
name: ${it.name}
last run: ${it.createdAt}
url: ${it.htmlUrl}
""".trimIndent()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ import kotlinx.serialization.Serializable

@Serializable
data class GitHubCommit(
val sha: String
val sha: String,
val commit: Commit
)

@Serializable
data class Commit(
val author: Author
)

@Serializable
data class Author(
val name: String,
val email: String,
val date: String
)

object GitHubCommitListDeserializer : ResponseDeserializable<List<GitHubCommit>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,8 @@
package flank.scripts.integration

import com.github.kittinunf.result.getOrNull
import flank.scripts.github.getGitHubWorkflowRunsSummary
import flank.scripts.github.objects.GitHubWorkflowRun
import java.time.Instant
import java.time.format.DateTimeFormatter
import flank.scripts.github.commons.getLastWorkflowRunDate

suspend fun getLastITWorkflowRunDate(token: String) = getLastWorkflowRunDate(
token = token,
workflowFileName = "full_suite_integration_tests.yml"
)

suspend fun getLastWorkflowRunDate(
token: String,
workflowName: String? = null,
workflowFileName: String? = null
): String = getGitHubWorkflowRunsSummary(
githubToken = token,
workflow = requireNotNull(
workflowName ?: workflowFileName
) { "** Missing either workflow name or workflow file name. Both can not be null" },
parameters = listOf(
"per_page" to 20,
"page" to 1
)
).getOrNull()
?.run {
workflowRuns
.filter { it.status != "in_progress" }
.filter { it.conclusion != "cancelled" }
.getOrNull(0)
.logRun()
?.createdAt.run { DateTimeFormatter.ISO_INSTANT.format(Instant.parse(this)) }
} ?: run {
println("** No workflow run found for ${workflowName ?: workflowFileName}")
DateTimeFormatter.ISO_INSTANT.format(Instant.now())
}

private fun GitHubWorkflowRun?.logRun() = this?.also {
println(
"""
** Last workflow run:
name: ${it.name}
last run: ${it.createdAt}
url: ${it.htmlUrl}
""".trimIndent()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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.zenhub.copyEstimation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.joinAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package flank.scripts.shell.firebase

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.subcommands
import flank.scripts.shell.firebase.apiclient.GenerateJavaClientCommand
import flank.scripts.shell.firebase.apiclient.UpdateApiJsonCommand

object FirebaseCommand : CliktCommand(name = "firebase", help = "Contains all firebase commands") {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.shell.firebase
package flank.scripts.shell.firebase.apiclient

import com.github.ajalt.clikt.core.CliktCommand
import flank.scripts.exceptions.ShellCommandException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.shell.firebase
package flank.scripts.shell.firebase.apiclient

import com.github.ajalt.clikt.core.CliktCommand
import flank.common.downloadFile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.pullrequest
package flank.scripts.zenhub

import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.Request
Expand All @@ -10,7 +10,7 @@ import com.github.kittinunf.result.success
import flank.scripts.utils.toJson
import kotlinx.serialization.Serializable

private const val FLANK_REPO_ID = 84221974
const val FLANK_REPO_ID = 84221974
internal const val ZENHUB_BASE_URL = "https://api.zenhub.com/p1/repositories/$FLANK_REPO_ID"

suspend fun copyEstimation(zenhubToken: String, issueNumber: Int, pullRequestNumber: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.pullrequest
package flank.scripts.zenhub

import com.github.kittinunf.fuel.core.ResponseDeserializable
import flank.scripts.utils.toObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.github.kittinunf.fuel.core.Client
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.pullrequest.ZENHUB_BASE_URL
import flank.scripts.release.updatebugsnag.BugSnagRequest
import flank.scripts.release.updatebugsnag.BugSnagResponse
import flank.scripts.utils.toJson
import flank.scripts.utils.toObject
import flank.scripts.zenhub.ZENHUB_BASE_URL

class FuelMockServer : Client {
override fun executeRequest(request: Request): Response {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package flank.scripts
import com.github.kittinunf.fuel.core.Method
import com.github.kittinunf.fuel.core.Request
import flank.scripts.ci.releasenotes.GitHubRelease
import flank.scripts.github.objects.Author
import flank.scripts.github.objects.Commit
import flank.scripts.github.objects.GitHubCommit
import flank.scripts.github.objects.GitHubCreateIssueCommentResponse
import flank.scripts.github.objects.GitHubCreateIssueResponse
Expand Down Expand Up @@ -58,7 +60,7 @@ private fun Request.isFailedGithubRequest() =
private fun Request.noIssueHeader() = request.headers["Authorization"].contains("token no-issue")

private val testGithubIssueList = listOf(
GitHubCommit("aaaaaaaaa")
GitHubCommit("aaaaaaaaa", Commit(Author("", "", "")))
).toJson()

private val testGithubIssueCommentList = listOf(
Expand Down
Loading