11package org.jetbrains.kotlinx.jupyter.build
22
3+ import kotlinx.serialization.Serializable
34import kotlinx.serialization.json.Json
45import kotlinx.serialization.json.JsonArray
6+ import kotlinx.serialization.json.JsonElement
57import kotlinx.serialization.json.JsonObject
68import kotlinx.serialization.json.JsonPrimitive
79import kotlinx.serialization.json.encodeToJsonElement
10+ import kotlinx.serialization.json.int
11+ import org.gradle.api.Project
812import org.gradle.process.ExecResult
913import org.gradle.process.ExecSpec
1014import org.gradle.tooling.BuildException
11- import java.io.File
12- import java.io.OutputStream
1315import org.http4k.core.Method
1416import org.http4k.core.Request
15- import org.jetbrains.kotlinx.jupyter.common.jsonObject
17+ import org.http4k.core.Response
18+ import org.jetbrains.kotlinx.jupyter.common.ResponseWrapper
1619import org.jetbrains.kotlinx.jupyter.common.httpRequest
20+ import org.jetbrains.kotlinx.jupyter.common.jsonObject
1721import org.jetbrains.kotlinx.jupyter.common.text
1822import org.jetbrains.kotlinx.jupyter.common.withBasicAuth
1923import org.jetbrains.kotlinx.jupyter.common.withJson
24+ import java.io.File
25+ import java.io.OutputStream
26+
27+ private val Project .libName get() = prop<String >(" jupyter.lib.name" )
28+ private val Project .libParamName get() = prop<String >(" jupyter.lib.param.name" )
29+ private val Project .libParamValue get() = prop<String >(" jupyter.lib.param.value" )
30+
31+ private val Project .prGithubUser get() = prop<String >(" jupyter.github.user" )
32+ private val Project .prGithubToken get() = prop<String >(" jupyter.github.token" )
33+
34+ private val Project .githubRepoOwner get() = prop<String >(" githubRepoUser" )
35+ private val Project .githubRepoName get() = prop<String >(" githubRepoName" )
36+
37+ @Serializable
38+ class NewPrData (
39+ val title : String ,
40+ val head : String ,
41+ val base : String ,
42+ )
43+
44+ @Serializable
45+ class SetLabelsData (
46+ val labels : List <String >,
47+ )
2048
2149fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks () {
2250 tasks.register(" updateKotlinVersion" ) {
@@ -49,9 +77,9 @@ fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks() {
4977
5078 val updateLibraryParamTask = tasks.register(" updateLibraryParam" ) {
5179 doLast {
52- val libName = project.property( " jupyter.lib.name " ) as String
53- val paramName = project.property( " jupyter.lib.param.name " ) as String
54- val paramValue = project.property( " jupyter.lib.param.value " ) as String
80+ val libName = project.libName
81+ val paramName = project.libParamName
82+ val paramValue = project.libParamValue
5583
5684 updateLibBranchName = " update-$libName -$paramName -$paramValue "
5785 updateLibraryParam(libName, paramName, paramValue)
@@ -79,8 +107,8 @@ fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks() {
79107 execGit(" commit" , " -m" , " [AUTO] Update library version" )
80108
81109 val repoUrl = rootProject.property(" pushRepoUrl" ) as String
82- execGit(" push" , " --force" , " -u" , repoUrl, getCurrentBranch() + " :" + updateLibBranchName!! ) {
83- this .standardOutput = object : OutputStream () {
110+ execGit(" push" , " --force" , " -u" , repoUrl, getCurrentBranch() + " :refs/heads/ " + updateLibBranchName!! ) {
111+ this .standardOutput = object : OutputStream () {
84112 override fun write (b : Int ) { }
85113 }
86114 }
@@ -93,24 +121,48 @@ fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks() {
93121 dependsOn(pushChangesTask)
94122
95123 doLast {
96- val user = rootProject.property(" jupyter.github.user" ) as String
97- val password = rootProject.property(" jupyter.github.token" ) as String
98- fun githubRequest (method : Method , request : String , json : Map <String , String >? = null): Int {
99- val response = httpRequest(Request (method, " https://api.github.com/$request " )
100- .withJson(Json .encodeToJsonElement(json))
101- .withBasicAuth(user, password)
124+ val user = rootProject.prGithubUser
125+ val password = rootProject.prGithubToken
126+ fun githubRequest (
127+ method : Method ,
128+ request : String ,
129+ json : JsonElement ,
130+ onFailure : (Response ) -> Unit ,
131+ ): ResponseWrapper {
132+ val response = httpRequest(
133+ Request (method, " https://api.github.com/$request " )
134+ .withJson(json)
135+ .withBasicAuth(user, password)
102136 )
103137 println (response.text)
104- return response.status.code
138+ if (! response.status.successful) {
139+ onFailure(response)
140+ }
141+ return response
105142 }
106143
107- val code = githubRequest(Method .POST , " repos/Kotlin/kotlin-jupyter/pulls" , mapOf (
108- " title" to " Update library versions" ,
109- " head" to updateLibBranchName!! ,
110- " base" to " master"
111- ))
112- if (code != 200 && code != 201 ) {
113- throw BuildException (" Creating PR failed with code $code " , null )
144+ val fullRepo = " ${rootProject.githubRepoOwner} /${rootProject.githubRepoName} "
145+ val prResponse = githubRequest(
146+ Method .POST , " repos/$fullRepo /pulls" ,
147+ Json .encodeToJsonElement(
148+ NewPrData (
149+ title = " Update `${rootProject.libName} ` library to `${rootProject.libParamValue} `" ,
150+ head = updateLibBranchName!! ,
151+ base = " master"
152+ )
153+ )
154+ ) { response ->
155+ throw BuildException (" Creating PR failed with code ${response.status.code} " , null )
156+ }
157+
158+ val prNumber = (prResponse.jsonObject[" number" ] as JsonPrimitive ).int
159+ githubRequest(
160+ Method .POST , " repos/$fullRepo /issues/$prNumber /labels" ,
161+ Json .encodeToJsonElement(
162+ SetLabelsData (listOf (" no-changelog" , " library-descriptors" ))
163+ )
164+ ) { response ->
165+ throw BuildException (" Cannot setup labels for created PR: ${response.text} " , null )
114166 }
115167 }
116168 }
0 commit comments