diff --git a/.github/badges/code_issues.svg b/.github/badges/code_issues.svg deleted file mode 100644 index f7c6c0de3..000000000 --- a/.github/badges/code_issues.svg +++ /dev/null @@ -1 +0,0 @@ -code issuescode issues44 \ No newline at end of file diff --git a/.github/workflows/code-analysis-latest.yml b/.github/workflows/code-analysis-latest.yml new file mode 100644 index 000000000..8141a60d1 --- /dev/null +++ b/.github/workflows/code-analysis-latest.yml @@ -0,0 +1,73 @@ +name: Code analysis (latest MATLAB) + +# Runs the same check as code-analysis.yml on the latest MATLAB +# release. code-analysis.yml uses a fixed release so that its result, and +# the README badge, change only when the code changes. A failure here means +# a newer release reports issues that the fixed release does not: resolve +# them, then raise matlab_release in code-analysis.yml. +# +# The analysis is skipped while the latest release is the one pinned in +# code-analysis.yml, because that workflow already covers it. + +on: + # Weekly, Monday 04:00 UTC + schedule: + - cron: '0 4 * * 1' + workflow_dispatch: + inputs: + force: + description: Run the analysis even if the latest release is the pinned release + type: boolean + default: false + +jobs: + compare-releases: + name: Compare latest and pinned MATLAB release + runs-on: ubuntu-latest + outputs: + latest_release: ${{ steps.compare.outputs.latest_release }} + run_analysis: ${{ steps.compare.outputs.run_analysis }} + steps: + - name: Check out code-analysis.yml + uses: actions/checkout@v6 + with: + sparse-checkout: .github/workflows/code-analysis.yml + sparse-checkout-cone-mode: false + + # setup-matlab resolves "latest" from the same MathWorks URL, which + # returns a lowercase release name such as r2026b + - name: Compare releases + id: compare + env: + FORCE: ${{ inputs.force || false }} + run: | + latest=$(curl -fsS --retry 3 https://ssd.mathworks.com/supportfiles/ci/matlab-release/v0/latest \ + | tr '[:upper:]' '[:lower:]') + pinned=$(grep -oE '^[[:space:]]*matlab_release:[[:space:]]*R[0-9]{4}[ab]' .github/workflows/code-analysis.yml \ + | grep -oE 'R[0-9]{4}[ab]' || true) + if [[ -z "$pinned" ]]; then + echo "::error::No matlab_release of the form R2026b found in code-analysis.yml" + exit 1 + fi + pinned=$(echo "$pinned" | tr '[:upper:]' '[:lower:]') + + if [[ "$latest" != "$pinned" || "$FORCE" == "true" ]]; then + runAnalysis=true + else + runAnalysis=false + fi + + echo "latest_release=$latest" >> "$GITHUB_OUTPUT" + echo "run_analysis=$runAnalysis" >> "$GITHUB_OUTPUT" + echo "Latest release: $latest, pinned release: $pinned, run analysis: $runAnalysis" \ + | tee -a "$GITHUB_STEP_SUMMARY" + + code-analysis: + name: Code analysis (latest MATLAB) + needs: compare-releases + if: needs.compare-releases.outputs.run_analysis == 'true' + uses: ehennestad/matbox-actions/.github/workflows/check-code-workflow.yml@v1 + with: + source_directory: "." + matlab_release: ${{ needs.compare-releases.outputs.latest_release }} + update_badges: false diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml new file mode 100644 index 000000000..20511ed30 --- /dev/null +++ b/.github/workflows/code-analysis.yml @@ -0,0 +1,35 @@ +name: Code analysis + +# Runs MATLAB Code Analyzer on every .m file in the repository. The job +# fails when an issue of severity warning or error remains (see +# tools/tasks/codecheckToolbox.m). The README shows this workflow's status +# as the code analysis badge. + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + code-analysis: + name: Code analysis + uses: ehennestad/matbox-actions/.github/workflows/check-code-workflow.yml@v1 + with: + # Check the whole repository, including the tools folder + source_directory: "." + + # A fixed release, so the result changes only when the code changes. + # The ignoredFiles setting in code/resources/codeAnalyzerConfiguration.json + # needs R2026b or later. + matlab_release: R2026b + matlab_use_cache: true + + # The README shows this workflow's status, so no badge file is + # generated or committed + update_badges: false diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml index 064187242..ffd6b53a5 100644 --- a/.github/workflows/run_tests.yml +++ b/.github/workflows/run_tests.yml @@ -56,21 +56,6 @@ jobs: release: ${{ matrix.MATLABVersion }} cache: true - # Check for MATLAB code issues in the project. - - name: Check for MATLAB code issues - if: matrix.MATLABVersion == env.LatestMATLABVersion - uses: matlab-actions/run-command@v3 - with: - command: addpath(genpath("tools")); codecheckToolbox() - - # Upload code issues report - - name: Upload SARIF file - if: matrix.MATLABVersion == env.LatestMATLABVersion - uses: github/codeql-action/upload-sarif@v3 - with: - # Path to SARIF file relative to the root of the repository - sarif_file: docs/reports/code_issues.sarif - # The interoperability test builds the same collection with the # Python reference implementation and compares the two exports. A # pull request runs against a pinned release, so it cannot go red @@ -111,26 +96,6 @@ jobs: addpath(genpath("tools")); testToolbox() - # Commit the updated code issues badge (if changed). - # Push events only, so badge commits land on main and nowhere else: - # committing them on a pull_request event rewrites the PR head branch - # and breaks any stack built on top of it. - - name: Commit svg badges if updated - if: matrix.MATLABVersion == env.LatestMATLABVersion && github.event_name == 'push' - continue-on-error: true - run: | - git config user.name "${{ github.workflow }} by ${{ github.actor }}" - git config user.email "${{ secrets.COMMIT_EMAIL_ADDRESS }}" - git fetch - - if [[ $(git add .github/badges/* --dry-run | wc -l) -gt 0 ]]; then - git add .github/badges/* - git commit -m "Update code issues badge" - git push -f - else - echo "Nothing to commit" - fi - - name: Upload code coverage report to Codecov (https://app.codecov.io/gh/openMetadataInitiative/openMINDS_MATLAB) if: matrix.MATLABVersion == env.LatestMATLABVersion uses: codecov/codecov-action@v6 diff --git a/README.md b/README.md index 10f713a58..701711710 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ [![View openMINDS_MATLAB on File Exchange](https://github.com/openMetadataInitiative/openMINDS_MATLAB/blob/gh-badges/.github/badges/matlab-file-exchange.svg)](https://se.mathworks.com/matlabcentral/fileexchange/134212-openminds_matlab) [![Run tests](https://github.com/openMetadataInitiative/openMINDS_MATLAB/actions/workflows/run_tests.yml/badge.svg)](https://github.com/openMetadataInitiative/openMINDS_MATLAB/actions/workflows/run_tests.yml) [![codecov](https://codecov.io/gh/openMetadataInitiative/openMINDS_MATLAB/graph/badge.svg?token=FTD5FHZSFA)](https://codecov.io/gh/openMetadataInitiative/openMINDS_MATLAB) -[![MATLAB Code Issues](.github/badges/code_issues.svg)](https://github.com/openMetadataInitiative/openMINDS_MATLAB/security/code-scanning) +[![Code analysis](https://github.com/openMetadataInitiative/openMINDS_MATLAB/actions/workflows/code-analysis.yml/badge.svg)](https://github.com/openMetadataInitiative/openMINDS_MATLAB/actions/workflows/code-analysis.yml)

