-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: Copy properties from issue to pull request (#1310)
* ci: Copy properties from issue to pull request * Clean up code * add some tests and refactor mock server * added tests * added github job * added documentation * update documentation and action * update tests * change for PR * change for PR * Update flank-scripts/src/main/kotlin/flank/scripts/pullrequest/GitHubIssuePropertiesCopy.kt Co-authored-by: pawelpasterz <[email protected]> * fix setting assignees * fixed test * add tests * Update GithubMockServerHandler.kt Co-authored-by: pawelpasterz <[email protected]>
- Loading branch information
1 parent
0c87150
commit ac5a2a8
Showing
24 changed files
with
700 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,26 @@ on: | |
- synchronize | ||
|
||
jobs: | ||
copy_properties: | ||
runs-on: macos-latest | ||
if: ${{ github.event.action == 'opened' }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Gradle Build flankScripts and add it to PATH | ||
run: | | ||
./flank-scripts/bash/buildFlankScripts.sh | ||
echo "./flank-scripts/bash" >> $GITHUB_PATH | ||
- name: Copy properties | ||
run: flankScripts pullRequest copyProperties --github-token=${{ secrets.GITHUB_TOKEN }} --zenhub-token=${{ secrets.ZENHUB_API_KEY }} --pr-number=${{ github.event.number }} | ||
check_title: | ||
runs-on: macos-latest | ||
steps: | ||
- name: Cancel Previous Runs | ||
uses: styfle/[email protected] | ||
with: | ||
access_token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: amannn/[email protected] | ||
if: github.event.pull_request.draft == false | ||
env: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
flank-scripts/src/main/kotlin/flank/scripts/ci/releasenotes/GithubPullRequest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
flank-scripts/src/main/kotlin/flank/scripts/github/GithubPullRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package flank.scripts.github | ||
|
||
import com.github.kittinunf.fuel.core.ResponseDeserializable | ||
import flank.scripts.utils.toObject | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class GithubPullRequest( | ||
@SerialName("html_url") val htmlUrl: String, | ||
val title: String, | ||
val number: Int, | ||
val assignees: List<GithubUser>, | ||
val labels: List<GitHubLabel> = emptyList(), | ||
val body: String = "", | ||
val head: GitHubHead? = null | ||
) | ||
|
||
@Serializable | ||
data class GithubUser( | ||
val login: String, | ||
@SerialName("html_url") val htmlUrl: String | ||
) | ||
|
||
object GithubPullRequestListDeserializer : ResponseDeserializable<List<GithubPullRequest>> { | ||
override fun deserialize(content: String): List<GithubPullRequest> = content.toObject() | ||
} | ||
|
||
object GithubPullRequestDeserializer : ResponseDeserializable<GithubPullRequest> { | ||
override fun deserialize(content: String): GithubPullRequest = content.toObject() | ||
} | ||
|
||
@Serializable | ||
data class GitHubLabel( | ||
val name: String | ||
) | ||
|
||
object GitHubLabelDeserializable : ResponseDeserializable<List<GitHubLabel>> { | ||
override fun deserialize(content: String): List<GitHubLabel> = content.toObject() | ||
} | ||
|
||
@Serializable | ||
data class GitHubHead( | ||
val ref: String | ||
) |
16 changes: 16 additions & 0 deletions
16
flank-scripts/src/main/kotlin/flank/scripts/pullrequest/FindReferenceIssue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package flank.scripts.pullrequest | ||
|
||
import flank.scripts.github.GithubPullRequest | ||
|
||
fun GithubPullRequest.findReferenceNumber() = | ||
(tryGetReferenceNumberFromBody() ?: tryGetReferenceNumberFromBranch()) | ||
?.trim() | ||
?.replace("#", "") | ||
?.toInt() | ||
|
||
private fun GithubPullRequest.tryGetReferenceNumberFromBody() = bodyReferenceRegex.find(body)?.value | ||
|
||
private fun GithubPullRequest.tryGetReferenceNumberFromBranch() = branchReferenceRegex.find(head?.ref.orEmpty())?.value | ||
|
||
private val bodyReferenceRegex = "#\\d+\\s".toRegex() | ||
private val branchReferenceRegex = "#\\d+".toRegex() |
55 changes: 55 additions & 0 deletions
55
flank-scripts/src/main/kotlin/flank/scripts/pullrequest/GitHubIssuePropertiesCopy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package flank.scripts.pullrequest | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
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 kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
|
||
object CopyProperties : | ||
CliktCommand(name = "copyProperties", help = "Copy properties from referenced issue to pull request") { | ||
|
||
private val githubToken by option(help = "Git Token").required() | ||
private val zenhubToken by option(help = "ZenHub api Token").required() | ||
private val prNumber by option(help = "Pull request number").int().required() | ||
|
||
override fun run() { | ||
runBlocking { | ||
getGitHubPullRequest(githubToken, prNumber) | ||
.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" } | ||
println("Found referenced issue #$issueNumber") | ||
launch(Dispatchers.IO) { copyGitHubProperties(githubToken, issueNumber, prNumber) } | ||
launch(Dispatchers.IO) { copyZenhubProperties(zenhubToken, issueNumber, prNumber) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun copyGitHubProperties( | ||
githubToken: String, | ||
baseIssueNumber: Int, | ||
prNumber: Int | ||
) = coroutineScope { | ||
listOf( | ||
launch { copyAssignees(githubToken, baseIssueNumber, prNumber) }, | ||
launch { copyLabels(githubToken, baseIssueNumber, prNumber) }, | ||
).joinAll() | ||
} | ||
|
||
private suspend fun copyZenhubProperties( | ||
zenhubToken: String, | ||
baseIssueNumber: Int, | ||
prNumber: Int | ||
) { | ||
copyEstimation(zenhubToken, baseIssueNumber, prNumber) | ||
} |
14 changes: 14 additions & 0 deletions
14
flank-scripts/src/main/kotlin/flank/scripts/pullrequest/PullRequestCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package flank.scripts.pullrequest | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.core.subcommands | ||
|
||
object PullRequestCommand : CliktCommand(name = "pullRequest") { | ||
|
||
init { | ||
subcommands(CopyProperties) | ||
} | ||
|
||
@Suppress("EmptyFunctionBlock") | ||
override fun run() {} | ||
} |
37 changes: 37 additions & 0 deletions
37
flank-scripts/src/main/kotlin/flank/scripts/pullrequest/SetAssignees.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package flank.scripts.pullrequest | ||
|
||
import com.github.kittinunf.fuel.Fuel | ||
import com.github.kittinunf.fuel.coroutines.awaitStringResult | ||
import com.github.kittinunf.result.getOrNull | ||
import com.github.kittinunf.result.map | ||
import com.github.kittinunf.result.onError | ||
import com.github.kittinunf.result.success | ||
import flank.scripts.github.appendGitHubHeaders | ||
import flank.scripts.github.getGitHubIssue | ||
import flank.scripts.utils.toJson | ||
import kotlinx.serialization.Serializable | ||
|
||
suspend fun copyAssignees(githubToken: String, baseIssueNumber: Int, pullRequestNumber: Int) { | ||
getGitHubIssue(githubToken, baseIssueNumber) | ||
.onError { println("Could not copy assignees because of ${it.message}") } | ||
.map { githubIssue -> githubIssue.assignees.map { it.login } } | ||
.getOrNull() | ||
?.let { setAssigneesToPullRequest(githubToken, pullRequestNumber, it) } | ||
} | ||
|
||
private suspend fun setAssigneesToPullRequest(githubToken: String, pullRequestNumber: Int, assignees: List<String>) { | ||
Fuel.post("https://api.github.com/repos/Flank/flank/issues/$pullRequestNumber/assignees") | ||
.appendGitHubHeaders(githubToken) | ||
.body(SetAssigneesRequest(assignees).toJson()) | ||
.awaitStringResult() | ||
.onError { | ||
println("Could not set assignees because of ${it.message}") | ||
it.printStackTrace() | ||
} | ||
.success { println("$assignees set to pull request #$pullRequestNumber") } | ||
} | ||
|
||
@Serializable | ||
private data class SetAssigneesRequest( | ||
val assignees: List<String> | ||
) |
43 changes: 43 additions & 0 deletions
43
flank-scripts/src/main/kotlin/flank/scripts/pullrequest/SetLabels.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package flank.scripts.pullrequest | ||
|
||
import com.github.kittinunf.fuel.Fuel | ||
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.map | ||
import com.github.kittinunf.result.onError | ||
import com.github.kittinunf.result.success | ||
import flank.scripts.exceptions.mapClientError | ||
import flank.scripts.exceptions.toGithubException | ||
import flank.scripts.github.GitHubLabelDeserializable | ||
import flank.scripts.github.appendGitHubHeaders | ||
import flank.scripts.utils.toJson | ||
import kotlinx.serialization.Serializable | ||
|
||
suspend fun copyLabels(githubToken: String, issueNumber: Int, pullRequestNumber: Int) { | ||
getLabelsFromIssue(githubToken, issueNumber) | ||
.onError { println("Could not copy labels because of ${it.message}") } | ||
.map { it.map { label -> label.name } } | ||
.getOrNull() | ||
?.run { setLabelsToPullRequest(githubToken, pullRequestNumber, this) } | ||
} | ||
|
||
private suspend fun getLabelsFromIssue(githubToken: String, issueNumber: Int) = | ||
Fuel.get("https://api.github.com/repos/Flank/flank/issues/$issueNumber/labels") | ||
.appendGitHubHeaders(githubToken) | ||
.awaitResult(GitHubLabelDeserializable) | ||
.mapClientError { it.toGithubException() } | ||
|
||
private suspend fun setLabelsToPullRequest(githubToken: String, pullRequestNumber: Int, labels: List<String>) { | ||
Fuel.post("https://api.github.com/repos/Flank/flank/issues/$pullRequestNumber/labels") | ||
.appendGitHubHeaders(githubToken) | ||
.body(SetLabelsRequest(labels).toJson()) | ||
.awaitStringResult() | ||
.onError { println("Could not set assignees because of ${it.message}") } | ||
.success { println("$labels set to pull request #$pullRequestNumber") } | ||
} | ||
|
||
@Serializable | ||
private data class SetLabelsRequest( | ||
val labels: List<String> | ||
) |
Oops, something went wrong.