Skip to content

Commit 2fdbed0

Browse files
authored
Create gh-stars-to-discord.yml
1 parent 9d632d6 commit 2fdbed0

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: GitHub Stars to Discord
2+
3+
on:
4+
schedule:
5+
- cron: '0 * * * *' # Every 1 hour. You can change the frequency as needed.
6+
workflow_dispatch: # Allows manual triggering
7+
8+
env:
9+
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
10+
GITHUB_REPOSITORY: 'codeharborhub/codeharborhub.github.io'
11+
12+
jobs:
13+
notify:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Get the current number of stars and check rate limit
18+
id: get_stars
19+
run: |
20+
response=$(curl -s -i https://api.github.com/repos/${{ env.GITHUB_REPOSITORY }})
21+
stars=$(echo "$response" | grep -oP '"stargazers_count":\s*\K\d+')
22+
rate_limit_remaining=$(echo "$response" | grep -Fi 'X-RateLimit-Remaining:' | awk -F': ' '{print $2}' | tr -d '\r')
23+
24+
echo "Current stars: $stars"
25+
echo "Rate limit remaining: $rate_limit_remaining"
26+
27+
echo "stars=$stars" >> $GITHUB_OUTPUT
28+
echo "rate_limit_remaining=$rate_limit_remaining" >> $GITHUB_OUTPUT
29+
30+
- name: Stop if rate limit is low
31+
if: ${{ steps.get_stars.outputs.rate_limit_remaining < 10 }}
32+
run: |
33+
echo "Rate limit is low, exiting"
34+
exit 1
35+
36+
- name: Cache previous stars count
37+
id: cache_previous_stars
38+
uses: actions/cache@v4
39+
with:
40+
path: previous_stars.txt
41+
key: stars-${{ github.repository }}-${{ github.run_id }}
42+
restore-keys: |
43+
stars-${{ github.repository }}-
44+
45+
- name: Load the previous number of stars
46+
id: load_previous_stars
47+
run: |
48+
if [ -f previous_stars.txt ]; then
49+
echo "Loading previous stars"
50+
PREVIOUS_STARS=$(cat previous_stars.txt)
51+
else
52+
echo "No previous stars found"
53+
PREVIOUS_STARS=0
54+
fi
55+
echo "Previous stars: $PREVIOUS_STARS"
56+
echo "previous_stars=$PREVIOUS_STARS" >> $GITHUB_OUTPUT
57+
58+
- name: Compare and notify if new stars
59+
if: ${{ steps.get_stars.outputs.stars != steps.load_previous_stars.outputs.previous_stars }}
60+
run: |
61+
NEW_STARS=${{ steps.get_stars.outputs.stars }}
62+
PREVIOUS_STARS=${{ steps.load_previous_stars.outputs.previous_stars }}
63+
if [ $NEW_STARS -gt $PREVIOUS_STARS ]; then
64+
echo "There are new stars: $NEW_STARS stars (previous: $PREVIOUS_STARS)"
65+
curl -H "Content-Type: application/json" \
66+
-d "{\"content\": \"The repository has gained new stars! Previous: $PREVIOUS_STARS → Total: $NEW_STARS\"}" \
67+
${DISCORD_WEBHOOK_URL}
68+
else
69+
echo "No new stars"
70+
fi
71+
72+
- name: Save current stars count
73+
run: |
74+
echo "${{ steps.get_stars.outputs.stars }}" > previous_stars.txt
75+
76+
- name: Update cache with new stars count
77+
uses: actions/cache@v4
78+
with:
79+
path: previous_stars.txt
80+
key: stars-${{ github.repository }}-${{ github.run_id }}

0 commit comments

Comments
 (0)