-
Notifications
You must be signed in to change notification settings - Fork 122
137 lines (107 loc) · 4.96 KB
/
dev_version_update.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
name: Dev Version Check and Update
on:
pull_request_review:
types: [submitted]
jobs:
check-version:
if: github.event.review.state == 'approved' && github.event.pull_request.base.ref == 'develop'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}
- name: Set up Julia
uses: julia-actions/setup-julia@latest
with:
version: '1.x'
- uses: julia-actions/cache@v2
- name: Configure Git
run: |
git config user.name "GitHub Actions Bot"
git config user.email "[email protected]"
- name: Check and Update Version
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
run: |
# Get PR base branch and export it
export BASE_BRANCH=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER" | \
jq -r .base.ref)
julia -e '
# Get base branch directly from environment variable
base_branch = ENV["BASE_BRANCH"]
# First ensure we have the base branch
run(`git fetch origin $(base_branch)`)
function parse_version(version_str)
# Extract M.m.p and optional dev number
base_pattern = r"^(\d+\.\d+\.\d+)(?:-dev\.(\d+))?$"
m = match(base_pattern, version_str)
if isnothing(m)
error("Invalid version format: $version_str")
end
version = m.captures[1]
dev_num = isnothing(m.captures[2]) ? nothing : parse(Int, m.captures[2])
return (version, dev_num)
end
function should_update_version(base_ver_str, current_ver_str)
base_ver, base_dev = parse_version(base_ver_str)
current_ver, current_dev = parse_version(current_ver_str)
# If versions differ, no update needed
if base_ver != current_ver
return false
end
# If base has dev number, we should increment from the base dev number
if !isnothing(base_dev)
return true
end
# If base has no dev number and current has none, add dev.1
if isnothing(base_dev) && isnothing(current_dev)
return true
end
return false
end
function get_new_version(base_ver_str, current_ver_str)
base_ver, base_dev = parse_version(base_ver_str)
current_ver, current_dev = parse_version(current_ver_str)
# If base has a dev number, increment from that
if !isnothing(base_dev)
return "$(base_ver)-dev.$(base_dev + 1)"
end
# If no dev numbers exist, start with dev.1
return "$(current_ver)-dev.1"
end
function check_and_update_version()
# Get the base branch version
base_content = read(pipeline(`git show origin/$(base_branch):Project.toml`), String)
# Get current branch version
current_content = read(joinpath(pwd(), "Project.toml"), String)
# Extract versions using regex
version_pattern = r"version = \"(.*?)\""
base_version = match(version_pattern, base_content).captures[1]
current_version = match(version_pattern, current_content).captures[1]
println("Base version: $base_version")
println("Current version: $current_version")
if should_update_version(base_version, current_version)
println("Version needs updating")
new_version = get_new_version(base_version, current_version)
# Update the file
new_content = replace(current_content,
"version = \"$current_version\"" =>
"version = \"$new_version\"")
write(joinpath(pwd(), "Project.toml"), new_content)
# Commit and push the change
run(`git add $(joinpath(pwd(), "Project.toml"))`)
run(`git commit -m "Bump version to $new_version"`)
run(`git push`)
println("Version updated to $new_version")
else
println("Version already updated")
end
end
check_and_update_version()'