Skip to content
Permalink
Browse files
refactoring to use built-in JDK11+ HttpClient
  • Loading branch information
paulk-asert committed Aug 11, 2021
1 parent 0cd6392 commit a63566fbcf8171e03b88d32ffe832b20dd4710af
Showing 4 changed files with 78 additions and 40 deletions.
@@ -0,0 +1,38 @@
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration

class HttpUtil {
private HttpUtil() {}

static HttpClient newClient() {
HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10))
.build()

}

static HttpRequest getRequest(url, user, password) {
HttpRequest.newBuilder()
.uri(new URI(url))
.header('Authorization', 'Basic ' + "$user:$password".getBytes('iso-8859-1').encodeBase64())
.GET()
.build()
}

static HttpRequest putRequest(url, json, user, password) {
def body = HttpRequest.BodyPublishers.ofString(json)
HttpRequest.newBuilder()
.uri(new URI(url))
.header('Authorization', 'Basic ' + "$user:$password".getBytes('iso-8859-1').encodeBase64())
.header('Content-Type', 'application/json')
.PUT(body)
.build()
}

static WrappedResponse send(HttpClient client, HttpRequest request) {
new WrappedResponse(client.send(request, HttpResponse.BodyHandlers.ofString()))
}
}
@@ -0,0 +1,14 @@
import java.net.http.HttpResponse
import groovy.json.JsonSlurper

class WrappedResponse {
@Delegate HttpResponse response

WrappedResponse(HttpResponse response) {
this.response = response
}

def getJson() {
new JsonSlurper().parseText(response.body())
}
}
@@ -1,11 +1,9 @@
import groovyx.net.http.HttpBuilder
import org.ajoberstar.grgit.Credentials
import org.ajoberstar.grgit.util.JGitUtil
import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
@@ -14,7 +12,6 @@ buildscript {
dependencies {
classpath "org.ajoberstar:grgit:${getProperty('version.grgit')}"
classpath "at.bxm.gradleplugins:gradle-svntools-plugin:${getProperty('version.svntools')}"
classpath "io.github.http-builder-ng:http-builder-ng-okhttp:${getProperty('version.httpbuilderng')}"
classpath "org.hidetake:gradle-ssh-plugin:${getProperty('version.sshplugin')}"
classpath "gradle.plugin.io.sdkman:gradle-sdkvendor-plugin:${getProperty('version.sdkmanplugin')}"
}
@@ -130,14 +127,11 @@ task populateFromCentral(dependsOn: assumesPropsSet) {
task unreleaseOnJira(dependsOn: jiraCheckPhase2) {
description = "Unrelease on Jira if the VOTE is cancelled"
doLast {
HttpBuilder.configure {
request.uri = 'https://issues.apache.org'
request.auth.basic apacheUser, apachePassword
}.put {
request.uri.path = "/jira/rest/api/2/version/$versionId"
request.body = /{ "released": false, "releaseDate": null }/
request.contentType = 'application/json'
}
def client = HttpUtil.newClient()
def request = HttpUtil.putRequest("https://issues.apache.org/jira/rest/api/2/version/$versionId",
/{ "released": false, "releaseDate": null }/, apacheUser, apachePassword)
def response = HttpUtil.send(client, request)
assert response.statusCode() == 200
}
}

@@ -3,15 +3,13 @@ import at.bxm.gradleplugins.svntools.tasks.SvnAdd
import at.bxm.gradleplugins.svntools.tasks.SvnCheckout
import at.bxm.gradleplugins.svntools.tasks.SvnCommit
import at.bxm.gradleplugins.svntools.tasks.SvnVersion
import groovyx.net.http.HttpBuilder
import org.ajoberstar.grgit.Credentials
import org.ajoberstar.grgit.Person
import org.ajoberstar.grgit.util.JGitUtil
import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
@@ -20,7 +18,6 @@ buildscript {
dependencies {
classpath "org.ajoberstar:grgit:${getProperty('version.grgit')}"
classpath "at.bxm.gradleplugins:gradle-svntools-plugin:${getProperty('version.svntools')}"
classpath "io.github.http-builder-ng:http-builder-ng-okhttp:${getProperty('version.httpbuilderng')}"
classpath "org.hidetake:gradle-ssh-plugin:${getProperty('version.sshplugin')}"
classpath "gradle.plugin.io.sdkman:gradle-sdkvendor-plugin:${getProperty('version.sdkmanplugin')}"
}
@@ -83,32 +80,30 @@ task checkCompatibility(type: Exec, dependsOn: [assumesBranch, checkout]) {

task jiraPrecheck(dependsOn: assumesRelVersion) {
doLast {
def prefix = '/jira/rest/api/2'
def jira = HttpBuilder.configure {
request.uri = 'https://issues.apache.org'
request.auth.basic apacheUser, apachePassword
}
def resp = jira.get {
request.uri.path = "$prefix/project/GROOVY/versions"
}
def versionFields = resp.find { it.name == relVersion }
def prefix = 'https://issues.apache.org/jira/rest/api/2'
def client = HttpUtil.newClient()
def request = HttpUtil.getRequest("$prefix/project/GROOVY/versions", apacheUser, apachePassword)
def response = HttpUtil.send(client, request)
assert response.statusCode() == 200
def versionFields = response.json.find { it.name == relVersion }
assert versionFields, "Version $relVersion not found in Jira!"
if (!project.hasProperty('skipJiraReleaseCheck')) {
assert !versionFields.released, "Version $relVersion already released!"
}
project.ext.versionId = versionFields.id
project.ext.projectId = versionFields.projectId

resp = jira.get {
request.uri.path = "$prefix/version/$versionId/unresolvedIssueCount"
}
if (resp.issuesUnresolvedCount) {
logger.warn "Warning found $resp.issuesUnresolvedCount unresolved issues for version $relVersion"
request = HttpUtil.getRequest("$prefix/version/$versionId/unresolvedIssueCount", apacheUser, apachePassword)
response = HttpUtil.send(client, request)
assert response.statusCode() == 200
if (response.json.issuesUnresolvedCount) {
logger.warn "Warning found $response.json.issuesUnresolvedCount unresolved issues for version $relVersion"
}
resp = jira.get {
request.uri.path = "$prefix/version/$versionId/relatedIssueCounts"
}
project.ext.fixCount = resp.issuesFixedCount

request = HttpUtil.getRequest("$prefix/version/$versionId/relatedIssueCounts", apacheUser, apachePassword)
response = HttpUtil.send(client, request)
assert response.statusCode() == 200
project.ext.fixCount = response.json.issuesFixedCount
}
}

@@ -280,14 +275,11 @@ task releaseOnJira(dependsOn: [assumesRelVersion, jiraPrecheck, createAndPushTag
group = "Pre-vote phase"
description = "Releases the version on JIRA"
doLast {
HttpBuilder.configure {
request.uri = 'https://issues.apache.org'
request.auth.basic apacheUser, apachePassword
}.put {
request.uri.path = "/jira/rest/api/2/version/$versionId"
request.body = /{ "released": true, "releaseDate": "$now" }/
request.contentType = 'application/json'
}
def client = HttpUtil.newClient()
def request = HttpUtil.putRequest("https://issues.apache.org/jira/rest/api/2/version/$versionId",
/{ "released": true, "releaseDate": "$now" }/, apacheUser, apachePassword)
def response = HttpUtil.send(client, request)
assert response.statusCode() == 200
}
}

0 comments on commit a63566f

Please sign in to comment.