Skip to content

Commit 2180b20

Browse files
committed
SPDX licenses bump automation
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 9d6e29f commit 2180b20

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Update SPDX licenses
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 * * *"
6+
workflow_dispatch:
7+
8+
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/controlling-permissions-for-github_token
9+
permissions: {}
10+
11+
jobs:
12+
update:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
# see https://github.com/actions/checkout
17+
uses: actions/checkout@v5
18+
- name: Set up JDK
19+
# see https://github.com/actions/setup-java
20+
uses: actions/setup-java@v5
21+
with:
22+
java-version: '21'
23+
distribution: 'zulu'
24+
java-package: jdk
25+
- name: update
26+
run: tools/updateSpdx.sh
27+
- name: Archive Schema
28+
# https://github.com/actions/upload-artifact
29+
uses: actions/upload-artifact@v4
30+
with:
31+
name: schema
32+
path: schema/spdx.*
33+
if-no-files-found: error
34+
# TODO:
35+
# - if no changes: abort; else:
36+
# - detect version - pull from JSON `$comment`
37+
# - commit the changes
38+
# - with message `bump SPDX licenses $version`
39+
# - to branch 'spdx-licenses/$version'
40+
# - signed as bot
41+
# - create pullresquest

tools/src/main/java/org/cyclonedx/tools/SpdxXsdGenerator.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,39 @@
2626
import java.nio.charset.StandardCharsets;
2727
import java.util.LinkedHashMap;
2828
import java.util.Map;
29+
import java.util.Objects;
2930
import java.util.Set;
3031

3132
public class SpdxXsdGenerator {
3233

33-
//todo : automatically obtain latest release from: https://api.github.com/repos/spdx/license-list-data/releases
34-
//todo : make configurable
35-
private static final String SPDX_VERSION = "3.27.0";
34+
public static void main(String[] args) throws Exception {
35+
String tagName = args.length == 0 || Objects.equals(args[0], "latest")
36+
? getLatestReleaseTagName()
37+
: args[0];
38+
new SpdxXsdGenerator(tagName)
39+
.generateSchemas();
40+
}
41+
42+
private static final String REPO = "spdx/license-list-data";
43+
44+
private static String getLatestReleaseTagName() throws Exception {
45+
String apiReleasesLatest = "https://api.github.com/repos/" + REPO + "/releases/latest";
46+
HttpResponse<JsonNode> apiResponse = Unirest.get(apiReleasesLatest).asJson();
47+
final JSONObject apiResponseRoot = apiResponse.getBody().getObject();
48+
return apiResponseRoot.getString("tag_name");
49+
}
50+
51+
private final String tagName;
3652

37-
public static void main(String args[]) throws Exception {
38-
String licenseUrl = "https://raw.githubusercontent.com/spdx/license-list-data/v" + SPDX_VERSION + "/json/licenses.json";
39-
String exceptionsUrl = "https://raw.githubusercontent.com/spdx/license-list-data/v" + SPDX_VERSION + "/json/exceptions.json";
53+
public SpdxXsdGenerator(String tagName) {
54+
this.tagName = tagName;
55+
}
56+
57+
public void generateSchemas() throws Exception {
58+
System.out.println("Generate Schemas for " + REPO + " tagName: " + tagName);
59+
60+
String licenseUrl = "https://raw.githubusercontent.com/" + REPO + "/" + tagName + "/json/licenses.json";
61+
String exceptionsUrl = "https://raw.githubusercontent.com/" + REPO + "/" + tagName + "/json/exceptions.json";
4062

4163
HttpResponse<JsonNode> licenseResponse = Unirest.get(licenseUrl).asJson();
4264
final JSONObject licenseRoot = licenseResponse.getBody().getObject();
@@ -62,15 +84,14 @@ public static void main(String args[]) throws Exception {
6284
createJsonSchema(licenseMap, exceptionMap);
6385
}
6486

65-
66-
private static void createXmlSchema(Map<String, String> licenses, Map<String, String> exceptions) throws IOException {
87+
private void createXmlSchema(Map<String, String> licenses, Map<String, String> exceptions) throws IOException {
6788
StringBuilder sb = new StringBuilder();
6889
sb
6990
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>").append("\n")
7091
.append("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"").append("\n")
7192
.append(indent(11)).append("elementFormDefault=\"qualified\"").append("\n")
7293
.append(indent(11)).append("targetNamespace=\"http://cyclonedx.org/schema/spdx\"").append("\n")
73-
.append(indent(11)).append("version=\"1.0-" + SPDX_VERSION + "\">").append("\n\n")
94+
.append(indent(11)).append("version=\"1.0-" + stripLeadingV(tagName) + "\">").append("\n\n")
7495
.append(indent(4)).append("<xs:simpleType name=\"licenseId\">").append("\n")
7596
.append(indent(8)).append("<xs:restriction base=\"xs:string\">").append("\n");
7697

@@ -90,13 +111,13 @@ private static void createXmlSchema(Map<String, String> licenses, Map<String, St
90111
FileUtils.writeStringToFile(file, sb.toString(), StandardCharsets.UTF_8);
91112
}
92113

93-
private static void createJsonSchema(Map<String, String> licenses, Map<String, String> exceptions) throws IOException {
114+
private void createJsonSchema(Map<String, String> licenses, Map<String, String> exceptions) throws IOException {
94115
StringBuilder sb = new StringBuilder();
95116
sb
96117
.append("{").append("\n")
97118
.append(indent(2)).append("\"$schema\": \"http://json-schema.org/draft-07/schema#\",").append("\n")
98119
.append(indent(2)).append("\"$id\": \"http://cyclonedx.org/schema/spdx.schema.json\",").append("\n")
99-
.append(indent(2)).append("\"$comment\": \"v1.0-" + SPDX_VERSION + "\",").append("\n")
120+
.append(indent(2)).append("\"$comment\": \"v1.0-" + stripLeadingV(tagName) + "\",").append("\n")
100121
.append(indent(2)).append("\"type\": \"string\",").append("\n")
101122
.append(indent(2)).append("\"enum\": [");
102123

@@ -144,4 +165,10 @@ private static String indent(int spaces) {
144165
return sb.toString();
145166
}
146167

168+
public static String stripLeadingV(String input) {
169+
if (input != null && input.length() > 1 && input.charAt(0) == 'v' ) {
170+
return input.substring(1);
171+
}
172+
return input;
173+
}
147174
}

0 commit comments

Comments
 (0)