Skip to content

Commit 7208b89

Browse files
authored
Merge pull request #89 from rudnerbjoern/dev
Update workflow and settings for improved schema processing
2 parents 2f69786 + dc0a772 commit 7208b89

File tree

3 files changed

+109
-71
lines changed

3 files changed

+109
-71
lines changed

.github/workflows/combine-schema.v3.2.yml

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on:
1515
paths:
1616
- '3.2/**'
1717
- 'scripts/**'
18-
- '.github/workflows/combine-schema.yml'
18+
- '.github/workflows/combine-schema.v3.2.yml'
1919

2020
jobs:
2121
combine-schema:
@@ -30,61 +30,79 @@ jobs:
3030
steps:
3131
- name: Checkout repository
3232
uses: actions/checkout@v4
33+
with:
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Set up Git
37+
run: |
38+
git config user.name "github-actions"
39+
git config user.email "[email protected]"
3340
3441
- name: Set up Python
3542
uses: actions/setup-python@v5
3643
with:
3744
python-version: '3.x'
3845

39-
- name: Install Python dependencies
40-
run: pip install -r scripts/requirements.txt
41-
42-
- name: Install xmlstarlet
43-
run: sudo apt-get update && sudo apt-get install -y xmlstarlet
44-
45-
- name: Combine XSD schema into single file
46-
run: python scripts/combine_schema.py $INPUT_FILE $OUTPUT_FILE
47-
48-
- name: Beautify and normalize XSD with xmlstarlet
46+
- name: Install dependencies
4947
run: |
50-
xmlstarlet fo --omit-decl $OUTPUT_FILE > tmp.xml && mv tmp.xml $OUTPUT_FILE
51-
xmlstarlet ed -P -L -u "//xs:documentation/text()" -x "normalize-space(.)" $OUTPUT_FILE
52-
rm -f tmp.xml
48+
sudo apt-get update && sudo apt-get install -y xmlstarlet
49+
pip install -r scripts/requirements.txt
5350
54-
- name: Set artifact path
55-
id: vars
56-
run: echo "path=$OUTPUT_FILE" >> "$GITHUB_OUTPUT"
51+
- name: Combine and clean XSD schema
52+
run: |
53+
set -e
54+
echo "Combining input schema: $INPUT_FILE"
55+
python scripts/combine_schema.py $INPUT_FILE $OUTPUT_FILE
5756
58-
- name: Upload merged XSD as artifact
59-
uses: actions/upload-artifact@v4
60-
with:
61-
name: combined-xsd
62-
path: ${{ steps.vars.outputs.path }}
57+
echo "Cleaning and formatting schema file..."
58+
xmlstarlet ed -N xs="http://www.w3.org/2001/XMLSchema" \
59+
-u '//xs:documentation' -x 'normalize-space(.)' "$OUTPUT_FILE" | \
60+
xmlstarlet fo > "${OUTPUT_FILE}.tmp"
61+
mv "${OUTPUT_FILE}.tmp" "$OUTPUT_FILE"
6362
64-
- name: Commit combined schema
65-
id: commit
63+
- name: Show file content before pull request
6664
run: |
67-
git config --global user.name "GitHub Action"
68-
git config --global user.email "[email protected]"
69-
git checkout -b auto/schema-update
70-
git add $OUTPUT_FILE || echo "No changes to add"
71-
git status
72-
if ! git diff --cached --quiet; then
73-
git commit -m "Auto-update combined schema"
74-
git push origin auto/schema-update
75-
echo "changes_present=true" >> "$GITHUB_OUTPUT"
76-
else
77-
echo "No changes to commit."
78-
echo "changes_present=false" >> "$GITHUB_OUTPUT"
79-
fi
65+
echo "Head:"
66+
head -n 20 "$OUTPUT_FILE"
67+
echo "Tail:"
68+
tail -n 20 "$OUTPUT_FILE"
8069
81-
- name: Create pull request with updated schema
82-
if: success() && steps.commit.outputs.changes_present == 'true'
83-
uses: peter-evans/create-pull-request@v5
70+
- name: Create pull request
71+
id: create-pull-request
72+
uses: peter-evans/create-pull-request@v6
8473
with:
8574
token: ${{ secrets.GITHUB_TOKEN }}
86-
commit-message: Auto-update combined schema
87-
branch: auto/schema-update
88-
title: 'Auto-update combined schema'
89-
body: 'This PR was automatically created by the Combine XSD Schema workflow.'
75+
commit-message: "Auto: Update itop_design.xsd"
76+
committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
77+
author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>
78+
branch: auto/update-schema
9079
base: main
80+
title: "Auto: Update itop_design.xsd"
81+
body: "This PR was automatically created by a GitHub Action."
82+
labels: auto-update
83+
draft: false
84+
delete-branch: true
85+
86+
- name: Enable automerge
87+
uses: "peter-evans/enable-pull-request-automerge@v3"
88+
with:
89+
token: ${{ secrets.GITHUB_TOKEN }}
90+
pull-request-number: ${{ steps.create-pull-request.outputs.pull-request-number }}
91+
merge-method: squash
92+
93+
- name: Delete branch after merge
94+
if: ${{ steps.create-pull-request.outputs.pull-request-operation == 'updated' || steps.create-pull-request.outputs.pull-request-operation == 'created' }}
95+
run: |
96+
echo "Waiting for merge..."
97+
for i in {1..30}; do
98+
MERGED_AT=$(gh pr view ${{ steps.create-pull-request.outputs.pull-request-url }} --json mergedAt --jq .mergedAt)
99+
if [ "$MERGED_AT" != "null" ]; then
100+
echo "PR merged at $MERGED_AT, deleting branch..."
101+
git push origin --delete auto/update-schema
102+
break
103+
fi
104+
echo "Not merged yet, retrying in 10s... ($i/30)"
105+
sleep 10
106+
done
107+
env:
108+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dashlet",
1010
"dashlets",
1111
"datamodels",
12+
"evans",
1213
"extkey",
1314
"fileref",
1415
"finalclass",

