diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml new file mode 100644 index 0000000..9011247 --- /dev/null +++ b/.github/workflows/checks.yml @@ -0,0 +1,57 @@ +# Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +name: CI Checks + +on: + push: + branches: + - main + pull_request: + workflow_dispatch: + workflow_call: + inputs: + rust-nightly-version: + description: 'Rust nightly version to use (YYYY-MM-DD), defaults to 2025-07-14 if not set' + required: false + type: string + python-version: + description: 'Python version to use for pre-commit environment' + required: false + type: string + pre-commit-version: + description: 'Version of pre-commit to install' + required: false + type: string + pre-commit-config-path: + description: 'Path to a custom .pre-commit-config.yml in the consumer repository' + required: false + type: string + license-config-path: + description: 'Path to a custom .licenserc.yml in the consumer repository' + required: false + type: string + +permissions: + contents: read + +jobs: + checks: + runs-on: ubuntu-latest + steps: + - name: Run checks + uses: eclipse-opensovd/cicd-workflows/pre-commit-action@init + with: + rust-nightly-version: ${{ inputs.rust-nightly-version }} + python-version: ${{ inputs.python-version }} + pre-commit-version: ${{ inputs.pre-commit-version }} + config-path: ${{ inputs.pre-commit-config-path }} + license-config-path: ${{ inputs.license-config-path }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f1c927 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +.vscode/ +.DS_Store diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 0000000..8bfa8a0 --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,8 @@ +# This is the list of the cicd-workflows's significant contributors. +# +# This does not necessarily list everyone who has contributed code, +# especially since many employees of one corporation may be contributing. +# To see the full list of contributors, see the revision history in +# source control. + +Alexander Mohr diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..137069b --- /dev/null +++ b/LICENSE @@ -0,0 +1,73 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + + (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + + You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + +To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..c94d5ba --- /dev/null +++ b/NOTICE @@ -0,0 +1,36 @@ +# Notices for Eclipse OpenSOVD + +This content is produced and maintained by the Eclipse OpenSOVD project. + +- [Project home](https://projects.eclipse.org/projects/automotive.opensovd) + +## Trademarks + +Eclipse, and the Eclipse Logo are registered trademarks of the Eclipse Foundation. + +## Copyright + +All content is the property of the respective authors or their employers. +For more information regarding authorship of content, please consult the +listed source code repository logs. + +## Declared Project Licenses + +This program and the accompanying materials are made available under the terms +of the Apache License Version 2.0 which is available at +. + +SPDX-License-Identifier: Apache-2.0 + +## Cryptography + +Content may contain encryption software. The country in which you are currently +may have restrictions on the import, possession, and use, and/or re-export to +another country, of encryption software. BEFORE using any encryption software, +please check the country's laws, regulations and policies concerning the import, +possession, or use, and re-export of encryption software, to see if this is +permitted. + +## Transitive dependencies + +This software includes transitive dependencies. While we strive to ensure that all included dependencies are compatible with the Apache License, Version 2.0, we cannot guarantee the licensing compliance of every transitive dependency. Users are advised to review the licenses of all dependencies to ensure full compliance with their legal requirements. diff --git a/README.md b/README.md index a4c16ca..b44cba7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,201 @@ -# cicd-workflows -Reusable GitHub Actions workflows for CI/CD automation +# Reusable GitHub Actions Workflows + +This repository contains **reusable GitHub Actions workflows** and **composite actions** designed to standardize CI/CD processes across multiple repositories in the Eclipse OpenSOVD project. + +## Features + +- 🔍 **Comprehensive Code Quality Checks**: YAML, Python, Rust, and TOML formatting and linting +- 📝 **Automated License Headers**: Automatically adds and validates Apache 2.0 license headers +- 🚀 **Fast Execution**: Uses modern tools like `uv`, `ruff`, and `taplo` for speed +- 🔧 **Auto-fix with Validation**: Formatters fix issues automatically but fail when changes are made +- 🌍 **Works Everywhere**: Run the same checks locally and in CI/CD pipelines +- ⚙️ **Highly Configurable**: Use default configs or provide your own + +## Using the Workflows in Your Repository + +To use a reusable workflow, create a workflow file inside **your repository** (e.g., `.github/workflows/ci.yml`) and reference the appropriate workflow from this repository. + +### Using the Reusable CI Checks Workflow + +The `checks.yml` workflow provides standardized pre-commit checks and license header validation. Add the following to your `.github/workflows/ci.yml`: + +```yaml +name: CI + +on: + push: + branches: [main] + pull_request: + +jobs: + checks: + uses: eclipse-opensovd/cicd-workflows/.github/workflows/checks.yml@main + with: + rust-nightly-version: "2025-07-14" # Optional, defaults to 2025-07-14 + python-version: "3.13" # Optional, defaults to 3.13 + pre-commit-version: "4.2" # Optional, defaults to 4.2 + pre-commit-config-path: "" # Optional, uses action's default config if not specified + license-config-path: "" # Optional, uses action's default config if not specified +``` + +#### Available Inputs + +- `rust-nightly-version` (optional): Rust nightly version to use for Rust formatting in the format `YYYY-MM-DD`. Defaults to `2025-07-14`. +- `python-version` (optional): Python version to use for pre-commit environment. Defaults to `3.13`. +- `pre-commit-version` (optional): Version of pre-commit to install. Defaults to `4.2`. +- `pre-commit-config-path` (optional): Path to a custom `.pre-commit-config.yml` in your repository. If not provided, uses the action's default config. +- `license-config-path` (optional): Path to a custom `.licenserc.yml` in your repository. If not provided, uses the action's default config. + +### Using Individual Actions + +You can also use the individual actions directly in your workflows: + +#### Pre-commit Checks Action + +Runs pre-commit hooks with standardized configuration: + +```yaml +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: eclipse-opensovd/cicd-workflows/pre-commit-action@main + with: + python-version: "3.13" # Optional, defaults to 3.13 + pre-commit-version: "4.2" # Optional, defaults to 4.2 + config-path: "" # Optional, uses action's default config if not specified +``` + +## Actions in This Repository + +### Pre-commit Action (`pre-commit-action/`) + +Provides comprehensive code quality checks via uv and pre-commit. +All formatters **automatically fix issues** and **fail when changes are made**. + +#### Checks Performed + +**File Validation:** +- YAML syntax validation +- Merge conflict detection +- End-of-file fixer (ensures files end with a newline) +- Trailing whitespace removal +- Mixed line ending normalization + +**Code Formatting:** +- **YAML**: Formatted with `yamlfmt` using basic formatter with retained line breaks +- **Python**: Formatted with `ruff format` (extremely fast Python formatter) +- **TOML**: Formatted and linted with `taplo` +- **Rust**: Formatted with `cargo fmt` (only if `Cargo.toml` exists) + - Long line and overflow checks + - Import order using `StdExternalCrate` grouping + - Import granularity using `Crate` setting + +**Linting:** +- **Python**: `ruff check` for linting and code quality + +**License Headers (Auto-fix):** +- **Apache SkyWalking Eyes**: Automatically adds or fixes license headers +- Runs twice for reliability: + 1. As a pre-commit hook (uses `license-eye` CLI, requires local installation) + 2. As a dedicated GitHub Action step (uses Docker, always available in CI) + +**How Auto-fix Works:** +When a formatter makes changes to your code, the pre-commit hook fails, requiring you to review and commit the changes. This ensures: +- All code modifications are tracked in version control +- Developers can review formatting changes before committing +- CI pipelines fail if code is not properly formatted + +**Inputs:** +- `python-version`: Python version for pre-commit environment (default: `3.13`) +- `pre-commit-version`: Version of pre-commit to install (default: `4.2`) +- `config-path`: Path to custom `.pre-commit-config.yml` (optional) +- `license-config-path`: Path to custom `.licenserc.yml` (optional) + + +## Running Checks Locally + +### Using uv for Pre-commit Checks + +[uv](https://docs.astral.sh/uv/) is a fast Python package manager that can run Python scripts without needing to install dependencies globally. + +#### In This Repository + +To run pre-commit checks locally in this repository: + +```bash +uv tool run pre-commit@4.2 run --all-files --config pre-commit-action/.pre-commit-config.yml +``` + +#### In Your Repository (Using This Action's Config) + +You have two options to run the same checks locally that run in CI: + +**Option 1: Using the `run_checks.py` script (One-off execution)** + +```bash +# Run with the default 'main' branch config +uv run https://raw.githubusercontent.com/eclipse-opensovd/cicd-workflows/main/run_checks.py + +# Or specify a different branch/tag/commit +uv run https://raw.githubusercontent.com/eclipse-opensovd/cicd-workflows/main/run_checks.py your-branch-name +``` + +This script will: +1. Download the shared pre-commit configuration from this repository +2. Download the shared license configuration (`.licenserc.yml`) +3. Set up the environment to run all checks (including license header validation) +4. Run all pre-commit checks against your code +5. Clean up temporary files automatically + +**Option 2: Using pre-commit directly (Recommended for development)** + +Create a `.pre-commit-config.yaml` file in your repository root: + +```yaml +repos: + - repo: local + hooks: + - id: shared-checks + name: Shared pre-commit checks + entry: uv run https://raw.githubusercontent.com/eclipse-opensovd/cicd-workflows/main/run_checks.py + language: system + pass_filenames: false +``` + +Then install and use pre-commit normally: + +```bash +# Install pre-commit hooks (runs automatically on git commit) +pre-commit install + +# Run manually on all files +pre-commit run --all-files + +# Run on staged files only +pre-commit run +``` + +**Custom Config**: If you've specified a custom `pre-commit-config-path` in your workflow, you can run pre-commit directly: +```bash +uv tool run pre-commit@4.2 run --all-files --config .pre-commit-config.yml +``` + +**Run Specific Hooks**: To run only the shared checks: +```bash +pre-commit run shared-checks --all-files +``` + +### Installing Required Tools + +#### uv (Required) + +[Install uv](https://docs.astral.sh/uv/getting-started/installation/) - Fast Python package manager and script runner. + +#### Apache SkyWalking Eyes (Required for License Checks) + +[Install SkyWalking Eyes](https://github.com/apache/skywalking-eyes#installation) - Required for local license header validation. + +#### Rust Toolchain (Required for Rust Projects) + +[Install Rust](https://www.rust-lang.org/tools/install) - Required if your project has a `Cargo.toml` file. diff --git a/pre-commit-action/.licenserc.yml b/pre-commit-action/.licenserc.yml new file mode 100644 index 0000000..10d4b0a --- /dev/null +++ b/pre-commit-action/.licenserc.yml @@ -0,0 +1,57 @@ +# Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +header: + license: + spdx-id: Apache-2.0 + copyright-owner: The Contributors to Eclipse OpenSOVD + software-name: opensovd-cda + content: | + Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) + + See the NOTICE file(s) distributed with this work for additional + information regarding copyright ownership. + + This program and the accompanying materials are made available under the + terms of the Apache License Version 2.0 which is available at + https://www.apache.org/licenses/LICENSE-2.0 + + SPDX-License-Identifier: Apache-2.0 + + language: + Rust: + extensions: + - .rs + comment_style_id: SlashAsterisk + + ODX: + type: data + tm_scope: text.xml + ace_mode: xml + codemirror_mode: xml + codemirror_mime_type: text/xml + comment_style_id: AngleBracket + extensions: + - .odx-cs + - .odx-c + - .odx-d + + paths: + - '**/*.toml' + - '**/Dockerfile*' + - '**/*.sh' + - '**/*.py' + - '**/*.yml' + - '**/*.rs' + - '**/*.yaml' + - '**/*.adoc' + - '**/*.odx*' + - '**/*.kt' diff --git a/pre-commit-action/.pre-commit-config.yml b/pre-commit-action/.pre-commit-config.yml new file mode 100644 index 0000000..70eb699 --- /dev/null +++ b/pre-commit-action/.pre-commit-config.yml @@ -0,0 +1,60 @@ +# Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: check-yaml + - id: check-merge-conflict + - id: end-of-file-fixer + - id: trailing-whitespace + - id: mixed-line-ending + - repo: https://github.com/google/yamlfmt + rev: v0.20.0 + hooks: + - id: yamlfmt + args: ["-formatter", "type=basic,retain_line_breaks_single=true"] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.14.5 + hooks: + - id: ruff-check + - id: ruff-format + - repo: https://github.com/ComPWA/taplo-pre-commit + rev: v0.9.3 + hooks: + - id: taplo-format + - id: taplo-lint + - repo: local + hooks: + - id: license-eye-fix + name: Fix License Headers (SkyWalking Eyes) + entry: bash -c 'license-eye -c "${LICENSE_EYE_CONFIG:-.licenserc.yml}" header fix' + language: system + pass_filenames: false + - id: cargo-fmt-long-lines + name: Rust Format - Fix long lines and unformatted code + entry: bash -c '[ -f Cargo.toml ] && cargo fmt -- --config error_on_unformatted=true,error_on_line_overflow=true,format_strings=true || exit 0' + language: system + types: [rust] + pass_filenames: false + - id: cargo-fmt-import-order + name: Rust Format - Fix import order + entry: bash -c '[ -f Cargo.toml ] && cargo fmt -- --config group_imports=StdExternalCrate || exit 0' + language: system + types: [rust] + pass_filenames: false + - id: cargo-fmt-imports-granularity + name: Rust Format - Fix imports granularity + entry: bash -c '[ -f Cargo.toml ] && cargo fmt -- --config imports_granularity=Crate || exit 0' + language: system + types: [rust] + pass_filenames: false diff --git a/pre-commit-action/action.yml b/pre-commit-action/action.yml new file mode 100644 index 0000000..35c57e4 --- /dev/null +++ b/pre-commit-action/action.yml @@ -0,0 +1,113 @@ +# Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 + +name: 'OpenSOVD Pre-Commit Checks' +description: 'Runs pre-commit hooks with a standardized configuration from this action.' + +inputs: + rust-nightly-version: + description: 'Rust nightly version to use (YYYY-MM-DD), defaults to 2025-07-14 if not set' + required: false + default: '2025-07-14' + python-version: + description: 'Python version to use for pre-commit environment' + required: false + default: '3.13' + pre-commit-version: + description: 'Version of pre-commit to install' + required: false + default: '4.2' + config-path: + description: > + Path to a custom .pre-commit-config.yml in the consumer repository. If not provided, the action uses its own config. + required: false + default: '' + license-config-path: + description: > + Path to a custom .licenserc.yml in the consumer repository. If not provided, the action uses its own config. + required: false + default: '' + +runs: + using: 'composite' + steps: + - name: Checkout consumer repository + uses: actions/checkout@v4 + with: + submodules: false + + - name: Determine input values with defaults + id: inputs + shell: bash + run: | + # Rust nightly version + if [[ -n "${{ inputs.rust-nightly-version }}" ]]; then + echo "rust_toolchain=nightly-${{ inputs.rust-nightly-version }}" >> "$GITHUB_OUTPUT" + else + echo "rust_toolchain=nightly-2025-07-14" >> "$GITHUB_OUTPUT" + fi + + # Python version + if [[ -n "${{ inputs.python-version }}" ]]; then + echo "python_version=${{ inputs.python-version }}" >> "$GITHUB_OUTPUT" + else + echo "python_version=3.13" >> "$GITHUB_OUTPUT" + fi + + # Pre-commit version + if [[ -n "${{ inputs.pre-commit-version }}" ]]; then + echo "precommit_version=${{ inputs.pre-commit-version }}" >> "$GITHUB_OUTPUT" + else + echo "precommit_version=4.2" >> "$GITHUB_OUTPUT" + fi + + # Pre-commit config path + if [[ -n "${{ inputs.config-path }}" ]]; then + echo "config_path=${{ inputs.config-path }}" >> "$GITHUB_OUTPUT" + else + echo "config_path=${GITHUB_ACTION_PATH}/.pre-commit-config.yml" >> "$GITHUB_OUTPUT" + fi + + # License config path + if [[ -n "${{ inputs.license-config-path }}" ]]; then + if [[ -f "${{ inputs.license-config-path }}" ]]; then + echo "license_config_path=${{ inputs.license-config-path }}" >> "$GITHUB_OUTPUT" + else + echo "Error: Custom license config not found at ${{ inputs.license-config-path }}" + exit 1 + fi + else + echo "license_config_path=${GITHUB_ACTION_PATH}/.licenserc.yml" >> "$GITHUB_OUTPUT" + fi + + - name: Install Rust toolchain + uses: dtolnay/rust-toolchain@nightly + with: + toolchain: ${{ steps.inputs.outputs.rust_toolchain }} + components: clippy, rustfmt + + - name: Check license headers + uses: apache/skywalking-eyes/header@v0.7.0 + with: + config: ${{ steps.inputs.outputs.license_config_path }} + + - name: Install uv + uses: astral-sh/setup-uv@v6 + with: + enable-cache: true + python-version: ${{ steps.inputs.outputs.python_version }} + activate-environment: true + + - name: Run pre-commit + shell: bash + env: + LICENSE_EYE_CONFIG: ${{ steps.inputs.outputs.license_config_path }} + run: uv tool run pre-commit@${{ steps.inputs.outputs.precommit_version }} run --all-files --config "${{ steps.inputs.outputs.config_path }}" diff --git a/run_checks.py b/run_checks.py new file mode 100755 index 0000000..61749bc --- /dev/null +++ b/run_checks.py @@ -0,0 +1,80 @@ +# Copyright (c) 2025 The Contributors to Eclipse OpenSOVD (see CONTRIBUTORS) +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# +#!/usr/bin/env python3 +# /// script +# dependencies = ["pre-commit==4.2"] +# /// + +import os +import subprocess +import sys +import tempfile +import urllib.error +import urllib.request +from pathlib import Path + +# Default to 'main' branch, but can be overridden via environment variable or argument +DEFAULT_BRANCH = "main" +BASE_URL = "https://raw.githubusercontent.com/eclipse-opensovd/cicd-workflows/{branch}/pre-commit-action" +CONFIG_URL_TEMPLATE = f"{BASE_URL}/.pre-commit-config.yml" +LICENSE_CONFIG_URL_TEMPLATE = f"{BASE_URL}/.licenserc.yml" + + +def main(): + branch = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_BRANCH + config_url = CONFIG_URL_TEMPLATE.format(branch=branch) + license_config_url = LICENSE_CONFIG_URL_TEMPLATE.format(branch=branch) + + print(f"Downloading pre-commit config from: {config_url}") + try: + # Download pre-commit config + with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f: + with urllib.request.urlopen(config_url) as response: + f.write(response.read().decode()) + config_path = f.name + + # Download license config + print(f"Downloading license config from: {license_config_url}") + with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f: + with urllib.request.urlopen(license_config_url) as response: + f.write(response.read().decode()) + license_config_path = f.name + + print("Running pre-commit checks...") + # Set LICENSE_EYE_CONFIG so the license-eye hook knows where to find the config + env = os.environ.copy() + env["LICENSE_EYE_CONFIG"] = license_config_path + + result = subprocess.run( + ["pre-commit", "run", "--all-files", "--config", config_path], + env=env, + check=False, + ) + + # Clean up temp files + Path(config_path).unlink(missing_ok=True) + Path(license_config_path).unlink(missing_ok=True) + sys.exit(result.returncode) + except urllib.error.HTTPError as e: + print(f"Error downloading config: {e}", file=sys.stderr) + print( + f"Make sure the branch '{branch}' exists in the repository.", + file=sys.stderr, + ) + sys.exit(1) + except Exception as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + + +if __name__ == "__main__": + main()