Skip to content

Commit

Permalink
feat: allow to define releaseAssets
Browse files Browse the repository at this point in the history
Now you can add files to be released to github using the extension:

```groovy
apply plugin: 'java'
task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}
semanticRelease {
    changeLog {
        releaseAssets(jar, sourcesJar)
    }
}
```


Closes #13
  • Loading branch information
tschulte committed Aug 11, 2016
1 parent 126b8f1 commit 52a5e01
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ semanticRelease {
The **ghToken** is mandatory.

```groovy
apply plugin: 'java'
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
semanticRelease {
changeLog {
releaseAsset = { ReleaseAssets assets, String currentTag ->
assets.upload(file("build/libs/value-${currentTag}.jar").bytes, "application/zip", "run-${currentTag}.jar")
}
releaseAssets(jar, sourcesJar)
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ class SemanticReleaseChangeLogService {

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

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

void setGhToken(String token) {
Expand Down Expand Up @@ -207,9 +211,8 @@ class SemanticReleaseChangeLogService {
repositoryUrl("compare/${previousTag}...${currentTag}")
}

@PackageScope
def releaseAsset = { ReleaseAssets assets, String currentTag ->
// waiting the custom implementation
void releaseAssets(Object... assets) {
releaseAssets += files(assets)
}

@PackageScope
Expand Down Expand Up @@ -243,7 +246,9 @@ class SemanticReleaseChangeLogService {

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

private boolean tagExists(Repo repo, String tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SemanticReleasePluginExtension {
@Inject
SemanticReleasePluginExtension(Project project) {
this.project = project
changeLog = new SemanticReleaseChangeLogService(project.grgit, project.release.tagStrategy)
changeLog = new SemanticReleaseChangeLogService(project.grgit, project.release.tagStrategy, project.&files)
releaseBranches = new SemanticReleaseCheckBranch()
branchNames = new SemanticReleaseAppendBranchNameStrategy(releaseBranches)
semanticStrategy = new SemanticReleaseNormalStrategy(project.grgit, changeLog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ class SemanticReleaseChangeLogServiceSpec extends Specification {
Grgit grgit = Mock()
TagStrategy tagStrategy = new TagStrategy()

Closure<Iterable<File>> files = { it instanceof Object[] ? it as List : [] }

@Subject
SemanticReleaseChangeLogService changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy)
SemanticReleaseChangeLogService changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy, files)

def "does not throw an exception if no ticket is referenced"() {
given:
Expand Down Expand Up @@ -179,7 +181,7 @@ class SemanticReleaseChangeLogServiceSpec extends Specification {
def "changeLog is generated"() {
given:
grgit = Grgit.open()
changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy)
changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy, files)
String mnemo = changeLogService.mnemo()

when:
Expand Down Expand Up @@ -220,7 +222,9 @@ class SemanticReleaseChangeLogServiceSpec extends Specification {
def "change log is uploaded to GitHub"() {
given:
grgit = Grgit.open()
changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy)
changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy, files)
File asset = new File('settings.gradle')
changeLogService.releaseAssets(asset)
String mnemo = changeLogService.mnemo()
String user = mnemo.substring(0, mnemo.indexOf("/"))
String repo = mnemo.substring(mnemo.indexOf("/") + 1)
Expand All @@ -241,12 +245,13 @@ class SemanticReleaseChangeLogServiceSpec extends Specification {

then:
release?.body() == 'changelog'
release?.assets().iterate().any { it.json().getString("name") == asset.name }
}

def "change log is not uploaded to GitHub when no gh token is set"() {
given:
grgit = Grgit.open()
changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy)
changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy, files)

when:
changeLogService.createGitHubVersion(new ReleaseVersion(previousVersion: '1.0.0', version: '1.0.1'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SemanticReleaseNormalStrategySpec extends Specification {

Grgit grgit = Mock()
TagStrategy tagStrategy = new TagStrategy()
SemanticReleaseChangeLogService changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy)
SemanticReleaseChangeLogService changeLogService = new SemanticReleaseChangeLogService(grgit, tagStrategy, { [] })

@Subject
SemanticReleaseNormalStrategy strategy = new SemanticReleaseNormalStrategy(grgit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,19 @@ class SemanticReleasePluginSpec extends ProjectSpec {
project.tasks.release.finalizedBy.getDependencies(project.tasks.release).contains(project.tasks.uploadArchives)
}

def "can define releaseAssets"() {
when:
project.with {
apply plugin: PLUGIN
apply plugin: 'java'
semanticRelease {
changeLog {
releaseAssets jar
}
}
}

then:
project.semanticRelease.changeLog.releaseAssets.toList().containsAll(project.jar.outputs.files.files)
}
}

0 comments on commit 52a5e01

Please sign in to comment.