github API
@Chenpinji
npx machina-cli add skill @Chenpinji/ghggh --openclawGitHub 仓库统计查询
在用户需要查询某个 GitHub 仓库的 star 数量和代码行数时,按下列方式执行。
1. Star 数量
使用 GitHub REST API(无需 token 即可查询公开仓库):
- 接口:
GET https://api.github.com/repos/{owner}/{repo} - 字段: 响应 JSON 中的
stargazers_count即为 star 数。
示例(将 owner/repo 换成实际仓库,如 vllm-project/vllm):
curl -s "https://api.github.com/repos/owner/repo" | jq '.stargazers_count'
或在代码中请求同一 URL,解析 JSON 取 stargazers_count。若请求受限可设置 User-Agent 头(如 User-Agent: curl)。
2. 代码行数
GitHub API 不直接提供“总代码行数”,常用做法是克隆仓库后本地统计。
方式 A:使用 cloc(推荐)
git clone --depth 1 https://github.com/owner/repo.git /tmp/repo-stat && cloc /tmp/repo-stat --json
安装:sudo apt install cloc 或 pip install cloc(若有)。从输出 JSON 中取 SUM 的 code 作为代码行数,并清理临时目录。
3. 输出格式
回复用户时建议包含:
- 仓库:
owner/repo - Stars: 数字
- 代码行数: 数字(并注明统计方式,如 cloc/tokei/近似)
若克隆或统计失败,说明原因并给出可替代方式(例如仅提供 star 数,或提供仓库语言占比链接)。
Overview
This skill fetches a repository's star count via the GitHub REST API and computes lines of code (LOC) by cloning the repo and running cloc. It helps answer questions about repository popularity, scale, and code size for any public GitHub project.
How This Skill Works
Stars are retrieved from GET https://api.github.com/repos/{owner}/{repo} and the stargazers_count field is returned. For LOC, clone the repo with depth 1 to /tmp/repo-stat and run cloc to obtain JSON, then read SUM.code for total code lines. If rate limits occur, you can add a User-Agent header (e.g., User-Agent: curl).
When to Use It
- User asks for the star count or stargazers of a GitHub repo (owner/repo).
- User asks for total lines of code (LOC) or code size of a repo.
- User requests repository statistics including stars and LOC for a public repo.
- Quick checks when token authentication is not available and only public data is needed.
- Provide a fallback when cloning or counting fails, e.g., report stars and note LOC couldn't be computed.
Quick Start
- Step 1: curl -s 'https://api.github.com/repos/owner/repo' | jq '.stargazers_count'
- Step 2: git clone --depth 1 https://github.com/owner/repo.git /tmp/repo-stat && cloc /tmp/repo-stat --json
- Step 3: Report: Repo: owner/repo; Stars: X; Code lines: Y (via cloc SUM.code)
Best Practices
- Use the REST API endpoint /repos/{owner}/{repo} to fetch stargazers_count.
- Prefer public repositories to avoid authentication and rely on rate limits; set a User-Agent header if needed.
- For LOC, clone with --depth 1 to minimize data and run cloc to obtain JSON; read SUM.code.
- Document the exact data source and method (API vs. local counting) when presenting results.
- Gracefully handle failures by reporting stars and explaining why LOC could not be computed.
Example Use Cases
- What are the stars for torvalds/linux?
- How many lines of code does vscode have?
- Show repo stats for apple/swift (stars + LOC).
- Stars for a public repo without using a token.
- If cloning or cloc fails, report stars and provide a link to the repo language stats.