Skip to content

Commit fb1e3b0

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

File tree

3 files changed

+161
-11
lines changed

3 files changed

+161
-11
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: Update SPDX licenses
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 * * *"
6+
workflow_dispatch:
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/controlling-permissions-for-github_token
13+
permissions: { }
14+
15+
jobs:
16+
update:
17+
name: Update Schemas
18+
runs-on: ubuntu-latest
19+
outputs:
20+
changed: ${{ steps.diff.outputs.changed }}
21+
version: ${{ steps.version.outputs.version }}
22+
timeout-minutes: 10
23+
steps:
24+
- name: Checkout
25+
# see https://github.com/actions/checkout
26+
uses: actions/checkout@v5
27+
with:
28+
ref: ${{ github.ref_name }}
29+
- name: Set up JDK
30+
# see https://github.com/actions/setup-java
31+
uses: actions/setup-java@v5
32+
with:
33+
java-version: '21'
34+
distribution: 'zulu'
35+
java-package: jdk
36+
- name: Update SPDX
37+
run: tools/updateSpdx.sh
38+
- name: detect version
39+
id: version
40+
run: |
41+
value=$( jq -r '.["$comment"]' schema/spdx.schema.json )
42+
echo "version=$value" >> $GITHUB_OUTPUT
43+
- name: Detect changes
44+
id: diff
45+
run: |
46+
if git diff --quiet -- 'schema/spdx.*'
47+
then
48+
echo "$GITHUB_REF_NAME is up-to-date"
49+
echo "changed=false" >> $GITHUB_OUTPUT
50+
else
51+
echo "$GITHUB_REF_NAME is not up-to-date"
52+
echo "changed=true" >> $GITHUB_OUTPUT
53+
fi
54+
- name: Artifact changes
55+
if: ${{ steps.diff.outputs.changed == 'true' }}
56+
# https://github.com/actions/upload-artifact
57+
uses: actions/upload-artifact@v4
58+
with:
59+
retention-days: 1
60+
name: schema-spdx
61+
path: schema/spdx.*
62+
if-no-files-found: error
63+
pullrequest:
64+
name: Pull-request Changes
65+
runs-on: ubuntu-latest
66+
needs: [ 'update' ]
67+
if: ${{ needs.update.outputs.changed == 'true' }}
68+
permissions:
69+
contents: write # push commits
70+
pull-requests: write # create pullrequests
71+
env:
72+
SB_VERSION: ${{ needs.update.outputs.version }}
73+
SB_BRANCH: ${{ github.ref_name }}_update-spdx/${{ needs.update.outputs.version }}
74+
steps:
75+
- name: Checkout
76+
# see https://github.com/actions/checkout
77+
uses: actions/checkout@v5
78+
with:
79+
ref: ${{ github.ref_name }}
80+
- name: Switch branch
81+
id: branch
82+
run: |
83+
set -eux
84+
git remote set-branches origin "$SB_BRANCH"
85+
if git ls-remote --exit-code --heads origin "$SB_BRANCH"
86+
then
87+
echo "existed=true" >> $GITHUB_OUTPUT
88+
git fetch --depth=1 origin "$SB_BRANCH"
89+
git checkout -b "$SB_BRANCH" "origin/$SB_BRANCH"
90+
else
91+
echo "existed=false" >> $GITHUB_OUTPUT
92+
git checkout -b "$SB_BRANCH"
93+
fi
94+
- name: Fetch changes
95+
# https://github.com/actions/download-artifact
96+
uses: actions/download-artifact@v5
97+
with:
98+
name: schema-spdx
99+
path: schema
100+
- name: Commit and push
101+
run: |
102+
set -eux
103+
if git diff --quiet -- 'schema/spdx.*'
104+
then
105+
echo "branch up-to-date"
106+
exit 0
107+
fi
108+
git config user.name 'spdx-license-bumber[bot]'
109+
git config user.email '[email protected]'
110+
git add -A schema
111+
git commit -s -m "feat: bump SPDX licenses $SB_VERSION"
112+
git push origin "$SB_BRANCH"
113+
- name: Pull request
114+
if: ${{ steps.branch.outputs.existed == 'false' }}
115+
run: >
116+
gh pr create
117+
--title "feat: bump SPDX Licenses $SB_VERSION"
118+
--body "$SB_VERSION"
119+
--base "$GITHUB_REF_NAME"
120+
--head "$SB_BRANCH"
121+
env:
122+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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
}

tools/updateSpdx.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ this_dir="$(dirname "$this")"
66
project_root="$(dirname "$this_dir")"
77
schema_dir="$project_root/schema"
88

9+
cd "$this_dir"
910
mvn clean \
1011
compile \
1112
exec:java -Dexec.mainClass='org.cyclonedx.tools.SpdxXsdGenerator' \

0 commit comments

Comments
 (0)