1
1
package org.jetbrains.kotlinx.jupyter.build
2
2
3
+ import kotlinx.serialization.Serializable
3
4
import kotlinx.serialization.json.Json
4
5
import kotlinx.serialization.json.JsonArray
6
+ import kotlinx.serialization.json.JsonElement
5
7
import kotlinx.serialization.json.JsonObject
6
8
import kotlinx.serialization.json.JsonPrimitive
7
9
import kotlinx.serialization.json.encodeToJsonElement
10
+ import kotlinx.serialization.json.int
11
+ import org.gradle.api.Project
8
12
import org.gradle.process.ExecResult
9
13
import org.gradle.process.ExecSpec
10
14
import org.gradle.tooling.BuildException
11
- import java.io.File
12
- import java.io.OutputStream
13
15
import org.http4k.core.Method
14
16
import 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
16
19
import org.jetbrains.kotlinx.jupyter.common.httpRequest
20
+ import org.jetbrains.kotlinx.jupyter.common.jsonObject
17
21
import org.jetbrains.kotlinx.jupyter.common.text
18
22
import org.jetbrains.kotlinx.jupyter.common.withBasicAuth
19
23
import 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
+ )
20
48
21
49
fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks () {
22
50
tasks.register(" updateKotlinVersion" ) {
@@ -49,9 +77,9 @@ fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks() {
49
77
50
78
val updateLibraryParamTask = tasks.register(" updateLibraryParam" ) {
51
79
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
55
83
56
84
updateLibBranchName = " update-$libName -$paramName -$paramValue "
57
85
updateLibraryParam(libName, paramName, paramValue)
@@ -79,8 +107,8 @@ fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks() {
79
107
execGit(" commit" , " -m" , " [AUTO] Update library version" )
80
108
81
109
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 () {
84
112
override fun write (b : Int ) { }
85
113
}
86
114
}
@@ -93,24 +121,48 @@ fun ProjectWithInstallOptions.prepareKotlinVersionUpdateTasks() {
93
121
dependsOn(pushChangesTask)
94
122
95
123
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)
102
136
)
103
137
println (response.text)
104
- return response.status.code
138
+ if (! response.status.successful) {
139
+ onFailure(response)
140
+ }
141
+ return response
105
142
}
106
143
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 )
114
166
}
115
167
}
116
168
}
0 commit comments