scripts/combine_schema.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
from lxml import etree
2+
from datetime import datetime
23
import os
34
import sys
5+
import tempfile
6+
import shutil
47

58
XSD_NS = "http://www.w3.org/2001/XMLSchema"
69
NSMAP = {"xs": XSD_NS}
710
INCLUDE_TAG = f"{{{XSD_NS}}}include"
811

9-
if len(sys.argv) != 3:
10-
print("Usage: python combine_schema.py <input_file> <output_file>")
11-
sys.exit(1)
12-
13-
MASTER_FILE = sys.argv[1]
14-
OUTPUT_FILE = sys.argv[2]
15-
1612
included_files = set()
1713

1814
def resolve_includes(tree, base_path):
@@ -24,44 +20,67 @@ def resolve_includes(tree, base_path):
2420
continue
2521
included_path = os.path.normpath(os.path.join(base_path, href))
2622
if included_path in included_files:
23+
print(f"Already included: {included_path}")
2724
root.remove(include)
2825
continue
29-
print(f"Including: {included_path}")
26+
print(f"Parsing and including: {included_path}")
3027
included_tree = etree.parse(included_path, etree.XMLParser(remove_blank_text=True))
3128
resolve_includes(included_tree, os.path.dirname(included_path))
3229
included_root = included_tree.getroot()
3330
for child in included_root:
34-
root.append(child)
31+
if child.tag != INCLUDE_TAG:
32+
root.append(child)
3533
root.remove(include)
3634
included_files.add(included_path)
3735

38-
os.makedirs(os.path.dirname(OUTPUT_FILE), exist_ok=True)
36+
def main():
37+
if len(sys.argv) != 3:
38+
print("Usage: python combine_schema.py <input_file> <output_file>")
39+
sys.exit(1)
3940

40-
parser = etree.XMLParser(remove_blank_text=True)
41-
master_tree = etree.parse(MASTER_FILE, parser)
42-
resolve_includes(master_tree, os.path.dirname(MASTER_FILE))
41+
input_file = sys.argv[1]
42+
output_file = sys.argv[2]
4343

44-
# Add project comment before root
45-
root = master_tree.getroot()
46-
comment = etree.Comment(
47-
"""
44+
print(f"Input file: {input_file}")
45+
print(f"Output file: {output_file}")
46+
47+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
48+
49+
parser = etree.XMLParser(remove_blank_text=True)
50+
print("Parsing master schema...")
51+
master_tree = etree.parse(input_file, parser)
52+
resolve_includes(master_tree, os.path.dirname(input_file))
53+
54+
root = master_tree.getroot()
55+
comment = etree.Comment(
56+
f"""
4857
This schema file was automatically generated from modular XSD components.
4958
Provided by the iTop-schema project by Björn Rudner.
5059
5160
Project website: https://rudnerbjoern.github.io/iTop-schema/
5261
GitHub repository: https://github.com/rudnerbjoern/iTop-schema
5362
63+
Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} UTC
64+
5465
Use this file to validate your iTop datamodels with confidence and consistency.
5566
"""
56-
)
57-
root.addprevious(comment)
67+
)
68+
root.addprevious(comment)
69+
70+
# Write to a temporary file first
71+
with tempfile.NamedTemporaryFile("wb", delete=False) as tmp:
72+
temp_path = tmp.name
73+
print(f"Writing combined schema to temporary file: {temp_path}")
74+
master_tree.write(
75+
tmp,
76+
pretty_print=True,
77+
xml_declaration=True,
78+
encoding="UTF-8"
79+
)
5880

59-
# Write formatted output
60-
etree.ElementTree(root).write(
61-
OUTPUT_FILE,
62-
pretty_print=True,
63-
xml_declaration=True,
64-
encoding="UTF-8"
65-
)
81+
print(f"Moving temporary file to final destination: {output_file}")
82+
shutil.move(temp_path, output_file)
83+
print(f"✅ Combined schema written to: {output_file}")
6684

67-
print(f"✅ Combined schema written to: {OUTPUT_FILE}")
85+
if __name__ == "__main__":
86+
main()

0 commit comments

Comments
 (0)