Skip to content

Commit

Permalink
Fixes #3 Upload File to Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverspryn authored Mar 28, 2019
1 parent 7e1e672 commit e68b402
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
7 changes: 5 additions & 2 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ buildscript {
url uri('../repo')
}
}

dependencies {
classpath "com.oliverspryn.gradle:gradle-slack-uploader-plugin:1.0.0"
}
Expand All @@ -20,7 +21,9 @@ repositories {
}

uploadFileToSlack {
comment "sample.txt"
channels "general", "random"
comment "Hello from Gradle!"
channels "gradle-slack"
enabled true
filePath "sample.txt"
token GRADLE_SLACK_UPLOADER_PLUGIN_TOKEN ?: "" // Defined in the global gradle.properties file
}
1 change: 1 addition & 0 deletions demo/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This came from Gradle!
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.oliverspryn.gradle

open class SlackUploaderExtension {

var channels: List<String>? = null
var comment: String? = null
var enabled = true
var filePath: String? = null
var token: String? = null

fun channels(vararg channels: String) {
this.channels = channels.toList()
Expand Down
35 changes: 31 additions & 4 deletions src/main/kotlin/com/oliverspryn/gradle/SlackUploaderTask.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.oliverspryn.gradle

import com.github.seratch.jslack.Slack
import com.github.seratch.jslack.api.methods.request.files.FilesUploadRequest
import com.oliverspryn.gradle.exceptions.FileUploadException
import com.oliverspryn.gradle.exceptions.MissingChannelException
import com.oliverspryn.gradle.exceptions.MissingFilePathException
import com.oliverspryn.gradle.exceptions.MissingTokenException
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.nio.file.Paths
Expand All @@ -15,10 +21,31 @@ open class SlackUploaderTask : DefaultTask() {

@TaskAction
fun doUpload() {
val fullPath = Paths.get(projectRoot, extension?.filePath).toAbsolutePath()

println("Comment: ${extension?.comment}")
println("Channels: ${extension?.channels?.joinToString()}")
println("File to Upload: $fullPath")
if (extension?.enabled != true) return
validateConfiguration()

val file = Paths.get(projectRoot, extension?.filePath).toFile()
val slack = Slack.getInstance()

val uploadRequest = FilesUploadRequest.builder()
.channels(extension?.channels)
.file(file)
.filename("${file.name}.${file.extension}")
.initialComment(extension?.comment ?: "")
.token(extension?.token)
.build()

val result = slack.methods().filesUpload(uploadRequest)

if (!result.isOk) {
throw FileUploadException(result.error)
}
}

private fun validateConfiguration() {
if (extension?.channels.isNullOrEmpty()) throw MissingChannelException()
if (extension?.filePath.isNullOrBlank()) throw MissingFilePathException()
if (extension?.token.isNullOrBlank()) throw MissingTokenException()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.oliverspryn.gradle.exceptions

class FileUploadException(reason: String) : Throwable(reason)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.oliverspryn.gradle.exceptions

class MissingChannelException : Throwable("Please specify at least one channel to send the file")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.oliverspryn.gradle.exceptions

class MissingFilePathException : Throwable("Please specify a file to upload")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.oliverspryn.gradle.exceptions

class MissingTokenException : Throwable("Missing the Slack access token")

0 comments on commit e68b402

Please sign in to comment.