From 4b25f5864dbf6f3d790d86eca00149b16f0f8ac7 Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Fri, 9 Jan 2026 05:33:34 +0100 Subject: [PATCH] feat(release): enhance release workflow with improvements Adds fallback to main branch if DEFAULT_BRANCH is empty and updates the way CHANGELOG.md and .version files are handled in the release process. Refactors checks for existing branches and includes detailed logging for transparency. This improves the robustness of the release workflow and ensures relevant files are created or updated correctly. --- .gitea/workflows/Release.yml | 118 +++++++++++++++++++++++++---------- 1 file changed, 85 insertions(+), 33 deletions(-) diff --git a/.gitea/workflows/Release.yml b/.gitea/workflows/Release.yml index 50cfa9f..6dce6f8 100644 --- a/.gitea/workflows/Release.yml +++ b/.gitea/workflows/Release.yml @@ -84,16 +84,15 @@ jobs: REPO=$(echo "${REPOSITORY}" | cut -d'/' -f2) API_URL="${GITEA_URL}/api/v1/repos/${OWNER}/${REPO}" + # Fallback to main if DEFAULT_BRANCH is empty + BASE_BRANCH="${DEFAULT_BRANCH:-main}" + echo "Using base branch: ${BASE_BRANCH}" + TITLE="chore(release): prepare for ${VERSION}" - # Read CHANGES.md and escape for JSON - DESCRIPTION=$(cat CHANGES.md | jq -Rs .) - DESCRIPTION="${DESCRIPTION:1:-1}" # Remove surrounding quotes from jq - - # Add squash merge reminder - DESCRIPTION="${DESCRIPTION} - - --- - **Note:** Please use **Squash Merge** when merging this PR." + # Read CHANGES.md content and add note (jq --arg will handle JSON escaping) + CHANGES_CONTENT=$(cat CHANGES.md) + PR_NOTE="**Note:** Please use **Squash Merge** when merging this PR." + DESCRIPTION="${CHANGES_CONTENT}"$'\n\n---\n\n'"${PR_NOTE}" echo "Checking for existing release PRs..." PRS=$(curl -sf \ @@ -102,9 +101,16 @@ jobs: PR_INDEX=$(echo "${PRS}" | jq -r '.[0].number // empty') echo "Checking for existing next-release branch..." - BRANCH_EXISTS=$(curl -sf \ + BRANCH_CHECK=$(curl -s -w "%{http_code}" -o /dev/null \ -H "Authorization: token ${TOKEN}" \ - "${API_URL}/branches/next-release" 2>/dev/null && echo "true" || echo "false") + "${API_URL}/branches/next-release") + echo "Branch check HTTP status: ${BRANCH_CHECK}" + if [ "${BRANCH_CHECK}" = "200" ]; then + BRANCH_EXISTS="true" + else + BRANCH_EXISTS="false" + fi + echo "Branch exists: ${BRANCH_EXISTS}" # Prepare CHANGELOG.md content CHANGELOG_CONTENT=$(base64 -w0 < CHANGELOG.md) @@ -174,28 +180,75 @@ jobs: "${API_URL}/contents/.version" fi else - echo "Creating new next-release branch with CHANGELOG.md..." - curl -sf -X POST \ - -H "Authorization: token ${TOKEN}" \ - -H "Content-Type: application/json" \ - --data "$(jq -n \ - --arg content "${CHANGELOG_CONTENT}" \ - --arg message "${TITLE}" \ - --arg branch "${DEFAULT_BRANCH}" \ - --arg new_branch "next-release" \ - '{content: $content, message: $message, branch: $branch, new_branch: $new_branch}')" \ - "${API_URL}/contents/CHANGELOG.md" + echo "Creating new next-release branch from ${BASE_BRANCH}..." - echo "Adding .version to next-release branch..." - curl -sf -X POST \ + # Check if CHANGELOG.md exists on base branch to determine create vs update + CHANGELOG_SHA=$(curl -sf \ -H "Authorization: token ${TOKEN}" \ - -H "Content-Type: application/json" \ - --data "$(jq -n \ - --arg content "${VERSION_CONTENT}" \ - --arg message "${TITLE}" \ - --arg branch "next-release" \ - '{content: $content, message: $message, branch: $branch}')" \ - "${API_URL}/contents/.version" + "${API_URL}/contents/CHANGELOG.md?ref=${BASE_BRANCH}" | jq -r '.sha // empty') + + if [ -n "${CHANGELOG_SHA}" ]; then + echo "Updating CHANGELOG.md (exists on ${BASE_BRANCH}) on new branch..." + RESPONSE=$(curl -s -w "\n%{http_code}" -X PUT \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/json" \ + --data "$(jq -n \ + --arg content "${CHANGELOG_CONTENT}" \ + --arg sha "${CHANGELOG_SHA}" \ + --arg message "${TITLE}" \ + --arg branch "${BASE_BRANCH}" \ + --arg new_branch "next-release" \ + '{content: $content, sha: $sha, message: $message, branch: $branch, new_branch: $new_branch}')" \ + "${API_URL}/contents/CHANGELOG.md") + else + echo "Creating CHANGELOG.md on new branch..." + RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/json" \ + --data "$(jq -n \ + --arg content "${CHANGELOG_CONTENT}" \ + --arg message "${TITLE}" \ + --arg branch "${BASE_BRANCH}" \ + --arg new_branch "next-release" \ + '{content: $content, message: $message, branch: $branch, new_branch: $new_branch}')" \ + "${API_URL}/contents/CHANGELOG.md") + fi + HTTP_CODE=$(echo "${RESPONSE}" | tail -1) + BODY=$(echo "${RESPONSE}" | sed '$d') + if [ "${HTTP_CODE}" -ge 400 ]; then + echo "Error with CHANGELOG.md: ${BODY}" + exit 1 + fi + + # Check if .version exists on base branch + VERSION_SHA=$(curl -sf \ + -H "Authorization: token ${TOKEN}" \ + "${API_URL}/contents/.version?ref=${BASE_BRANCH}" | jq -r '.sha // empty') + + if [ -n "${VERSION_SHA}" ]; then + echo "Updating .version on next-release branch..." + curl -sf -X PUT \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/json" \ + --data "$(jq -n \ + --arg content "${VERSION_CONTENT}" \ + --arg sha "${VERSION_SHA}" \ + --arg message "${TITLE}" \ + --arg branch "next-release" \ + '{content: $content, sha: $sha, message: $message, branch: $branch}')" \ + "${API_URL}/contents/.version" + else + echo "Creating .version on next-release branch..." + curl -sf -X POST \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/json" \ + --data "$(jq -n \ + --arg content "${VERSION_CONTENT}" \ + --arg message "${TITLE}" \ + --arg branch "next-release" \ + '{content: $content, message: $message, branch: $branch}')" \ + "${API_URL}/contents/.version" + fi fi if [ -n "${PR_INDEX}" ]; then @@ -275,8 +328,7 @@ jobs: REPO=$(echo "${REPOSITORY}" | cut -d'/' -f2) API_URL="${GITEA_URL}/api/v1/repos/${OWNER}/${REPO}" - MESSAGE=$(cat CHANGES.md | jq -Rs .) - MESSAGE="${MESSAGE:1:-1}" # Remove surrounding quotes + MESSAGE=$(cat CHANGES.md) echo "Creating release ${VERSION}..." curl -sf -X POST \