Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Dec 1, 2023
1 parent ec6943e commit c9a21e2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
37 changes: 24 additions & 13 deletions .github/workflows/check_authors.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#!/bin/bash

# Only check the first 10 email addresses found in the PR
EMAILS=$(git log $1 --pretty=format:"%ae" | sort | uniq | head -n 10)

for EMAIL in $EMAILS ; do
# Check if the email exists in the AUTHORS file
if grep -Fq "$EMAIL" AUTHORS; then
echo "Author's email is in the AUTHORS file."
is_in_authors() {
TARGET=$1
if grep -Fq "$TARGET" AUTHORS; then
echo "Found $TARGET in AUTHORS"
return 0
else
# Post a comment on the PR
COMMENT="Hello! Welcome and thank you for your contribution. It appears that you might be a new contributor to our project. To acknowledge your work appropriately, we kindly ask you to add your email to the AUTHORS file. This helps us maintain a record of all our valuable contributors. Thanks again for your involvement!"
PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}')
COMMENTS_URL=$(jq -r .pull_request.comments_url "$GITHUB_EVENT_PATH")
echo "Could not find $TARGET in AUTHORS"
return 1
fi
}

# Only check the first 10 committers found in the PR
COMMITTERS=$(git log $1 --pretty=format:"%an;%ae" | sort | uniq | head -n 10)

curl -s -S -H "Authorization: Bearer $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL"
for COMMITTER in "$COMMITTERS" ; do
# split sentence in two parts seperated by a ;
NAME=$(echo $COMMITTER | cut -d ";" -f 1)
EMAIL=$(echo $COMMITTER | cut -d ";" -f 2)

if is_in_authors "$EMAIL" == 0 && is_in_authors "$NAME" == 0; then
echo "This author is not found in the AUTHORS file"
exit 1
fi
done
done

echo "All authors are found in the AUTHORS file."
exit 0
44 changes: 43 additions & 1 deletion .github/workflows/pr-author-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,46 @@ jobs:
run: |
git fetch origin ${{ github.base_ref }}
git fetch origin ${{ github.head_ref }}
sh .github/workflows/check_authors.sh "origin/${{ github.base_ref }}..origin/${{ github.head_ref }}"
sh .github/workflows/check_authors.sh "origin/${{ github.base_ref }}..origin/${{ github.head_ref }}"
echo "::set-output name=result::$?"
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
# Make sure the body has the comment-includes string. We can't add metadata to comments, which would have been a nice to have.
body-includes: "[Author check]"

# Delete comment when author is in AUTHORS file and we have a comment id
- name: Delete comment
uses: actions/github-script@v3
if: steps.check-author.outputs.result == '0' && steps.fc.outputs.comment-id != ''
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
script: |
github.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ steps.fc.outputs.comment-id }},
})
# Post the comment (or replace it) when a committer is not found in the AUTHORS file
- name: Post comment if not found
uses: peter-evans/create-or-update-comment@v3
if: steps.check-author.outputs.result == '1'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Welcome and thank you for your contribution. It appears that you might be a new contributor to our project.
To acknowledge your work appropriately, we kindly ask you to add your name and/or email to the AUTHORS file.
This helps us maintain a record of all our valuable contributors.
Thanks again for your involvement!
[Author check]
"
edit-mode: replace
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# Please keep this list sorted alphabetically or run `python scripts/sort_authors.py` to auto-sort the authors list.

Ahmed Ibrahim <[email protected]>
Joshua Thijssen <[email protected]>

Niklas Scheerhoorn <[email protected]>
Zach Weaver <[email protected]>

0 comments on commit c9a21e2

Please sign in to comment.