-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incompatible changes will lead to added labels and failing actions (#580
- Loading branch information
1 parent
552f60a
commit 49bb196
Showing
5 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
|