diff --git a/.github/workflows/check_schemas.yaml b/.github/workflows/check_schemas.yaml index 6af7747f..d1c6148a 100644 --- a/.github/workflows/check_schemas.yaml +++ b/.github/workflows/check_schemas.yaml @@ -42,4 +42,4 @@ jobs: env: ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} run: | - python tests/validate_schemas.py $ALL_CHANGED_FILES + hed_validate_schemas $ALL_CHANGED_FILES diff --git a/.github/workflows/update_and_convert_schemas.yaml b/.github/workflows/update_and_convert_schemas.yaml new file mode 100644 index 00000000..f95b0e61 --- /dev/null +++ b/.github/workflows/update_and_convert_schemas.yaml @@ -0,0 +1,89 @@ +name: Validate and update schemas + +on: + pull_request: + types: [opened, synchronize, reopened] + branches: ["*"] + +jobs: + process-changed-files: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # OR "2" -> To retrieve the preceding commit. + ref: ${{ github.head_ref }} + + # This part might need revision. This avoids the extra merge commit. + - name: Fetch updates from the target branch + run: | + git config --global user.name 'GitHub Actions' + git config --global user.email 'actions@github.com' + git fetch origin ${{ github.event.pull_request.base.ref }} + git rebase origin/${{ github.event.pull_request.base.ref }} + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Determine if branch is main + run: | + if [ "${{ github.base_ref }}" = "main" ]; then + echo "SINCE_LAST_REMOTE_COMMIT=false" >> $GITHUB_ENV + else + echo "SINCE_LAST_REMOTE_COMMIT=true" >> $GITHUB_ENV + fi + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v41 + with: + since_last_remote_commit: ${{ env.SINCE_LAST_REMOTE_COMMIT }} + + - name: List all changed files + env: + ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + run: | + for file in "$ALL_CHANGED_FILES"; do + echo "$file was changed" + done + + - name: Install dependencies + run: pip install git+https://github.com/hed-standard/hed-python.git@develop + + - name: Update schemas + env: + ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} + run: | + SOURCE_BRANCH="${{ github.head_ref }}" + TARGET_BRANCH="${{ github.base_ref }}" + echo "Source Branch: $SOURCE_BRANCH" + echo "Target Branch: $TARGET_BRANCH" + if [ "$SOURCE_BRANCH" = "develop" ] && [ "$TARGET_BRANCH" = "main" ]; then + echo "Error: Source branch 'develop' is not allowed for this operation." + exit 1 + elif [ "$TARGET_BRANCH" = "main" ]; then + hed_update_schemas $ALL_CHANGED_FILES --set-ids + else + hed_update_schemas $ALL_CHANGED_FILES + fi + + - name: Check for changes + id: check-changes + run: | + if ! git diff --quiet || ! git diff --cached --quiet || git ls-files --others --exclude-standard | grep -q .; then + echo "changes_exist=true" >> $GITHUB_ENV + else + echo "changes_exist=false" >> $GITHUB_ENV + fi + + - name: Commit and push changes + if: env.changes_exist == 'true' + run: | + git add . + git commit -m "Automatic file updates" + git push --force-with-lease origin HEAD:refs/heads/${{ github.head_ref }} + + diff --git a/tests/validate_schemas.py b/tests/validate_schemas.py deleted file mode 100644 index fe22969d..00000000 --- a/tests/validate_schemas.py +++ /dev/null @@ -1,56 +0,0 @@ -import sys -from hed.schema import load_schema, from_string, load_schema_version -from hed.errors import get_printable_issue_string, HedFileError, SchemaWarnings - - -def main(arg_list=None): - # Trigger a local cache hit - _ = load_schema_version("8.2.0") - validation_issues = [] - saving_failures = [] - if not arg_list: - arg_list = sys.argv[1:] - - for file_path in arg_list: # sys.argv[1:] contains all the arguments passed to the script - print(file_path) - try: - if file_path.endswith(".xml") or file_path.endswith(".mediawiki"): - base_schema = load_schema(file_path) - issues = base_schema.check_compliance() - issues = [issue for issue in issues if issue["code"] != SchemaWarnings.SCHEMA_PRERELEASE_VERSION_USED] - if issues: - validation_issues.extend(issues) - print(get_printable_issue_string(issues, title=file_path)) - - mediawiki_string = base_schema.get_as_mediawiki_string() - reloaded_schema = from_string(mediawiki_string, schema_format=".mediawiki") - - if reloaded_schema != base_schema: - error_text = f"Failed to reload {file_path} as mediawiki. There is either a problem with the source file, or the saving/loading code." - saving_failures.append(error_text) - print(error_text) - - xml_string = base_schema.get_as_xml_string() - reloaded_schema = from_string(xml_string, schema_format=".xml") - - if reloaded_schema != base_schema: - error_text = f"Failed to reload {file_path} as xml. There is either a problem with the source file, or the saving/loading code." - saving_failures.append(error_text) - print(error_text) - except HedFileError as e: - print(f"Saving/loading error: {e.message}") - if e.issues: - print(get_printable_issue_string(e.issues, title=file_path)) - continue - - # if saving_failures: - # for issue in saving_failures: - # print(issue) - - if validation_issues or saving_failures: - return 1 - return 0 - - -if __name__ == "__main__": - exit(main())