-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
85 lines (73 loc) · 2.83 KB
/
add-coauthors.yml
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
name: Add Co-Authors
on:
pull_request:
types: [closed]
pull_request_review:
types: [submitted]
jobs:
check-approval:
runs-on: ubuntu-latest
if: github.event.review.state == 'approved'
steps:
- name: Add approval status comment
uses: actions/github-script@v6
with:
script: |
const { owner, repo, number } = context.issue;
await github.rest.issues.createComment({
owner,
repo,
issue_number: number,
body: `✅ PR has been approved! CI process initiated.\nCI Run: ${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`
});
add-coauthors:
needs: check-approval
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "noreply@github.com"
- name: Add co-authors
run: |
# Get the merge commit SHA
MERGE_COMMIT_SHA=$(git rev-parse HEAD)
# Get the base branch (target of PR)
BASE_BRANCH="${{ github.event.pull_request.base.ref }}"
# Find merge base
MERGE_BASE=$(git merge-base HEAD $BASE_BRANCH)
# Get list of commits in PR
COMMITS=$(git log --format="%H" $MERGE_BASE..$MERGE_COMMIT_SHA)
# Initialize co-authors list
CO_AUTHORS=""
# Loop through commits
for commit in $COMMITS; do
# Get commit author email and name
AUTHOR_EMAIL=$(git log -1 --format="%ae" $commit)
AUTHOR_NAME=$(git log -1 --format="%an" $commit)
# Add to co-authors if not already present
AUTHOR_LINE="Co-authored-by: $AUTHOR_NAME <$AUTHOR_EMAIL>"
if [[ ! $CO_AUTHORS =~ $AUTHOR_LINE ]]; then
CO_AUTHORS="$CO_AUTHORS\n$AUTHOR_LINE"
fi
done
# Amend the merge commit with co-authors
if [ ! -z "$CO_AUTHORS" ]; then
git fetch origin $BASE_BRANCH
git checkout $BASE_BRANCH
git pull origin $BASE_BRANCH
echo -e "\n$CO_AUTHORS" | git commit --amend -F -
git push origin $BASE_BRANCH
# Add comment to PR
gh pr comment ${{ github.event.pull_request.number }} --body "✨ Added co-authors to merge commit.\nCI Status: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}