Skip to content

Commit 698d7dc

Browse files
committed
Fix GitHub rate limit.
1 parent 02d003e commit 698d7dc

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

.github/workflows/deploy.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,38 @@ jobs:
3434
- name: List Installed Dependencies
3535
run: composer show -D
3636

37-
- name: Deploy
38-
run: php ./tempest deploy
37+
- name: Get latest GitHub Statistics
38+
id: stats
3939
env:
4040
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
run: |
42+
set -e
43+
44+
# Get stargazers
45+
STARS=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework | jq '.stargazers_count // empty')
46+
STARS_K=""
47+
if [ -n "$STARS" ]; then
48+
if [ "$STARS" -gt 999 ]; then
49+
STARS_K=$(awk "BEGIN { printf \"%.1fK\", $STARS/1000 }")
50+
else
51+
STARS_K=$STARS
52+
fi
53+
fi
54+
55+
# Get latest release tag
56+
TAG=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework/releases/latest | jq -r '.tag_name // empty')
57+
58+
echo "Stars: $STARS_K"
59+
echo "Latest Version: $TAG"
60+
61+
# Push these values to the output
62+
echo "stars=$STARS_K" >> "$GITHUB_OUTPUT"
63+
echo "latest_tag=$TAG" >> "$GITHUB_OUTPUT"
4164
65+
- name: Deploy
66+
run: php ./tempest deploy
67+
env:
68+
TEMPEST_BUILD_STARGAZERS: ${{ steps.stats.outputs.stars }}
69+
TEMPEST_BUILD_LATEST_RELEASE: ${{ steps.stats.outputs.latest_tag }}
4270

4371

src/GitHub/GetLatestRelease.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,16 @@ public function __construct(
1717

1818
public function __invoke(): ?string
1919
{
20-
// Added by Aidan Casey to combat the GitHub rate limits.
21-
// We will inject the GH_TOKEN using our workflow.
22-
$headers = [];
23-
24-
if ($githubToken = env('GH_TOKEN')) {
25-
$headers['Authorization'] = 'Bearer ' . $githubToken;
20+
if ($latestRelease = env('TEMPEST_BUILD_LATEST_RELEASE')) {
21+
return $latestRelease;
2622
}
2723

2824
// Default release to the currently running version of Tempest.
2925
$defaultRelease = sprintf('v%s', Kernel::VERSION);
3026

3127
try {
3228
$body = $this->httpClient
33-
->get(
34-
uri: 'https://api.github.com/repos/tempestphp/tempest-framework/releases/latest',
35-
headers: $headers,
36-
)
29+
->get('https://api.github.com/repos/tempestphp/tempest-framework/releases/latest')
3730
->body;
3831

3932
return json_decode($body)->tag_name ?? $defaultRelease;

src/GitHub/GetStargazersCount.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@ public function __construct(
1616

1717
public function __invoke(): ?string
1818
{
19-
// Added by Aidan Casey to combat the GitHub rate limits.
20-
// We will inject the GH_TOKEN using our workflow.
21-
$headers = [];
22-
23-
if ($githubToken = env('GH_TOKEN')) {
24-
$headers['Authorization'] = 'Bearer ' . $githubToken;
19+
if ($stargazers = env('TEMPEST_BUILD_STARGAZERS')) {
20+
return $stargazers;
2521
}
2622

2723
try {
28-
$body = $this->httpClient->get(
29-
uri: 'https://api.github.com/repos/tempestphp/tempest-framework',
30-
headers: $headers,
31-
)->body;
24+
$body = $this->httpClient
25+
->get(uri: 'https://api.github.com/repos/tempestphp/tempest-framework')
26+
->body;
3227
$stargazers = json_decode($body)->stargazers_count ?? null;
3328

3429
return $stargazers > 999

0 commit comments

Comments
 (0)