Skip to content

Commit

Permalink
slack message optimization, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
warlordofmars committed Feb 11, 2019
1 parent 816e425 commit 6874c8f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@ import org.gradle.api.tasks.TaskState
class SlackAttachmentCreator {
private static final String COLOR_PASSED = 'good'
private static final String COLOR_FAILED = 'danger'
private static final String PROJECT_TITLE = 'Project'
private static final String STATUS_TITLE = 'Status'
private static final String TASK_TITLE = 'Task'
private static final String STARTING_TASKS_TITLE = 'Starting Tasks'
private static final String FAILURE_TITLE = 'Failure'
private static final String DESCRIPTION_TITLE = 'Description'
private static final String TASK_RESULT_TITLE = 'Task Result'
private static final String TASK_RESULT_PASSED = 'Passed'
private static final String TASK_RESULT_FAILED = 'Failed'
private static final String BRANCH_TITLE = 'Branch'
private static final String AUTHOR_TITLE = 'Author'
private static final String COMMIT_TITLE = 'Commit'
private static final String TASKS_TITLE = 'Tasks'
private static final String TESTS_TITLE = 'Tests'
private static final String RUNTIME_TITLE = 'Runtime'
private static final String RUNNING_STATUS = 'Running :woman-running:'
private static final String COMPLETED_STATUS = 'Completed :white_check_mark:'
private static final String FAILURE_STATUS = 'Failure :fire:'
private static final String TASK_TITLE = 'task'
private static final String FAILURE_TITLE = 'failure reason'
private static final String DESCRIPTION_TITLE = 'description'
private static final String BRANCH_TITLE = 'branch'
private static final String AUTHOR_TITLE = 'author'
private static final String COMMIT_TITLE = 'commit'
private static final String TASKS_TITLE = 'tasks'
private static final String TESTS_TITLE = 'tests'
private static final String RUNTIME_TITLE = 'runtime'
private static final String RUNNING_STATUS = 'running :woman-running:'
private static final String COMPLETED_STATUS = 'completed :white_check_mark:'
private static final String FAILURE_STATUS = 'failed :fire:'

static Attachment buildSlackAttachment(Task task, TaskState state, String taskLog) {

Expand All @@ -37,37 +31,13 @@ class SlackAttachmentCreator {
def rootProject = task.getProject().rootProject
def gradle = rootProject.getGradle()
def completedTasks = rootProject.completedTasks.size()
def startingTasks = gradle.startParameter.taskNames.join(", ")
def startingTasks = gradle.startParameter.taskNames.collect { "`${it}`" }.join(", ")
def totalTasks = gradle.getTaskGraph().getAllTasks().size()

def completed = completedTasks == totalTasks

def fields = []

Field projectField = Field.builder()
.title(PROJECT_TITLE)
.value("${task.getProject().rootProject.name}")
.valueShortEnough(true)
.build()

fields << projectField

Field startingTasksField = Field.builder()
.title(STARTING_TASKS_TITLE)
.value("${startingTasks}")
.valueShortEnough(true)
.build()

fields << startingTasksField

Field statusField = Field.builder()
.title(STATUS_TITLE)
.value(failure ? FAILURE_STATUS : completed ? COMPLETED_STATUS : RUNNING_STATUS)
.valueShortEnough(true)
.build()

fields << statusField

if(rootProject.ext.has('getRuntime')) {
def runtime = rootProject.getRuntime()

Expand All @@ -92,7 +62,7 @@ class SlackAttachmentCreator {

Field taskField = Field.builder()
.title(TASK_TITLE)
.value(":${task.getProject().name}:${task.getName()}")
.value("${task.getProject().name} : ${task.getName()}")
.valueShortEnough(true)
.build()

Expand All @@ -111,22 +81,44 @@ class SlackAttachmentCreator {
}

}

if(rootProject.ext.has('getTestCounts')) {
def testCounts = rootProject.getTestCounts()

Field testsField = Field.builder()
.title(TESTS_TITLE)
.value("${testCounts['total']} total / ${testCounts['success']} successful / ${testCounts['failed']} failed / ${testCounts['skipped']} skipped")
.valueShortEnough(false)
.build()

fields << testsField
}

if(rootProject.hasProperty('git')) {

Field branchField = Field.builder()
.title(BRANCH_TITLE)
.value(rootProject.git.branch.current.name)
.value()
.valueShortEnough(true)
.build()

def branch = rootProject.git.branch.current.name

if(rootProject.group.startsWith('com.github') && rootProject.group.split('\\.').size() > 2) {
def organization = rootProject.group.split('\\.')[2]
def gitHubUrl = "https://github.com/${organization}/${rootProject.name}/tree/${branch}"
branchField.setValue("<${gitHubUrl}|${branch}>")
} else {
branchField.setValue(branch)
}

fields << branchField

def lastCommmit = rootProject.git.head()

Field authorField = Field.builder()
.title(AUTHOR_TITLE)
.value(lastCommmit.author.email)
.value("<mailto:${lastCommmit.author.email}|${lastCommmit.author.name}>")
.valueShortEnough(true)
.build()

Expand All @@ -141,18 +133,6 @@ class SlackAttachmentCreator {
fields << commitField
}

if(rootProject.ext.has('getTestCounts')) {
def testCounts = rootProject.getTestCounts()

Field testsField = Field.builder()
.title(TESTS_TITLE)
.value("${testCounts['total']} total / ${testCounts['success']} successful / ${testCounts['failed']} failed / ${testCounts['skipped']} skipped")
.valueShortEnough(false)
.build()

fields << testsField
}

if (!success && failure != null && failure.getCause() != null) {

Field failureField = Field.builder()
Expand All @@ -165,13 +145,35 @@ class SlackAttachmentCreator {

}

String status = failure ? FAILURE_STATUS : completed ? COMPLETED_STATUS : RUNNING_STATUS

Attachment attachment = Attachment.builder()
.fields(fields)
.color(success ? COLOR_PASSED : COLOR_FAILED)
.text("started with tasks ${startingTasks} at _${rootProject.buildStartTime}_")
.build()

// String messageHeader = "Build " + ( success ? TASK_RESULT_PASSED : TASK_RESULT_FAILED)
// attachments.setFallback(messageHeader)
if(System.env.containsKey('BUILD_NUMBER')) {
if(rootProject.hasProperty('git')) {
attachment.title = "${task.getProject().rootProject.name} build #${System.env.BUILD_NUMBER} on ${rootProject.git.branch.current.name} ${status}"
attachment.footer = "${task.getProject().rootProject.name} ${rootProject.git.branch.current.name} #${System.env.BUILD_NUMBER}"
} else {
attachment.title = "${task.getProject().rootProject.name} build #${System.env.BUILD_NUMBER} ${status}"
attachment.footer = "${task.getProject().rootProject.name} #${System.env.BUILD_NUMBER}"
}
} else {
if(rootProject.hasProperty('git')) {
attachment.title = "${task.getProject().rootProject.name} build on ${rootProject.git.branch.current.name} ${status}"
attachment.footer = "${task.getProject().rootProject.name} ${rootProject.git.branch.current.name}"
} else {
attachment.title = "${task.getProject().rootProject.name} build ${status}"
attachment.footer = "${task.getProject().rootProject.name}"
}
}

if(System.env.containsKey('RUN_DISPLAY_URL')) {
attachment.titleLink = System.env.RUN_DISPLAY_URL
}

return attachment
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import com.github.seratch.jslack.api.model.Channel
import com.github.seratch.jslack.api.model.Attachment
import com.github.seratch.jslack.api.methods.response.chat.ChatUpdateResponse
import com.github.seratch.jslack.api.methods.response.chat.ChatPostMessageResponse
import com.github.seratch.jslack.api.methods.response.files.FilesUploadResponse
import com.github.seratch.jslack.api.methods.response.reactions.ReactionsAddResponse
import com.github.seratch.jslack.api.methods.request.chat.ChatPostMessageRequest
import com.github.seratch.jslack.api.methods.request.chat.ChatUpdateRequest
import com.github.seratch.jslack.api.methods.request.files.FilesUploadRequest
import com.github.seratch.jslack.api.methods.request.reactions.ReactionsAddRequest

import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -21,6 +25,9 @@ import org.gradle.api.tasks.TaskState

import org.ajoberstar.grgit.Grgit

import java.util.Date
import groovy.time.TimeCategory


class SlackPlugin implements Plugin<Project> {

Expand All @@ -35,11 +42,12 @@ class SlackPlugin implements Plugin<Project> {

project.getRootProject().ext {
completedTasks = []
startTime = System.nanoTime()
buildStartTime = new Date()
getRuntime = {
def diff = System.nanoTime() - startTime
def seconds = diff/1000/1000/1000
return String.format("%.2f", seconds)
use(TimeCategory) {
def duration = new Date() - buildStartTime
return duration.seconds
}
}
git = Grgit.open(dir: project.getRootProject().projectDir)
}
Expand Down Expand Up @@ -106,9 +114,68 @@ class SlackPlugin implements Plugin<Project> {
.channel(general.getId())
.ts(project.firstMessageTimestamp)
.attachments([slackMessage])
.build());
.build())
}

boolean success = failure == null

def rootProject = task.getProject().rootProject
def gradle = rootProject.getGradle()
def completedTasks = rootProject.completedTasks.size()
def startingTasks = gradle.startParameter.taskNames.join(", ")
def totalTasks = gradle.getTaskGraph().getAllTasks().size()

def completed = completedTasks == totalTasks

if(completed || failure) {

def buildLog = FilesUploadRequest.builder()
.token(token)
.channels([general.getId()])
.threadTs(project.firstMessageTimestamp)
.content(mTaskLogBuilder.toString())
.build()

if(System.env.containsKey('BUILD_NUMBER')) {
if(rootProject.hasProperty('git')) {
buildLog.title = "${project.name}-${rootProject.git.branch.current.name}-build-${System.env.BUILD_NUMBER}.log"
} else {
buildLog.title = "${project.name}-build-${System.env.BUILD_NUMBER}.log"
}
} else {
if(rootProject.hasProperty('git')) {
buildLog.title = "${project.name}-${rootProject.git.branch.current.name}-build.log"
} else {
buildLog.title = "${project.name}-build.log"
}
}

FilesUploadResponse buildLogUpload = slack.methods().filesUpload(buildLog)


ReactionsAddRequest reaction = ReactionsAddRequest.builder()
.token(token)
.channel(general.getId())
.timestamp(project.firstMessageTimestamp)
.build()


if(completed && success) {
reaction.name = 'tada'
}

if(failure) {
reaction.name = 'cry'
}

ReactionsAddResponse successReaction = slack.methods().reactionsAdd(reaction)
}







}

void recordFirstMessageTimestamp(String firstMessageTimestamp, Project project) {
Expand Down

0 comments on commit 6874c8f

Please sign in to comment.