Rewrite Slack reporter to Kotlin

This commit is contained in:
Pavel Punegov
2019-02-15 19:26:43 +03:00
committed by Pavel Punegov
parent ac1d893857
commit 46d32c6064
2 changed files with 74 additions and 81 deletions
@@ -1,81 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin
import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory
import groovy.json.JsonSlurper
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
/**
* Created by minamoto on 2/7/17.
*/
class Reporter extends DefaultTask {
private String buildLogUrlTab(String buildId, String buildTypeId) {
return tabUrl(buildId, buildTypeId, "buildLog")
}
private String tabUrl(String buildId, String buildTypeId, tab) {
return "http://buildserver.labs.intellij.net/viewLog.html?buildId=${buildId}&buildTypeId=${buildTypeId}&tab=${tab}"
}
private String testReportUrl(String buildId, String buildTypeId) {
return tabUrl(buildId, buildTypeId,"testsInfo")
}
def reportHome
@TaskAction
void report() {
def teamcityConfig = System.getenv("TEAMCITY_BUILD_PROPERTIES_FILE")
if (teamcityConfig == null)
throw new RuntimeException("Can't load teamcity config")
def buildProperties = new Properties()
buildProperties.load(new FileInputStream(teamcityConfig))
def buildId = buildProperties.'teamcity.build.id'
def buildTypeId = buildProperties.'teamcity.buildType.id'
def logUrl = buildLogUrlTab(buildId, buildTypeId)
def testReportUrl = testReportUrl(buildId, buildTypeId)
def epilogue = "\nlog url: $logUrl\ntest report url: $testReportUrl"
def stats = new RunExternalTestGroup.Statistics()
def resultsFile = new File(reportHome, "external/results.json")
def report
if (resultsFile.exists()) {
def obj = new JsonSlurper().parse(resultsFile)
stats.total = obj.statistics.total
stats.passed = obj.statistics.passed
stats.failed = obj.statistics.failed
stats.error = obj.statistics.error
stats.skipped = obj.statistics.skipped
report = "total: ${stats.total}\npassed: ${stats.passed}\nfailed: ${stats.failed}\n" +
"error:${stats.error}\nskipped:${stats.skipped} ${epilogue}"
} else {
report = "Unable to get results\nBuild has probably failed${epilogue}"
}
println(report)
def session = new SlackSessionFactory().createWebSocketSlackSession(buildProperties.'konan-reporter-token')
session.connect()
def channel = session.findChannelByName(buildProperties.'konan-channel-name')
session.sendMessage(channel, "Hello, аборигены Котлина!\n текущий статус:\n${report}")
session.disconnect()
}
}
@@ -0,0 +1,74 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package org.jetbrains.kotlin
import com.ullink.slack.simpleslackapi.impl.SlackSessionFactory
import groovy.json.JsonSlurper
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.io.FileInputStream
import java.util.*
open class Reporter : DefaultTask() {
private fun buildLogUrlTab(buildId: String, buildTypeId: String): String = tabUrl(buildId, buildTypeId, "buildLog")
private fun tabUrl(buildId: String, buildTypeId: String, tab: String): String =
"http://buildserver.labs.intellij.net/viewLog.html?buildId=$buildId&buildTypeId=$buildTypeId&tab=$tab"
private fun testReportUrl(buildId: String, buildTypeId: String): String = tabUrl(buildId, buildTypeId, "testsInfo")
@Input
lateinit var reportHome: String
@TaskAction
fun report() {
val teamcityConfig = System.getenv("TEAMCITY_BUILD_PROPERTIES_FILE")
?: throw RuntimeException("Can't load teamcity config")
val buildProperties = Properties()
buildProperties.load(FileInputStream(teamcityConfig))
val buildId = buildProperties.getProperty("teamcity.build.id")
val buildTypeId = buildProperties.getProperty("teamcity.buildType.id")
val logUrl = buildLogUrlTab(buildId, buildTypeId)
val testReportUrl = testReportUrl(buildId, buildTypeId)
var epilogue = "\nlog url: $logUrl\ntest report url: $testReportUrl"
val resultsFile = File(reportHome, "external/results.json")
val report: String = if (resultsFile.exists()) {
@Suppress("UNCHECKED_CAST")
val obj = JsonSlurper().parse(resultsFile) as? AbstractMap<String, Any>
?: throw IllegalStateException("Got incorrect JSON object: ${resultsFile.absolutePath}")
@Suppress("UNCHECKED_CAST")
val statsJSON = obj["statistics"] as? AbstractMap<String, Int>
?: throw IllegalStateException("Got incorrect statistics object in JSON:" +
resultsFile.absolutePath)
val stats = Statistics(
passed = statsJSON["passed"]!!,
failed = statsJSON["failed"]!!,
error = statsJSON["error"]!!,
skipped = statsJSON["skipped"]!!)
val total = statsJSON["total"]!!
if (total != stats.total) {
val message = "WARNING: total amount of tests doesn't match with the sum of failed, passed, error & skipped"
println(message)
epilogue += message
}
"total: ${stats.total}\npassed: ${stats.passed}\nfailed: ${stats.failed}\n" +
"error:${stats.error}\nskipped:${stats.skipped} $epilogue"
} else {
"Unable to get results\nBuild has probably failed$epilogue"
}
println(report)
with(SlackSessionFactory.createWebSocketSlackSession(buildProperties.getProperty("konan-reporter-token"))) {
connect()
sendMessage(findChannelByName(buildProperties.getProperty("konan-channel-name")),
"Hello, аборигены Котлина!\n текущий статус:\n$report")
disconnect()
}
}
}