Skip to content

Commit cb6c94e

Browse files
author
James Williams
authored
Open PR tool (#524)
## What is the goal of this PR? We've created a tool that can be used to open a PR on GitHub. Now, programatically creating PRs is possible and convenient. ## What are the changes implemented in this PR? As above. ## Example usage ``` bazel run @vaticle_dependencies//tool/github:open-pr -- \ --repo=vaticle/typedb \ --head-branch=development \ --base-branch=master \ --title="Release" \ --body="Replace me" \ --token=$GITHUB_TOKEN ``` ## Defaults By default, we provide an empty body for the PR and 'vaticle-bot' for the username.
1 parent d4f15f1 commit cb6c94e

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tool/common/deps.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ maven_artifacts = [
1717
"com.google.http-client:google-http-client",
1818
"org.zeroturnaround:zt-exec",
1919
"info.picocli:picocli",
20+
"commons-io:commons-io",
21+
"org.kohsuke:github-api",
2022
"org.jetbrains.compose.compiler:compiler",
2123
"org.jsoup:jsoup",
2224
]

tool/github/BUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
6+
load("@vaticle_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
7+
8+
kt_jvm_library(
9+
name = "open-pr-jar",
10+
srcs = glob(["*.kt"]),
11+
data = glob(["template/**/*"]),
12+
deps = [
13+
"@vaticle_bazel_distribution//common",
14+
"@vaticle_bazel_distribution//common/shell",
15+
"@maven//:org_zeroturnaround_zt_exec",
16+
"@maven//:commons_io_commons_io",
17+
"@maven//:info_picocli_picocli",
18+
"@maven//:org_kohsuke_github_api",
19+
],
20+
visibility = ["//visibility:private"],
21+
)
22+
23+
java_binary(
24+
name = "open-pr",
25+
runtime_deps = ["open-pr-jar"],
26+
main_class = "com.vaticle.dependencies.tool.github.OpenPR",
27+
visibility = ["//visibility:public"],
28+
)
29+
30+
checkstyle_test(
31+
name = "checkstyle",
32+
include = glob(["*"]),
33+
license_type = "mpl-header",
34+
)

tool/github/OpenPR.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
package com.vaticle.dependencies.tool.github
8+
9+
import org.kohsuke.github.GitHub
10+
import picocli.CommandLine
11+
import java.util.concurrent.Callable
12+
import kotlin.system.exitProcess
13+
14+
object OpenPR: Callable<Int> {
15+
private const val DEFAULT_USERNAME = "vaticle-bot"
16+
17+
@CommandLine.Option(names = ["--repo"], description = ["The repository to open the PR on"])
18+
private lateinit var repo: String
19+
20+
@CommandLine.Option(names = ["--head-branch"], description = ["The merging branch"])
21+
private lateinit var headBranch: String
22+
23+
@CommandLine.Option(names = ["--base-branch"], description = ["The branch being merged into"])
24+
private lateinit var baseBranch: String
25+
26+
@CommandLine.Option(names = ["--title"], description = ["The title of the PR"])
27+
private lateinit var title: String
28+
29+
@CommandLine.Option(names = ["--body"], description = ["The body of the PR"])
30+
private var body: String = ""
31+
32+
@CommandLine.Option(names = ["--username"], description = ["The GitHub username of the user opening the PR"])
33+
private var username: String = DEFAULT_USERNAME
34+
35+
@CommandLine.Option(names = ["--token"], description = ["The GitHub authentication token"])
36+
private lateinit var token: String
37+
38+
@JvmStatic
39+
fun main(args: Array<String>) {
40+
exitProcess(CommandLine(OpenPR).execute(*args))
41+
}
42+
43+
override fun call(): Int {
44+
val gh = GitHub.connect(username, token)
45+
gh.getRepository(repo).createPullRequest(title, headBranch, baseBranch, body)
46+
return 0
47+
}
48+
}

0 commit comments

Comments
 (0)