#!/bin/bash set -euo pipefail # Pre-commit hook for bumpver to generate changelog from Forgejo merge requests # This script queries the Forgejo API for merged pull requests since the last release # and appends them to the changelog file in AsciiDoc format # Configuration FORGEJO_URL="https://servala.app.codey.ch" REPO_OWNER="servala" REPO_NAME="servala-portal" CHANGELOG_FILE="docs/modules/ROOT/pages/web-portal-changelog.adoc" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check for required environment variable if [ -z "${FORGEJO_TOKEN:-}" ]; then echo -e "${RED}Error: FORGEJO_TOKEN environment variable is not set${NC}" echo "Please set FORGEJO_TOKEN to your Forgejo API token" exit 1 fi # Get the new version from pyproject.toml (bumpver updates this before running the hook) NEW_VERSION=$(grep -E 'current_version = ".*"' pyproject.toml | head -1 | sed -E 's/current_version = "(.*)"/\1/') echo -e "${GREEN}Generating changelog for version: ${NEW_VERSION}${NC}" # Get the previous tag PREVIOUS_TAG=$(git tag -l --sort=-v:refname | head -2 | tail -1) if [ -z "$PREVIOUS_TAG" ]; then echo -e "${YELLOW}Warning: No previous tag found, using all merge requests${NC}" # Get date of first commit SINCE_DATE=$(git log --reverse --format=%aI | head -1) else echo -e "${GREEN}Previous version: ${PREVIOUS_TAG}${NC}" # Get the date of the previous tag SINCE_DATE=$(git log -1 --format=%aI "${PREVIOUS_TAG}") fi echo -e "${GREEN}Fetching merge requests since ${SINCE_DATE}${NC}" # Query Forgejo API for closed/merged pull requests # Forgejo API returns pull requests sorted by updated time API_URL="${FORGEJO_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/pulls?state=closed&sort=updated&limit=100" RESPONSE=$(curl -s -H "Authorization: token ${FORGEJO_TOKEN}" "${API_URL}") # Check if curl was successful if [ $? -ne 0 ]; then echo -e "${RED}Error: Failed to fetch pull requests from Forgejo API${NC}" exit 1 fi # Filter merged PRs since the previous tag and group by labels # Using jq to parse JSON, filter by merge date, and group by labels CHANGELOG_ENTRIES=$(echo "${RESPONSE}" | jq -r --arg since "${SINCE_DATE}" ' # Filter merged PRs since the last tag [.[] | select(.merged_at != null and .merged_at > $since)] | sort_by(.merged_at) | reverse | # Group by primary label (first label if multiple, or "Uncategorized") group_by( if (.labels | length) > 0 then .labels[0].name else "Uncategorized" end ) | # Format each group map( # Group header "=== " + ( if .[0].labels | length > 0 then .[0].labels[0].name else "Uncategorized" end ) + "\n" + # List items in this group (map("* " + .title + " (link:" + .html_url + "[#" + (.number | tostring) + "])") | join("\n")) ) | join("\n\n") ') if [ -z "$CHANGELOG_ENTRIES" ]; then echo -e "${YELLOW}Warning: No merged pull requests found since ${PREVIOUS_TAG}${NC}" CHANGELOG_ENTRIES="=== Uncategorized\n\n* No changes recorded" fi # Create changelog section CHANGELOG_SECTION=" == ${NEW_VERSION} ${CHANGELOG_ENTRIES} " # Check if changelog file exists if [ ! -f "$CHANGELOG_FILE" ]; then echo -e "${RED}Error: Changelog file ${CHANGELOG_FILE} not found${NC}" exit 1 fi # Create temporary file with new changelog entry at the top TMP_FILE=$(mktemp) { # Keep the title head -1 "$CHANGELOG_FILE" # Add the new changelog section echo "$CHANGELOG_SECTION" # Add the rest of the file (skip the title) tail -n +2 "$CHANGELOG_FILE" } > "$TMP_FILE" # Replace the original file mv "$TMP_FILE" "$CHANGELOG_FILE" chmod 0644 "$CHANGELOG_FILE" # Add the changelog file to git git add "$CHANGELOG_FILE" echo -e "${GREEN}Changelog updated successfully${NC}" echo -e "${GREEN}Added ${CHANGELOG_FILE} to git staging area${NC}" # Save changelog for post-commit hook CHANGELOG_DIR=".git/changelog" mkdir -p "$CHANGELOG_DIR" echo "$CHANGELOG_ENTRIES" > "${CHANGELOG_DIR}/${NEW_VERSION}.txt" exit 0