|
| 1 | +name: Validate YAML against Schema |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | +# - 'Published/**/rule.yml' |
| 7 | + - 'Unpublished/**/rule.yml' |
| 8 | + types: [opened, synchronize, reopened] |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + paths_override: |
| 12 | + description: > |
| 13 | + Space-separated list of rule YAML files to validate |
| 14 | + (e.g. "Published/CORE-000001/rule.yml"). Leave blank to validate |
| 15 | + all changed rule files detected from the last commit. |
| 16 | + required: false |
| 17 | + default: '' |
| 18 | + |
| 19 | +# Only one run per PR branch at a time; cancel superseded runs. |
| 20 | +concurrency: |
| 21 | + group: schema-validate-${{ github.ref }} |
| 22 | + cancel-in-progress: true |
| 23 | + |
| 24 | +jobs: |
| 25 | + validate-schema: |
| 26 | + name: Validate rule YAML against JSON Schema |
| 27 | + runs-on: ubuntu-latest |
| 28 | + permissions: |
| 29 | + contents: read |
| 30 | + pull-requests: write |
| 31 | + |
| 32 | + env: |
| 33 | + # draft/2020-12 schema — switch to rule-merged if you need $defs inlined |
| 34 | + SCHEMA_URL: >- |
| 35 | + https://raw.githubusercontent.com/cdisc-org/cdisc-rules-engine/refs/heads/main/resources/schema/rule/CORE-base.json |
| 36 | +
|
| 37 | + steps: |
| 38 | + # ----------------------------------------------------------------------- |
| 39 | + # 1. Checkout |
| 40 | + # ----------------------------------------------------------------------- |
| 41 | + - name: Checkout repository |
| 42 | + uses: actions/checkout@v6 |
| 43 | + with: |
| 44 | + fetch-depth: 0 |
| 45 | + |
| 46 | + # ----------------------------------------------------------------------- |
| 47 | + # 2. Set up Python |
| 48 | + # ----------------------------------------------------------------------- |
| 49 | + - name: Set up Python 3.12 |
| 50 | + uses: actions/setup-python@v6 |
| 51 | + with: |
| 52 | + python-version: '3.12' |
| 53 | + |
| 54 | + # ----------------------------------------------------------------------- |
| 55 | + # 3. Install validation dependencies |
| 56 | + # jsonschema 4.x ships full draft/2020-12 support out of the box. |
| 57 | + # ----------------------------------------------------------------------- |
| 58 | + - name: Install Python dependencies |
| 59 | + run: | |
| 60 | + python -m pip install --upgrade pip |
| 61 | + pip install "jsonschema[format-nongpl]>=4.18" "PyYAML>=6.0" |
| 62 | +
|
| 63 | + # ----------------------------------------------------------------------- |
| 64 | + # 4. Detect changed rule YAML files |
| 65 | + # ----------------------------------------------------------------------- |
| 66 | + - name: Detect changed rule files |
| 67 | + id: changed |
| 68 | + run: | |
| 69 | + if [ -n "${{ github.event.inputs.paths_override }}" ]; then |
| 70 | + # Manual override |
| 71 | + FILES="${{ github.event.inputs.paths_override }}" |
| 72 | + elif [ "${{ github.event_name }}" = "pull_request" ]; then |
| 73 | + FILES=$(git diff --name-only \ |
| 74 | + "origin/${{ github.base_ref }}...HEAD" \ |
| 75 | + -- 'Published/**/rule.yml' 'Unpublished/**/rule.yml' \ |
| 76 | + | tr '\n' ' ') |
| 77 | + else |
| 78 | + # workflow_dispatch without override — validate all rule files |
| 79 | + FILES=$(find Published Unpublished -name "rule.yml" | tr '\n' ' ') |
| 80 | + fi |
| 81 | +
|
| 82 | + echo "files=$FILES" >> "$GITHUB_OUTPUT" |
| 83 | + echo "Detected rule files: $FILES" |
| 84 | +
|
| 85 | + if [ -z "$FILES" ]; then |
| 86 | + echo "no_files=true" >> "$GITHUB_OUTPUT" |
| 87 | + else |
| 88 | + echo "no_files=false" >> "$GITHUB_OUTPUT" |
| 89 | + fi |
| 90 | +
|
| 91 | + # ----------------------------------------------------------------------- |
| 92 | + # 5. Run schema validation |
| 93 | + # ----------------------------------------------------------------------- |
| 94 | + - name: Validate YAML against schema |
| 95 | + id: validate |
| 96 | + if: steps.changed.outputs.no_files == 'false' |
| 97 | + continue-on-error: true |
| 98 | + run: | |
| 99 | + python .github/scripts/validate_yaml_schema.py \ |
| 100 | + "${{ env.SCHEMA_URL }}" \ |
| 101 | + ${{ steps.changed.outputs.files }} |
| 102 | +
|
| 103 | + - name: No rule files changed |
| 104 | + if: steps.changed.outputs.no_files == 'true' |
| 105 | + run: | |
| 106 | + echo "No rule YAML files were changed — nothing to validate." |
| 107 | + echo "## Schema Validation" >> "$GITHUB_STEP_SUMMARY" |
| 108 | + echo "No \`rule.yml\` files were changed in this PR." >> "$GITHUB_STEP_SUMMARY" |
| 109 | +
|
| 110 | + # ----------------------------------------------------------------------- |
| 111 | + # 6. Upload report artifact |
| 112 | + # ----------------------------------------------------------------------- |
| 113 | + - name: Upload schema validation report |
| 114 | + if: always() && steps.changed.outputs.no_files == 'false' |
| 115 | + uses: actions/upload-artifact@v7 |
| 116 | + with: |
| 117 | + name: schema-validation-report |
| 118 | + path: schema_validation_report.md |
| 119 | + if-no-files-found: warn |
| 120 | + |
| 121 | + # ----------------------------------------------------------------------- |
| 122 | + # 7. Post report as PR comment |
| 123 | + # ----------------------------------------------------------------------- |
| 124 | + - name: Post report to PR |
| 125 | + if: > |
| 126 | + always() && |
| 127 | + github.event_name == 'pull_request' && |
| 128 | + steps.changed.outputs.no_files == 'false' |
| 129 | + uses: actions/github-script@v9 |
| 130 | + with: |
| 131 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 132 | + script: | |
| 133 | + const fs = require('fs'); |
| 134 | + const reportPath = 'schema_validation_report.md'; |
| 135 | + const runUrl = `${context.payload.repository.html_url}/actions/runs/${context.runId}`; |
| 136 | +
|
| 137 | + let body = '## Schema Validation Results\n\n'; |
| 138 | +
|
| 139 | + if (fs.existsSync(reportPath)) { |
| 140 | + const content = fs.readFileSync(reportPath, 'utf8'); |
| 141 | + body += '<details>\n<summary><strong>Click to expand</strong></summary>\n\n'; |
| 142 | + body += content; |
| 143 | + body += '\n</details>\n'; |
| 144 | + } else { |
| 145 | + body += '_No schema validation report was generated._\n'; |
| 146 | + } |
| 147 | +
|
| 148 | + body += `\n[View workflow run](${runUrl})`; |
| 149 | +
|
| 150 | + const marker = 'Schema Validation Results'; |
| 151 | + const { data: comments } = await github.rest.issues.listComments({ |
| 152 | + owner: context.repo.owner, |
| 153 | + repo: context.repo.repo, |
| 154 | + issue_number: context.issue.number, |
| 155 | + }); |
| 156 | +
|
| 157 | + const existing = comments.find( |
| 158 | + c => c.user.type === 'Bot' && c.body.includes(marker) |
| 159 | + ); |
| 160 | +
|
| 161 | + if (existing) { |
| 162 | + await github.rest.issues.updateComment({ |
| 163 | + owner: context.repo.owner, |
| 164 | + repo: context.repo.repo, |
| 165 | + comment_id: existing.id, |
| 166 | + body, |
| 167 | + }); |
| 168 | + } else { |
| 169 | + await github.rest.issues.createComment({ |
| 170 | + owner: context.repo.owner, |
| 171 | + repo: context.repo.repo, |
| 172 | + issue_number: context.issue.number, |
| 173 | + body, |
| 174 | + }); |
| 175 | + } |
| 176 | +
|
| 177 | + # ----------------------------------------------------------------------- |
| 178 | + # 8. Write report to workflow summary |
| 179 | + # ----------------------------------------------------------------------- |
| 180 | + - name: Write report to workflow summary |
| 181 | + if: always() && steps.changed.outputs.no_files == 'false' |
| 182 | + run: | |
| 183 | + [ -f schema_validation_report.md ] \ |
| 184 | + && cat schema_validation_report.md >> "$GITHUB_STEP_SUMMARY" \ |
| 185 | + || true |
| 186 | +
|
| 187 | + # ----------------------------------------------------------------------- |
| 188 | + # 9. Fail the job if validation found errors |
| 189 | + # ----------------------------------------------------------------------- |
| 190 | + - name: Check validation outcome |
| 191 | + if: steps.validate.outcome == 'failure' |
| 192 | + run: | |
| 193 | + echo "::error::Schema validation failed — see the report above." |
| 194 | + exit 1 |
0 commit comments