2019-12-04 02:36:16 +01:00
|
|
|
/**
|
|
|
|
Wraps the main/important part of a job, executes it, and then publishes a comment to GitHub with the status.
|
|
|
|
|
|
|
|
It will check for the existence of GHPRB env variables before doing any actual PR work,
|
|
|
|
so it can be used to wrap code that is executed in both PR and non-PR contexts.
|
|
|
|
|
|
|
|
Inside the comment, it will hide a JSON blob containing build data (status, etc).
|
|
|
|
|
|
|
|
Then, the next time it posts a comment, it will:
|
|
|
|
1. Read the previous comment and parse the json
|
|
|
|
2. Create a new comment, add a summary of up to 5 previous builds to it, and append this build's data to the hidden JSON
|
|
|
|
3. Delete the old comment
|
|
|
|
|
|
|
|
So, there is only ever one build status comment on a PR at any given time, the most recent one.
|
|
|
|
*/
|
|
|
|
def withDefaultPrComments(closure) {
|
|
|
|
catchError {
|
|
|
|
catchError {
|
|
|
|
closure()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.ENABLE_GITHUB_PR_COMMENTS || !isPr()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
def status = buildUtils.getBuildStatus()
|
|
|
|
if (status == "ABORTED") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
def lastComment = getLatestBuildComment()
|
|
|
|
def info = getLatestBuildInfo(lastComment) ?: [:]
|
|
|
|
info.builds = (info.builds ?: []).takeRight(5) // Rotate out old builds
|
|
|
|
|
|
|
|
def message = getNextCommentMessage(info)
|
|
|
|
postComment(message)
|
|
|
|
|
2020-01-01 03:42:16 +01:00
|
|
|
if (lastComment && lastComment.user.login == 'kibanamachine') {
|
2019-12-04 02:36:16 +01:00
|
|
|
deleteComment(lastComment.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether or not this currently executing build was triggered via a PR in the elastic/kibana repo
|
|
|
|
def isPr() {
|
|
|
|
return !!(env.ghprbPullId && env.ghprbPullLink && env.ghprbPullLink =~ /\/elastic\/kibana\//)
|
|
|
|
}
|
|
|
|
|
|
|
|
def getLatestBuildComment() {
|
|
|
|
return getComments()
|
|
|
|
.reverse()
|
2020-01-01 03:42:16 +01:00
|
|
|
.find { (it.user.login == 'elasticmachine' || it.user.login == 'kibanamachine') && it.body =~ /<!--PIPELINE/ }
|
2019-12-04 02:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def getBuildInfoFromComment(commentText) {
|
|
|
|
def matches = commentText =~ /(?ms)<!--PIPELINE(.*?)PIPELINE-->/
|
|
|
|
if (!matches || !matches[0]) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return toJSON(matches[0][1].trim())
|
|
|
|
}
|
|
|
|
|
|
|
|
def getLatestBuildInfo() {
|
|
|
|
return getLatestBuildInfo(getLatestBuildComment())
|
|
|
|
}
|
|
|
|
|
|
|
|
def getLatestBuildInfo(comment) {
|
|
|
|
return comment ? getBuildInfoFromComment(comment) : null
|
|
|
|
}
|
|
|
|
|
|
|
|
def createBuildInfo() {
|
|
|
|
return [
|
|
|
|
status: buildUtils.getBuildStatus(),
|
|
|
|
url: env.BUILD_URL,
|
|
|
|
number: env.BUILD_NUMBER,
|
|
|
|
commit: getCommitHash()
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
def getHistoryText(builds) {
|
|
|
|
if (!builds || builds.size() < 1) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
def list = builds
|
|
|
|
.reverse()
|
|
|
|
.collect { build ->
|
|
|
|
if (build.status == "SUCCESS") {
|
|
|
|
return "* :green_heart: [Build #${build.number}](${build.url}) succeeded ${build.commit}"
|
2020-01-27 22:26:24 +01:00
|
|
|
} else if(build.status == "UNSTABLE") {
|
|
|
|
return "* :yellow_heart: [Build #${build.number}](${build.url}) was flaky ${build.commit}"
|
2019-12-04 02:36:16 +01:00
|
|
|
} else {
|
|
|
|
return "* :broken_heart: [Build #${build.number}](${build.url}) failed ${build.commit}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.join("\n")
|
|
|
|
|
|
|
|
return "### History\n${list}"
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:26:24 +01:00
|
|
|
def getTestFailuresMessage() {
|
|
|
|
def failures = testUtils.getFailures()
|
|
|
|
if (!failures) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
def messages = []
|
2020-01-28 04:12:07 +01:00
|
|
|
messages << "---\n\n### [Test Failures](${env.BUILD_URL}testReport)"
|
2020-01-27 22:26:24 +01:00
|
|
|
|
2020-01-28 04:12:07 +01:00
|
|
|
failures.take(3).each { failure ->
|
2020-01-27 22:26:24 +01:00
|
|
|
messages << """
|
|
|
|
<details><summary>${failure.fullDisplayName}</summary>
|
|
|
|
|
|
|
|
[Link to Jenkins](${failure.url})
|
2020-01-28 04:12:07 +01:00
|
|
|
"""
|
2020-01-27 22:26:24 +01:00
|
|
|
|
2020-01-28 04:12:07 +01:00
|
|
|
if (failure.stdOut) {
|
|
|
|
messages << "\n#### Standard Out\n```\n${failure.stdOut}\n```"
|
|
|
|
}
|
2020-01-27 22:26:24 +01:00
|
|
|
|
2020-01-28 04:12:07 +01:00
|
|
|
if (failure.stdErr) {
|
|
|
|
messages << "\n#### Standard Error\n```\n${failure.stdErr}\n```"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failure.stacktrace) {
|
|
|
|
messages << "\n#### Stack Trace\n```\n${failure.stacktrace}\n```"
|
|
|
|
}
|
|
|
|
|
|
|
|
messages << "</details>\n\n---"
|
2020-01-27 22:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (failures.size() > 3) {
|
|
|
|
messages << "and ${failures.size() - 3} more failures, only showing the first 3."
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages.join("\n")
|
|
|
|
}
|
|
|
|
|
2019-12-04 02:36:16 +01:00
|
|
|
def getNextCommentMessage(previousCommentInfo = [:]) {
|
2020-01-27 22:26:24 +01:00
|
|
|
def info = previousCommentInfo ?: [:]
|
2019-12-04 02:36:16 +01:00
|
|
|
info.builds = previousCommentInfo.builds ?: []
|
|
|
|
|
|
|
|
def messages = []
|
2020-01-27 22:26:24 +01:00
|
|
|
def status = buildUtils.getBuildStatus()
|
2019-12-04 02:36:16 +01:00
|
|
|
|
2020-01-27 22:26:24 +01:00
|
|
|
if (status == 'SUCCESS') {
|
2019-12-04 02:36:16 +01:00
|
|
|
messages << """
|
|
|
|
## :green_heart: Build Succeeded
|
|
|
|
* [continuous-integration/kibana-ci/pull-request](${env.BUILD_URL})
|
|
|
|
* Commit: ${getCommitHash()}
|
|
|
|
"""
|
2020-01-27 22:26:24 +01:00
|
|
|
} else if(status == 'UNSTABLE') {
|
|
|
|
def message = """
|
|
|
|
## :yellow_heart: Build succeeded, but was flaky
|
|
|
|
* [continuous-integration/kibana-ci/pull-request](${env.BUILD_URL})
|
|
|
|
* Commit: ${getCommitHash()}
|
|
|
|
""".stripIndent()
|
|
|
|
|
|
|
|
def failures = retryable.getFlakyFailures()
|
|
|
|
if (failures && failures.size() > 0) {
|
|
|
|
def list = failures.collect { " * ${it.label}" }.join("\n")
|
|
|
|
message += "* Flaky suites:\n${list}"
|
|
|
|
}
|
|
|
|
|
|
|
|
messages << message
|
2019-12-04 02:36:16 +01:00
|
|
|
} else {
|
|
|
|
messages << """
|
|
|
|
## :broken_heart: Build Failed
|
|
|
|
* [continuous-integration/kibana-ci/pull-request](${env.BUILD_URL})
|
|
|
|
* Commit: ${getCommitHash()}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
2020-01-27 22:26:24 +01:00
|
|
|
messages << getTestFailuresMessage()
|
|
|
|
|
2019-12-04 02:36:16 +01:00
|
|
|
if (info.builds && info.builds.size() > 0) {
|
|
|
|
messages << getHistoryText(info.builds)
|
|
|
|
}
|
|
|
|
|
|
|
|
messages << "To update your PR or re-run it, just comment with:\n`@elasticmachine merge upstream`"
|
|
|
|
|
|
|
|
info.builds << createBuildInfo()
|
|
|
|
|
|
|
|
messages << """
|
|
|
|
<!--PIPELINE
|
|
|
|
${toJSON(info).toString()}
|
|
|
|
PIPELINE-->
|
|
|
|
"""
|
|
|
|
|
|
|
|
return messages
|
|
|
|
.findAll { !!it } // No blank strings
|
2020-01-27 22:26:24 +01:00
|
|
|
.collect { it.stripIndent().trim() } // This just allows us to indent various strings above, but leaves them un-indented in the comment
|
2019-12-04 02:36:16 +01:00
|
|
|
.join("\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
def withGithubCredentials(closure) {
|
|
|
|
withCredentials([
|
|
|
|
string(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7', variable: 'GITHUB_TOKEN'),
|
|
|
|
]) {
|
|
|
|
closure()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def postComment(message) {
|
|
|
|
if (!isPr()) {
|
|
|
|
error "Trying to post a GitHub PR comment on a non-PR or non-elastic PR build"
|
|
|
|
}
|
|
|
|
|
|
|
|
withGithubCredentials {
|
|
|
|
return githubApi.post("repos/elastic/kibana/issues/${env.ghprbPullId}/comments", [ body: message ])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def getComments() {
|
|
|
|
withGithubCredentials {
|
|
|
|
return githubIssues.getComments(env.ghprbPullId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def deleteComment(commentId) {
|
|
|
|
withGithubCredentials {
|
|
|
|
def path = "repos/elastic/kibana/issues/comments/${commentId}"
|
|
|
|
return githubApi([ path: path ], [ method: "DELETE" ])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def getCommitHash() {
|
|
|
|
return env.ghprbActualCommit
|
|
|
|
}
|