Dev Version Check and Update #13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()' |