Skip to content

Commit

Permalink
Mbs 9937 lintlink (#633)
Browse files Browse the repository at this point in the history
* MBS-9937 extract teamcity link accessor

* MBS-9937 add build link to lint report
  • Loading branch information
dsvoronin authored Nov 15, 2020
1 parent 6b43586 commit ebfeff0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.avito.android.lint.slack.LintSlackReporter
import com.avito.slack.SlackClient
import com.avito.slack.model.SlackChannel
import com.avito.utils.logging.CILogger
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.junit.jupiter.api.Test
import java.io.File

Expand All @@ -27,7 +28,11 @@ internal class LintSlackAlertIntegrationTest {

val lintSlackReporter: LintSlackReporter = LintSlackReporter.Impl(slackClient, logger)

lintSlackReporter.report(reportModels, testChannel)
lintSlackReporter.report(
lintReport = reportModels,
channel = testChannel,
buildUrl = "https://stubbuildurl".toHttpUrl()
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.avito.bitbucket.atlassianCredentials
import com.avito.bitbucket.bitbucketConfig
import com.avito.utils.gradle.BuildEnvironment
import com.avito.utils.gradle.buildEnvironment
import com.avito.utils.gradle.envArgs
import com.avito.utils.logging.ciLogger
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -37,8 +36,6 @@ open class LintReportPlugin : Plugin<Project> {
}
)
}

buildId.set(project.envArgs.build.id)
}

if (app.buildEnvironment is BuildEnvironment.CI) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import com.avito.android.lint.html.LintReportMerger
import com.avito.android.lint.model.LintIssue
import com.avito.android.lint.model.LintReportModel
import com.avito.android.lint.model.hasErrors
import com.avito.android.lint.teamcity.TeamcityBuildLinkAccessor
import com.avito.bitbucket.Bitbucket
import com.avito.bitbucket.Severity
import com.avito.git.gitState
import com.avito.impact.configuration.internalModule
import com.avito.kotlin.dsl.getMandatoryStringProperty
import com.avito.utils.logging.ciLogger
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
Expand All @@ -28,9 +26,6 @@ abstract class LintReportTask : DefaultTask(), BuildVerdictTask {
@get:Input
abstract val abortOnError: Property<Boolean>

@get:Input
abstract val buildId: Property<Int>

@get:Internal
abstract val bitbucket: Property<Bitbucket>

Expand Down Expand Up @@ -100,6 +95,7 @@ abstract class LintReportTask : DefaultTask(), BuildVerdictTask {

private fun sendToBitbucket(reports: List<LintReportModel>) {
val git = project.gitState()
val teamcityBuildLinkAccessor: TeamcityBuildLinkAccessor = TeamcityBuildLinkAccessor.Impl(project)

if (!git.isPresent || !bitbucket.isPresent) {
project.ciLogger.info("Sending to bitbucket skipped")
Expand All @@ -117,7 +113,7 @@ abstract class LintReportTask : DefaultTask(), BuildVerdictTask {
targetCommitHash = targetBranch.commit,
key = "android-lint",
title = "Android Lint",
link = mergedReportLink(),
link = teamcityBuildLinkAccessor.getLintArtifactUrl(),
issues = reports
.filterIsInstance<LintReportModel.Valid>()
.flatMap { it.issues }
Expand All @@ -139,11 +135,6 @@ abstract class LintReportTask : DefaultTask(), BuildVerdictTask {
}
}

private fun mergedReportLink(): HttpUrl {
val teamcityUrl = project.getMandatoryStringProperty("teamcityUrl").removeSuffix("/")
return "${teamcityUrl}/repository/download/AvitoAndroid_Build/${buildId.get()}:id/${project.name}/build/reports/lint-results-release.html".toHttpUrl()
}

private fun failIfNeeded(reports: List<LintReportModel>, report: File) {
if (!abortOnError.getOrElse(true)) return
val failed = reports.filter { it.hasErrors() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avito.android.lint.slack

import com.avito.android.lint.LintResultsParser
import com.avito.android.lint.teamcity.TeamcityBuildLinkAccessor
import com.avito.slack.SlackClient
import com.avito.slack.model.SlackChannel
import com.avito.utils.logging.ciLogger
Expand Down Expand Up @@ -34,16 +35,21 @@ abstract class LintSlackReportTask : DefaultTask() {
lintHtml = lintHtml.get().asFile
)

val teamcityBuildLinkAccessor = createTeamcityBuildLinkAccessor()

createLintSlackAlert().report(
lintReport = models,
channel = SlackChannel(slackReportChannel.get())
channel = SlackChannel(slackReportChannel.get()),
buildUrl = teamcityBuildLinkAccessor.getBuildUrl()
)
}

private fun createLintParser(): LintResultsParser = LintResultsParser(
log = project.ciLogger
)

private fun createTeamcityBuildLinkAccessor(): TeamcityBuildLinkAccessor = TeamcityBuildLinkAccessor.Impl(project)

private fun createLintSlackAlert(): LintSlackReporter = LintSlackReporter.Impl(
slackClient = slackClient.get(),
logger = ciLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import com.avito.android.lint.model.LintReportModel
import com.avito.slack.SlackClient
import com.avito.slack.model.SlackChannel
import com.avito.utils.logging.CILogger
import okhttp3.HttpUrl

interface LintSlackReporter {

fun report(
lintReport: LintReportModel,
channel: SlackChannel
channel: SlackChannel,
buildUrl: HttpUrl
)

class Impl(
Expand All @@ -22,14 +24,15 @@ interface LintSlackReporter {

override fun report(
lintReport: LintReportModel,
channel: SlackChannel
channel: SlackChannel,
buildUrl: HttpUrl
) {
if (shouldSendAlert(lintReport)) {
logger.debug("$tag: Sending lint alert...")

slackClient.uploadHtml(
channel = channel,
message = buildSlackMessage(lintReport),
message = buildSlackMessage(lintReport, buildUrl),
file = lintReport.htmlFile
).fold(
{ logger.debug("$tag: Report sent successfully") },
Expand All @@ -44,9 +47,10 @@ interface LintSlackReporter {
return (model is LintReportModel.Valid) && model.issues.any { it.severity == LintIssue.Severity.ERROR }
}

private fun buildSlackMessage(model: LintReportModel): String {
private fun buildSlackMessage(model: LintReportModel, buildUrl: HttpUrl): String {
return buildString {
appendln("*Critical lint problems detected for project ${model.projectRelativePath}!*")
appendln("*Critical lint problems detected for project ${model.projectRelativePath}*")
appendln("Build: <$buildUrl|link>")
appendln()

if (model is LintReportModel.Valid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.avito.android.lint.teamcity

import com.avito.kotlin.dsl.getMandatoryStringProperty
import com.avito.utils.gradle.envArgs
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.gradle.api.Project

interface TeamcityBuildLinkAccessor {

fun getBuildUrl(): HttpUrl

fun getLintArtifactUrl(): HttpUrl

class Impl(private val project: Project) : TeamcityBuildLinkAccessor {

private val buildId = project.envArgs.build.id

private val teamcityUrl = project.getMandatoryStringProperty("teamcityUrl").removeSuffix("/")

override fun getBuildUrl(): HttpUrl {
return "$teamcityUrl/viewLog.html?buildId=$buildId".toHttpUrl()
}

override fun getLintArtifactUrl(): HttpUrl {
return "$teamcityUrl/repository/download/AvitoAndroid_Build/$buildId:id/${project.name}/build/reports/lint-results-release.html".toHttpUrl()
}
}
}

0 comments on commit ebfeff0

Please sign in to comment.