Skip to content

Commit

Permalink
Incompatible changes will lead to added labels and failing actions (#580
Browse files Browse the repository at this point in the history
)

Co-authored-by: Michael Schneider <micha.schneider@sap.com>
  • Loading branch information
albertmink and schneidermic0 authored Mar 19, 2024
1 parent 552f60a commit 49bb196
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/compatibility/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
59 changes: 59 additions & 0 deletions .github/compatibility/json-schema-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const difftool = require('json-schema-diff-validator')
const exec = require('@actions/exec');
const core = require('@actions/core');
const {readFileSync} = require('node:fs');


let getChangedSchema = async () => {
let stdout = '';
const options = {
listeners: {
stdout: (data) => {
stdout += data.toString();
},
stderr: (data) => {
core.error(data.toString());
}
}
};

await exec.exec('git diff --name-only remotes/origin/main..HEAD', [], options);

const pattern = new RegExp('file-formats/[a-z]{4}/[a-z]+-v[0-9]+\.json$', 'i');
const lines = stdout.split("\n");
const changedSchema = lines.filter(line => pattern.test(line));

return changedSchema;
}

const processFile = async (file) => {
const dataNew = readFileSync(`../../${file}`, 'utf8');
const schemaNew = JSON.parse(dataNew);

try {
await exec.exec(`git checkout remotes/origin/main -- `, [file], { cwd: `../../`} );
} catch (error) {
core.info(`File ${file} is not known to main branch.`);
// file is not on main branch, so we continue and compare the file to itself (no harm)
}

try {
const dataOld = readFileSync(`../../${file}`, 'utf8');
const schemaOld = JSON.parse(dataOld);

difftool.validateSchemaCompatibility(schemaOld, schemaNew);
} catch (error) {
core.setFailed(error.toString());
}
}


async function run() {

const changedSchema = await getChangedSchema();
for (const schema of changedSchema) {
await processFile(schema);
}
}

run();
125 changes: 125 additions & 0 deletions .github/compatibility/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .github/compatibility/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "json-schema-compatible",
"version": "1.0.0",
"description": "checks for incompatible changes in JSON Schema",
"main": "json-schema-diff.js",
"scripts": {
"check": "node json-schema-diff"
},
"author": "",
"license": "ISC",
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1",
"json-schema-diff-validator": "^0.4.1"
}
}
58 changes: 58 additions & 0 deletions .github/workflows/json-compatibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Compatibility Check

on:
pull_request:

jobs:
compatible:
name: Is change incompatible
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
- name: Run script
id: check_incompatibility
run: |
cd .github/compatibility
npm ci
npm run check
- name: Add label PR on failure
if: failure() && steps.check_incompatibility.outcome == 'failure' # Only runs if your script failed.
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: 'incompatible-changes',
color: 'FF0000'
}).catch(err => console.log(`Label already exists`))
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['incompatible-changes']
})
github-token: ${{secrets.GITHUB_TOKEN}}

- name: Remove label PR on success
if: steps.check_incompatibility.outcome == 'success'
uses: actions/github-script@v7
with:
script: |
github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
name: 'incompatible-changes'
}).then(() => {
console.log('Label removed successfully!');
}).catch((error) => {
console.error('An error occurred while removing the label:', error);
})
github-token: ${{secrets.GITHUB_TOKEN}}

0 comments on commit 49bb196

Please sign in to comment.