GitHub Repo Starred or Forked Notification #3197
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "GitHub Repo Starred or Forked Notification" | |
on: | |
# Runs your workflow when someone forks a repository. | |
fork: | |
# Runs your workflow when the workflow's repository is starred. | |
# https://docs.github.com/cn/github-ae@latest/actions/using-workflows/events-that-trigger-workflows#watch | |
watch: | |
types: [started] | |
jobs: | |
bot: | |
runs-on: ubuntu-latest | |
steps: | |
- if: ${{ github.event_name == 'fork' }} | |
run: | | |
echo "🎉 triggered by a ${{ github.event_name }} event." | |
echo "event_name=Forked🍴" >> $GITHUB_ENV | |
- if: ${{ github.event_name == 'watch' }} | |
run: | | |
echo "🎉 triggered by a ${{ github.event_name }} event." | |
echo "event_name=Starred✨" >> $GITHUB_ENV | |
- name: Get repository information | |
run: | | |
result=$(curl -s -H "Authorization: token ${{ secrets.READ_REPO_STAR_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}") | |
stars=$(echo $result | jq '.stargazers_count') | |
forks=$(echo $result | jq '.forks_count') | |
repo_name=$(echo $result | jq -r '.name') | |
echo "Number of stars: $stars" | |
echo "Number of forks: $forks" | |
# Save the value to env | |
echo "repo_stars=$stars" >> $GITHUB_ENV | |
echo "repo_forks=$forks" >> $GITHUB_ENV | |
echo "repo_name=$repo_name" >> $GITHUB_ENV | |
- name: Get repo download count | |
run: | | |
download_count=$(curl -s -H "Authorization: token ${{ secrets.READ_REPO_STAR_TOKEN }}" "https://api.github.com/repos/${{ github.repository }}/releases" | jq '.[].assets[].download_count' | awk '{sum += $1} END {print sum}') | |
echo "Number of downloads: $download_count" | |
echo "download_count=$download_count" >> $GITHUB_ENV | |
- name: Get user information | |
id: check_conditions | |
run: | | |
earn_star_count=$(curl -s -H "Authorization: token ${{ secrets.READ_REPO_STAR_TOKEN }}" "https://api.github.com/users/${{ github.actor }}/repos" | jq '[.[] | .stargazers_count] | add // 0') | |
commit_count=$(curl -s -H "Authorization: token ${{ secrets.READ_REPO_STAR_TOKEN }}" "https://api.github.com/search/commits?q=author:${{ github.actor }}" | jq -r '.total_count // 0') | |
follower_count=$(curl -s -H "Authorization: token ${{ secrets.READ_REPO_STAR_TOKEN }}" "https://api.github.com/users/${{ github.actor }}" | jq '.followers // 0') | |
echo "Star count: $earn_star_count" | |
echo "Commit count: $commit_count" | |
echo "Follower count: $follower_count" | |
echo "earn_star_count=$earn_star_count" >> $GITHUB_ENV | |
echo "commit_count=$commit_count" >> $GITHUB_ENV | |
echo "follower_count=$follower_count" >> $GITHUB_ENV | |
if [[ $commit_count -ge 0 ]]; then | |
echo "user_conditions_met=true" >> $GITHUB_ENV | |
else | |
echo "user_conditions_met=false" >> $GITHUB_ENV | |
fi | |
- name: Convert body to HTML | |
run: | | |
html_body="" | |
html_body+="Stargazer: <b><a href='${{ github.server_url }}/${{ github.actor }}'>${{ github.actor }}</a></b> (Earn stars: ${{ env.earn_star_count }}, Commits: ${{ env.commit_count }}, Followers: ${{ env.follower_count }})<br><br>" | |
html_body+="Stars: <b>${{ env.repo_stars }}</b><br><br>" | |
html_body+="Forks: <b>${{ env.repo_forks }}</b><br><br>" | |
html_body+="Downloads: <b>${{ env.download_count }}</b><br><br>" | |
html_body+="Repo: <a href='${{ github.server_url }}/${{ github.repository }}'>${{ github.repository }}</a><br><br>" | |
html_body+="Stargazers list: <a href='${{ github.server_url }}/${{ github.repository }}/stargazers'>${{ github.repository }}/stargazers</a>" | |
echo "html_body=$html_body" >> $GITHUB_ENV | |
echo "html body: ${{ env.html_body }}" | |
- name: "Send mail" | |
if: env.user_conditions_met == 'true' | |
uses: dawidd6/action-send-mail@v3 | |
with: | |
server_address: smtp.gmail.com | |
server_port: 465 | |
username: ${{ secrets.GMAIL_BOT_USERNAME }} | |
password: ${{ secrets.GMAIL_BOT_PASSWORD }} | |
subject: ${{ github.actor }} ${{ env.event_name }} ${{ env.repo_name }} | |
# List stargazers https://github.com/tisfeng/Easydict/stargazers | |
html_body: ${{ env.html_body }} | |
to: ${{ secrets.RECEIVER_EMAIL }} | |
from: GitHub Actions |