-
-
Notifications
You must be signed in to change notification settings - Fork 0
252 lines (235 loc) · 9.51 KB
/
Copy pathversion-controller.yml
File metadata and controls
252 lines (235 loc) · 9.51 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
---
# .github/workflows/version-controller.yml
name: Version Controller
on:
push:
branches:
- dev
- test
- prod
- main
permissions:
contents: write
pull-requests: write
jobs:
version-controller:
runs-on: ubuntu-latest
env:
REPO_NAME: ${{ github.event.repository.name }}
REPO_FULL: ${{ github.repository }}
steps:
- name: Checkout Repository
id: checkout_repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
id: setup_python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install Dependencies
id: install_dependencies
run: |
pip install bump2version toml
- name: Get Current Version
id: get_version
run: |
VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['tool']['poetry']['version'])")
echo "current_version=$VERSION" >> $GITHUB_OUTPUT
- name: Get Latest Commit Message
id: get_commit
run: |
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo "commit_message<<EOF" >> $GITHUB_OUTPUT
echo "$COMMIT_MESSAGE" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Determine Branch
id: determine_branch
run: |
CURRENT_BRANCH=${GITHUB_REF#refs/heads/}
if [ "$CURRENT_BRANCH" = "dev" ]; then
NEXT_BRANCH="test"
elif [ "$CURRENT_BRANCH" = "test" ]; then
NEXT_BRANCH="prod"
elif [ "$CURRENT_BRANCH" = "prod" ]; then
NEXT_BRANCH="main"
else
NEXT_BRANCH=""
fi
echo "next_branch=$NEXT_BRANCH" >> $GITHUB_OUTPUT
echo "current_branch=$CURRENT_BRANCH" >> $GITHUB_OUTPUT
- name: Add Modules
id: add_modules
run: |
ls -la
git submodule add --force -b ${{ steps.determine_branch.outputs.current_branch }} https://github.com/JuanVilla424/scripts.git
- name: Run Changelog Generator
id: run_changelog
run: |
python scripts/generate_changelog/main.py
- name: Commit Changelog Updates
id: commit_changelog
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add CHANGELOG.md || true
git diff --cached --quiet || git commit -m "docs(core): update changelog"
- name: Check for Forbidden Character
id: check_arrow
run: |
COMMIT_MSG="${COMMIT_MESSAGE}"
if [[ "$COMMIT_MSG" == *"→"* && "$COMMIT_MSG" == *"Bump version:"* ]]; then
echo "contains_arrow=true" >> $GITHUB_OUTPUT
else
echo "contains_arrow=false" >> $GITHUB_OUTPUT
fi
env:
COMMIT_MESSAGE: ${{ steps.get_commit.outputs.commit_message }}
- name: Create Tag
id: create_tag
if: steps.check_arrow.outputs.contains_arrow == 'true'
run: |
VERSION=${{ steps.get_version.outputs.current_version }}
BRANCH_NAME=${{ steps.determine_branch.outputs.current_branch }}
TAG_NAME="v${VERSION}-${BRANCH_NAME}"
if git rev-parse "$TAG_NAME" >/dev/null 2>&1; then
echo "Tag $TAG_NAME already exists — skipping creation"
else
git tag "$TAG_NAME"
fi
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
- name: Push Tag
id: push_tag
if: steps.check_arrow.outputs.contains_arrow == 'true' && steps.create_tag.outputs.tag_name != ''
run: |
git push origin "${{ steps.create_tag.outputs.tag_name }}" || echo "Tag already on remote — skipping"
- name: Ensure on Current Branch
id: ensure_branch
if: steps.check_arrow.outputs.contains_arrow == 'true'
run: |
git checkout "${{ steps.determine_branch.outputs.current_branch }}"
- name: Create Pull Request
id: create_pull_request
if: steps.check_arrow.outputs.contains_arrow == 'true' && github.ref != 'refs/heads/main'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const head = '${{ steps.determine_branch.outputs.current_branch }}';
const base = '${{ steps.determine_branch.outputs.next_branch }}';
const version = '${{ steps.create_tag.outputs.tag_name }}';
const title = `🔖 From ${head} → Bump version: ${version} into ${base}`;
const body = `Automatically created pull request for release ${version} into ${base} branch.`;
const { data: existingPRs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: head,
base: base,
state: 'open'
});
if (existingPRs.length === 0) {
const { data: pr } = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
head: head,
base: base,
title: title,
body: body
});
console.log(`Created PR #${pr.number}: ${pr.html_url}`);
} else {
console.log(`PR already exists: ${existingPRs[0].html_url}`);
}
- name: Push Tag for Main Branch
id: push_tag_to_main
if: github.ref == 'refs/heads/main' && steps.check_arrow.outputs.contains_arrow == 'true'
run: |
VERSION=${{ steps.get_version.outputs.current_version }}
TAG_NAME="v${VERSION}"
git tag "$TAG_NAME"
git push origin "$TAG_NAME"
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
- name: Generate Release Notes
id: release_notes
if: github.ref == 'refs/heads/main' && steps.check_arrow.outputs.contains_arrow == 'true'
run: |
TAG="${{ steps.push_tag_to_main.outputs.tag_name }}"
PREV_TAG=$(git describe --tags --abbrev=0 "${TAG}^" 2>/dev/null || echo "")
if [ -z "$PREV_TAG" ]; then
COMMITS=$(git log "$TAG" --pretty=format:"%s")
else
COMMITS=$(git log "${PREV_TAG}..${TAG}" --pretty=format:"%s")
fi
FEATURES=""
FIXES=""
DOCS=""
REFACTORS=""
PERF=""
CHORES=""
STYLES=""
TESTS=""
OTHER=""
while IFS= read -r line; do
case "$line" in
"Bump version:"*|"Merge branch"*|"Merge pull request"*) continue ;;
esac
clean=$(echo "$line" | sed 's/^[^a-zA-Z]* *//')
if echo "$clean" | grep -qE '^(feat|fix|docs|style|refactor|perf|test|chore)(\([^)]+\))?:'; then
TYPE=$(echo "$clean" | sed -E 's/^(feat|fix|docs|style|refactor|perf|test|chore).*/\1/')
DESC=$(echo "$clean" | sed -E 's/^(feat|fix|docs|style|refactor|perf|test|chore)(\([^)]+\))?:\s*//')
DESC=$(echo "$DESC" | sed -E 's/\s*\[(minor|major|patch) candidate\]\s*$//')
SCOPE=$(echo "$clean" | sed -nE 's/^[a-z]+\(([^)]+)\):.*/\1/p')
if [ -n "$SCOPE" ]; then
ENTRY="- **${SCOPE}**: ${DESC}"
else
ENTRY="- ${DESC}"
fi
case "$TYPE" in
feat) FEATURES="${FEATURES}${ENTRY}\n" ;;
fix) FIXES="${FIXES}${ENTRY}\n" ;;
docs) DOCS="${DOCS}${ENTRY}\n" ;;
style) STYLES="${STYLES}${ENTRY}\n" ;;
refactor) REFACTORS="${REFACTORS}${ENTRY}\n" ;;
perf) PERF="${PERF}${ENTRY}\n" ;;
test) TESTS="${TESTS}${ENTRY}\n" ;;
chore) CHORES="${CHORES}${ENTRY}\n" ;;
esac
else
if [ -n "$clean" ]; then
OTHER="${OTHER}- ${clean}\n"
fi
fi
done <<< "$COMMITS"
BODY="## What's Changed\n\n"
[ -n "$FEATURES" ] && BODY="${BODY}### Features\n${FEATURES}\n"
[ -n "$FIXES" ] && BODY="${BODY}### Bug Fixes\n${FIXES}\n"
[ -n "$DOCS" ] && BODY="${BODY}### Documentation\n${DOCS}\n"
[ -n "$REFACTORS" ] && BODY="${BODY}### Refactors\n${REFACTORS}\n"
[ -n "$PERF" ] && BODY="${BODY}### Performance\n${PERF}\n"
[ -n "$STYLES" ] && BODY="${BODY}### Styles\n${STYLES}\n"
[ -n "$TESTS" ] && BODY="${BODY}### Tests\n${TESTS}\n"
[ -n "$CHORES" ] && BODY="${BODY}### Chores\n${CHORES}\n"
[ -n "$OTHER" ] && BODY="${BODY}### Other\n${OTHER}\n"
if [ -n "$PREV_TAG" ]; then
BODY="${BODY}**Full Changelog**: https://github.com/${REPO_FULL}/compare/${PREV_TAG}...${TAG}\n"
fi
{
echo "body<<RELEASE_NOTES_EOF"
echo -e "$BODY"
echo "RELEASE_NOTES_EOF"
} >> "$GITHUB_OUTPUT"
env:
REPO_FULL: ${{ env.REPO_FULL }}
- name: Create GitHub Release
id: create_release
if: github.ref == 'refs/heads/main' && steps.check_arrow.outputs.contains_arrow == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.push_tag_to_main.outputs.tag_name }}
name: Release ${{ steps.push_tag_to_main.outputs.tag_name }}
body: ${{ steps.release_notes.outputs.body }}
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}