Skip to content

Commit

Permalink
address issue #16 & #17
Browse files Browse the repository at this point in the history
remote metaClass extensions and some unnecessary logging info
  • Loading branch information
BreadMoirai committed Mar 11, 2019
1 parent 6600693 commit 141eb79
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 110 deletions.
2 changes: 1 addition & 1 deletion 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.3.0'
version = '2.2.5'

repositories {
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.github.breadmoirai.githubreleaseplugin

import com.github.breadmoirai.GithubRelease
import com.github.breadmoirai.githubreleaseplugin.exceptions.PropertyNotSetException
import groovy.json.JsonSlurper
import okhttp3.OkHttpClient
Expand All @@ -25,15 +24,12 @@ import okhttp3.Response
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.zeroturnaround.exec.ProcessExecutor

import java.util.concurrent.Callable

class ChangeLogSupplier implements Callable<String> {

private static final Logger log = LoggerFactory.getLogger(ChangeLogSupplier.class)

private final Project project

private final Provider<CharSequence> owner
Expand Down Expand Up @@ -98,7 +94,7 @@ class ChangeLogSupplier implements Callable<String> {
List releases = new JsonSlurper().parse(response.body().bytes()) as List
// find current release if exists
int index = releases.findIndexOf { release -> (release.tag_name == tag) }
if (releases.isEmpty()) {
if (releases.isEmpty() || (releases.size() == 1 && index == 0)) {
CharSequence exe = this.executable.getOrNull()
if (exe == null) {
throw new PropertyNotSetException("exe")
Expand All @@ -113,7 +109,7 @@ class ChangeLogSupplier implements Callable<String> {
.trim()
} else {
// get the next release before the current release
// if current release does not ezist, then gets the most recent release
// if current release does not exist, then gets the most recent release
Object lastRelease = releases.get(index + 1)
String lastTag = lastRelease.tag_name
String tagUrl = "https://api.github.com/repos/$owner/$repo/git/refs/tags/$lastTag"
Expand All @@ -134,7 +130,7 @@ class ChangeLogSupplier implements Callable<String> {

@Override
String call() {
log.info ':githubRelease Generating Release Body with Commit History'
println ':githubRelease Generating Release Body with Commit History'
CharSequence current = currentCommit.get()
CharSequence last = lastCommit.get()
List<String> opts = options.get()*.toString()
Expand All @@ -155,9 +151,12 @@ class ChangeLogSupplier implements Callable<String> {
throw new Error('Failed to run git executable to find commit history. ' +
'Please specify the path to the git executable.\n')
}
else throw e
}
}



public void setCurrentCommit(Provider<? extends CharSequence> currentCommit) {
this.currentCommit.set(currentCommit)
}
Expand Down Expand Up @@ -254,12 +253,16 @@ class ChangeLogSupplier implements Callable<String> {
setExecutable { gitExecutable }
}

@Deprecated
static Request.Builder createRequestWithHeaders(CharSequence authorization) {
return new Request.Builder()
.addHeader('Authorization', authorization.toString())
.addHeader('User-Agent', "breadmoirai github-release-gradle-plugin")
.addHeader('Accept', 'application/vnd.github.v3+json')
.addHeader('Content-Type', 'application/json')
}

@Override
public String toString() {
return call()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ class GithubApi {

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

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

Response deleteReleaseByUrl(String url) {
println 'githubRelease DELETING RELEASE ' + url
println 'githubRelease DELETING RELEASE'
connect(url) {
requestMethod = "DELETE"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.github.breadmoirai.githubreleaseplugin


import com.github.breadmoirai.githubreleaseplugin.ast.ExtensionClass
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
Expand Down Expand Up @@ -104,21 +103,39 @@ class GithubReleaseExtension {

final Project project

@SuppressWarnings("GroovyAssignabilityCheck")
GithubReleaseExtension(Project project) {
this.project = project
final ObjectFactory objectFactory = project.objects
owner = objectFactory.namedProperty("owner", CharSequence)
repo = objectFactory.namedProperty("repo", CharSequence)
authorization = objectFactory.namedProperty("authorization", CharSequence)
tagName = objectFactory.namedProperty("tagName", CharSequence)
targetCommitish = objectFactory.namedProperty("targetCommitish", CharSequence)
releaseName = objectFactory.namedProperty("releaseName", CharSequence)
body = objectFactory.namedProperty("body", CharSequence)
draft = objectFactory.namedProperty("draft", Boolean)
prerelease = objectFactory.namedProperty("prerelease", Boolean)
owner = objectFactory.property(CharSequence)
repo = objectFactory.property(CharSequence)
authorization = objectFactory.property(CharSequence)
tagName = objectFactory.property(CharSequence)
targetCommitish = objectFactory.property(CharSequence)
releaseName = objectFactory.property(CharSequence)
body = objectFactory.property(CharSequence)
draft = objectFactory.property(Boolean)
prerelease = objectFactory.property(Boolean)
releaseAssets = project.files()
overwrite = objectFactory.namedProperty("overwrite", Boolean)
allowUploadToExisting = objectFactory.namedProperty("allowUploadToExisting", Boolean)
overwrite = objectFactory.property(Boolean)
allowUploadToExisting = objectFactory.property(Boolean)

owner {
def group = project.group.toString()
return group.substring(group.lastIndexOf('.') + 1)
}
repo {
project.name ?: project.rootProject?.name ?: project.rootProject?.rootProject?.name
}
tagName { "v${project.version}" }
targetCommitish { 'master' }
releaseName { "v${project.version}" }
draft { false }
prerelease { false }
// authorization has no default value
body { "" }
overwrite { false }
allowUploadToExisting { false }
}

Callable<String> changelog(@DelegatesTo(ChangeLogSupplier) final Closure closure) {
Expand All @@ -143,7 +160,6 @@ class GithubReleaseExtension {
this.releaseAssets.setFrom(assets)
}


void setToken(CharSequence token) {
this.authorization.set("Token $token")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,18 @@

package com.github.breadmoirai.githubreleaseplugin

import com.github.breadmoirai.githubreleaseplugin.exceptions.PropertyNotSetException

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.util.concurrent.Callable

class GithubReleasePlugin implements Plugin<Project> {

private final static Logger log = LoggerFactory.getLogger(GithubReleasePlugin.class)
public static boolean infoEnabled = false
private Project project

static {
Provider.metaClass.name = "Undefined"
Provider.metaClass.getOrThrow << {
def val = delegate.getOrNull()
if (val == null) {
String name = delegate.name
throw new PropertyNotSetException(name)
}
return val
}
Provider.metaClass.getOrDefault << { Closure<?> defaultValue ->
def val = delegate.getOrNull()
if (val == null) {
return defaultValue()
}
return val
}
ObjectFactory.metaClass.namedProperty << { String name, Class<?> valueType ->
def provider = delegate.property(valueType)
provider.name = name
return provider
}
}

@Override
void apply(Project project) {
this.project = project
Expand All @@ -82,39 +53,6 @@ class GithubReleasePlugin implements Plugin<Project> {
setAllowUploadToExisting ext.allowUploadToExistingProvider
}
}

// project.afterEvaluate {
// def self = project.plugins.findPlugin(GithubReleasePlugin)
//
// if (self) {
// log.debug "Assigning default values for GithubReleasePlugin"
// GithubReleaseExtension e = project.extensions.getByType GithubReleaseExtension
// setOrElse e.owner, {
// def group = project.group.toString()
// group.substring(group.lastIndexOf('.') + 1)
// }
// setOrElse e.repo, {
// project.name ?: project.rootProject?.name ?: project.rootProject?.rootProject?.name
// }
// setOrElse e.tagName, { "v${project.version}" }
// setOrElse e.targetCommitish, { 'master' }
// setOrElse e.releaseName, {
// e.tagName.get()
// }
// setOrElse e.draft, { false }
// setOrElse e.prerelease, { false }
// // authorization has no default value
// setOrElse e.body, { "" }
// setOrElse e.overwrite, { false }
// setOrElse e.allowUploadToExisting, { false }
// }
// }
}
//
// private <T> void setOrElse(Property<T> prop, Callable<T> value) {
// if (!prop.isPresent()) {
// prop.set project.provider(value)
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.github.breadmoirai.githubreleaseplugin

import com.github.breadmoirai.githubreleaseplugin.ast.ExtensionClass
import com.github.breadmoirai.githubreleaseplugin.exceptions.PropertyNotSetException
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection
Expand Down Expand Up @@ -83,30 +82,20 @@ class GithubReleaseTask extends DefaultTask {

@TaskAction
void publishRelease() {
final CharSequence authValue = authorization.getOrThrow()
final CharSequence authValue = authorization.get()
final GithubApi api = new GithubApi(authValue)
final CharSequence ownerValue = owner.getOrDefault {
try {
return project.group.toString().substring(group.lastIndexOf('.') + 1)
} catch(Exception ignored) {
throw new PropertyNotSetException("owner")
}
}
final CharSequence repoValue = repo.getOrDefault {
def repo = project.name ?: project.rootProject?.name ?: project.rootProject?.rootProject?.name
if (repo == null) throw new PropertyNotSetException("repo")
return repo
}
final CharSequence tagValue = tagName.getOrDefault { "v${project.version}" }
final CharSequence ownerValue = owner.get()
final CharSequence repoValue = repo.get()
final CharSequence tagValue = tagName.get()

def previousRelease = api.findReleaseByTag ownerValue, repoValue, tagValue
switch (previousRelease.code) {
case 200:
println ":githubRelease EXISTING RELEASE FOUND ${previousRelease.body.html_url}"
if (this.overwrite.getOrElse(false)) {
if (this.overwrite.get()) {
deleteRelease(api, previousRelease)
createRelease(api, ownerValue, repoValue, tagValue)
} else if (this.allowUploadToExisting.getOrElse(false) && (releaseAssets.size() > 0)) {
} else if (this.allowUploadToExisting.get() && (releaseAssets.size() > 0)) {
println ':githubRelease UPLOADING ASSETS TO EXISTING RELEASE'
uploadAssetsToUrl api, previousRelease.body.upload_url as String
} else {
Expand Down Expand Up @@ -136,11 +125,11 @@ class GithubReleaseTask extends DefaultTask {
private void createRelease(GithubApi api, CharSequence ownerValue, CharSequence repoValue, CharSequence tagValue) {
def response = api.postRelease ownerValue.toString(), repoValue.toString(), [
tag_name : tagValue,
target_commitish: targetCommitish.getOrElse('master'),
name : releaseName.getOrElse(tagValue),
body : body.getOrElse(""),
draft : draft.getOrElse(false),
prerelease : prerelease.getOrElse(false)
target_commitish: targetCommitish.get(),
name : releaseName.get(),
body : body.get(),
draft : draft.get(),
prerelease : prerelease.get()
]
if (response.code != 201) {
if (response.code == 404) {
Expand Down

0 comments on commit 141eb79

Please sign in to comment.