#!/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 <