From 9d7d14c0d0ef2569a1d8f93724fea68dd21ac21a Mon Sep 17 00:00:00 2001 From: Daniel von Atzigen Date: Tue, 5 Nov 2024 14:53:21 +0100 Subject: [PATCH] Add --- .github/scripts/remove-packages.js | 8 +-- .github/scripts/remove.js | 18 +++++ .github/workflows/code-quality.yml | 103 +++++++++++++++++++---------- ui/Dockerfile | 2 +- 4 files changed, 91 insertions(+), 40 deletions(-) create mode 100644 .github/scripts/remove.js diff --git a/.github/scripts/remove-packages.js b/.github/scripts/remove-packages.js index 61a3f7dfc..c4896bc7d 100644 --- a/.github/scripts/remove-packages.js +++ b/.github/scripts/remove-packages.js @@ -1,6 +1,6 @@ -const { Octokit } = require("@octokit/rest"); - const removePackageVersions = async (imageUrl, imageVersions) => { + const { Octokit } = await import("@octokit/rest"); + const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN, }); @@ -11,7 +11,7 @@ const removePackageVersions = async (imageUrl, imageVersions) => { await octokit.rest.packages.deletePackageVersionForOrg({ package_type: "container", package_name: imageName, - username: imageOwner, + org: imageOwner, package_version_id: imageId, }); } @@ -26,7 +26,7 @@ const loadOutdatedVersionIds = async (octokit, imageOwner, imageName, versions) const response = await octokit.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({ package_type: "container", package_name: imageName, - username: imageOwner, + org: imageOwner, page, }); if (response.data.length === 0) { diff --git a/.github/scripts/remove.js b/.github/scripts/remove.js new file mode 100644 index 000000000..0bc075e6d --- /dev/null +++ b/.github/scripts/remove.js @@ -0,0 +1,18 @@ +const removePackageVersions = async (imageUrl) => { + const { Octokit } = await import("@octokit/rest"); + + const octokit = new Octokit({ + auth: process.env.GITHUB_TOKEN, + }); + + const [_imageHost, imageOwner, imageName] = imageUrl.split("/"); + await octokit.rest.packages.deletePackageForOrg({ + package_type: "container", + package_name: imageName, + org: imageOwner, + }); +}; + +module.exports = { + removePackageVersions, +}; diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index 73fd197b3..bbae09675 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -34,41 +34,11 @@ jobs: uses: actions/dependency-review-action@v4 - check-changelog: - name: "Check CHANGELOG" - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - # Fetch full history for comparison - fetch-depth: 0 - - name: Determine if branch is a feature branch - id: check_feature_branch - run: | - if [[ "${{ github.head_ref || github.ref_name }} " =~ ^feature/* ]]; then - echo "is_feature=true" >> $GITHUB_ENV - else - echo "is_feature=false" >> $GITHUB_ENV - fi - - name: Check if CHANGELOG.md has changed - if: env.is_feature == 'true' - run: | - # Compare the CHANGELOG.md file in the current branch with the `develop` branch - if git diff --name-only origin/develop...HEAD | grep -q '^CHANGELOG.md$'; then - echo "CHANGELOG.md has been updated." - else - echo "CHANGELOG.md has not been updated." - exit 1 - fi - - install-ui: name: "Install UI" runs-on: ubuntu-latest needs: - dependency-review - - check-changelog steps: - name: Checkout repository uses: actions/checkout@v4 @@ -165,7 +135,6 @@ jobs: runs-on: ubuntu-latest needs: - dependency-review - - check-changelog steps: - name: Checkout repository uses: actions/checkout@v4 @@ -210,9 +179,6 @@ jobs: rm -r tests/ echo "fn main() {}" > src/main.rs cargo build --all-targets --locked --quiet - - rustup component add rustfmt - rustup component add clippy check-api: @@ -264,6 +230,7 @@ jobs: toolchain: ${{ env.RUST_VERSION }} profile: minimal override: true + components: clippy, rustfmt - name: Restore cargo registry uses: actions/cache/restore@v4 with: @@ -286,7 +253,7 @@ jobs: - name: Run clippy run: | cd api - cargo clippy --frozen + cargo clippy --frozen --quiet test-api: @@ -346,3 +313,69 @@ jobs: run: | kill $(cat api.pid) docker compose down + + + check-changelog: + name: "Check CHANGELOG" + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + # Fetch full history for comparison + fetch-depth: 0 + - name: Determine if branch is a feature branch + id: check_feature_branch + run: | + if [[ "${{ github.head_ref || github.ref_name }} " =~ ^feature/* ]]; then + echo "is_feature=true" >> $GITHUB_ENV + else + echo "is_feature=false" >> $GITHUB_ENV + fi + - name: Check if CHANGELOG.md has changed + if: env.is_feature == 'true' + continue-on-error: true + run: | + # Compare the CHANGELOG.md file in the current branch with the `develop` branch + if git diff --name-only origin/develop...HEAD | grep -q '^CHANGELOG.md$'; then + echo "CHANGELOG.md has been updated." + else + echo "::warning file=CHANGELOG.md::CHANGELOG.md has not been updated." + exit 1 + exit 1 + fi + + + prefer-single-commit: + name: "Prefer Single Commit" + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + # Fetch full history for comparison + fetch-depth: 0 + - name: Count commits + id: count_commits + run: | + commit_count=$(git rev-list --count HEAD ^origin/${{ github.event.pull_request.base.ref }}) + echo "commit_count=$commit_count" >> $GITHUB_ENV + - name: Fail if more than one commit + if: env.commit_count > 1 + run: | + echo "::error title=Please Squash your PR::Pull request contains more than one commit ($commit_count commits). Please squash your commits." + + cleanup: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup node + run: | + npm install @octokit/rest + - name: Remove old versions + uses: actions/github-script@v7 + with: + script: | + const { removePackageVersions } = require('./.github/scripts/remove.js'); + await removePackageVersions("ghcr.io/swisstopo/swissgeol-viewer-app-api") diff --git a/ui/Dockerfile b/ui/Dockerfile index dcd9cf221..c39659765 100644 --- a/ui/Dockerfile +++ b/ui/Dockerfile @@ -3,7 +3,7 @@ FROM node:22-alpine as build WORKDIR /app COPY . . -RUN npm install --no-scripts +RUN npm install --ignore-scripts RUN npm run build --omit=dev