# 如何使用 StackQL 查詢 GitHub 存放庫

> 將 GitHub 存放庫當作 SQL 資料表 github.repos.repos 查詢，依組織或使用者列出、依語言、可見性與活動篩選，並以 SQL 更新存放庫設定。

Source: https://stackql.io/zh-hant/ai/how-tos/query-github-repositories-with-stackql

GitHub 存放庫以資料表 `github.repos.repos` 的形式公開。依組織或使用者列出存放庫，會對應到相應的 GitHub REST 作業，並由 `WHERE` 子句選擇：`org = '...'` 使用依組織列出的作業，`username = '...'` 使用依使用者列出的作業。

## 必要條件

- 已安裝 GitHub Provider：`REGISTRY PULL github;`
- 選用：用於存取私人資料與提高速率限制的憑證，請參閱[如何讓 StackQL 向 GitHub 進行驗證](/ai/how-tos/authenticate-stackql-to-github)

## 列出組織的存放庫

```sql
SELECT name, description, language, stargazers_count
FROM github.repos.repos
WHERE org = 'stackql';
```

對公開的 `stackql` 組織執行時，此查詢會傳回 `stackql` 引擎存放庫（Go，撰寫本文時有 861 顆星）和 `stackql-provider-registry` 等資料列，這些都是即時 API 資料，不涉及任何快照。

## 依存放庫屬性篩選

標準 SQL 述詞可套用至任何資料行：

```sql
SELECT name, visibility, default_branch, archived
FROM github.repos.repos
WHERE org = 'my-org'
AND archived = false
AND visibility = 'public';
```

實用的治理資料行包括 `private`、`visibility`、`archived`、`fork`、`default_branch`、`delete_branch_on_merge`、`has_wiki`、`topics`、`created_at`、`pushed_at` 和 `security_and_analysis`。`DESCRIBE github.repos.repos` 會列出所有資料行。

## 存放庫清單之外

`github.repos` 服務將存放庫的其餘介面公開為同層級的資料表：`github.repos.branches`、`github.repos.commits`、`github.repos.releases`、`github.repos.collaborators`、`github.repos.webhooks`、`github.repos.deployments` 等。使用下列陳述式探索這些資料表：

```sql
SHOW RESOURCES IN github.repos;
```

## 更新存放庫

變更存放庫設定使用 `UPDATE` 陳述式；此方法需要 `owner` 和 `repo`，本文欄位使用 `data__` 前置詞：

```sql
UPDATE github.repos.repos
SET data__delete_branch_on_merge = true
WHERE owner = 'my-org' AND repo = 'my-repo';
```

將其套用至清單查詢的結果集，只需兩個陳述式即可治理所有存放庫。這種模式很適合搭配將 GitHub 資料與雲端資源聯結的跨 Provider 查詢。

## 相關概念

- [如何讓 StackQL 向 GitHub 進行驗證](/ai/how-tos/authenticate-stackql-to-github)：權杖與速率限制
- [什麼是 SQL for APIs？](/ai/canonical-definitions/what-is-sql-for-apis)：此處使用的 REST 對 SQL 對應方式
- [開始使用 StackQL](/ai/tutorials/getting-started-with-stackql)：以 GitHub 作為無須憑證的第一個 Provider
- [如何搭配 AI 代理程式使用 StackQL](/ai/how-tos/use-stackql-with-ai-agents)：代理程式透過 MCP 執行這些查詢
- [StackQL vs Steampipe](/ai/comparisons/stackql-vs-steampipe)：比較唯讀的替代方案
