diff --git a/.github/workflows/replace-claude.yaml b/.github/workflows/replace-claude.yaml new file mode 100644 index 00000000..86ef70ae --- /dev/null +++ b/.github/workflows/replace-claude.yaml @@ -0,0 +1,67 @@ +name: Take Ownership of Claude PRs +on: + pull_request: + types: [opened, synchronize] + +# Rational: claude is a tool, not an author. +jobs: + rewrite-author: + # Adjust this filter if your bot profile matches a specific string + # or leave it wide open to catch any AI-assisted PR branch + if: contains(github.event.pull_request.labels.*.name, 'claude') || github.event.pull_request.user.login == 'YOUR_GITHUB_USERNAME' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + fetch-depth: 0 + + - name: Checkout PR Branch + run: git checkout ${{ github.event.pull_request.head.ref }} + + - name: Fetch Dynamic User Profile Info + id: get_user + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # 1. Capture the exact username who opened the PR + USERNAME="${{ github.event.pull_request.user.login }}" + + # 2. Get the user ID to construct a valid GitHub noreply email + USER_ID=$(curl -s -H "Authorization: token $GH_TOKEN" "https://github.com" | jq '.id') + + # 3. Query the GitHub API for the user's public Name profile field + PUBLIC_NAME=$(curl -s -H "Authorization: token $GH_TOKEN" "https://github.com" | jq -r '.name') + + # Fallback to username if your GitHub "Name" profile field is blank + if [ "$PUBLIC_NAME" == "null" ] || [ -z "$PUBLIC_NAME" ]; then + PUBLIC_NAME="$USERNAME" + fi + + # Construct your standard, privacy-protected GitHub email template + NOREPLY_EMAIL="${USER_ID}+${USERNAME}@users.noreply.github.com" + + # Export variables securely to subsequent steps + echo "git_name=$PUBLIC_NAME" >> $GITHUB_OUTPUT + echo "git_email=$NOREPLY_EMAIL" >> $GITHUB_OUTPUT + + - name: Configure Git Credentials Dynamically + run: | + git config --global user.name "${{ steps.get_user.outputs.git_name }}" + git config --global user.email "${{ steps.get_user.outputs.git_email }}" + + - name: Rewrite Commit Authors + run: | + # Dynamically swaps out the author metadata using the fetched credentials + git filter-branch -f --env-filter ' + export GIT_AUTHOR_NAME="${{ steps.get_user.outputs.git_name }}" + export GIT_AUTHOR_EMAIL="${{ steps.get_user.outputs.git_email }}" + export GIT_COMMITTER_NAME="${{ steps.get_user.outputs.git_name }}" + export GIT_COMMITTER_EMAIL="${{ steps.get_user.outputs.git_email }}" + ' origin/${{ github.event.pull_request.base.ref }}..HEAD + + - name: Force Push Clean Commits + run: git push origin ${{ github.event.pull_request.head.ref }} --force