-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3836e94
commit 45e4749
Showing
5 changed files
with
37 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ pluginManagement { | |
} | ||
} | ||
|
||
rootProject.name = 'slack-uploader' | ||
rootProject.name = 'slackuploader' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 27 additions & 35 deletions
62
src/main/kotlin/com/oliverspryn/gradle/SlackUploaderTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |