Skip to content

Commit

Permalink
[BRE-30] PR Version labels (#284)
Browse files Browse the repository at this point in the history
* Add version reusable workflow

* Add PR version to enforce version label
  • Loading branch information
michalchecinski authored Jun 6, 2024
1 parent f670f35 commit 60e6af5
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/_enforce-labels.yml
Original file line number Diff line number Diff line change
@@ -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
104 changes: 104 additions & 0 deletions .github/workflows/_version.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 60e6af5

Please sign in to comment.