Skip to content

Commit

Permalink
feat: allow specifying name, label and contentType of releaseAssets
Browse files Browse the repository at this point in the history
refactor on the way.

Add new repo closure to semanticRelease extension.
Here both ghToken and all releaseAssets must be given. Mark old
changeLog.ghToken as deprecated

Add new UpdateGithubRelease task. The release is now updated using this
task instead of release.doLast
  • Loading branch information
tschulte committed Aug 13, 2016
1 parent 93d21f6 commit 8b5995e
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 159 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ task sourcesJar(type: Jar) {
from sourceSets.main.allSource
}
semanticRelease {
changeLog {
releaseAssets(jar, sourcesJar)
repo {
releaseAsset jar
// optionally set name, label and/or contentType
releaseAsset sourcesJar, name: "the-sources.jar", label: 'the sources jar', contentType: 'application/zip'
// or
// releaseAsset sourcesJar name "the-sources.jar" label "the sources jar" contentType "application/zip"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sourceSets {
group = 'de.gliderpilot.gradle.semantic-release'

semanticRelease {
changeLog.ghToken = project.ext.ghToken
repo.ghToken = project.ext.ghToken
}

task integTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.gliderpilot.gradle.semanticrelease

import groovy.transform.PackageScope
import org.gradle.api.Task
import org.gradle.api.tasks.bundling.AbstractArchiveTask

abstract class GitRepo {

private Map<File, Asset> releaseAssets = [:].withDefault { file -> new Asset(file) }

@PackageScope
Collection<Asset> getReleaseAssets() {
return releaseAssets.values()
}


Asset releaseAsset(Map<String, String> params = [:], AbstractArchiveTask task) {
params.builtBy = task
releaseAsset(params, task.outputs.files.singleFile)
}

Asset releaseAsset(Map<String, String> params = [:], File file) {
Asset asset = releaseAssets[file]
params.each { key, value ->
asset."$key" = value
}
asset
}

abstract String diffUrl(String previousTag, String currentTag)

abstract String commitUrl(String abbreviatedId)
}

class Asset {
final File file
Task builtBy
String name
String label
String contentType

Asset(File file) {
this.file = file
name = file.name
contentType = URLConnection.guessContentTypeFromName(name) ?: "application/octet-stream"
}

Asset name(String name) {
this.name = name
this
}

Asset label(String label) {
this.label = label
this
}

Asset contentType(String contentType) {
this.contentType = contentType
this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.gliderpilot.gradle.semanticrelease

import com.jcabi.github.Github
import com.jcabi.github.RtGithub
import groovy.transform.Memoized
import groovy.transform.PackageScope
import org.ajoberstar.grgit.Grgit

import java.util.regex.Matcher

class GithubRepo extends GitRepo {

private final Grgit grgit

private Github github

@PackageScope
Github getGithub() {
github
}

GithubRepo(Grgit grgit) {
this.grgit = grgit
}

void setGhToken(String token) {
if (token)
github = new RtGithub(token)
}

@PackageScope
@Memoized
String getMnemo() {
String repositoryUrl = grgit.remote.list().find { it.name == 'origin' }.url
Matcher matcher = repositoryUrl =~ /.*github.com[\/:]((?:.+?)\/(?:.+?))(?:\.git)/
if (!matcher)
return null
return matcher.group(1)
}

private String repositoryUrl(String suffix) {
if (!mnemo)
return null
return "https://github.com/${mnemo}/$suffix"
}

String diffUrl(String previousTag, String currentTag) {
if (!(previousTag && currentTag))
return null
repositoryUrl("compare/${previousTag}...${currentTag}")
}

String commitUrl(String abbreviatedId) {
if (!abbreviatedId)
return null
repositoryUrl("commit/${abbreviatedId}")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package de.gliderpilot.gradle.semanticrelease

import com.github.zafarkhaja.semver.Version
import com.jcabi.github.*
import com.jcabi.github.Github
import groovy.text.SimpleTemplateEngine
import groovy.text.Template
import groovy.transform.Memoized
Expand All @@ -29,8 +29,6 @@ import org.ajoberstar.grgit.Grgit
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging

import java.util.regex.Matcher

/**
* Created by tobias on 7/26/15.
*/
Expand All @@ -40,34 +38,31 @@ class SemanticReleaseChangeLogService {

private final TagStrategy tagStrategy
private final Grgit grgit
private final GitRepo repo

@PackageScope
Github github
private Closure<Iterable<File>> files
private Iterable<File> releaseAssets

SemanticReleaseChangeLogService(Grgit grgit, TagStrategy tagStrategy, Closure<Iterable<File>> files) {
SemanticReleaseChangeLogService(Grgit grgit, GitRepo repo, TagStrategy tagStrategy) {
this.grgit = grgit
this.repo = repo
this.tagStrategy = tagStrategy
this.files = files
releaseAssets = files()
}

@Deprecated
void setGhToken(String token) {
if (token)
github = new RtGithub(token)
logger.warn("semanticRelease.changeLog.ghToken is deprecated and will be removed in v2.0.0")
logger.warn("use semanticRelease.gitRepo.ghToken instead")
repo.ghToken = token
}

@Deprecated
Github getGithub() {
logger.warn("semanticRelease.changeLog.github is deprecated and will be removed in v2.0.0")
github
repo.github
}

@Deprecated
void setGithub(Github github) {
logger.warn("semanticRelease.changeLog.github is deprecated and will be removed in v2.0.0")
this.github = github
repo.github = github
}

/**
Expand Down Expand Up @@ -107,7 +102,7 @@ class SemanticReleaseChangeLogService {
Template template = new SimpleTemplateEngine().createTemplate(getClass().getResource('/CHANGELOG.md'))
template.make([
title : null,
versionUrl: versionUrl(previousTag, currentTag),
versionUrl: repo.diffUrl(previousTag, currentTag),
service : this,
version : version.version,
fix : byTypeGroupByComponent(commits, 'fix'),
Expand Down Expand Up @@ -175,7 +170,7 @@ class SemanticReleaseChangeLogService {

@PackageScope
def commitish = { Commit commit ->
String url = repositoryUrl("commit/${commit.abbreviatedId}")
String url = repo.commitUrl(commit.abbreviatedId)
url ? "[${commit.abbreviatedId}]($url)" : "${commit.abbreviatedId}"
}

Expand All @@ -186,35 +181,6 @@ class SemanticReleaseChangeLogService {
}.groupBy(component).sort { a, b -> a.key <=> b.key }
}

@PackageScope
@Memoized
String mnemo() {
String repositoryUrl = grgit.remote.list().find { it.name == 'origin' }.url
Matcher matcher = repositoryUrl =~ /.*github.com[\/:]((?:.+?)\/(?:.+?))(?:\.git)/
if (!matcher)
return null
return matcher.group(1)
}

@PackageScope
Closure<String> repositoryUrl = { String suffix ->
String mnemo = mnemo()
if (!mnemo)
return null
return "https://github.com/${mnemo}/$suffix"
}

@PackageScope
Closure<String> versionUrl = { String previousTag, String currentTag ->
if (!(previousTag && currentTag))
return null
repositoryUrl("compare/${previousTag}...${currentTag}")
}

void releaseAssets(Object... assets) {
releaseAssets += files(assets)
}

@PackageScope
@Memoized
List<Commit> commits(Version previousVersion) {
Expand All @@ -228,36 +194,4 @@ class SemanticReleaseChangeLogService {
}
}

@PackageScope
void createGitHubVersion(ReleaseVersion version) {
String mnemo = mnemo()
if (!mnemo)
return
if (!github)
return
String tag = tagStrategy.toTagString(version.version)

Repo repo = github.repos().get(new Coordinates.Simple(mnemo))

// check for the existance of the tag using the api -> #3
long start = System.currentTimeMillis()
while (!tagExists(repo, tag) && System.currentTimeMillis() - start < 60000) {
}

Release release = repo.releases().create(tag)
new Release.Smart(release).body(changeLog(commits(Version.valueOf(version.previousVersion)), version).toString())
releaseAssets.each {
release.assets().upload(it.bytes, URLConnection.guessContentTypeFromName(it.name) ?: "application/octet-stream", it.name)
}
}

private boolean tagExists(Repo repo, String tag) {
try {
repo.git().references().get("refs/tags/$tag").json()
return true
} catch (Throwable t) {
return false
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ class SemanticReleasePlugin implements Plugin<Project> {
SemanticReleasePluginExtension semanticReleaseExtension = extensions.create("semanticRelease", SemanticReleasePluginExtension, project)
ReleasePluginExtension releaseExtension = extensions.findByType(ReleasePluginExtension)
def releaseTask = tasks.release
releaseTask.doLast {
if (project.version.inferredVersion.createTag) {
semanticReleaseExtension.changeLog.createGitHubVersion(project.version.inferredVersion)
}
}
tasks.create("updateGithubRelease", UpdateGithubRelease)
releaseTask.finalizedBy project.tasks.updateGithubRelease
releaseExtension.with {
versionStrategy semanticReleaseExtension.releaseStrategy
defaultVersionStrategy = semanticReleaseExtension.snapshotStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import javax.inject.Inject
class SemanticReleasePluginExtension {

final Project project
final GitRepo repo
final SemanticReleaseChangeLogService changeLog
final SemanticReleaseCheckBranch releaseBranches
final SemanticReleaseAppendBranchNameStrategy branchNames
Expand All @@ -39,12 +40,8 @@ class SemanticReleasePluginExtension {
@Inject
SemanticReleasePluginExtension(Project project) {
this.project = project
def files = { Object[] args ->
if (args)
project.tasks.release.dependsOn args
project.files(args)
}
changeLog = new SemanticReleaseChangeLogService(project.grgit, project.release.tagStrategy, files)
this.repo = new GithubRepo(project.grgit)
changeLog = new SemanticReleaseChangeLogService(project.grgit, repo, project.release.tagStrategy)
releaseBranches = new SemanticReleaseCheckBranch()
branchNames = new SemanticReleaseAppendBranchNameStrategy(releaseBranches)
semanticStrategy = new SemanticReleaseNormalStrategy(project.grgit, changeLog)
Expand All @@ -70,6 +67,10 @@ class SemanticReleasePluginExtension {
ConfigureUtil.configure(closure, changeLog)
}

def repo(Closure closure) {
ConfigureUtil.configure(closure, repo)
}

def releaseBranches(Closure closure) {
ConfigureUtil.configure(closure, releaseBranches)
}
Expand Down
Loading

0 comments on commit 8b5995e

Please sign in to comment.