docker/rust/scripts/update-pr.sh
Victor Martinez Montané b2fcf71157 feat: refactor scripts & bump versions (#1)
Co-authored-by: JasterV <49537445+JasterV@users.noreply.github.com>
Reviewed-on: https://codeberg.org/JasterV/docker/pulls/1
2026-04-23 01:52:28 +02:00

161 lines
5.7 KiB
Bash
Executable file

#!/bin/sh
set -e
# ==============================================================================
# Global Configuration & Defaults
# ==============================================================================
# Woodpecker settings are passed as PLUGIN_* environment variables
TOKEN="${PLUGIN_TOKEN}"
BASE_BRANCH="${PLUGIN_BASE_BRANCH:-"main"}"
TEMP_BRANCH="${PLUGIN_TEMP_BRANCH:-"release-plz-update"}"
BOT_NAME="${PLUGIN_BOT_NAME:-"release-plz-bot"}"
BOT_EMAIL="${PLUGIN_BOT_EMAIL:-"bot@codeberg.org"}"
COMMIT_MESSAGE="${PLUGIN_COMMIT_MESSAGE:-"chore: release-plz update"}"
PR_TITLE="${PLUGIN_PR_TITLE:-"chore: release-plz update"}"
# Use Woodpecker CI built-in variables
REPO_FULL_NAME="${CI_REPO}" # e.g., "username/repo"
API_URL="https://codeberg.org/api/v1/repos/${REPO_FULL_NAME}"
# ------------------------------------------------------------------------------
# Checks if the required environment variables are provided before running the rest of the script.
#
# Arguments: None
# ------------------------------------------------------------------------------
validate_environment() {
if [ -z "$TOKEN" ]; then
echo "Error: 'token' setting is missing. Please provide a Codeberg Access Token."
exit 1
fi
}
# ------------------------------------------------------------------------------
# Executes the release-plz tool to bump versions and generate changelogs based on conventional commits.
#
# Arguments: None
# ------------------------------------------------------------------------------
run_release_plz() {
echo "--- Running release-plz update ---"
release-plz update --verbose --manifest-path="$(pwd)/Cargo.toml"
}
# ------------------------------------------------------------------------------
# Checks the current git workspace to see if release-plz modified any files (like Cargo.toml or CHANGELOG.md).
#
# Arguments: None
#
# Returns: 0 (True) if there are modified files, 1 (False) if clean.
# ------------------------------------------------------------------------------
has_changes() {
if [ -z "$(git status --porcelain)" ]; then
return 1 # False: No changes
else
return 0 # True: Changes exist
fi
}
# ------------------------------------------------------------------------------
# Configures the git bot identity, commits the modified files,and force-pushes them to a temporary branch on Codeberg.
#
# Arguments: None
# ------------------------------------------------------------------------------
commit_and_push_changes() {
echo "Committing changes to branch: $TEMP_BRANCH..."
git checkout -B "$TEMP_BRANCH"
git add .
# Using environment variables temporarily sets the identity for THIS commit only,
# preventing any accidental overwrites to a developer's global or local git config.
GIT_AUTHOR_NAME="$BOT_NAME" \
GIT_AUTHOR_EMAIL="$BOT_EMAIL" \
GIT_COMMITTER_NAME="$BOT_NAME" \
GIT_COMMITTER_EMAIL="$BOT_EMAIL" \
git commit -m "$COMMIT_MESSAGE"
echo "Pushing changes to Codeberg..."
# Force push to update the branch (and the linked PR if it already exists)
git push -f "https://$TOKEN@codeberg.org/${REPO_FULL_NAME}.git" "$TEMP_BRANCH"
}
# ------------------------------------------------------------------------------
# Queries the Codeberg API to see if a Pull Request already exists from our temporary branch to the main base branch.
#
# Arguments: None
#
# Returns: 0 (True) if an open PR exists, 1 (False) if it does not.
# ------------------------------------------------------------------------------
has_open_pull_request() {
local pr_search
# Search for an open PR from our specific head branch to the base branch
pr_search=$(curl -s -H "Authorization: token $TOKEN" \
"$API_URL/pulls?state=open&head=$TEMP_BRANCH&base=$BASE_BRANCH")
# Check if the JSON response contains our branch name in the "head" object
if echo "$pr_search" | grep -q "\"head\":{\"label\":\"$TEMP_BRANCH\""; then
return 0 # True
else
return 1 # False
fi
}
# ------------------------------------------------------------------------------
# Constructs a JSON payload and sends it to the Codeberg API to open a brand new Pull Request.
#
# Arguments: None
# ------------------------------------------------------------------------------
create_pull_request() {
local pr_payload
local create_response
echo "No open PR found. Creating a new one..."
pr_payload=$(cat <<EOF
{
"base": "$BASE_BRANCH",
"head": "$TEMP_BRANCH",
"title": "$PR_TITLE",
"body": "This is an automated PR generated by [release-plz](https://github.com/release-plz/release-plz) via Woodpecker CI."
}
EOF
)
create_response=$(curl -s -X 'POST' "$API_URL/pulls" \
-H "Authorization: token $TOKEN" \
-H "Content-Type: application/json" \
-d "$pr_payload")
# Verify if creation was successful by looking for an ID in the response
if echo "$create_response" | grep -q "\"id\":"; then
echo "Successfully created Pull Request!"
else
echo "Failed to create Pull Request. Response:"
echo "$create_response"
exit 1
fi
}
# ==============================================================================
# Main Execution Block
# ==============================================================================
validate_environment
run_release_plz
# If there are no changes, we can exit cleanly right away.
if ! has_changes; then
echo "No changes detected. Repository is up to date."
exit 0
fi
echo "Changes detected. Preparing to update Codeberg..."
commit_and_push_changes
echo "Checking for existing Pull Request..."
if has_open_pull_request; then
echo "An open Pull Request already exists, it has been updated."
else
create_pull_request
fi
echo "--- Release-plz plugin task completed ---"