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: Enable properties file in flank scripts #1450

Merged
merged 19 commits into from
Jan 11, 2021
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
7 changes: 7 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ tasks {

subprojects {
apply(plugin = Plugins.KTLINT_GRADLE_PLUGIN)
afterEvaluate {
if (tasks.findByName("test") != null) {
tasks.test {
systemProperty("useDefaultProperties", "")
}
}
}
}

repositories {
Expand Down
1 change: 1 addition & 0 deletions common/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ build/
out/
.gradle/
local.properties
flank-debug.properties
18 changes: 18 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}

file("flank-debug.properties").run {
if (!exists() && System.getenv("CI") == null) writeText(
"""
#zenhub.repo-id=84221974
#
#repo.flank=Flank/flank
#repo.gcloud_cli=Flank/gcloud_cli
#repo.test-artifacts=Flank/test_artifacts
#
#integration.workflow-filename=integration_tests_pointer.yml
#integration.issue-poster=github-actions[bot]
#
#sdk-check.workflow-filename=update_dependencies_pointer.yml
#sdk-check.issue-poster=github-actions[bot]
""".trimIndent()
)
}
3 changes: 3 additions & 0 deletions common/src/main/kotlin/flank/common/Environment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package flank.common

fun isCI() = System.getenv("CI") != null
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.shell.utils
package flank.common

import java.nio.file.Files
import java.nio.file.Path
Expand All @@ -20,3 +20,5 @@ val flankFixturesTmpPath =
Paths.get(rootDirectoryPathString, "test_runner", "src", "test", "kotlin", "ftl", "fixtures", "tmp").toString()
val flankFixturesIosTmpPath =
Paths.get(flankFixturesTmpPath, "ios").toString()

val flankCommonRootPathString = Paths.get(rootDirectoryPathString, "common").toString()
52 changes: 52 additions & 0 deletions common/src/main/kotlin/flank/common/config/FlankProperties.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package flank.common.config

import flank.common.flankCommonRootPathString
import flank.common.isCI
import java.nio.file.Paths
import java.util.Properties

private const val ZENHUB_REPO_ID = "zenhub.repo-id"
private const val FLANK_REPO = "repo.flank"
private const val GCLOUD_REPO = "repo.gcloud_cli"
private const val ARTIFACTS_REPO = "repo.test-artifacts"
private const val IT_WORKFLOW_FILE = "integration.workflow-filename"
private const val IT_USER = "integration.issue-poster"
private const val SDK_WORKFLOW = "sdk-check.workflow-filename"
private const val SDK_USER = "sdk-check.issue-poster"

private val defaults = Properties().apply {
setProperty(ZENHUB_REPO_ID, "84221974")
setProperty(FLANK_REPO, "Flank/flank")
setProperty(GCLOUD_REPO, "Flank/gcloud_cli")
setProperty(ARTIFACTS_REPO, "Flank/test_artifacts")
setProperty(IT_WORKFLOW_FILE, "integration_tests_pointer.yml")
setProperty(IT_USER, "github-actions[bot]")
setProperty(SDK_WORKFLOW, "update_dependencies_pointer.yml")
setProperty(SDK_USER, "github-actions[bot]")
}

class SafeProperties(defaults: Properties) : Properties(defaults) {
override fun get(key: Any?) = (key as String).run {
requireNotNull(if (shouldUseDefaults()) defaults.getProperty(key) else getProperty(key))
}
}

// default properties should be used in CI and during tests
private fun shouldUseDefaults() = isCI() || isTest()

private fun isTest() = System.getProperty("useDefaultProperties") != null

private val props = SafeProperties(defaults).also { prop ->
with(Paths.get("$flankCommonRootPathString/flank-debug.properties").toFile()) {
if (exists()) prop.load(inputStream())
}
}

val zenhubRepositoryID = Integer.parseInt(props[ZENHUB_REPO_ID])
val flankRepository = props[FLANK_REPO]
val flankGcloudCLIRepository = props[GCLOUD_REPO]
val flankTestArtifactsRepository = props[ARTIFACTS_REPO]
val integrationOpenedIssueUser = props[IT_USER]
val updatesOpenedUser = props[SDK_USER]
val fullSuiteWorkflowFilename = props[IT_WORKFLOW_FILE]
val updateDependenciesWorkflowFilename = props[SDK_WORKFLOW]
18 changes: 17 additions & 1 deletion flank-scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,20 @@ Available commands are:
Applies a pre-commit hook that runs Ktlint on a commit and fails if there are lint issues. It is set to automatically fix issues if it can.

### `applyKtlintToIdea`
Applies Ktlint styling to the current idea project, so that correct code styling is upheld.
Applies Ktlint styling to the current idea project, so that correct code styling is upheld.

## Testing
To test your script with different settings use `flank-debug.properties` file. Uncomment and replace with desired values.
Properties are skipped by git and should not be attached to a commit. Note, `test` task ignores your own properties and will use the default.

#### List of possible configs
| Key | Description | Default value |
|:----------------|:---------------------|:---:|
|`zenhub.repo-id`|Flank's repo ID in zenhub app. Used to create new epics.|`84221974`|
|`repo.flank`|Flank test runner repo. Essential property for github client.|`Flank/flank`|
|`repo.gcloud_cli`|Flank's fork of gcloud sdk repo.|`Flank/gcloud_cli`|
|`repo.test-artifacts`|Flank's source of artifacts (apks, binaries, etc) used for testing|`Flank/test_artifacts`|
|`integration.workflow-filename`|GH Action integration tests workflow file. Used to fetch list of commits since it's last run.|`full_suite_integration_tests.yml`|
|`integration.issue-poster`|Name of account that creates IT issue|`github-actions[bot]`|
|`sdk-check.workflow-filename`|GH Action dependencies update workflow file. Used to fetch list of commits since it's last run.|`update_dependencies_and_client.yml`|
|`sdk-check.issue-poster`|Name of account that creates dependencies issues/epics|`github-actions[bot]`|
2 changes: 1 addition & 1 deletion flank-scripts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.3.4"
version = "1.4.0"
group = "com.github.flank"

application {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package flank.scripts.contribution.ideaktlint

import com.github.ajalt.clikt.core.CliktCommand
import flank.common.currentPath
import flank.common.deleteFile
import flank.common.downloadFile
import flank.common.goToRoot
import flank.common.logLn
import flank.scripts.shell.utils.currentPath
import flank.scripts.shell.utils.goToRoot
import flank.scripts.utils.isWindows
import flank.scripts.utils.runCommand
import kotlinx.coroutines.runBlocking
Expand Down
56 changes: 44 additions & 12 deletions flank-scripts/src/main/kotlin/flank/scripts/github/GithubApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import com.github.kittinunf.fuel.core.Parameters
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.extensions.authentication
import com.github.kittinunf.fuel.coroutines.awaitResult
import com.github.kittinunf.fuel.coroutines.awaitStringResult
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.onError
import com.github.kittinunf.result.success
import com.jcabi.github.Coordinates
import com.jcabi.github.Release
import com.jcabi.github.Releases
import com.jcabi.github.Repo
import com.jcabi.github.RtGithub
import flank.common.config.flankRepository
import flank.scripts.ci.releasenotes.GitHubRelease
import flank.scripts.ci.releasenotes.GithubReleaseDeserializable
import flank.scripts.exceptions.mapClientErrorToGithubException
Expand All @@ -23,6 +26,9 @@ import flank.scripts.github.objects.GitHubCreateIssueCommentResponseDeserializer
import flank.scripts.github.objects.GitHubCreateIssueRequest
import flank.scripts.github.objects.GitHubCreateIssueResponse
import flank.scripts.github.objects.GitHubCreateIssueResponseDeserializer
import flank.scripts.github.objects.GitHubLabelDeserializable
import flank.scripts.github.objects.GitHubSetAssigneesRequest
import flank.scripts.github.objects.GitHubSetLabelsRequest
import flank.scripts.github.objects.GitHubUpdateIssueRequest
import flank.scripts.github.objects.GitHubWorkflowRunsSummary
import flank.scripts.github.objects.GithubPullRequest
Expand All @@ -32,76 +38,102 @@ import flank.scripts.github.objects.GithubWorkflowRunsSummaryDeserializer
import flank.scripts.utils.toJson
import java.lang.Exception

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

// ============= HTTP GITHUB API =============
suspend fun getPrDetailsByCommit(commitSha: String, githubToken: String, repo: String = FLANK_REPO): Result<List<GithubPullRequest>, Exception> =
suspend fun getPrDetailsByCommit(commitSha: String, githubToken: String, repo: String = flankRepository): 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, repo: String = FLANK_REPO): Result<GitHubRelease, Exception> =
suspend fun getLatestReleaseTag(githubToken: String, repo: String = flankRepository): Result<GitHubRelease, Exception> =
Fuel.get("$URL_BASE/$repo/releases/latest")
.appendGitHubHeaders(githubToken)
.awaitResult(GithubReleaseDeserializable)
.mapClientErrorToGithubException()

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

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

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

suspend fun getGitHubCommitList(githubToken: String, parameters: Parameters = emptyList(), repo: String = FLANK_REPO): Result<List<GitHubCommit>, Exception> =
suspend fun getGitHubCommitList(githubToken: String, parameters: Parameters = emptyList(), repo: String = flankRepository): 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(), repo: String = FLANK_REPO): Result<GitHubWorkflowRunsSummary, Exception> =
suspend fun getGitHubWorkflowRunsSummary(githubToken: String, workflow: String, parameters: Parameters = emptyList(), repo: String = flankRepository): Result<GitHubWorkflowRunsSummary, Exception> =
Fuel.get("$URL_BASE/$repo/actions/workflows/$workflow/runs", parameters)
.appendGitHubHeaders(githubToken)
.awaitResult(GithubWorkflowRunsSummaryDeserializer)
.mapClientErrorToGithubException()

