forked from ratpack/ratpack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomData.gradle
105 lines (94 loc) · 3.46 KB
/
customData.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import org.gradle.internal.exceptions.LocationAwareException
def GITHUB_REPO = 'https://github.com/george-moberly/ratpack'
def FORUM_PARAMS = '%22&body=put%20your%20build%20scan%20url%20here!&category=Help%2FDiscuss&tags=buildscan-provided'
// Local or CI
if (System.getenv('BUILD_NUMBER')) {
buildScan.tag 'CI'
buildScan.tag 'JENKINS'
buildScan.link 'Jenkins Build', System.getenv('BUILD_URL')
} else {
buildScan.tag 'DEV'
}
// Git commit id
def commitId = 'git rev-parse --verify HEAD'.execute().text.trim()
if (commitId) {
buildScan.value 'Git Commit ID', commitId
buildScan.link 'Source', "$GITHUB_REPO/tree/$commitId"
}
// Git branch name
def branchName = 'git rev-parse --abbrev-ref HEAD'.execute().text.trim()
if (branchName) {
buildScan.value 'Git Branch Name', branchName
}
// Git dirty local state
def status = 'git status --porcelain'.execute().text
if (status) {
buildScan.tag "dirty"
buildScan.value "Git Status", status
}
// Git diff
buildScan.buildFinished {
def diff = 'git diff'.execute().text
if (diff) {
def baseUrl = new URL('https://api.github.com/gists')
String credentials = "$gistUsername:$gistToken"
String basicAuth = "Basic ${credentials.bytes.encodeBase64()}"
try {
HttpURLConnection connection = (HttpURLConnection) baseUrl.openConnection()
connection.with {
setRequestProperty("Authorization", basicAuth)
doOutput = true
requestMethod = 'POST'
outputStream.withWriter { writer ->
jsonRequest(writer, gradle.rootProject, diff)
}
def parser = new JsonSlurper()
def url = parser.parse(content.text.bytes).html_url
buildScan.link('Git diff', url)
}
} catch (ex) {
gradle.rootProject.logger.warn("Unable to publish diff to Gist", ex)
}
}
}
// Checkstyle violations
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (task instanceof Checkstyle) {
if (state.failure) {
def checkstyle = new XmlSlurper().parse(task.reports.xml.destination)
def errors = checkstyle.file.collect {
String filePath = task.project.rootProject.relativePath([email protected]())
it.error.collect { "${filePath}:${it.@line}:${it.@column} \u2192 ${it.@message}" }
}.flatten()
errors.each { task.project.buildScan.value 'Checkstyle Issue', it }
}
}
}
// Creation of a topic in Gradle Forum
buildScan.buildFinished { result ->
if (result.failure) {
if (result.failure instanceof LocationAwareException) {
if (!result.failure.reportableCauses.isEmpty()) {
def reportMessage = result.failure.reportableCauses[0].message
def message = reportMessage.replace(' ', "%20")
def link = "http://discuss.gradle.org/new-topic?title=Build%20fails%20with%20%22$message$FORUM_PARAMS"
buildScan.link("Create Gradle Forum Topic", link)
}
}
}
}
static void jsonRequest(Writer out, Project project, String diff) {
def builder = new JsonBuilder()
builder {
description("Git diff for $project.name")
'public'(false)
files {
"${project.name}.diff" {
content diff
}
}
}
builder.writeTo(out)
}