|
20 | 20 | package org.commonwl.view.git; |
21 | 21 |
|
22 | 22 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import java.io.IOException; |
23 | 26 | import java.io.Serializable; |
24 | 27 | import java.net.URI; |
25 | 28 | import java.net.URISyntaxException; |
| 29 | +import java.nio.file.Path; |
26 | 30 | import java.util.Objects; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
27 | 33 |
|
28 | 34 | /** Represents all the parameters necessary to access a file/directory with Git */ |
29 | 35 | @JsonIgnoreProperties( |
30 | 36 | value = {"internalUrl"}, |
31 | 37 | ignoreUnknown = true) |
32 | 38 | public class GitDetails implements Serializable { |
33 | 39 |
|
| 40 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 41 | + |
| 42 | + private static final String SPDX_PREFIX = "https://spdx.org/licenses/"; |
| 43 | + |
34 | 44 | private String repoUrl; |
35 | 45 | private String branch; |
36 | 46 | private String path; |
@@ -276,4 +286,53 @@ public String toSummary() { |
276 | 286 | return String.format( |
277 | 287 | "repoUrl: %s branch: %s path: %s packedId: %s", repoUrl, branch, path, packedId); |
278 | 288 | } |
| 289 | + |
| 290 | + /** |
| 291 | + * Retrieves license details from the repo, if present. |
| 292 | + * |
| 293 | + * @param workTree the path to the locally cloned repo |
| 294 | + * @return The license URI |
| 295 | + */ |
| 296 | + public String getLicense(Path workTree) throws GitLicenseException { |
| 297 | + try { |
| 298 | + String[] command = {"licensee", "detect", "--json", workTree.toString()}; |
| 299 | + if (logger.isTraceEnabled()) { |
| 300 | + logger.trace("Calling " + String.join(" ", command)); |
| 301 | + } |
| 302 | + Process process = Runtime.getRuntime().exec(command, null); |
| 303 | + ObjectMapper mapper = new ObjectMapper(); |
| 304 | + JsonNode jsonLicenses = mapper.readTree(process.getInputStream()); |
| 305 | + if (logger.isTraceEnabled()) { |
| 306 | + logger.trace( |
| 307 | + "Licensee retrieved the following licenses:\n" + jsonLicenses.toPrettyString()); |
| 308 | + } |
| 309 | + int size = jsonLicenses.withArray("licenses").size(); |
| 310 | + if (size > 0) { |
| 311 | + String licenseCandidate = |
| 312 | + jsonLicenses.withArray("matched_files").get(0).get("filename").asText(); |
| 313 | + String licenseLink = getRawUrl(null, licenseCandidate); |
| 314 | + if (logger.isWarnEnabled() && size > 1) { |
| 315 | + logger.warn( |
| 316 | + "There are " |
| 317 | + + size |
| 318 | + + " identified license files in the " |
| 319 | + + repoUrl |
| 320 | + + " repository. " |
| 321 | + + "Taking the first one: " |
| 322 | + + licenseLink); |
| 323 | + } |
| 324 | + String key = jsonLicenses.withArray("licenses").get(0).get("key").asText(); |
| 325 | + if (!"other".equals(key)) { |
| 326 | + return SPDX_PREFIX + jsonLicenses.withArray("licenses").get(0).get("spdx_id").asText(); |
| 327 | + } else { |
| 328 | + return licenseLink; |
| 329 | + } |
| 330 | + } else { |
| 331 | + return null; |
| 332 | + } |
| 333 | + } catch (IOException e) { |
| 334 | + throw new GitLicenseException( |
| 335 | + "While attempting to detect license for " + workTree + ": " + e.getMessage(), e); |
| 336 | + } |
| 337 | + } |
279 | 338 | } |
0 commit comments