Files
InfiniteAPI/.github/workflows/update-version.yml
T
Renato Alcara a905c0127f Enable auto-merge for version update PRs
Updated the workflow to enable auto-merge for pull requests with version updates.
2026-02-18 20:16:37 -03:00

361 lines
13 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Update WhatsApp Version
on:
schedule:
# Run daily at 09:00 UTC (optimized timing after WhatsApp deploys)
- cron: '0 9 * * *'
workflow_dispatch:
inputs:
force:
description: 'Force create PR even if version unchanged (useful for re-triggering)'
required: false
default: 'false'
type: boolean
# Dynamic run name with version
run-name: "Update WhatsApp Version ${{ github.run_number }}"
permissions:
contents: write
pull-requests: write
actions: write
jobs:
update-version:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Setup Node and Corepack
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Enable Corepack and Set Yarn Version
run: |
corepack enable
corepack prepare yarn@4.x --activate
- name: Configure Git for HTTPS
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Force git to use HTTPS instead of SSH for GitHub repositories
# IMPORTANT: Order matters! More specific rules first, then general ones
git config --global url."https://github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://github.com/".insteadOf "git@github.com:"
# Configure authentication (last so it doesn't interfere with SSH -> HTTPS conversion)
git config --global url."https://x-access-token:${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"
# Verify git config applied correctly
echo "✅ Git URL rewrites configured for HTTPS"
git config --global --get-regexp url
- name: Restore Yarn Cache
uses: actions/cache@v4
id: yarn-cache
with:
path: |
node_modules
.yarn/cache
.yarn/install-state.gz
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install packages
run: yarn install --immutable
- name: Update WhatsApp Web Version
id: update_version
run: |
# Run the update script, capture exit code
# Exit codes: 0=success, 1=error, 2=fetch failed (fallback used)
set +e
yarn update:version
EXIT_CODE=$?
set -e
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
# Read and output the updated version
VERSION=$(cat src/Defaults/baileys-version.json | grep -oP '"version":\s*\[\s*\K[0-9,\s]+' | tr -d ' ')
VERSION_STRING=$(echo $VERSION | sed 's/,/./g')
echo "version=$VERSION_STRING" >> $GITHUB_OUTPUT
echo "📱 WhatsApp Version: $VERSION_STRING"
# Only fail on fatal errors (exit code 1)
if [ $EXIT_CODE -eq 1 ]; then
echo "::error::Script failed with fatal error"
exit 1
fi
# Exit code 2 means fetch failed but fallback was used - this is a warning
if [ $EXIT_CODE -eq 2 ]; then
echo "::warning::Could not fetch latest version from WhatsApp servers"
fi
- name: Check for changes
id: check_changes
run: |
FORCE="${{ inputs.force }}"
if git diff --quiet; then
echo "has_changes=false" >> $GITHUB_OUTPUT
if [ "$FORCE" == "true" ]; then
echo "force_mode=true" >> $GITHUB_OUTPUT
echo "::notice::Force mode enabled - will create PR even without changes"
else
echo "force_mode=false" >> $GITHUB_OUTPUT
fi
else
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "force_mode=false" >> $GITHUB_OUTPUT
fi
- name: Create Pull Request
id: create_pr
if: steps.check_changes.outputs.has_changes == 'true' || steps.check_changes.outputs.force_mode == 'true'
env:
GH_TOKEN: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
run: |
BRANCH_NAME="update-version/stable"
FORCE="${{ inputs.force }}"
VERSION="${{ steps.update_version.outputs.version }}"
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Check if branch exists and delete it
git push origin --delete "$BRANCH_NAME" 2>/dev/null || true
# Create new branch
git checkout -b "$BRANCH_NAME"
# Only add the single source of truth file
git add src/Defaults/baileys-version.json
# Create commit (allow empty if force mode)
if [ "$FORCE" == "true" ] && git diff --cached --quiet; then
git commit --allow-empty -m "chore: force WhatsApp Web version check"
else
git commit -m "chore: update WhatsApp Web version to v$VERSION"
fi
git push origin "$BRANCH_NAME"
# Create PR using GitHub CLI with version in title
PR_TITLE="chore: update WhatsApp Web version to v$VERSION"
PR_BODY="Automated WhatsApp Web version update.
This PR updates the WhatsApp Web client revision to the latest version fetched from \`web.whatsapp.com\`.
**Version:** \`$VERSION\`
**Single source of truth:**
- \`src/Defaults/baileys-version.json\` (other files import from here)
**Update frequency:** Daily at 09:00 UTC"
if [ "$FORCE" == "true" ]; then
PR_BODY="$PR_BODY
> **Note:** This PR was created with force mode enabled."
fi
# Create PR (compatible with older gh CLI versions)
set +e
PR_URL=$(gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base master \
--head "$BRANCH_NAME" 2>&1)
PR_CREATE_EXIT=$?
set -e
if [ $PR_CREATE_EXIT -eq 0 ]; then
echo "✅ PR created successfully: $PR_URL"
# Extract PR number from URL
PR_NUMBER=$(echo "$PR_URL" | grep -oP '\d+$')
echo "📋 PR Number: #$PR_NUMBER"
# Save PR number for next step
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
else
# PR creation failed - could be duplicate or other error
echo "⚠️ Could not create PR. Checking if PR already exists..."
# Check for existing PR (without --json for compatibility)
EXISTING_PR=$(gh pr list --head "$BRANCH_NAME" --state open 2>/dev/null | head -1)
if [ -n "$EXISTING_PR" ]; then
echo "️ PR already exists for branch $BRANCH_NAME"
echo "$EXISTING_PR"
# Extract PR number from existing PR
PR_NUMBER=$(echo "$EXISTING_PR" | grep -oP '^\d+')
if [ -n "$PR_NUMBER" ]; then
echo "📋 Existing PR Number: #$PR_NUMBER"
# Save PR number for auto-merge step
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
fi
else
echo "❌ PR creation failed with error:"
echo "$PR_URL"
fi
fi
- name: Enable Auto-merge
if: steps.create_pr.outputs.pr_number != ''
env:
GH_TOKEN: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER="${{ steps.create_pr.outputs.pr_number }}"
VERSION="${{ steps.update_version.outputs.version }}"
echo "🔀 Enabling auto-merge for PR #$PR_NUMBER (v$VERSION)..."
# Enable auto-merge with squash method
set +e
AUTO_MERGE_OUTPUT=$(gh pr merge "$PR_NUMBER" \
--repo "${{ github.repository }}" \
--squash \
--auto \
--delete-branch 2>&1)
AUTO_MERGE_EXIT=$?
set -e
if [ $AUTO_MERGE_EXIT -eq 0 ]; then
echo "✅ Auto-merge enabled for PR #$PR_NUMBER (v$VERSION)"
echo "$AUTO_MERGE_OUTPUT"
echo "⏳ PR will be merged automatically once all checks pass"
else
echo "::warning::Failed to enable auto-merge for PR #$PR_NUMBER (v$VERSION)"
echo "::group::Auto-merge Error Output"
echo "$AUTO_MERGE_OUTPUT"
echo "::endgroup::"
# Fall back to waiting for checks and then merging
echo "🔄 Falling back to wait-and-merge strategy..."
# Wait for checks to start
echo "⏳ Waiting 30 seconds for checks to start..."
sleep 30
# Wait for checks to complete (max 5 minutes)
echo "⏳ Waiting for checks to complete..."
WAIT_COUNT=0
MAX_WAIT=60 # 60 * 5 seconds = 5 minutes
while [ $WAIT_COUNT -lt $MAX_WAIT ]; do
set +e
CHECK_STATUS=$(gh pr checks "$PR_NUMBER" --repo "${{ github.repository }}" 2>&1)
CHECK_EXIT=$?
set -e
if [ $CHECK_EXIT -eq 0 ]; then
echo "📊 Current check status:"
echo "$CHECK_STATUS"
# Check if all checks passed
if echo "$CHECK_STATUS" | grep -q "All checks have passed"; then
echo "✅ All checks passed!"
break
fi
# Check if any check failed
if echo "$CHECK_STATUS" | grep -qE "(fail|failure|error)"; then
echo "❌ Some checks failed, aborting merge"
exit 1
fi
fi
WAIT_COUNT=$((WAIT_COUNT + 1))
echo "⏳ Waiting... ($WAIT_COUNT/$MAX_WAIT)"
sleep 5
done
if [ $WAIT_COUNT -ge $MAX_WAIT ]; then
echo "::warning::Timeout waiting for checks to complete"
echo "::warning::PR #$PR_NUMBER requires manual merge: https://github.com/${{ github.repository }}/pull/$PR_NUMBER"
exit 0
fi
# Try to merge now that checks passed
echo "🔀 Attempting to merge PR #$PR_NUMBER..."
set +e
MERGE_OUTPUT=$(gh pr merge "$PR_NUMBER" \
--repo "${{ github.repository }}" \
--squash \
--delete-branch \
--yes 2>&1)
MERGE_EXIT=$?
set -e
if [ $MERGE_EXIT -eq 0 ]; then
echo "✅ PR #$PR_NUMBER (v$VERSION) merged successfully"
echo "$MERGE_OUTPUT"
else
echo "::error::Failed to merge PR #$PR_NUMBER (v$VERSION)"
echo "::group::Merge Error Output"
echo "$MERGE_OUTPUT"
echo "::endgroup::"
# Try with admin flag
echo "🔧 Attempting merge with admin override..."
set +e
ADMIN_OUTPUT=$(gh pr merge "$PR_NUMBER" \
--repo "${{ github.repository }}" \
--squash \
--delete-branch \
--admin 2>&1)
ADMIN_EXIT=$?
set -e
if [ $ADMIN_EXIT -eq 0 ]; then
echo "✅ PR #$PR_NUMBER (v$VERSION) merged with admin override"
echo "$ADMIN_OUTPUT"
else
echo "::error::Failed to merge even with admin override"
echo "::group::Admin Merge Error Output"
echo "$ADMIN_OUTPUT"
echo "::endgroup::"
echo "::warning::Consider configuring a Personal Access Token (PAT) with full repo permissions as GH_PAT secret"
echo "::warning::PR #$PR_NUMBER requires manual merge: https://github.com/${{ github.repository }}/pull/$PR_NUMBER"
exit 0
fi
fi
fi
- name: Summary
if: always()
run: |
echo "### WhatsApp Version Update" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
EXIT_CODE="${{ steps.update_version.outputs.exit_code }}"
HAS_CHANGES="${{ steps.check_changes.outputs.has_changes }}"
FORCE_MODE="${{ steps.check_changes.outputs.force_mode }}"
VERSION="${{ steps.update_version.outputs.version }}"
echo "**Version:** \`$VERSION\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ "$EXIT_CODE" == "2" ]; then
echo "⚠️ Warning: Could not fetch latest version from WhatsApp servers" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
fi
if [ "$HAS_CHANGES" == "true" ]; then
echo "✅ New version detected and PR created with auto-merge" >> $GITHUB_STEP_SUMMARY
elif [ "$FORCE_MODE" == "true" ]; then
echo "🔄 Force mode: PR created (no version change)" >> $GITHUB_STEP_SUMMARY
else
echo "️ Already up to date" >> $GITHUB_STEP_SUMMARY
fi