forked from rospogrigio/localtuya
-
Notifications
You must be signed in to change notification settings - Fork 70
150 lines (123 loc) · 6.18 KB
/
draft_release.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
###########################################################
# Author: xZetsubou aka Bander #
# #
# Generate draft release of commits since latest version. #
###########################################################
name: Generate Draft Release
on:
push:
workflow_dispatch:
branches:
- master
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": "🌀 Reverts" }
{"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,%cL in [`%h`](https://github.com/${{github.repository}}/commit/%H)' --reverse > commits.md
echo "" >> commits.md
declare -A cache_nickname;
while IFS= read -r line; do
author_info=$(echo "$line" | grep -oP 'by @\K.*?(?= in)')
commit=$(echo "$line" | grep -oP '/commit/\K[a-f0-9]+')
IFS=$',' read -r name email mail_name _ <<< "$author_info"
if [[ "$email" != *"noreply"* ]]; then
if [[ -v cache_nickname[$email] ]]; then
username="${cache_nickname[$email]}"
else
username=$(curl --silent --fail \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/search/commits?q=$commit" \
| jq -r '.items[0].author.login')
cache_nickname[$email]=$username
fi
if [[ $username != null ]]; then
sed -i "s#$author_info#$username#g" "commits.md"
continue
fi
fi
if [[ "$email" =~ (^[0-9]+)?\+?([a-zA-Z0-9]+)@ ]]; then
id="${BASH_REMATCH[1]}"; mail_name="${BASH_REMATCH[2]}";
username=$name && [[ -n $id ]] && username=$mail_name
fi
sed -i "s#$author_info#$username#g" "commits.md"
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