docker/release-plz/update-pr.sh

91 lines
2.9 KiB
Bash
Executable file

#!/bin/sh
set -e
# --- 1. 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}"
# --- 2. Validation ---
if [ -z "$TOKEN" ]; then
echo "Error: 'token' setting is missing. Please provide a Codeberg Access Token."
exit 1
fi
# --- 3. Run release-plz update ---
echo "--- Running release-plz update ---"
release-plz update
# Check if any files were changed (Cargo.toml, CHANGELOG.md, etc.)
if [ -z "$(git status --porcelain)" ]; then
echo "No changes detected. Repository is up to date."
exit 0
fi
echo "Changes detected. Preparing to update Codeberg..."
# --- 4. Git Setup & Push ---
# Configure identity for the commit
git config --global user.email "$BOT_EMAIL"
git config --global user.name "$BOT_NAME"
# Create or switch to the feature branch
git checkout -B "$TEMP_BRANCH"
git add .
git commit -m "$COMMIT_MESSAGE"
# Force push to update the branch (and the linked PR if it exists)
echo "Pushing changes to branch: $TEMP_BRANCH..."
git push -f "https://$TOKEN@codeberg.org/${REPO_FULL_NAME}.git" "$TEMP_BRANCH"
# --- 5. Pull Request Management ---
echo "Checking for existing Pull Request..."
# 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 search result contains our branch name
PR_EXISTS=$(echo "$PR_SEARCH" | grep -q "\"head\":{\"label\":\"$TEMP_BRANCH\"" && echo "yes" || echo "no")
if [ "$PR_EXISTS" = "no" ]; then
echo "No open PR found. Creating a new one..."
# Construct the JSON payload for the new PR
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/MarcoIeni/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")
# Check if creation was successful
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
else
echo "An open Pull Request already exists. Codeberg has updated it via the push."
fi
echo "--- Release-plz plugin task completed ---"