PB-579: Use PR body instead of title to set the bump type #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Auto Label and SemVer Release Title Reusable Workflow | ||
# Auto PR Release Title. When creating a Release or Hotfix, the PR title is automatically set. | ||
# | ||
# For new release (develop -> master PR) the title is set to `New Release <next-version> - <bump-type>` | ||
# with bump-type taken from the original title or from the change log commit message. | ||
# By default bump-type if not specified or found is set to minor or patch if it is a hot fix PR. | ||
# | ||
# For hotfix (hotfix-* -> master PR) the title is set to `<next-version> - <original-title> - <bump-type>` | ||
# | ||
# Finally add a Label to the PR based on HEAD branch name. These labels are then used to categorize | ||
# the release notes. | ||
# NOTE: this workflow only works for github pull_request trigger | ||
# Usage example: | ||
# on: | ||
# pull_request: | ||
# types: | ||
# - opened | ||
# - reopened | ||
# - synchronize | ||
# - edited | ||
on: | ||
workflow_call: | ||
jobs: | ||
pr-title: | ||
name: Set PR title | ||
runs-on: ubuntu-latest | ||
env: | ||
PR_TITLE: ${{ github.event.pull_request.title }} | ||
PR_BODY: ${{ github.event.pull_request.body }} | ||
steps: | ||
- name: Get Default Bump Type from PR information | ||
id: bump_type | ||
run: | | ||
bump_type=minor | ||
echo "Initial default bump type ${bump_type}" | ||
# Set default bump type for hotfixes | ||
case "${{ github.head_ref }}" in | ||
hotfix-* | bugfix-* | bug-* | dependabot/*) | ||
bump_type=patch | ||
echo "Hotfix branch detected set default bump to ${bump_type}" | ||
;; | ||
esac | ||
echo "Set default bump type based on PR infos" | ||
echo "PR_TITLE=${PR_TITLE}" | ||
echo "PR_BODY" | ||
echo "----------------" | ||
echo ${PR_BODY} | ||
echo "----------------" | ||
case "${PR_BODY}" in | ||
*#major*) bump_type=major ;; | ||
*#minor*) bump_type=minor ;; | ||
*#patch*) bump_type=patch ;; | ||
esac | ||
case "${PR_TITLE}" in | ||
*#major*) bump_type=major ;; | ||
*#minor*) bump_type=minor ;; | ||
*#patch*) bump_type=patch ;; | ||
esac | ||
echo "Remove bump type from original title" | ||
pr_title="${PR_TITLE}" | ||
pr_title=$(echo ${pr_title} | sed "s/[ ]*-*[ ]*#${bump_type}//") | ||
echo "Remove bump type from original body" | ||
pr_body="${PR_BODY}" | ||
pr_body=$(echo ${pr_body} | sed "s/[ ]*-*[ ]*#${bump_type}//") | ||
echo "pr_title=${pr_title}" >> $GITHUB_OUTPUT | ||
echo "Original title set to '${pr_title}'" | ||
echo "pr_body=${pr_body}" >> $GITHUB_OUTPUT | ||
echo "Original body set to '${pr_body}'" | ||
echo "default_bump=${bump_type}" >> $GITHUB_OUTPUT | ||
echo "Bump type set to ${bump_type}" | ||
- uses: actions/checkout@v4 | ||
if: ${{ github.base_ref == 'master' }} | ||
with: | ||
fetch-depth: 0 | ||
ref: master | ||
# This step is needed in order for the geoadmin/github-tag-action@master action to work properly | ||
# Note also that we need to set the PR title and body as commit message in order to be able to | ||
# overwrite any bump type by any previous commit | ||
- name: Do the merge but don't push it | ||
if: ${{ github.base_ref == 'master' }} | ||
run: | | ||
cd ${GITHUB_WORKSPACE} | ||
git config user.email "[email protected]" | ||
git config user.name "Github PR Auto Title Workflow" | ||
git fetch origin ${{ github.head_ref }}:${{ github.head_ref }} | ||
git merge ${{ github.head_ref }} --no-ff -m "${PR_TITLE}\n\n${PR_BODY}" | ||
- name: Bump version (without tagging) | ||
if: ${{ github.base_ref == 'master' }} | ||
id: get_tag | ||
uses: geoadmin/github-tag-action@master | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
WITH_V: true | ||
DEFAULT_BUMP: ${{ steps.bump_type.outputs.default_bump }} | ||
RELEASE_BRANCHES: master | ||
TAG_CONTEXT: repo | ||
DRY_RUN: true | ||
VERBOSE: true | ||
- name: Set `New Release` PR title if needed | ||
if: ${{ github.base_ref == 'master' && github.head_ref == 'develop' }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
PR_NUMBER: ${{ github.event.number }} | ||
run: | | ||
gh api /repos/${{ github.repository }}/pulls/${PR_NUMBER} \ | ||
--method "PATCH" \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
-f "title=New Release ${{ steps.get_tag.outputs.new_tag }}" \ | ||
-f "$(cat <<EOF | ||
body=${{ steps.bump_type.outputs.pr_body }} | ||
\#${{ steps.get_tag.outputs.part }} | ||
EOF | ||
)" | ||
#-f "body=${{ steps.bump_type.outputs.pr_body }}\n#${{ steps.get_tag.outputs.part }}" | ||
- name: Add default bump type based on PR information | ||
if: ${{ github.head_ref != 'develop' }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
PR_NUMBER: ${{ github.event.number }} | ||
run: | | ||
gh api /repos/${{ github.repository }}/pulls/${PR_NUMBER} \ | ||
--method "PATCH" \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
-f "title=${{ steps.bump_type.outputs.pr_title }}" \ | ||
-f "body=${{ steps.bump_type.outputs.pr_body }}\n#${{ steps.bump_type.outputs.default_bump }}" | ||
pr-labeler: | ||
name: Set PR label | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: PR Auto Labeler | ||
uses: release-drafter/release-drafter@v6 | ||
with: | ||
config-name: release-drafter-labeler-config.yml | ||
disable-autolabeler: false | ||
disable-releaser: true | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |