From 2dec2e1fd4a9701abc5285fb38215e980dbe7e40 Mon Sep 17 00:00:00 2001 From: Vincent Koppen Date: Fri, 17 Jan 2025 14:47:04 +0100 Subject: [PATCH 1/3] publish to pypi Signed-off-by: Vincent Koppen --- .github/workflows/build-test-and-sonar.yml | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 .github/workflows/build-test-and-sonar.yml diff --git a/.github/workflows/build-test-and-sonar.yml b/.github/workflows/build-test-and-sonar.yml new file mode 100644 index 0000000..4b8d933 --- /dev/null +++ b/.github/workflows/build-test-and-sonar.yml @@ -0,0 +1,171 @@ +# SPDX-FileCopyrightText: Contributors to the Power Grid Model project +# +# SPDX-License-Identifier: MPL-2.0 + + +name: Build, Test, Sonar and Publish + +on: + push: + branches: + - main + # run pipeline on pull request + pull_request: + # run pipeline on merge queue + merge_group: + # run pipeline from another workflow + workflow_call: + inputs: + create_release: + type: boolean + description: Create a (pre-)release when CI passes + default: false + required: false + # run this workflow manually from the Actions tab + workflow_dispatch: + inputs: + create_release: + type: boolean + description: Create a (pre-)release when CI passes + default: false + required: true + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-main + cancel-in-progress: true + +jobs: + + build-python: + runs-on: ubuntu-latest + outputs: + version: ${{ steps.version.outputs.version }} + steps: + + - name: Checkout source code + uses: actions/checkout@v4 + + - name: Setup Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Build + run: | + pip install requests build + python set_pypi_version.py + python -m build --outdir wheelhouse . + + - name: Save version + id: version + run: echo "version=$(cat PYPI_VERSION)" >> $GITHUB_OUTPUT + + - name: Store built wheel file + uses: actions/upload-artifact@v4 + with: + name: power-grid-model-ds + path: wheelhouse/ + + sonar-cloud: + permissions: + contents: write + runs-on: ubuntu-latest + steps: + + - name: Checkout source code + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + + - name: Setup Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install in develop mode + run: | + pip install -e .[dev] + + - name: Test and Coverage + run: | + coverage run -m pytest + coverage xml + coverage report --fail-under=80 + + - name: SonarCloud Scan + if: ${{ (github.event_name == 'push') || (github.event.pull_request.head.repo.owner.login == 'PowerGridModel') }} + uses: SonarSource/sonarqube-scan-action@v4 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + tests: + needs: build-python + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + python: ["3.10", "3.11", "3.12", "3.13"] + fail-fast: false + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout source code + uses: actions/checkout@v4 + + - name: Setup Python ${{ matrix.python }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python }} + + - name: Load built wheel file + uses: actions/download-artifact@v4 + with: + name: power-grid-model-ds + path: wheelhouse/ + + - name: Install built wheel file + run: pip install power-grid-model-ds[dev]==${{ needs.build-python.outputs.version }} --find-links=wheelhouse + + - name: Unit test and coverage + run: pytest --verbose + + publish: + needs: + - build-python + - tests + - sonar-cloud + permissions: + contents: write + env: + TWINE_USERNAME: ${{ secrets.PYPI_USER }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASS }} + runs-on: ubuntu-latest + steps: + - name: Setup Python 3.11 + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Load built wheel file + uses: actions/download-artifact@v4 + with: + name: power-grid-model-ds + path: wheelhouse/ + + - name: Upload wheels + if: (github.event_name == 'push') || ((github.event_name == 'workflow_dispatch') && (github.event.inputs.create_release == 'true')) + run: | + pip install twine + echo "Publish to PyPI..." + twine upload --verbose wheelhouse/* + + - name: Release + if: (github.event_name == 'push') || ((github.event_name == 'workflow_dispatch') && (github.event.inputs.create_release == 'true')) + uses: softprops/action-gh-release@v2 + with: + files: | + ./wheelhouse/* + tag_name: v${{ needs.build-python.outputs.version }} + prerelease: ${{github.ref != 'refs/heads/main'}} + generate_release_notes: true + target_commitish: ${{ github.sha }} From b39aa4b6954ba1169262f231d5133a71ac75a738 Mon Sep 17 00:00:00 2001 From: Vincent Koppen Date: Fri, 17 Jan 2025 15:04:52 +0100 Subject: [PATCH 2/3] chore: update version Signed-off-by: Vincent Koppen --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 171538e..9f8e9b6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0 \ No newline at end of file +1.0 \ No newline at end of file From 78c7f2037c9388c7589d4f508af18bbfaa6940e0 Mon Sep 17 00:00:00 2001 From: Vincent Koppen Date: Fri, 17 Jan 2025 15:15:19 +0100 Subject: [PATCH 3/3] fix: get correct version from pypi Signed-off-by: Vincent Koppen --- set_pypi_version.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/set_pypi_version.py b/set_pypi_version.py index 61eb728..234fda7 100644 --- a/set_pypi_version.py +++ b/set_pypi_version.py @@ -7,6 +7,7 @@ import os +import re from pathlib import Path import requests @@ -44,9 +45,13 @@ def get_pypi_latest(): response = requests.get("https://pypi.org/pypi/power-grid-model-ds/json") if response.status_code == 404: return 0, 0, 0 - data = response.json() - version = str(data["info"]["version"]) - return (int(x) for x in version.split(".")) + version = str(response.json()["info"]["version"]) + + version_pattern = re.compile(r"^\d+\.\d+\.\d+") + match = version_pattern.match(version) + if not match: + raise ValueError(f"Invalid version format: {version}") + return (int(x) for x in match.group(0).split(".")) def get_new_version(major, minor, latest_major, latest_minor, latest_patch):