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 \