From 60e6af5e347afebbe6620eb7378608ea2958c5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ch=C4=99ci=C5=84ski?= Date: Thu, 6 Jun 2024 13:03:41 +0200 Subject: [PATCH] [BRE-30] PR Version labels (#284) * Add version reusable workflow * Add PR version to enforce version label --- .github/workflows/_enforce-labels.yml | 29 +++++++ .github/workflows/_version.yml | 104 ++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 .github/workflows/_enforce-labels.yml create mode 100644 .github/workflows/_version.yml diff --git a/.github/workflows/_enforce-labels.yml b/.github/workflows/_enforce-labels.yml new file mode 100644 index 00000000..72dd79f8 --- /dev/null +++ b/.github/workflows/_enforce-labels.yml @@ -0,0 +1,29 @@ +--- +name: Enforce PR labels + +on: + workflow_call: + +jobs: + enforce-label: + if: ${{ contains(github.event.*.labels.*.name, 'hold') || contains(github.event.*.labels.*.name, 'needs-qa') }} + name: Enforce label + runs-on: ubuntu-22.04 + steps: + - name: Check for label + run: | + echo "PRs with the hold or needs-qa labels cannot be merged" + echo "### :x: PRs with the hold or needs-qa labels cannot be merged" >> $GITHUB_STEP_SUMMARY + exit 1 + + enforce-version-label: + if: ${{ contains(github.event.*.labels.*.name, 'version') }} + name: Enforce version label + runs-on: ubuntu-22.04 + + steps: + - name: Check for label + run: | + echo "PR without the version label cannot be merged." + echo "### :x: PR without the version label cannot be merged" >> $GITHUB_STEP_SUMMARY + exit 1 diff --git a/.github/workflows/_version.yml b/.github/workflows/_version.yml new file mode 100644 index 00000000..681bbfaa --- /dev/null +++ b/.github/workflows/_version.yml @@ -0,0 +1,104 @@ +--- +name: _version +run-name: Calculate Version + +on: + workflow_call: + inputs: + is-release: + type: boolean + required: true + outputs: + version: + description: "version to be built" + value: ${{ jobs.version.outputs.version }} + +jobs: + version: + name: Calculate Version + runs-on: ubuntu-22.04 + outputs: + version: ${{ steps.calculate.outputs.version }} + steps: + - name: Checkout Repo + uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 + with: + fetch-depth: 0 + + - name: Get PR ID + id: pr + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + commit_message=$( + curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GH_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }} | \ + jq -r ".commit.message" + ) + ID=$(echo "$commit_message" | head -1 | grep -o "(#.*)" | grep -o "[0-9]*") + echo "id=$ID" >> $GITHUB_OUTPUT + + - name: Get version bump type + if: ${{ inputs.is-release }} + id: bump-type + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ steps.pr.outputs.id }} + run: | + version_tag=$( + curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GH_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/labels | \ + jq -r ".[].name" | grep "version" + ) + + # Single Version label Enforcement (should go in CI...) + if [[ $(echo $version_tag | wc -w) -gt 1 ]]; then + echo "[!] multiple version labels found!" + exit 1 + fi + + version_type=$(echo $version_tag | cut -d ":" -f 2) + echo "Version Bump Type: $version_type" + echo "type=$version_type" >> $GITHUB_OUTPUT + + - name: Calculate next version + id: calculate + env: + VERSION_TYPE: ${{ steps.bump-type.outputs.type }} + run: | + echo -e "\nCalculating next version..." + + latest_tag_version=$(git tag --sort=committerdate --list | tail -1) + latest_version=${latest_tag_version:1} # remove 'v' from tag version + + if [[ "${{ inputs.is-release }}" == "true" ]]; then + latest_major_version=$(echo $latest_version | cut -d "." -f 1) + latest_minor_version=$(echo $latest_version | cut -d "." -f 2) + latest_patch_version=$(echo $latest_version | cut -d "." -f 3) + + echo " latest_version: $latest_version" + echo " latest_major_version: $latest_major_version" + echo " latest_minor_version: $latest_minor_version" + echo " latest_patch_version: $latest_patch_version" + + if [[ "$VERSION_TYPE" == "major" ]]; then + next_version="$(($latest_major_version + 1)).0.0" + elif [[ "$VERSION_TYPE" == "minor" ]]; then + next_version="${latest_major_version}.$(($latest_minor_version + 1)).0" + elif [[ "$VERSION_TYPE" == "patch" ]]; then + next_version="${latest_major_version}.${latest_minor_version}.$(($latest_patch_version + 1))" + else + next_version="$latest_version+${{ steps.pr.outputs.id }}" + fi + + echo "Next Version: $next_version" + echo "version=$next_version" >> $GITHUB_OUTPUT + else + echo "version=$latest_version+${{ steps.pr.outputs.id }}" >> $GITHUB_OUTPUT + fi