Skip to content

#2524 update readme and add api files #2

#2524 update readme and add api files

#2524 update readme and add api files #2

Workflow file for this run

name: Backport changes
on:
pull_request:
types:
- closed
branches:
- main
jobs:
get-labels:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
branches: ${{ steps.get-labels.outputs.branches }}
user: ${{ steps.get-labels.outputs.user }}
valid: ${{ steps.get-labels.outputs.valid }}
steps:
- name: Determine branches to cherry-pick to
id: get-labels
run: |
set -e
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.ACTIONS_BOT_TOKEN }}" https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }})
LABELS_JSON=$(echo "$RESPONSE" | jq .labels[])
USER=$(echo $RESPONSE | jq -r '.user.login')
if [[ $? -ne 0 ]]; then
echo "Failed to fetch labels from GitHub API."
exit 1
fi
BACKPORT_LABELS=$(echo "$LABELS_JSON" | jq -r 'if type == "object" then [.] else . end | map(select(.name | startswith("backport to")))[] | .name')
if [[ -z "$BACKPORT_LABELS" ]]; then
echo "valid=false" >> $GITHUB_OUTPUT
exit 0
else
echo "valid=true" >> $GITHUB_OUTPUT
fi
BRANCHES=""
if [[ $BACKPORT_LABELS == "backport to all versions" ]]; then
PAGE=1
ALL_BRANCHES=""
# Loop to fetch all branches with 'v/' prefix from the GitHub API
while : ; do
RAW_RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.ACTIONS_BOT_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/branches?per_page=100&page=$PAGE")
if [[ $(echo "$RAW_RESPONSE" | jq '. | length') -eq 0 ]]; then
break
fi
PAGE_BRANCHES=$(echo "$RAW_RESPONSE" | jq -r '.[].name' | grep '^v/')
ALL_BRANCHES+=$PAGE_BRANCHES$'\n'
((PAGE++))
done
# Prepare the BRANCHES variable, remove trailing comma and newline
BRANCHES=$(echo "$ALL_BRANCHES" | tr '\n' ',')
BRANCHES=${BRANCHES%,} # Removing the trailing comma and newline
else
BRANCH_NAMES=$(echo "$BACKPORT_LABELS" | grep -o 'backport to v/[0-9]\+\.[0-9]\+' | sed -e 's/backport to //')
BRANCHES=$(echo "$BRANCH_NAMES" | tr '\n' ',')
BRANCHES=${BRANCHES::-1} # Removing the trailing comma
fi
# Convert BRANCHES into a valid JSON array
BRANCHES_ARRAY=$(echo "$BRANCHES" | tr ', ' '\n\n' | sed 's/^/"/;s/$/"/' | tr '\n' ',' | sed 's/,$//')
BRANCHES_ARRAY="[$BRANCHES_ARRAY]"
echo "branches=$BRANCHES_ARRAY" >> $GITHUB_OUTPUT
echo "user=$USER" >> $GITHUB_OUTPUT
backport:
needs: get-labels
if: needs.get-labels.outputs.branches != '' && needs.get-labels.outputs.valid == 'true'
strategy:
matrix:
branch: ${{fromJson(needs.get-labels.outputs.branches)}}
runs-on: ubuntu-latest
steps:
# Checkout the specified branch from redpanda repository.
- name: Checkout redpanda repository
uses: actions/checkout@v3
with:
fetch-depth: 0
repository: redpanda-data/docs
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
- name: Set up git config
run: |
echo "Setting up git config..."
git config user.name "GitHub Actions Bot"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Checkout maintenance branch and cherry-pick
run: |
echo "Fetching latest changes..."
git fetch
# Check if the branch exists
BRANCH_CHECK=$(git ls-remote --heads origin ${{ matrix.branch }})
if [[ -z "$BRANCH_CHECK" ]]; then
echo "Branch ${{ matrix.branch }} does not exist. Skipping."
exit 0
fi
echo "Checking out branch: ${{ matrix.branch }}..."
git checkout ${{ matrix.branch }} || (echo "Failed to checkout branch: ${{ matrix.branch }}." && exit 1)
echo "Cherry-picking changes..."
# Attempt cherry-pick and capture any errors
if ! git cherry-pick -x $GITHUB_SHA; then
echo "Cherry-pick had conflicts for branch ${{ matrix.branch }}. Creating GitHub issue for manual intervention."
# Create a GitHub issue
issue_title="Manual backport required for ${{ matrix.branch }}"
issue_body="A conflict occurred while backporting commit $GITHUB_SHA to branch ${{ matrix.branch }}. Manual intervention is required.\
\nTo manually apply the change, you must cherry-pick it locally and fix the conflicts:\
\n\`\`\`bash\
\n git fetch origin\
\n git checkout ${{ matrix.branch }}\
\n git pull origin ${{ matrix.branch }}\
\n git cherry-pick $GITHUB_SHA\
\n # Resolve any merge conflicts here, then commit the changes\
\n git push origin ${{ matrix.branch }}\
\n\`\`\`\
\nIf you no longer want to backport the change to this version, close this issue."
response=$(curl -sS -w "%{http_code}" -X POST \
-H "Authorization: token ${{ secrets.ACTIONS_BOT_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues \
-d "{ \"title\": \"$issue_title\", \"body\": \"$issue_body\", \"assignees\": [\"${{ needs.get-labels.outputs.user }}\"] }")
status_code=$(echo "$response" | tail -n 1)
if [[ $status_code -lt 200 || $status_code -gt 299 ]]; then
echo "Failed to create GitHub issue. HTTP status code: $status_code"
echo "$response"
exit 1
fi
json_response=$(echo "$response" | head -n -1)
issue_url=$(echo "$json_response" | jq '.html_url')
git cherry-pick --abort
echo "Failed to cherry-pick. Manual intervention might be needed. See the created issue: $issue_url"
else
echo "Pushing changes to branch: ${{ matrix.branch }}..."
git push
fi