forked from elastic/elasticsearch
/
LicenseHeadersTask.java
250 lines (221 loc) · 10.4 KB
/
LicenseHeadersTask.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.gradle.internal.precommit;
import org.apache.rat.Defaults;
import org.apache.rat.ReportConfiguration;
import org.apache.rat.analysis.IHeaderMatcher;
import org.apache.rat.analysis.util.HeaderMatcherMultiplexer;
import org.apache.rat.anttasks.SubstringLicenseMatcher;
import org.apache.rat.api.RatException;
import org.apache.rat.document.impl.FileDocument;
import org.apache.rat.license.SimpleLicenseFamily;
import org.apache.rat.report.RatReport;
import org.apache.rat.report.claim.ClaimStatistic;
import org.apache.rat.report.xml.XmlReportFactory;
import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Checks files for license headers..
*/
@CacheableTask
public abstract class LicenseHeadersTask extends DefaultTask {
public LicenseHeadersTask() {
setDescription("Checks sources for missing, incorrect, or unacceptable license headers");
}
/**
* The list of java files to check. protected so the afterEvaluate closure in the
* constructor can write to it.
*/
@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.RELATIVE)
public List<FileCollection> getJavaFiles() {
return getSourceFolders().get();
}
@Internal
public abstract ListProperty<FileCollection> getSourceFolders();
public File getReportFile() {
return reportFile;
}
public void setReportFile(File reportFile) {
this.reportFile = reportFile;
}
public List<String> getApprovedLicenses() {
return approvedLicenses;
}
public void setApprovedLicenses(List<String> approvedLicenses) {
this.approvedLicenses = approvedLicenses;
}
public List<String> getExcludes() {
return excludes;
}
public Map<String, String> getAdditionalLicenses() {
return additionalLicenses;
}
public void setExcludes(List<String> excludes) {
this.excludes = excludes;
}
@OutputFile
private File reportFile = new File(getProject().getBuildDir(), "reports/licenseHeaders/rat.xml");
/**
* Allowed license families for this project.
*/
@Input
private List<String> approvedLicenses = new ArrayList<String>(Arrays.asList("SSPL+Elastic License", "Generated", "Vendored"));
/**
* Files that should be excluded from the license header check. Use with extreme care, only in situations where the license on the
* source file is compatible with the codebase but we do not want to add the license to the list of approved headers (to avoid the
* possibility of inadvertently using the license on our own source files).
*/
@Input
private List<String> excludes = new ArrayList<String>();
/**
* Additional license families that may be found. The key is the license category name (5 characters),
* followed by the family name and the value list of patterns to search for.
*/
@Input
protected Map<String, String> additionalLicenses = new HashMap<String, String>();
/**
* Add a new license type.
* <p>
* The license may be added to the {@link #approvedLicenses} using the {@code familyName}.
*
* @param categoryName A 5-character string identifier for the license
* @param familyName An expanded string name for the license
* @param pattern A pattern to search for, which if found, indicates a file contains the license
*/
public void additionalLicense(final String categoryName, String familyName, String pattern) {
if (categoryName.length() != 5) {
throw new IllegalArgumentException("License category name must be exactly 5 characters, got " + categoryName);
}
additionalLicenses.put(categoryName + familyName, pattern);
}
@TaskAction
public void runRat() {
ReportConfiguration reportConfiguration = new ReportConfiguration();
reportConfiguration.setAddingLicenses(true);
List<IHeaderMatcher> matchers = new ArrayList<>();
matchers.add(Defaults.createDefaultMatcher());
// BSD 4-clause stuff (is disallowed below)
// we keep this here, in case someone adds BSD code for some reason, it should never be allowed.
matchers.add(subStringMatcher("BSD4 ", "Original BSD License (with advertising clause)", "All advertising materials"));
// Apache
matchers.add(subStringMatcher("AL ", "Apache", "Licensed to Elasticsearch B.V. under one or more contributor"));
// Generated resources
matchers.add(subStringMatcher("GEN ", "Generated", "ANTLR GENERATED CODE"));
// Vendored Code
matchers.add(subStringMatcher("VEN ", "Vendored", "@notice"));
// Dual SSPLv1 and Elastic
matchers.add(subStringMatcher("DUAL", "SSPL+Elastic License", "the Elastic License 2.0 or the Server"));
for (Map.Entry<String, String> additional : additionalLicenses.entrySet()) {
String category = additional.getKey().substring(0, 5);
String family = additional.getKey().substring(5);
matchers.add(subStringMatcher(category, family, additional.getValue()));
}
reportConfiguration.setHeaderMatcher(new HeaderMatcherMultiplexer(matchers.toArray(new IHeaderMatcher[matchers.size()])));
reportConfiguration.setApprovedLicenseNames(approvedLicenses.stream().map(license -> {
SimpleLicenseFamily simpleLicenseFamily = new SimpleLicenseFamily();
simpleLicenseFamily.setFamilyName(license);
return simpleLicenseFamily;
}).toArray(SimpleLicenseFamily[]::new));
ClaimStatistic stats = generateReport(reportConfiguration, getReportFile());
boolean unknownLicenses = stats.getNumUnknown() > 0;
boolean unApprovedLicenses = stats.getNumUnApproved() > 0;
if (unknownLicenses || unApprovedLicenses) {
getLogger().error("The following files contain unapproved license headers:");
unapprovedFiles(getReportFile()).stream().forEachOrdered(unapprovedFile -> getLogger().error(unapprovedFile));
throw new GradleException("Check failed. License header problems were found. Full details: " + reportFile.getAbsolutePath());
}
}
private IHeaderMatcher subStringMatcher(String licenseFamilyCategory, String licenseFamilyName, String substringPattern) {
SubstringLicenseMatcher substringLicenseMatcher = new SubstringLicenseMatcher();
substringLicenseMatcher.setLicenseFamilyCategory(licenseFamilyCategory);
substringLicenseMatcher.setLicenseFamilyName(licenseFamilyName);
SubstringLicenseMatcher.Pattern pattern = new SubstringLicenseMatcher.Pattern();
pattern.setSubstring(substringPattern);
substringLicenseMatcher.addConfiguredPattern(pattern);
return substringLicenseMatcher;
}
private ClaimStatistic generateReport(ReportConfiguration config, File xmlReportFile) {
try {
Files.deleteIfExists(reportFile.toPath());
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(xmlReportFile));
return toXmlReportFile(config, bufferedWriter);
} catch (IOException | RatException exception) {
throw new GradleException("Cannot generate license header report for " + getPath(), exception);
}
}
private ClaimStatistic toXmlReportFile(ReportConfiguration config, Writer writer) throws RatException, IOException {
ClaimStatistic stats = new ClaimStatistic();
RatReport standardReport = XmlReportFactory.createStandardReport(new XmlWriter(writer), stats, config);
standardReport.startReport();
for (FileCollection dirSet : getSourceFolders().get()) {
for (File f : dirSet.getAsFileTree().matching(patternFilterable -> patternFilterable.exclude(getExcludes()))) {
standardReport.report(new FileDocument(f));
}
}
standardReport.endReport();
writer.flush();
writer.close();
return stats;
}
private static List<String> unapprovedFiles(File xmlReportFile) {
try {
NodeList resourcesNodes = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(xmlReportFile)
.getElementsByTagName("resource");
return elementList(resourcesNodes).stream()
.filter(
resource -> elementList(resource.getChildNodes()).stream()
.anyMatch(n -> n.getTagName().equals("license-approval") && n.getAttribute("name").equals("false"))
)
.map(e -> e.getAttribute("name"))
.sorted()
.collect(Collectors.toList());
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new GradleException("Error parsing xml report " + xmlReportFile.getAbsolutePath());
}
}
private static List<Element> elementList(NodeList resourcesNodes) {
List<Element> nodeList = new ArrayList<>(resourcesNodes.getLength());
for (int idx = 0; idx < resourcesNodes.getLength(); idx++) {
nodeList.add((Element) resourcesNodes.item(idx));
}
return nodeList;
}
}