diff --git a/build.gradle b/build.gradle index 9302b4d..c1ae66a 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ plugins { } group = 'com.github.breadmoirai' -version = '2.3.0' +version = '2.2.5' repositories { jcenter() diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy index 7d25a82..8e722aa 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy @@ -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 @@ -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 { - private static final Logger log = LoggerFactory.getLogger(ChangeLogSupplier.class) - private final Project project private final Provider owner @@ -98,7 +94,7 @@ class ChangeLogSupplier implements Callable { 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") @@ -113,7 +109,7 @@ class ChangeLogSupplier implements Callable { .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" @@ -134,7 +130,7 @@ class ChangeLogSupplier implements Callable { @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 opts = options.get()*.toString() @@ -155,9 +151,12 @@ class ChangeLogSupplier implements Callable { 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 currentCommit) { this.currentCommit.set(currentCommit) } @@ -254,7 +253,6 @@ class ChangeLogSupplier implements Callable { setExecutable { gitExecutable } } - @Deprecated static Request.Builder createRequestWithHeaders(CharSequence authorization) { return new Request.Builder() .addHeader('Authorization', authorization.toString()) @@ -262,4 +260,9 @@ class ChangeLogSupplier implements Callable { .addHeader('Accept', 'application/vnd.github.v3+json') .addHeader('Content-Type', 'application/json') } + + @Override + public String toString() { + return call() + } } \ No newline at end of file diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy index f011d52..6e63985 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy @@ -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" } diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy index 605097e..31da90e 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy @@ -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 @@ -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 changelog(@DelegatesTo(ChangeLogSupplier) final Closure closure) { @@ -143,7 +160,6 @@ class GithubReleaseExtension { this.releaseAssets.setFrom(assets) } - void setToken(CharSequence token) { this.authorization.set("Token $token") } diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleasePlugin.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleasePlugin.groovy index 03f6126..696b0c3 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleasePlugin.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleasePlugin.groovy @@ -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 { 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 @@ -82,39 +53,6 @@ class GithubReleasePlugin implements Plugin { 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 void setOrElse(Property prop, Callable value) { -// if (!prop.isPresent()) { -// prop.set project.provider(value) -// } -// } } diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy index c4266b0..a9e5c2f 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy @@ -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 @@ -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 { @@ -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) {