Skip to content

changes made according to the comments. #1

changes made according to the comments.

changes made according to the comments. #1

Workflow file for this run

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 }}