-
Notifications
You must be signed in to change notification settings - Fork 9
137 lines (109 loc) · 4.37 KB
/
python-bump.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
name: Increment Version
on:
workflow_dispatch:
inputs:
next-version:
description: "override with custom version number"
required: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
bump:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: pipx install poetry
- uses: actions/setup-python@v4
with:
python-version: '3.10.x'
cache: 'poetry'
- run: poetry install
- name: Run bash script
shell: bash
run: |
# Gather new git history from after the last release
git log --format="%s" > log.txt
touch subjects.txt
touch types.txt
while IFS= read -r LINE; do
# Read line until colon symbol ( : )
SUBJECT_TYPE=$(echo $LINE | awk -F: '{print $1}')
# Stop iterating git log when reaching latest release
if [[ $SUBJECT_TYPE == 'release' ]]; then
break
fi
# Else continue saving subjects and commit types
echo "- $LINE" >> subjects.txt
echo $SUBJECT_TYPE >> types.txt
done < log.txt
# Save current release's version
VERSION=$(poetry version --short)
# Decide if programmatically finding next version or if manually provided one by user
METHOD="manual"
NEXT="${{ github.event.inputs.next-version }}"
if [[ -z "$NEXT" ]]; then
METHOD="programmatic"
# Determine which version number to bump
BUMP_TYPE="nv" # Meaning non-versioning commit, or affecting something outside the API. Basically ignore the types: build, chore, ci, docs, perf, refactor, revert, style, test.
while IFS= read -r TYPE; do
if [[ "$TYPE" == "BREAK" ]]; then
BUMP_TYPE="major"
break
elif [[ "$BUMP_TYPE" != "minor" ]]; then
if [[ "$TYPE" == "feat" ]]; then
BUMP_TYPE="minor"
elif [[ "$TYPE" == "fix" ]]; then
BUMP_TYPE="patch"
fi
fi
done < types.txt
# Instantiate version numbers
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
# Get version number for next release
if [ "$BUMP_TYPE" == "major" ]; then
# Backwards incompatible API changes
NEXT="$((MAJOR+1)).0.0"
elif [ "$BUMP_TYPE" == "minor" ]; then
# Backwards compatible API additions/changes
NEXT="$MAJOR.$((MINOR+1)).0"
elif [ "$BUMP_TYPE" == "patch" ]; then
# Bug fixes not affecting the API
NEXT="$MAJOR.$MINOR.$((PATCH+1))"
else
NEXT=$VERSION
fi
fi
if [[ "$VERSION" != "$NEXT" ]]; then
# Create body of Pull Request
echo "### History" >> subjects.txt
echo "" >> subjects.txt
echo "Next: $NEXT ($METHOD)" >> subjects.txt
echo "Latest: $VERSION" >> subjects.txt
echo "### Release" >> subjects.txt
# Reverses all lines in subjects.txt
tac subjects.txt > temp.txt
BODY=$(cat temp.txt)
# Clean up files
rm log.txt
rm subjects.txt
rm types.txt
rm temp.txt
# Bump version in pyproject.toml
sed -i "s/version = \"$VERSION\"/version = \"$NEXT\"/g" pyproject.toml
# Set up git config for github-actions bot
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
# Set author to the user running workflow
AUTHOR="${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>"
# Checkout new branch
git checkout -B "release-$NEXT"
# Commit pyproject.toml
git add pyproject.toml
git commit -m "release: bump poetry version in pyproject.toml" --author="$AUTHOR"
# Push branch
git push --set-upstream origin "release-$NEXT" --atomic
# Open Pull Request
gh pr create --base "main" --title "release: increment version from $VERSION to $NEXT" --body "$BODY"
fi