suspend fun postNewIssueComment(githubToken: String, issueNumber: Int, payload: GitHubCreateIssueCommentRequest, repo: String = FLANK_REPO): Result<GitHubCreateIssueCommentResponse, Exception> =
suspend fun postNewIssueComment(githubToken: String, issueNumber: Int, payload: GitHubCreateIssueCommentRequest, repo: String = flankRepository): 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, repo: String = FLANK_REPO): Result<GitHubCreateIssueResponse, Exception> =
suspend fun postNewIssue(githubToken: String, payload: GitHubCreateIssueRequest, repo: String = flankRepository): Result<GitHubCreateIssueResponse, Exception> =
Fuel.post("$URL_BASE/$repo/issues")
.appendGitHubHeaders(githubToken)
.body(payload.toJson())
.awaitResult(GitHubCreateIssueResponseDeserializer)
.mapClientErrorToGithubException()

fun patchIssue(githubToken: String, issueNumber: Int, payload: GitHubUpdateIssueRequest, repo: String = FLANK_REPO): Result<ByteArray, Exception> =
suspend fun getLabelsFromIssue(githubToken: String, issueNumber: Int, repo: String = flankRepository) =
Fuel.get("$URL_BASE/$repo/issues/$issueNumber/labels")
.appendGitHubHeaders(githubToken)
.awaitResult(GitHubLabelDeserializable)
.mapClientErrorToGithubException()

