+
Skip to content

A Cloudflare Worker to generate dynamic SVG shields for your GitHub repository and user statistics. Display your project's popularity (stars ⭐, forks 🍴) and your overall GitHub contributions with neat, auto-updating badges!

Notifications You must be signed in to change notification settings

akazwz/github-shields

Repository files navigation

GitHub Shields Service

A Cloudflare Worker to generate dynamic SVG shields for your GitHub repository and user statistics. Display your project's popularity (stars ⭐, forks 🍴) and your overall GitHub contributions with neat, auto-updating badges!

🚀 Live Demo

GitHub Stars GitHub Forks

✨ Features

  • Dynamic SVG Shields: Generate shields for GitHub repository stars and forks.
  • User Statistics Shields: Generate shields for a user's total stars and total forks across all their repositories.
  • JSON Data: Access the raw statistics in JSON format.
  • Cloudflare Workers Powered: Fast, reliable, and globally distributed.
  • KV Caching: Efficiently caches responses to reduce GitHub API load and provide quick responses. Cache duration is 10 minutes for repository stats and 60 minutes for user stats.

🚀 API Endpoints

Replace :owner, :repo, and :metric with your desired values. The base URL for these endpoints will be your Cloudflare Worker's URL.

Repository Statistics

  1. Get Repository Stats (JSON)

    • Endpoint: /:owner/:repo
    • Description: Retrieves statistics for a specific repository.
    • Example: /your-username/your-repo-name
    • Response:
      {
        "stars": 123,
        "forks": 45
        // ... other stats from getRepoStats if any
      }
  2. Get Repository Shield (SVG)

    • Endpoint: /:owner/:repo/shields/:metric
    • Description: Generates an SVG shield for a specific repository metric.
    • Supported :metric values: stars, forks.
    • Example: /your-username/your-repo-name/shields/stars
    • Example (Markdown): ![GitHub Stars](https://github-shields.zwz.workers.dev/your-username/your-repo-name/shields/stars)

User Statistics

  1. Get User Total Stats (JSON)

    • Endpoint: /:owner/stars (Note: This endpoint returns all user stats including total forks, despite "stars" in the path)
    • Description: Retrieves total stars and forks for a specific user across all their repositories.
    • Example: /your-username/stars
    • Response:
      {
        "totalStars": 1234,
        "totalForks": 567
      }
  2. Get User Shield (SVG)

    • Endpoint: /:owner/shields/:metric
    • Description: Generates an SVG shield for a user's total metric.
    • Supported :metric values: stars, forks.
    • Example: /your-username/shields/stars
    • Example (Markdown): ![GitHub Total Stars](https://github-shields.zwz.workers.dev/your-username/shields/stars)

⚙️ How It Works

  • The service receives a request for a shield or JSON data.
  • It first checks Cloudflare KV for a cached response.
  • If a fresh cached response exists, it's returned immediately.
  • Otherwise, it fetches the latest stats from the GitHub API.
  • The stats are then used to generate an SVG shield or a JSON response.
  • The new stats are stored in KV for future requests.
    • Repository stats are cached for 10 minutes (expirationTtl: 600).
    • User stats are cached for 60 minutes (expirationTtl: 3600).

🛠️ Deployment

This service is designed to be deployed as a Cloudflare Worker. You'll need to:

  1. Set up a Cloudflare account and a Worker.
  2. Configure a KV namespace and bind it to your worker as KV.
  3. Set up GH_TOKEN as a secret in your worker environment if you need to overcome GitHub API rate limits for unauthenticated requests. (The provided code doesn't explicitly show token usage in getRepoStats or getUserStats, but it's a common practice for robust GitHub API interaction).

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an Issue.

📄 License

This project is open source. You can specify your license here (e.g., MIT License).

About

A Cloudflare Worker to generate dynamic SVG shields for your GitHub repository and user statistics. Display your project's popularity (stars ⭐, forks 🍴) and your overall GitHub contributions with neat, auto-updating badges!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载