forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (72 loc) · 3.24 KB
/
Copy pathsquash-pr.yml
File metadata and controls
91 lines (72 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# .github/workflows/squash-pr.yml
name: Squash Commits
on:
pull_request:
types:
- labeled
permissions:
# Permissions are still useful for the initial checkout and reading PR data
contents: read
pull-requests: read
jobs:
squash:
if: github.event.label.name == 'squash-and-merge'
runs-on: ubuntu-latest
steps:
# Step 1: Checkout using the PAT to have write access later
- name: Checkout PR Branch
uses: actions/checkout@v4
with:
# Use the PAT so we can push back to the repo
token: ${{ secrets.ACTIONS_PAT }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
# Step 2: Configure git with the user associated with the PAT
# This makes the commit appear from a user, not a bot
- name: Configure Git User
run: |
# Use a real user's info (or a dedicated bot user)
# to ensure other actions are triggered.
git config user.name "Your Name or Bot Name"
git config user.email "your-email@example.com"
# Step 3: Squash commits and create a detailed commit message
- name: Squash Commits and Generate Message
run: |
set -e # Exit on error
BASE_BRANCH=${{ github.event.pull_request.base.ref }}
MERGE_BASE=$(git merge-base origin/$BASE_BRANCH HEAD)
echo "Found merge base: $MERGE_BASE"
# --- GENERATE COMMIT MESSAGE ---
# 1. Get the subject from the very first commit
COMMIT_SUBJECT=$(git log --reverse --format=%s $MERGE_BASE..HEAD | head -n 1)
# 2. Get a formatted list of all commit messages for the body
COMMIT_BODY=$(git log --format="* %s (%h)" $MERGE_BASE..HEAD)
# --- GENERATE CO-AUTHOR TRAILERS ---
# 3. Get the name and email of the user running the commit
COMMITTER_INFO="$(git config user.name) <$(git config user.email)>"
# 4. Get a unique, sorted list of all other authors
# The "|| true" prevents the script from failing if grep finds no other authors
CO_AUTHORS=$(git log --format="%an <%ae>" $MERGE_BASE..HEAD | sort -u | grep -vF "$COMMITTER_INFO" || true)
# 5. Build the "Co-authored-by:" trailers
TRAILERS=""
if [ -n "$CO_AUTHORS" ]; then
while IFS= read -r AUTHOR; do
TRAILERS="${TRAILERS}Co-authored-by: ${AUTHOR}\n"
done <<< "$CO_AUTHORS"
fi
# --- PERFORM THE SQUASH AND COMMIT ---
echo "New commit subject: $COMMIT_SUBJECT"
# Add a check to prevent empty commits
if [ -z "$COMMIT_SUBJECT" ]; then
echo "Could not determine a commit subject. Aborting."
exit 1
fi
git reset --soft $MERGE_BASE
# Create the new commit with the subject, body, AND co-author trailers
# The -m flags are concatenated to form the final message.
git commit -m "$COMMIT_SUBJECT" -m "$COMMIT_BODY" -m "$TRAILERS"
# Step 4: Force-push the squashed commit
- name: Force-Push to PR Branch
run: |
BRANCH_NAME=${{ github.event.pull_request.head.ref }}
git push --force origin HEAD:$BRANCH_NAME