Skip to content

Reassign Copilot Commit #26

Reassign Copilot Commit

Reassign Copilot Commit #26

name: Reassign Copilot Commit
on:
workflow_dispatch:
inputs:
pr_number:
description: 'Pull request number to check (for debugging)'
required: false
type: string
branch:
description: 'Branch to checkout (for debugging)'
required: false
type: string
pull_request:
types: [labeled]
jobs:
check-copilot-commits:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.label.name == 'revoke_copilot_commit')
steps:
- name: Set branch to checkout
id: setbranch
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
BRANCH=""
if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.pr_number }}" ]; then
BRANCH=$(gh pr view ${{ github.event.inputs.pr_number }} --repo elastic/kibana --json headRefName -q '.headRefName')
elif [ -n "${{ github.event.pull_request.head.ref }}" ]; then
BRANCH="${{ github.event.pull_request.head.ref }}"
fi
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "Branch to checkout: $BRANCH"
- name: Checkout PR code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ steps.setbranch.outputs.branch }}
- name: Get PR number
id: prnum
run: |
if [ -n "${{ github.event.inputs.pr_number }}" ]; then
echo "PR_NUMBER=${{ github.event.inputs.pr_number }}" >> $GITHUB_ENV
elif [ -n "${{ github.event.pull_request.number }}" ]; then
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
else
echo "::error::PR number not found. Provide pr_number input or run on PR event."
exit 1
fi
- name: Get PR commits
id: commits
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
COMMITS=$(gh pr view $PR_NUMBER --repo elastic/kibana --json commits -q '.commits[].oid')
COMMITS_CSV=$(echo "$COMMITS" | paste -sd, -)
echo "COMMITS=$COMMITS_CSV" >> $GITHUB_ENV
echo "$COMMITS" > commits.txt
- name: Check for Copilot as author
id: check_copilot
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
COPILOT_EMAILS="github-actions[bot]@users.noreply.github.com [email protected] [email protected] [email protected]"
FOUND=0
for COMMIT in $(cat commits.txt); do
echo "Checking commit: $COMMIT"
AUTHOR_EMAIL=$(git show -s --format='%ae' $COMMIT)
echo "Author email: $AUTHOR_EMAIL"
for EMAIL in $COPILOT_EMAILS; do
echo "Checking against Copilot email: $EMAIL"
if [ "$AUTHOR_EMAIL" = "$EMAIL" ]; then
FOUND=1
break
fi
done
done
echo "COPILOT_FOUND=$FOUND" >> $GITHUB_ENV
echo "found=$FOUND" >> $GITHUB_OUTPUT
- name: Get user info for commit author
id: userinfo
if: steps.check_copilot.outputs.found == '1' || github.event_name == 'workflow_dispatch'
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
USER_LOGIN="${{ github.actor }}"
USER_EMAIL=""
USER_NAME=""
if [ "${{ github.event_name }}" = "pull_request" ]; then
USER_LOGIN="${{ github.event.sender.login }}"
fi
USER_API=$(gh api users/$USER_LOGIN)
USER_NAME=$(echo "$USER_API" | jq -r '.name // .login')
USER_EMAIL=$(echo "$USER_API" | jq -r '.email')
if [ -z "$USER_EMAIL" ] || [ "$USER_EMAIL" = "null" ]; then
USER_EMAIL="[email protected]"
fi
echo "user_name=$USER_NAME" >> $GITHUB_OUTPUT
echo "user_email=$USER_EMAIL" >> $GITHUB_OUTPUT
echo "Using author: $USER_NAME <$USER_EMAIL>"
- name: Squash and re-author commits as PR labeler or workflow actor
if: steps.check_copilot.outputs.found == '1'
env:
GIT_AUTHOR_NAME: ${{ steps.userinfo.outputs.user_name }}
GIT_AUTHOR_EMAIL: ${{ steps.userinfo.outputs.user_email }}
GIT_COMMITTER_NAME: ${{ steps.userinfo.outputs.user_name }}
GIT_COMMITTER_EMAIL: ${{ steps.userinfo.outputs.user_email }}
GH_TOKEN: ${{ secrets.GH_PAT }}
run: |
set -e
echo "⚠️ Copilot is the author of one or more commits. Attempting to squash and re-author all commits as $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>."
# Get the base branch (assume PR is up to date with base)
BASE_BRANCH=$(gh pr view $PR_NUMBER --repo elastic/kibana --json baseRefName -q '.baseRefName')
echo "Base branch: $BASE_BRANCH"
# Find merge base
MERGE_BASE=$(git merge-base origin/$BASE_BRANCH HEAD)
echo "Merge base: $MERGE_BASE"
# Gather unique Copilot author emails from commits
COPILOT_EMAILS="github-actions[bot]@users.noreply.github.com [email protected] [email protected] [email protected]"
COAUTHORS=""
for COMMIT in $(cat commits.txt); do
AUTHOR_NAME=$(git show -s --format='%an' $COMMIT)
AUTHOR_EMAIL=$(git show -s --format='%ae' $COMMIT)
for EMAIL in $COPILOT_EMAILS; do
if [ "$AUTHOR_EMAIL" = "$EMAIL" ]; then
TRAILER="Co-authored-by: $AUTHOR_NAME <$AUTHOR_EMAIL>"
if ! echo "$COAUTHORS" | grep -q "$TRAILER"; then
COAUTHORS="$COAUTHORS\n$TRAILER"
fi
fi
done
done
# Squash all commits since merge base
git reset --soft $MERGE_BASE
COMMIT_MSG="Squashed all commits and re-authored as $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
if [ -n "$COAUTHORS" ]; then
COMMIT_MSG="$COMMIT_MSG\n\n$(echo -e "$COAUTHORS" | grep Co-authored-by)"
fi
git commit -a -m "$COMMIT_MSG"
# Force push to current branch
git push --force-with-lease
echo "✅ All commits squashed, re-authored, and Copilot credited as co-author(s)."
- name: Fail if Copilot is author and squash failed or not permitted
if: steps.check_copilot.outputs.found == '1' && (failure() || cancelled())
run: |
echo "❌ Copilot is the author of one or more commits in this PR. Please rebase and change the commit author to yourself before merging. If this is a fork or protected branch, you must do this locally."
exit 1