#!/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"}" CARGO_PATH="${PLUGIN_CARGO_PATH:-"~/woodpecker/.cargo"}" # 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 ---" export CARGO_PATH="${CARGO_PATH}" ls -R pwd release-plz update --verbose --manifest-path="$(pwd)/Cargo.toml" # 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 <