suspend fun setLabelsToPullRequest(githubToken: String, pullRequestNumber: Int, labels: List<String>, repo: String = flankRepository) {
Fuel.post("$URL_BASE/$repo/issues/$pullRequestNumber/labels")
.appendGitHubHeaders(githubToken)
.body(GitHubSetLabelsRequest(labels).toJson())
.awaitStringResult()
.onError { println("Could not set assignees because of ${it.message}") }
.success { println("$labels set to pull request #$pullRequestNumber") }
}

suspend fun setAssigneesToPullRequest(githubToken: String, pullRequestNumber: Int, assignees: List<String>, repo: String = flankRepository) {
Fuel.post("$URL_BASE/$repo/issues/$pullRequestNumber/assignees")
.appendGitHubHeaders(githubToken)
.body(GitHubSetAssigneesRequest(assignees).toJson())
.awaitStringResult()
.onError {
println("Could not set assignees because of ${it.message}")
it.printStackTrace()
}
.success { println("$assignees set to pull request #$pullRequestNumber") }
}

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

fun deleteOldTag(tag: String, username: String, password: String, repo: String = FLANK_REPO): Result<ByteArray, Exception> =
fun deleteOldTag(tag: String, username: String, password: String, repo: String = flankRepository): Result<ByteArray, Exception> =
Fuel.delete("$URL_BASE/$repo/git/refs/tags/$tag")
.authentication()
.basic(username, password)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package flank.scripts.github.objects

import kotlinx.serialization.Serializable

@Serializable
data class GitHubSetAssigneesRequest(
val assignees: List<String>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package flank.scripts.github.objects

import kotlinx.serialization.Serializable

@Serializable
data class GitHubSetLabelsRequest(
val labels: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package flank.scripts.integration

import com.github.kittinunf.result.getOrElse
import com.github.kittinunf.result.onError
import flank.common.config.integrationOpenedIssueUser
import flank.scripts.github.getGitHubIssueList

suspend fun checkForOpenedITIssues(token: String): Int? = getGitHubIssueList(
githubToken = token,
parameters = listOf(
"creator" to "github-actions[bot]",
"creator" to integrationOpenedIssueUser,
"state" to "open",
"labels" to "IT_Failed"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flank.scripts.integration

import flank.common.config.flankRepository
import flank.scripts.github.objects.GithubPullRequest
import java.time.Instant
import java.time.LocalDateTime
Expand Down Expand Up @@ -33,7 +34,7 @@ private val successTemplate = { lastRun: String, runId: String, url: String ->
"""
|### Full suite IT run :white_check_mark: SUCCEEDED :white_check_mark:
|**Timestamp:** ${makeHumanFriendly(lastRun)}
|**Job run:** [$runId](https://github.com/Flank/flank/actions/runs/$runId)
|**Job run:** [$runId](https://github.com/$flankRepository/actions/runs/$runId)
|**Build scan URL:** $url
|**Closing issue**
""".trimMargin()
Expand All @@ -43,7 +44,7 @@ private val failureTemplate = { lastRun: String, runId: String, url: String ->
"""
|### Full suite IT run :x: FAILED :x:
|**Timestamp:** ${makeHumanFriendly(lastRun)}
|**Job run:** [$runId](https://github.com/Flank/flank/actions/runs/$runId)
|**Job run:** [$runId](https://github.com/$flankRepository/actions/runs/$runId)
|**Build scan URL:** $url
""".trimMargin()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package flank.scripts.integration

import flank.common.config.fullSuiteWorkflowFilename
import flank.scripts.github.commons.getLastWorkflowRunDate

suspend fun getLastITWorkflowRunDate(token: String) = getLastWorkflowRunDate(
token = token,
workflowFileName = "integration_tests_pointer.yml"
workflowFileName = fullSuiteWorkflowFilename
)
Loading