From 96b7cfc3e25d8fdd593cc61c898c904a725cc062 Mon Sep 17 00:00:00 2001 From: Tyler Bertrand Date: Mon, 24 Apr 2023 15:47:47 -0500 Subject: [PATCH] Use String over CharSequence Interacting with CharSequence can result in some syntactical overhead compared to interacting with Strings. As an example, when using the tagName from the githubRelease extension, passing that value to another API requires converting that value to a String. This tends to add some amount of noise to the code as most APIs use Strings for such things. --- .../ChangeLogSupplier.groovy | 60 +++++++++---------- .../githubreleaseplugin/GithubApi.groovy | 14 ++--- .../GithubReleaseExtension.groovy | 47 +++++++-------- .../GithubReleaseTask.groovy | 42 ++++++------- 4 files changed, 81 insertions(+), 82 deletions(-) diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy index dc3164a..3745d78 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/ChangeLogSupplier.groovy @@ -35,23 +35,23 @@ class ChangeLogSupplier implements Callable { private final Project project - private final Provider owner - private final Provider repo - private final Provider authorization - private final Provider tag + private final Provider owner + private final Provider repo + private final Provider authorization + private final Provider tag private final Provider dryRun - final Property executable - final Property currentCommit - final Property lastCommit - final ListProperty options + final Property executable + final Property currentCommit + final Property lastCommit + final ListProperty options private final OkHttpClient client = GithubApi.client ChangeLogSupplier(Project project, - Provider ownerProvider, - Provider repoProvider, - Provider authorizationProvider, - Provider tagProvider, + Provider ownerProvider, + Provider repoProvider, + Provider authorizationProvider, + Provider tagProvider, Provider dryRunProvider) { this.project = project def objects = project.objects @@ -60,10 +60,10 @@ class ChangeLogSupplier implements Callable { this.authorization = authorizationProvider this.tag = tagProvider this.dryRun = dryRunProvider - this.executable = objects.property(CharSequence) - this.currentCommit = objects.property(CharSequence) - this.lastCommit = objects.property(CharSequence) - this.options = objects.listProperty(CharSequence) + this.executable = objects.property(String) + this.currentCommit = objects.property(String) + this.lastCommit = objects.property(String) + this.options = objects.listProperty(String) setExecutable 'git' setCurrentCommit "HEAD" setLastCommit { this.getLastReleaseCommit() } @@ -74,21 +74,21 @@ class ChangeLogSupplier implements Callable { * Looks for the previous release's targetCommitish * @return */ - private CharSequence getLastReleaseCommit() { + private String getLastReleaseCommit() { log 'Searching for previous release on Github' - CharSequence owner = this.owner.getOrNull() + String owner = this.owner.getOrNull() if (owner == null) { throw new PropertyNotSetException("owner") } - CharSequence repo = this.repo.getOrNull() + String repo = this.repo.getOrNull() if (repo == null) { throw new PropertyNotSetException("repo") } - CharSequence auth = authorization.getOrNull() + String auth = authorization.getOrNull() if (auth == null) { throw new PropertyNotSetException("auth") } - CharSequence tag = this.tag.getOrNull() + String tag = this.tag.getOrNull() if (tag == null) { throw new PropertyNotSetException("tag") } @@ -109,13 +109,13 @@ class ChangeLogSupplier implements Callable { log "$i : ${releases[i].tag_name}" } if (releases.isEmpty() || (releases.size() == 1 && index == 0) || index + 1 == releases.size()) { - CharSequence exe = this.executable.getOrNull() + String exe = this.executable.getOrNull() if (exe == null) { throw new PropertyNotSetException("exe") } log "Previous release not found" log "Searching for earliest commit" - List cmd = [exe, "rev-list", "--max-parents=0", "--max-count=1", "HEAD"]*.toString() + List cmd = [exe, "rev-list", "--max-parents=0", "--max-count=1", "HEAD"] log "Running `${cmd.join(' ')}`" def result = new ProcessExecutor() .command(cmd) @@ -145,10 +145,10 @@ class ChangeLogSupplier implements Callable { @Memoized String call() { log 'Creating...' - CharSequence current = currentCommit.get() - CharSequence last = lastCommit.get() - List opts = options.get()*.toString() - CharSequence get = executable.getOrNull() + String current = currentCommit.get() + String last = lastCommit.get() + List opts = options.get() + String get = executable.getOrNull() if (get == null) { throw new PropertyNotSetException('get') } @@ -185,7 +185,7 @@ class ChangeLogSupplier implements Callable { this.options.set(options.toList()) } - public void setOptions(Iterable options) { + public void setOptions(Iterable options) { this.options.set options } @@ -193,11 +193,11 @@ class ChangeLogSupplier implements Callable { this.options.set(options.toList()) } - public void options(Iterable options) { + public void options(Iterable options) { this.options.set options } - public void addOption(CharSequence option) { + public void addOption(String option) { this.options.add(option) } diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy index 6e2af22..f167d01 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubApi.groovy @@ -32,9 +32,9 @@ class GithubApi { private final Map defaultHeaders private final Tika tika = new Tika() - GithubApi(CharSequence authorization) { + GithubApi(String authorization) { this.defaultHeaders = [ - 'Authorization': authorization.toString(), + 'Authorization': authorization, 'User-Agent' : 'breadmoirai github-release-gradle-plugin', 'Accept' : 'application/vnd.github.v3+json', 'Content-Type' : 'application/json' @@ -72,14 +72,14 @@ class GithubApi { return r } - Response findReleaseByTag(CharSequence owner, CharSequence repo, CharSequence tagName) { + Response findReleaseByTag(String owner, String repo, String tagName) { String releaseUrl = "$endpoint/repos/$owner/$repo/releases/tags/$tagName" connect(releaseUrl) { get() } } - Response findTagByName(CharSequence owner, CharSequence repo, CharSequence tagName) { + Response findTagByName(String owner, String repo, String tagName) { String tagUrl = "$endpoint/repos/$owner/$repo/git/refs/tags/$tagName" connect(tagUrl) { get() @@ -98,7 +98,7 @@ class GithubApi { } } - Response postRelease(CharSequence owner, CharSequence repo, Map data) { + Response postRelease(String owner, String repo, Map data) { String releaseUrl = "$endpoint/repos/$owner/$repo/releases" connect(releaseUrl) { post RequestBody.create(JsonOutput.toJson(data), MEDIATYPE_JSON) @@ -111,14 +111,14 @@ class GithubApi { } } - Response getReleases(CharSequence owner, CharSequence repo) { + Response getReleases(String owner, String repo) { String releaseUrl = "$endpoint/repos/$owner/$repo/releases" connect(releaseUrl) { get() } } - Response getCommits(CharSequence owner, CharSequence repo) { + Response getCommits(String owner, String repo) { String commitsUrl = "$endpoint/repo/$owner/$repo/commits" println ':githubRelease RETRIEVING COMMITS ' + commitsUrl connect(commitsUrl) { diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy index 8a7b26b..4a6e12e 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseExtension.groovy @@ -96,20 +96,20 @@ import java.util.concurrent.Callable class GithubReleaseExtension { - final Property owner - final Property repo - final Property authorization - final Property tagName - final Property targetCommitish - final Property releaseName + final Property owner + final Property repo + final Property authorization + final Property tagName + final Property targetCommitish + final Property releaseName final Property generateReleaseNotes - final Property body + final Property body final Property draft final Property prerelease final Property overwrite final Property allowUploadToExisting final Property dryRun - final Property apiEndpoint + final Property apiEndpoint final ConfigurableFileCollection releaseAssets @@ -122,25 +122,24 @@ class GithubReleaseExtension { GithubReleaseExtension(Project project) { this.project = project final ObjectFactory objectFactory = project.objects - owner = objectFactory.property(CharSequence) - repo = objectFactory.property(CharSequence) - authorization = objectFactory.property(CharSequence) - tagName = objectFactory.property(CharSequence) - targetCommitish = objectFactory.property(CharSequence) - releaseName = objectFactory.property(CharSequence) + owner = objectFactory.property(String) + repo = objectFactory.property(String) + authorization = objectFactory.property(String) + tagName = objectFactory.property(String) + targetCommitish = objectFactory.property(String) + releaseName = objectFactory.property(String) generateReleaseNotes = objectFactory.property(Boolean) - body = objectFactory.property(CharSequence) + body = objectFactory.property(String) draft = objectFactory.property(Boolean) prerelease = objectFactory.property(Boolean) releaseAssets = project.files() overwrite = objectFactory.property(Boolean) allowUploadToExisting = objectFactory.property(Boolean) dryRun = objectFactory.property(Boolean) - apiEndpoint = objectFactory.property(CharSequence) + apiEndpoint = objectFactory.property(String) owner { - def group = project.group.toString() - return group.substring(group.lastIndexOf('.') + 1) + return project.group.substring(project.group.lastIndexOf('.') + 1) } repo { project.name ?: project.rootProject?.name ?: project.rootProject?.rootProject?.name @@ -181,27 +180,27 @@ class GithubReleaseExtension { this.releaseAssets.setFrom(assets) } - void setToken(CharSequence token) { + void setToken(String token) { this.authorization.set("Token $token") } - void token(CharSequence token) { + void token(String token) { this.authorization.set("Token $token") } - void setToken(Provider token) { + void setToken(Provider token) { this.authorization.set(token.map { "Token $it" }) } - void token(Provider token) { + void token(Provider token) { this.authorization.set(token.map { "Token $it" }) } - void setToken(Callable token) { + void setToken(Callable token) { this.authorization.set(project.provider(token).map { "Token $it" }) } - void token(Callable token) { + void token(Callable token) { this.authorization.set(project.provider(token).map { "Token $it" }) } diff --git a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy index 584a07d..1f16c02 100644 --- a/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy +++ b/src/main/groovy/com/github/breadmoirai/githubreleaseplugin/GithubReleaseTask.groovy @@ -33,21 +33,21 @@ import java.nio.charset.StandardCharsets class GithubReleaseTask extends DefaultTask { @Input - final Property owner + final Property owner @Input - final Property repo + final Property repo @Input - final Property authorization + final Property authorization @Input - final Property tagName + final Property tagName @Input - final Property targetCommitish + final Property targetCommitish @Input - final Property releaseName + final Property releaseName @Input final Property generateReleaseNotes @Input - final Property body + final Property body @Input final Property draft @Input @@ -62,26 +62,26 @@ class GithubReleaseTask extends DefaultTask { @Input final Property dryRun @Input - final Property apiEndpoint + final Property apiEndpoint GithubReleaseTask() { this.setGroup('publishing') final ObjectFactory objectFactory = project.objects - owner = objectFactory.property(CharSequence) - repo = objectFactory.property(CharSequence) - authorization = objectFactory.property(CharSequence) - tagName = objectFactory.property(CharSequence) - targetCommitish = objectFactory.property(CharSequence) - releaseName = objectFactory.property(CharSequence) + owner = objectFactory.property(String) + repo = objectFactory.property(String) + authorization = objectFactory.property(String) + tagName = objectFactory.property(String) + targetCommitish = objectFactory.property(String) + releaseName = objectFactory.property(String) generateReleaseNotes = objectFactory.property(Boolean) - body = objectFactory.property(CharSequence) + body = objectFactory.property(String) draft = objectFactory.property(Boolean) prerelease = objectFactory.property(Boolean) releaseAssets = project.files() overwrite = objectFactory.property(Boolean) allowUploadToExisting = objectFactory.property(Boolean) dryRun = objectFactory.property(Boolean) - apiEndpoint = objectFactory.property(CharSequence) + apiEndpoint = objectFactory.property(String) } private void log(String message) { @@ -102,11 +102,11 @@ class GithubReleaseTask extends DefaultTask { log "This task is a dry run. All API calls that would modify the repo are disabled. API calls that access the repo information are not disabled. Use this to show what actions would be executed." } GithubApi.endpoint = apiEndpoint.get() - final CharSequence authValue = authorization.get() + final String authValue = authorization.get() final GithubApi api = new GithubApi(authValue) - final CharSequence ownerValue = owner.get() - final CharSequence repoValue = repo.get() - final CharSequence tagValue = tagName.get() + final String ownerValue = owner.get() + final String repoValue = repo.get() + final String tagValue = tagName.get() log 'CHECKING FOR PREVIOUS RELEASE' def previousRelease = api.findReleaseByTag ownerValue, repoValue, tagValue switch (previousRelease.code) { @@ -155,7 +155,7 @@ class GithubReleaseTask extends DefaultTask { } } - private void createRelease(GithubApi api, CharSequence ownerValue, CharSequence repoValue, CharSequence tagValue) { + private void createRelease(GithubApi api, String ownerValue, String repoValue, String tagValue) { def values = [ tag_name : tagValue,