forked from kt3k/coveralls-gradle-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoveralls.groovy
executable file
·173 lines (121 loc) · 4.35 KB
/
coveralls.groovy
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/env groovy
// coveralls.groovy /
import groovy.json.*
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2')
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.POST
@Grab(group='org.apache.httpcomponents', module='httpmime', version='4.3')
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.ContentType
API_HOST = 'https://coveralls.io'
API_PATH = '/api/v1/jobs'
COBERTURA_REPORT_PATH = 'build/reports/cobertura/coverage.xml'
// model for coveralls io report's source file report
class SourceReport {
String name;
String source;
List<Integer> coverage;
public SourceReport(String name, String source, List<Integer> coverage) {
this.name = name;
this.source = source;
this.coverage = coverage;
}
}
// model for coveralls io report
class Report {
String service_job_id;
String service_name;
List<SourceReport> source_files;
public Report() {
}
public Report(String serviceName, String serviceJobId, List<SourceReport> sourceFiles) {
this.service_name = serviceName;
this.service_job_id = serviceJobId;
this.source_files = sourceFiles;
}
public String toJson() {
JsonBuilder json = new JsonBuilder(this)
return json.toString()
}
}
class SourceReportFactory {
public static List<SourceReport> createFromCoberturaXML(File file) {
Node coverage = new XmlParser().parse(file)
String sourceDir = coverage.sources.source.text() + '/'
Map a = [:]
coverage.packages.package.classes.class.each() {
Map cov = a.get(it.'@filename', [:])
it.lines.line.each() {
cov[it.'@number'.toInteger() - 1] = it.'@hits'.toInteger()
}
}
List<SourceReport> reports = new ArrayList<SourceReport>()
a.each { String filename, Map cov ->
File sourceFile = new File(sourceDir + filename)
String source = sourceFile.text
List r = [null] * source.readLines().size()
cov.each { Integer line, Integer hits ->
r[line] = hits
}
String relPath = new File('.').toURI().relativize( sourceFile.toURI() ).toString()
reports.add new SourceReport(relPath, source, r)
}
return reports
}
}
// model for ci service info
class ServiceInfo {
String serviceName;
String serviceJobId;
public ServiceInfo(String serviceName, String serviceJobId) {
this.serviceName = serviceName;
this.serviceJobId = serviceJobId;
}
}
class ServiceInfoFactory {
public static createFromEnvVar() {
if (System.getenv('TRAVIS') == 'true') {
return new ServiceInfo('travis-ci', System.getenv('TRAVIS_JOB_ID'))
}
// cannot create service info from env var
return null
}
}
void postToCoveralls(String json) {
HTTPBuilder http = new HTTPBuilder(API_HOST + API_PATH)
http.request(POST) { req ->
req.entity = MultipartEntityBuilder.create().addBinaryBody('json_file', json.getBytes('UTF-8'), ContentType.APPLICATION_JSON, 'json_file').build()
response.success = { resp, reader ->
println resp.statusLine
println resp.getAllHeaders()
println resp.getData()
System.out << reader
}
response.failure = { resp, reader ->
println resp.statusLine
println resp.getAllHeaders()
println resp.getData()
System.out << reader
}
}
}
void main() {
ServiceInfo serviceInfo = ServiceInfoFactory.createFromEnvVar()
if (serviceInfo == null) {
println 'no available service'
return
}
println 'service name: ' + serviceInfo.serviceName
println 'service job id: ' + serviceInfo.serviceJobId
File file = new File(COBERTURA_REPORT_PATH)
if (!file.exists()) {
println 'covertura report not available: ' + file.absolutePath
return
}
println 'cobertura report file: ' + file.absolutePath
List<SourceReport> sourceReports = SourceReportFactory.createFromCoberturaXML file
Report rep = new Report(serviceInfo.serviceName, serviceInfo.serviceJobId, sourceReports)
println rep.toJson()
postToCoveralls rep.toJson()
}
main()