Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/check-api-limit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Check GitHub API Rate Limit"

on:
workflow_dispatch: {}

jobs:
check-rate-limit:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
id: checkout-repo
uses: actions/checkout@v4

- name: Run GitHub API Rate Limit Check
id: check-rate-limit
uses: ./
with:
github-token: ${{ github.token }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# mac
.DS_STORE
366 changes: 183 additions & 183 deletions LICENSE

Large diffs are not rendered by default.

125 changes: 123 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,123 @@
# github-rate-limit-check
GitHub Action to get current GitHub rate limit summary.
# 📊 GitHub API Rate Limit Check

## **Description**

This GitHub Action provides an **automated, real-time check** of your repository’s GitHub API rate limits, giving you **instant visibility** into your API consumption. With detailed reporting and proactive alerts, you can **prevent unexpected disruptions** and **optimize API usage** in your workflows.

Designed for **reliability and clarity**, this composite action:

- Calls the **GitHub Rate Limit API** and extracts real-time usage metrics.
- Formats and logs the results professionally in both **workflow logs** and **GitHub Step Summary**.
- **Warns** when remaining API requests fall below a threshold (default: `100`).
- **Fails fast** when limits are exhausted to avoid unexpected workflow failures.
- **Handles missing data gracefully**, preventing false alarms from disabled or unavailable API endpoints.

Whether you're running frequent automation, managing API-driven integrations, or monitoring GitHub resources, this action ensures you **stay informed and in control** of your API quota.

---

## 🚀 **Why Use This Action?**

✔️ **Prevents Workflow Failures** – Stops workflows from running when API limits are exceeded.
✔️ **Proactive Warnings** – Get alerts when remaining requests drop below a safe threshold.
✔️ **Clear, Professional Reporting** – View a **clean summary table** in GitHub Actions for quick insights.
✔️ **Safe and Robust Handling** – Avoids errors due to missing or unavailable API data.
✔️ **Simple Integration** – Plug it into any workflow with minimal setup.

---

## 📦 **Usage**

### **Workflow Example**

Add the following to your **GitHub Actions workflow** to monitor API usage before running rate-limited tasks.

```yaml
jobs:
check-rate-limit:
name: Check GitHub API Rate Limit
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
id: checkout-repo
uses: actions/checkout@v4

- name: Run GitHub API Rate Limit Check
id: check-rate-limit
uses: coderrob/github-api-rate-limit-check@v1
with:
github-token: ${{ github.token }}
```

---

## 📜 **How It Works**

1. **Fetches Rate Limit Data** → Calls the `https://api.github.com/rate_limit` endpoint.
2. **Parses and Processes Data** → Extracts core, GraphQL, and search API quotas.
3. **Logs Results** → Displays a structured **table** in `GITHUB_STEP_SUMMARY` and console logs.
4. **Triggers Alerts**:
- **Warning** (`⚠️`) if remaining API requests **drop below 100**.
- **Error** (`❌`) if limits are **fully exhausted** (only if that API type is enabled).
5. **Fails the Workflow (if needed)** → Prevents unintended execution when API access is unavailable.

---

## 📊 **Example Output**

### **Console Logs**

```bash
GitHub API Rate Limit Status:
--------------------------------------------
Core API: 85 / 5000 (Resets at 2025-02-20 15:00:00 UTC)
GraphQL: 4500 / 5000 (Resets at 2025-02-20 15:00:00 UTC)
Search: 0 / 30 (Resets at 2025-02-20 15:00:00 UTC)
--------------------------------------------
⚠️ GitHub API Rate Limit is low (85 remaining)!
❌ GitHub Search API Rate Limit has been fully exhausted!
```

---

### 📊 **GitHub API Rate Limit Summary**

| API Type | Remaining | Limit | Reset Time (UTC) |
| ------------ | --------- | ----- | ------------------- |
| **Core API** | **85** | 5000 | 2025-02-20 15:00:00 |
| **GraphQL** | **4500** | 5000 | 2025-02-20 15:00:00 |
| **Search** | **0** | 30 | 2025-02-20 15:00:00 |

⚠️ **Warning:** GitHub API rate limit is low (85 remaining).
❌ **Error:** GitHub Search API rate limit has been fully exhausted.

⏳ API limits reset periodically. Plan requests accordingly.

---

## 🛠 **Inputs**

| Name | Required | Description |
| -------------- | -------- | ------------------------------------------------------------ |
| `github-token` | ✅ Yes | GitHub token used for authentication (`${{ github.token }}`) |

---

## 🎯 **Best Use Cases**

- **Before running API-heavy workflows** (e.g., fetching issue data, triggering GitHub Actions via API).
- **Monitoring CI/CD jobs that rely on GitHub API calls** to avoid failures mid-run.
- **Logging and auditing API usage** across your organization’s GitHub actions.

---

## 🏆 **Take Control of Your GitHub API Usage**

This action gives you the **power to monitor, manage, and prevent API failures before they happen**. Whether you're a solo developer or running large-scale automation, **stay ahead of rate limits and keep your workflows running smoothly**. 🚀

---

### **🔗 Related Resources**

- [GitHub API Rate Limits Docs](https://docs.github.com/en/rest/rate-limit)
- [GitHub Actions Documentation](https://docs.github.com/en/actions)
41 changes: 41 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Copyright 2025 Robert Lindley
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
---
author: "Robert Lindley (coderrob)"
name: GitHub API Rate Limit Check
description: This GitHub Action provides an automated, real-time check of your
repository's GitHub API rate limits, giving you instant visibility
into your API consumption. With detailed reporting and proactive
alerts, you can prevent unexpected disruptions and optimize API
usage in your workflows.

branding:
icon: "bar-chart"
color: "blue"

inputs:
github-token:
default: ${{ github.token }}
description: GitHub Token for API authentication
required: false

runs:
using: composite
steps:
- name: Call GitHub Rate Limit API
id: github-rate-limit-api
shell: bash
run: bash "${{ github.action_path }}/scripts/entrypoint.sh" "${{ inputs.github-token }}"
114 changes: 114 additions & 0 deletions scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash

#
# Copyright 2025 Robert Lindley
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e # Exit on error

GITHUB_TOKEN=$1
API_URL="https://api.github.com/rate_limit"

echo "Calling GitHub Rate Limit API..."
RESPONSE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" $API_URL)

if [ -z "$RESPONSE" ]; then
echo "::error::Failed to retrieve rate limit data from GitHub API."
exit 1
fi

echo "$RESPONSE" >rate_limit.json

parse_safe() {
local value=$(jq -r "$1 // empty" rate_limit.json)
echo "${value:-0}" # Default to 0 if empty/null
}

RATE_LIMIT=$(parse_safe '.rate.limit')
RATE_REMAINING=$(parse_safe '.rate.remaining')
RATE_RESET=$(parse_safe '.rate.reset')

GRAPHQL_LIMIT=$(parse_safe '.graphql.limit')
GRAPHQL_REMAINING=$(parse_safe '.graphql.remaining')
GRAPHQL_RESET=$(parse_safe '.graphql.reset')

SEARCH_LIMIT=$(parse_safe '.search.limit')
SEARCH_REMAINING=$(parse_safe '.search.remaining')
SEARCH_RESET=$(parse_safe '.search.reset')

format_time() {
if [ "$1" -gt 0 ]; then
date -u -d "@$1" "+%Y-%m-%d %H:%M:%S UTC"
else
echo "N/A"
fi
}

RESET_TIME=$(format_time "$RATE_RESET")
GRAPHQL_RESET_TIME=$(format_time "$GRAPHQL_RESET")
SEARCH_RESET_TIME=$(format_time "$SEARCH_RESET")

echo "GitHub API Rate Limit Status:"
echo "--------------------------------------------"
echo " Core API: $RATE_REMAINING / $RATE_LIMIT (Resets at $RESET_TIME)"
echo " GraphQL: $GRAPHQL_REMAINING / $GRAPHQL_LIMIT (Resets at $GRAPHQL_RESET_TIME)"
echo " Search: $SEARCH_REMAINING / $SEARCH_LIMIT (Resets at $SEARCH_RESET_TIME)"
echo "--------------------------------------------"

# Core API Limits
if [ "$RATE_LIMIT" -gt 0 ] && [ "$RATE_REMAINING" -le 0 ]; then
echo "::error::❌ GitHub API Rate Limit has been fully exhausted!"
exit 1
elif [ "$RATE_LIMIT" -gt 0 ] && [ "$RATE_REMAINING" -lt 100 ]; then
echo "::warning::⚠️ GitHub API Rate Limit is low ($RATE_REMAINING remaining)!"
fi

# GraphQL Limits
if [ "$GRAPHQL_LIMIT" -gt 0 ] && [ "$GRAPHQL_REMAINING" -le 0 ]; then
echo "::error::❌ GitHub GraphQL API Rate Limit has been fully exhausted!"
exit 1
elif [ "$GRAPHQL_LIMIT" -gt 0 ] && [ "$GRAPHQL_REMAINING" -lt 100 ]; then
echo "::warning::⚠️ GitHub GraphQL API Rate Limit is low ($GRAPHQL_REMAINING remaining)!"
fi

# Search Limits
if [ "$SEARCH_LIMIT" -gt 0 ] && [ "$SEARCH_REMAINING" -le 0 ]; then
echo "::error::❌ GitHub Search API Rate Limit has been fully exhausted!"
exit 1
elif [ "$SEARCH_LIMIT" -gt 0 ] && [ "$SEARCH_REMAINING" -lt 100 ]; then
echo "::warning::⚠️ GitHub Search API Rate Limit is low ($SEARCH_REMAINING remaining)!"
fi

# GitHub Step Summary
echo "# :bar_chart: GitHub API Rate Limit Summary" >>$GITHUB_STEP_SUMMARY
echo "" >>$GITHUB_STEP_SUMMARY
echo "| API Type | Remaining | Limit | Reset Time (UTC) |" >>$GITHUB_STEP_SUMMARY
echo "|-----------|----------|-------|------------------|" >>$GITHUB_STEP_SUMMARY
echo "| Core API | $RATE_REMAINING | $RATE_LIMIT | $RESET_TIME |" >>$GITHUB_STEP_SUMMARY
echo "| GraphQL | $GRAPHQL_REMAINING | $GRAPHQL_LIMIT | $GRAPHQL_RESET_TIME |" >>$GITHUB_STEP_SUMMARY
echo "| Search | $SEARCH_REMAINING | $SEARCH_LIMIT | $SEARCH_RESET_TIME |" >>$GITHUB_STEP_SUMMARY
echo "" >>$GITHUB_STEP_SUMMARY

if [ "$RATE_LIMIT" -gt 0 ] && [ "$RATE_REMAINING" -lt 100 ]; then
echo "⚠️ **Warning:** GitHub API rate limit is low ($RATE_REMAINING remaining)." >>$GITHUB_STEP_SUMMARY
fi
if [ "$GRAPHQL_LIMIT" -gt 0 ] && [ "$GRAPHQL_REMAINING" -lt 100 ]; then
echo "⚠️ **Warning:** GitHub GraphQL API rate limit is low ($GRAPHQL_REMAINING remaining)." >>$GITHUB_STEP_SUMMARY
fi
if [ "$SEARCH_LIMIT" -gt 0 ] && [ "$SEARCH_REMAINING" -lt 100 ]; then
echo "⚠️ **Warning:** GitHub Search API rate limit is low ($SEARCH_REMAINING remaining)." >>$GITHUB_STEP_SUMMARY
fi

echo "⏳ API limits reset hourly. Plan requests accordingly." >>$GITHUB_STEP_SUMMARY
Loading