Skip to content

Maybe auto-merge #11710

Maybe auto-merge

Maybe auto-merge #11710

name: Maybe auto-merge
on:
pull_request_target:
branches: [main]
types: [opened, reopened, synchronize, ready_for_review]
workflow_run:
workflows: ["CI", "CodeQL"]
types: [completed]
schedule:
- cron: "*/15 * * * *"
workflow_dispatch:
inputs:
pr:
description: "Optional PR number to retry"
required: false
type: string
jobs:
label-and-merge:
runs-on: ubuntu-latest
permissions:
actions: write
contents: write
issues: write
pull-requests: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Capture trusted scripts
run: |
set -euo pipefail
mkdir -p "$RUNNER_TEMP/trusted-scripts"
cp .github/scripts/label-pr.sh "$RUNNER_TEMP/trusted-scripts/label-pr.sh"
cp .github/scripts/label_pr_csv_diff.py "$RUNNER_TEMP/trusted-scripts/label_pr_csv_diff.py"
cp .github/scripts/close-linked-company-request-issues.sh "$RUNNER_TEMP/trusted-scripts/close-linked-company-request-issues.sh"
cp .github/scripts/dispatch-company-production-sync.sh "$RUNNER_TEMP/trusted-scripts/dispatch-company-production-sync.sh"
cp .github/scripts/maybe-auto-merge-pr.sh "$RUNNER_TEMP/trusted-scripts/maybe-auto-merge-pr.sh"
cp .github/scripts/dispatch-pr-checks.sh "$RUNNER_TEMP/trusted-scripts/dispatch-pr-checks.sh"
chmod +x "$RUNNER_TEMP/trusted-scripts/"*.sh
- name: Select PRs
id: prs
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
EVENT_NAME: ${{ github.event_name }}
INPUT_PR: ${{ github.event.inputs.pr || '' }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
set -euo pipefail
prs_file="$RUNNER_TEMP/company-prs.txt"
: > "$prs_file"
owner="${REPO%%/*}"
default_branch="${DEFAULT_BRANCH:-main}"
select_open_company_prs() {
gh pr list --repo "$REPO" --state open --limit 100 \
--json number,isDraft,headRefName,headRepositoryOwner \
--jq ".[] | select(.isDraft == false and .headRepositoryOwner.login == \"$owner\" and (.headRefName | startswith(\"add-company/\"))) | .number"
}
if [[ "$EVENT_NAME" == "pull_request_target" ]]; then
jq -r '.pull_request.number' "$GITHUB_EVENT_PATH" >> "$prs_file"
elif [[ "$EVENT_NAME" == "workflow_run" ]]; then
branch=$(jq -r '.workflow_run.head_branch // ""' "$GITHUB_EVENT_PATH")
head_repo=$(jq -r '.workflow_run.head_repository.full_name // ""' "$GITHUB_EVENT_PATH")
if [[ "$head_repo" == "$REPO" && "$branch" == "$default_branch" ]]; then
select_open_company_prs >> "$prs_file"
else
jq -r '.workflow_run.pull_requests[]?.number' "$GITHUB_EVENT_PATH" >> "$prs_file"
if [[ ! -s "$prs_file" && "$head_repo" == "$REPO" && "$branch" == add-company/* ]]; then
gh pr list --repo "$REPO" --state open --head "$branch" \
--json number --jq '.[].number' >> "$prs_file"
fi
fi
elif [[ -n "$INPUT_PR" ]]; then
echo "$INPUT_PR" >> "$prs_file"
else
select_open_company_prs >> "$prs_file"
fi
sort -nu "$prs_file" > "$RUNNER_TEMP/company-prs.sorted.txt"
count=$(wc -l < "$RUNNER_TEMP/company-prs.sorted.txt" | tr -d ' ')
echo "count=$count" >> "$GITHUB_OUTPUT"
echo "file=$RUNNER_TEMP/company-prs.sorted.txt" >> "$GITHUB_OUTPUT"
cat "$RUNNER_TEMP/company-prs.sorted.txt"
- name: Label, rebase, and merge
if: steps.prs.outputs.count != '0'
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
TRUSTED_SCRIPTS_DIR: ${{ runner.temp }}/trusted-scripts
run: |
set -euo pipefail
while IFS= read -r pr; do
[[ -z "$pr" ]] && continue
echo "::group::Process PR #$pr"
PR="$pr" "$TRUSTED_SCRIPTS_DIR/maybe-auto-merge-pr.sh"
echo "::endgroup::"
done < "${{ steps.prs.outputs.file }}"