Skip to content

Reassign Copilot Commit #21

Reassign Copilot Commit

Reassign Copilot Commit #21

name: Reassign Copilot Commit
on:
# pull_request:
# types: [opened, synchronize, reopened]
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
jobs:
check-copilot-commits:
runs-on: ubuntu-latest
steps:
- name: Checkout PR code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.inputs.branch || github.head_ref || github.ref_name }}
- 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 --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: Squash and re-author commits as [email protected]
if: steps.check_copilot.outputs.found == '1'
env:
GIT_AUTHOR_NAME: niros1
GIT_AUTHOR_EMAIL: [email protected]
GIT_COMMITTER_NAME: niros1
GIT_COMMITTER_EMAIL: [email protected]
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 [email protected]."
# Get the base branch (assume PR is up to date with base)
BASE_BRANCH=$(gh pr view $PR_NUMBER --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 [email protected]"
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