Skip to content

Commit

Permalink
Ver 2.2.7 Fix #19 revert http changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BreadMoirai committed Apr 1, 2019
1 parent 9e7bfca commit 007b02a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 43 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# github-release
[![Gradle Plugin Portal](https://img.shields.io/badge/version-2.2.6-blue.svg)](https://plugins.gradle.org/plugin/com.github.breadmoirai.github-release/2.2.6)
[![Gradle Plugin Portal](https://img.shields.io/badge/version-2.2.7-blue.svg)](https://plugins.gradle.org/plugin/com.github.breadmoirai.github-release/2.2.7)

[i19]: https://github.com/BreadMoirai/github-release-gradle-plugin/issues/19
[i17]: https://github.com/BreadMoirai/github-release-gradle-plugin/issues/17
[i16]: https://github.com/BreadMoirai/github-release-gradle-plugin/issues/16
[i14]: https://github.com/BreadMoirai/github-release-gradle-plugin/issues/14
Expand All @@ -14,6 +15,9 @@ This plugin is not endorsed by Github.
This plugin uses [OkHttp](http://square.github.io/okhttp/) to send a POST requests to the github api that creates a release and uploads specified assets.

## Changelog
2.2.7
- Address [#19][i19]. Replaced mime type detector with Apache Tika.

2.2.6
- Address [#14][i14] with new property [`apiEndpoint`](https://github.com/BreadMoirai/github-release-gradle-plugin/wiki#apiEndpoint)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ExtensionPropertyASTTransformation extends AbstractASTTransformation {

// create getter method
// getPropertyProvider
def providerClassNode = createParameterizedNode(Provider, genericType)
final providerClassNode = createParameterizedNode(Provider, genericType)
classNode.addMethod new MethodNode(
"get${fieldNameCap}Provider",
ACC_PROTECTED,
Expand All @@ -61,12 +61,13 @@ class ExtensionPropertyASTTransformation extends AbstractASTTransformation {
[] as ClassNode[],
new ReturnStatement(fieldVar)
).tap {
it.addAnnotation new AnnotationNode(new ClassNode(Input))
addAnnotation new AnnotationNode(new ClassNode(Input))
}

//create setter methods
// setProperty(T value)
def paramValue = new Parameter(genericType.type, fieldName)
final type = genericType.type
final paramValue = new Parameter(type, fieldName)
classNode.addMethod(new MethodNode(
"set${fieldNameCap}",
ACC_PUBLIC,
Expand All @@ -87,8 +88,8 @@ class ExtensionPropertyASTTransformation extends AbstractASTTransformation {
))

// setProp(Provider<? extends T> value)
def providerWildClassNode = createParameterizedNode(Provider, GenericsUtils.buildWildcardType(genericType.type))
def paramProvider = new Parameter(providerWildClassNode, fieldName)
final providerWildClassNode = createParameterizedNode(Provider, GenericsUtils.buildWildcardType(type))
final paramProvider = new Parameter(providerWildClassNode, fieldName)
classNode.addMethod(new MethodNode(
"set${fieldNameCap}",
ACC_PUBLIC,
Expand All @@ -109,7 +110,7 @@ class ExtensionPropertyASTTransformation extends AbstractASTTransformation {
))

// setProp(Callable<? extends T> callable)
def paramCallable = new Parameter(createParameterizedNode(Callable, GenericsUtils.buildWildcardType(genericType.type)), fieldName)
final paramCallable = new Parameter(createParameterizedNode(Callable, GenericsUtils.buildWildcardType(type)), fieldName)
classNode.addMethod(new MethodNode(
"set${fieldNameCap}",
ACC_PUBLIC,
Expand Down Expand Up @@ -147,6 +148,29 @@ class ExtensionPropertyASTTransformation extends AbstractASTTransformation {
)
)
))

if (type.typeClass == CharSequence) {
final type1 = new ClassNode(String)
final paramValue1 = new Parameter(type1, fieldName)
classNode.addMethod(new MethodNode(
"set${fieldNameCap}",
ACC_PUBLIC,
ClassHelper.VOID_TYPE,
[paramValue1] as Parameter[],
[] as ClassNode[],
new ExpressionStatement(new MethodCallExpression(new FieldExpression(fieldNode), "set", new VariableExpression(paramValue1)))
))

// prop(T value)
classNode.addMethod(new MethodNode(
fieldName,
ACC_PUBLIC,
ClassHelper.VOID_TYPE,
[paramValue1] as Parameter[],
[] as ClassNode[],
new ExpressionStatement(new MethodCallExpression(new FieldExpression(fieldNode), "set", new VariableExpression(paramValue1)))
))
}
}

private static ClassNode createParameterizedNode(Class returnType, GenericsType genericType) {
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugins {
}

group = 'com.github.breadmoirai'
version = '2.2.6'
version = '2.2.7'

repositories {
jcenter()
Expand All @@ -34,9 +34,9 @@ repositories {
dependencies {
compileOnly project(':ast')

implementation 'org.codehaus.groovy:groovy-all:2.5.4'
implementation 'org.codehaus.groovy:groovy-all:2.5.6'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.j256.simplemagic:simplemagic:1.10'
compile group: 'org.apache.tika', name: 'tika-core', version: '1.20'
compile 'org.zeroturnaround:zt-exec:1.10'

compileOnly 'com.gradle.publish:plugin-publish-plugin:0.9.7'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ChangeLogSupplier implements Callable<String> {
private final Property<CharSequence> currentCommit
private final Property<CharSequence> lastCommit
private final Property<List> options
private final OkHttpClient client = GithubApi.client

ChangeLogSupplier(GithubReleaseExtension extension, Project project) {
this.project = project
Expand Down Expand Up @@ -83,7 +84,7 @@ class ChangeLogSupplier implements Callable<String> {

// query the github api for releases
String releaseUrl = "${GithubApi.endpoint}/repos/$owner/$repo/releases"
Response response = new OkHttpClient().newCall(createRequestWithHeaders(auth)
Response response = client.newCall(createRequestWithHeaders(auth)
.url(releaseUrl)
.get()
.build()
Expand Down Expand Up @@ -113,7 +114,7 @@ class ChangeLogSupplier implements Callable<String> {
Object lastRelease = releases.get(index + 1)
String lastTag = lastRelease.tag_name
String tagUrl = "${GithubApi.endpoint}/repos/$owner/$repo/git/refs/tags/$lastTag"
Response tagResponse = new OkHttpClient()
Response tagResponse = client
.newCall(createRequestWithHeaders(auth)
.url(tagUrl)
.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package com.github.breadmoirai.githubreleaseplugin

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

import javax.net.ssl.HttpsURLConnection
import java.nio.file.Files
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import org.apache.tika.Tika

class GithubApi {

static String endpoint = "https://api.github.com"
public static final MediaType MEDIATYPE_JSON = MediaType.parse("application/json")

private final Map<String, String> defaultHeaders
private final Tika tika = new Tika()

GithubApi(CharSequence authorization) {
this.defaultHeaders = [
Expand All @@ -21,87 +25,79 @@ class GithubApi {
]
}

public static final OkHttpClient client = new OkHttpClient()

/**
* Opens the specified {@code url} and sends an http request after applying the {@code closure}.
* The response is read and returned. The response body is parsed as JSON and represented as an field
* in the returned {@link Response}.
* @param url the api endpoint
* @param closure a closure that adds any necessary configuration to the url connection such as the requestMethod
* @param closure a closure that adds any necessary configuration OkHttp Request
* @return The response containing the status code, status message, response headers, and the body as an object
*/
Response connect(String url, @DelegatesTo(HttpsURLConnection) Closure closure) {
(new URL(url).openConnection() as HttpsURLConnection).with { connection ->
defaultHeaders.forEach { key, value ->
setRequestProperty key, value
Response connect(String url, @DelegatesTo(Request.Builder) Closure closure) {
client.newCall(new Request.Builder().tap {
delegate.url url
defaultHeaders.each { name, value ->
header name, value
}
closure.setDelegate(connection)
closure.setDelegate delegate
closure()
def code = responseCode
if (code >= 400) {
return new Response(code, responseMessage, errorStream.text, headerFields)
} else {
return new Response(code, responseMessage, inputStream.text, headerFields)
}
}.build()).execute().withCloseable { response ->
new Response(response.code(), response.message(), response.body().string(), response.headers().toMultimap())
}
}

Response findReleaseByTag(CharSequence owner, CharSequence repo, CharSequence tagName) {
String releaseUrl = "$endpoint/repos/$owner/$repo/releases/tags/$tagName"
println ':githubRelease CHECKING FOR PREVIOUS RELEASE'
connect(releaseUrl) {
requestMethod = 'GET'
get()
}
}

Response findTagByName(CharSequence owner, CharSequence repo, CharSequence tagName) {
String tagUrl = "$endpoint/repos/$owner/$repo/git/refs/tags/$tagName"
connect(tagUrl) {
requestMethod = "GET"
get()
}
}

Response deleteReleaseByUrl(String url) {
println ':githubRelease DELETING RELEASE'
connect(url) {
requestMethod = "DELETE"
delete()
}
}

Response uploadFileToUrl(String url, File asset) {
println ':githubRelease UPLOADING ' + asset.name
connect(url) {
requestMethod = "PUT"
setRequestProperty('Content-Type', Files.probeContentType(asset.toPath()))
doOutput = true
Files.copy(asset.toPath(), outputStream)
put RequestBody.create(MediaType.parse(tika.detect(asset)), asset)
}
}

Response postRelease(CharSequence owner, CharSequence repo, Map data) {
String releaseUrl = "$endpoint/repos/$owner/$repo/releases"
println ':githubRelease CREATING NEW RELEASE'
connect(releaseUrl) {
requestMethod = "POST"
doOutput = true
outputStream.withPrintWriter {
it.write(JsonOutput.toJson(data))
}
post RequestBody.create(MEDIATYPE_JSON, JsonOutput.toJson(data))
}
}

Response getReleases(CharSequence owner, CharSequence repo) {
String releaseUrl = "$endpoint/repos/$owner/$repo/releases"
println ':githubRelease RETRIEVING RELEASES ' + releaseUrl
connect(releaseUrl) {
requestMethod = "GET"
get()
}
}

Response getCommits(CharSequence owner, CharSequence repo) {
String commitsUrl = "$endpoint/repo/$owner/$repo/commits"
println ':githubRelease RETRIEVING COMMITS ' + commitsUrl
connect(commitsUrl) {
requestMethod = "GET"
get()
}
}

Expand Down

0 comments on commit 007b02a

Please sign in to comment.