Skip to content

Commit 2e1efc6

Browse files
AztecBotludamad
andcommitted
fix: squash commit attribution and fork behaviour
We now have aztec bot author and all human authors co-author correctly push to forks as well Co-authored-by: ludamad <[email protected]>
1 parent 84397c2 commit 2e1efc6

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

scripts/merge-train/squash-pr.sh

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@ branch="$2"
1313
base_branch="$3"
1414
base_sha="$4"
1515

16-
# Get PR info including author
17-
pr_info=$(gh pr view "$pr_number" --json title,body,author)
16+
# Get PR info including author and repository information
17+
pr_info=$(gh pr view "$pr_number" --json title,body,author,headRepository,isCrossRepository)
1818
pr_title=$(echo "$pr_info" | jq -r '.title')
1919
pr_body=$(echo "$pr_info" | jq -r '.body // ""')
2020
pr_author=$(echo "$pr_info" | jq -r '.author.login')
21+
head_repo=$(echo "$pr_info" | jq -r '.headRepository.nameWithOwner')
22+
is_fork=$(echo "$pr_info" | jq -r '.isCrossRepository')
2123

22-
# Try to get author email from the most recent non-merge commit
23-
author_email=$(git log --no-merges -1 --format='%ae')
24-
author_name=$(git log --no-merges -1 --format='%an')
25-
26-
# Fall back to GitHub username if needed
27-
if [[ -z "$author_email" ]] || [[ "$author_email" == "null" ]]; then
28-
author_email="${pr_author}@users.noreply.github.com"
29-
author_name="$pr_author"
30-
fi
24+
# We'll use AztecBot as the committer
25+
author_name="AztecBot"
26+
author_email="[email protected]"
3127

3228
# Create a temporary worktree to do the squashing
3329
worktree_dir=$(mktemp -d)
@@ -58,6 +54,20 @@ git fetch origin "$base_branch"
5854
# Find the merge-base between our branch and the base branch
5955
merge_base=$(git merge-base "$original_head" "origin/$base_branch")
6056

57+
# Collect all unique authors from non-merge commits that are not in the base branch
58+
# Get all commits between merge_base and HEAD, excluding merges
59+
authors_info=$(git log "$merge_base..$original_head" --no-merges --format='%an <%ae>' | sort -u)
60+
61+
# Build Co-authored-by trailers
62+
co_authors=""
63+
while IFS= read -r author_line; do
64+
# Skip empty lines and AztecBot itself
65+
if [[ -n "$author_line" ]] && [[ "$author_line" != *"AztecBot"* ]] && [[ "$author_line" != *"[email protected]"* ]]; then
66+
co_authors="${co_authors}Co-authored-by: ${author_line}
67+
"
68+
fi
69+
done <<< "$authors_info"
70+
6171
# Reset to the merge-base
6272
git reset --hard "$merge_base"
6373

@@ -71,13 +81,28 @@ git merge "$original_head" --no-edit || {
7181
# Now squash all the PR commits into one by resetting to the base
7282
git reset --soft "$merge_base"
7383

74-
# Create commit with PR title and body
84+
# Create commit with PR title, body, and co-authors
7585
commit_message="$pr_title${pr_body:+
7686
77-
$pr_body}"
87+
$pr_body}${co_authors:+
88+
89+
$co_authors}"
7890
git commit -m "$commit_message" --no-verify
7991

80-
# Push (use full ref to handle case where branch doesn't exist on remote)
81-
git push --force origin "HEAD:refs/heads/$branch"
92+
# Push to the correct repository (fork or origin)
93+
if [[ "$is_fork" == "true" ]]; then
94+
# It's a fork - need to push to the fork repository
95+
echo "Detected fork: pushing to $head_repo"
96+
97+
# Add the fork as a remote (assumes GITHUB_TOKEN env var is set from workflow)
98+
git remote add fork "https://x-access-token:${GITHUB_TOKEN}@github.com/${head_repo}.git"
99+
100+
# Push to the fork
101+
git push --force fork "HEAD:refs/heads/$branch"
102+
else
103+
# Not a fork - push to origin as before
104+
echo "Not a fork: pushing to origin"
105+
git push --force origin "HEAD:refs/heads/$branch"
106+
fi
82107

83108
echo "Squashed PR #$pr_number!"

0 commit comments

Comments
 (0)