Installation • diff --git a/code/resources/codeAnalyzerConfiguration.json b/code/resources/codeAnalyzerConfiguration.json new file mode 100644 index 000000000..fdb410844 --- /dev/null +++ b/code/resources/codeAnalyzerConfiguration.json @@ -0,0 +1,8 @@ +{ + "name": "openMINDS_MATLAB", + "description": "Excludes generated classes where the openMINDS type and one of its properties share a name, which Code Analyzer reports as REDEF.", + "ignoredFiles": [ + "generated/resources/v1.0/types/+openminds/+core/+miscellaneous/URL.m", + "generated/resources/v2.0/types/+openminds/+core/+miscellaneous/URL.m" + ] +} diff --git a/tools/tasks/codecheckToolbox.m b/tools/tasks/codecheckToolbox.m index 1d123ab94..e8418a5ed 100644 --- a/tools/tasks/codecheckToolbox.m +++ b/tools/tasks/codecheckToolbox.m @@ -1,9 +1,35 @@ -function issues = codecheckToolbox() +function issues = codecheckToolbox(options) % codecheckToolbox - Identify code issues for openMINDS_MATLAB toolbox +% +% issues = codecheckToolbox() checks every .m file in the repository and +% throws an error if an issue of severity warning or error remains. +% +% issues = codecheckToolbox(FoldersToCheck=folders) checks only the .m +% files in the given folders, which are relative to the repository root. +% The ehennestad/matbox-actions check-code action passes this argument. +% +% The ignoredFiles setting in code/resources/codeAnalyzerConfiguration.json +% lists files that Code Analyzer skips (MATLAB R2026b or later). + + arguments + options.FoldersToCheck (1,:) string = string.empty + end ommtools.installMatBox("commit") projectRootDirectory = ommtools.projectdir(); - + + if isempty(options.FoldersToCheck) + foldersToCheck = string.empty; + else + foldersToCheck = fullfile(projectRootDirectory, options.FoldersToCheck); + end + + % ShowIssues prints the table of remaining issues, so a failed CI run + % shows which files to fix. The status of the Code analysis workflow is + % the README badge, so no badge file is created. issues = matbox.tasks.codecheckToolbox(projectRootDirectory, ... - "CreateBadge", true); + FoldersToCheck=foldersToCheck, ... + CreateBadge=false, ... + RequireIssuesResolved=true, ... + ShowIssues=true); end diff --git a/tools/tests/oneOffs/personArray.m b/tools/tests/oneOffs/personArray.m index 180b91a06..d2f598168 100644 --- a/tools/tests/oneOffs/personArray.m +++ b/tools/tests/oneOffs/personArray.m @@ -23,8 +23,8 @@ "contactInformation", contacts(person.email) }; if ommtest.oneoffs.currentSchemaMajorVersion() >= 5 - personArguments = [personArguments, { ... - "preferredName", join([person.givenName, person.familyName], " ")}]; + preferredName = join([person.givenName, person.familyName], " "); + personArguments = [personArguments, {"preferredName", preferredName}]; %#ok end persons(end+1) = openminds.core.Person(personArguments{:}); %#ok