Skip to content

Commit

Permalink
Fixes #8 Fix Upload Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverspryn committed Oct 3, 2019
1 parent 3836e94 commit 45e4749
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.50"
}

group "com.oliverspryn.gradle"
version "2.0.0"
group 'com.oliverspryn.gradle'
version '2.1.0'

gradlePlugin {
plugins {
Expand All @@ -21,6 +21,7 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
maven { url 'https://jitpack.io' }
jcenter()
}

Expand All @@ -31,8 +32,8 @@ dependencies {
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50"

// jSlack
implementation "com.github.seratch:jslack:2.2.3"
// Slack API
implementation "com.github.allbegray:slack-api:1.7.0.RELEASE"
}

uploadArchives {
Expand Down
3 changes: 2 additions & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
buildscript {
repositories {
maven {
maven { url 'https://jitpack.io' }
url uri("../repo")
}
}

dependencies {
classpath "com.oliverspryn.gradle:slack-uploader:2.0.0"
classpath "com.oliverspryn.gradle:slackuploader:2.0.0"
}
}

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pluginManagement {
}
}

rootProject.name = 'slack-uploader'
rootProject.name = 'slackuploader'
7 changes: 2 additions & 5 deletions src/main/kotlin/com/oliverspryn/gradle/SlackUploaderPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import org.gradle.api.Project

class SlackUploaderPlugin : Plugin<Project> {

@Suppress("UnstableApiUsage") // create() is incubating
override fun apply(project: Project) {
val extension = project.extensions.create("uploadFileToSlack", SlackUploaderExtension::class.java)

project.tasks.create("uploadFileToSlack", SlackUploaderTask::class.java) { task ->
task.extension = extension
task.projectRoot = project.projectDir.absolutePath
}
project.tasks.create("uploadFileToSlack", SlackUploaderTask::class.java, extension)
}
}
62 changes: 27 additions & 35 deletions src/main/kotlin/com/oliverspryn/gradle/SlackUploaderTask.kt
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
package com.oliverspryn.gradle

import com.github.seratch.jslack.Slack
import com.github.seratch.jslack.api.methods.request.files.FilesUploadRequest
import allbegray.slack.webapi.SlackWebApiClientImpl
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
import javax.inject.Inject

open class SlackUploaderTask : DefaultTask() {

// region Configuration

var extension: SlackUploaderExtension? = null
var projectRoot: String? = null

// endregion
open class SlackUploaderTask @Inject constructor(
private val extension: SlackUploaderExtension
) : DefaultTask() {

@TaskAction
fun doUpload() {

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)
if (!extension.enabled) return

val channels = extension.channels?.toMutableList() ?: throw MissingChannelException()
val filePath = extension.filePath ?: throw MissingFilePathException()
val token = extension.token ?: throw MissingTokenException()

val file = Paths.get(project.rootDir.absolutePath, filePath).toFile()
val slack = SlackWebApiClientImpl(token)

try {
channels.forEach {
slack.uploadFile(
file,
file.extension,
file.name,
file.name,
extension.comment ?: "",
it
)
}
} catch (e: Exception) {
throw FileUploadException(e.localizedMessage)
}
}

private fun validateConfiguration() {
if (extension?.channels.isNullOrEmpty()) throw MissingChannelException()
if (extension?.filePath.isNullOrBlank()) throw MissingFilePathException()
if (extension?.token.isNullOrBlank()) throw MissingTokenException()
}
}

0 comments on commit 45e4749

Please sign in to comment.