Skip to content

ci: cleansup

ci: cleansup #7

Workflow file for this run

###########################################################
# Author: xZetsubou aka Bander #
# #
# Generate draft release of commits since latest version. #
###########################################################
name: Generate Draft Release
on:
push:
workflow_dispatch:
branches:
- master # Adjust this to the branch you want to track
- add_auto_draft_Release
permissions:
contents: write
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
release_commits: |
{"section": "breaking_changes", "prefix": "break", "header": "💥 Breaking Changes"}
{"section": "fixes", "prefix": "fix", "header": "🐛 Fixes" }
{"section": "feats", "prefix": "feat", "header": "✨ Feats" }
{"section": "improvements", "prefix": "perf", "header": "⚡ Improvements" }
{"section": "refactors", "prefix": "refactor", "header": "♻️ Refactors" }
{"section": "chores", "prefix": "chore", "header": "🧹 Chores" }
{"section": "docs", "prefix": "doc", "header": "📚 Docs" }
{"section": "revert", "prefix": "revert", "header": "🌀 Revert" }
{"section": "cis", "prefix": "ci", "header": "🛠️ CI" }
{"section": "tests", "prefix": "test", "header": "✅ Tests" }
{"section": "more", "prefix": "", "header": "➕ More" }
jobs:
generate-release-draft:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all tags and history
- name: Get latest release tag
id: latest-release
run: |
latest_tag=$(curl --silent --fail \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/latest" \
| jq -r .tag_name)
echo "Latest release tag: $latest_tag"
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
- name: Get commits from latest release to HEAD
run: |
git fetch --tags
git log ${{ env.latest_tag }}..HEAD --pretty=format:'%s by @%an-%ae in [`%h`](https://github.com/${{github.repository}}/commit/%H)' --reverse > commits.md
echo "" >> commits.md
while IFS= read -r line; do
commit_hash=$(echo $line | awk '{print $1}')
email=$(echo $line | awk -F"by @" '{print (NF>1)? $NF : ""}'| awk '{print $1}')
if [[ "$email" =~ @?([a-zA-Z]+)\-([0-9]+)?\+?([a-zA-Z]+) ]]; then
name="${BASH_REMATCH[1]}"; id="${BASH_REMATCH[2]}"; mail_name="${BASH_REMATCH[3]}";
username=$name && [[ -n $id ]] && username=$mail_name
sed -i "s#$email#$username#g" "commits.md"
fi
done < commits.md
- name: Parse commit messages and categorize
id: parse_commits
run: |
declare -A commits_map;
while IFS= read -r line; do
first_lower=$(echo "$line" | awk '{print $1}' | tr '[:upper:]' '[:lower:]')
scope=$(echo "$line" | cut -d' ' -f1)
commit_desc=$(echo "$line" | cut -d' ' -f2-)
parsed_commit="- **$scope** $commit_desc\n"
added=false
while read -r item; do
section=$(echo "$item" | jq -r '.section')
prefix=$(echo "$item" | jq -r '.prefix')
if [[ "$first_lower" == $prefix*: ]]; then
commits_map[$section]+=$parsed_commit && added=true && break
fi
done < <(echo '${{env.release_commits}}')
[[ $added == false ]] && commits_map["more"]+=$parsed_commit
done < commits.md
for key in "${!commits_map[@]}"; do
echo "$key=${commits_map[$key]}" >> $GITHUB_OUTPUT
done
- name: Create Release Notes
id: release-notes
run: |
notes="#### 🚨 **Note**: This draft has been released in $(date +'%Y.%m.%d - %H:%M:%S')\n\n"
while read -r item; do
section=$(echo "$item" | jq -r '.section') && [[ ! "$section" =~ [^[:space:]] ]] && continue
header=$(echo "$item" | jq -r '.header')
[[ ! "$section" =~ [^[:space:]] ]] && continue
section_commits=$(echo '${{ toJson(steps.parse_commits.outputs) }}' | jq -r ".$section")
if [ -n "$section_commits" ] && [ "$section_commits" != null ]; then
notes+="### $header \n $section_commits \n"
fi
done < <(echo '${{env.release_commits}}')
echo -e $notes > commits.md
- name: Remove old Draft
id: remove_drafts
continue-on-error: true
run:
gh release delete "Draft Latest version"
- name: Create Release Draft
uses: softprops/action-gh-release@v1
with:
name: "Draft Latest version"
tag_name: "Draft Latest version"
body_path: commits.md
draft: true