feat: allow organization members in webhook chat
This commit is contained in:
1 parent
3adf4a41a2
commit
625ba79dbb
5 files changed
+82
-5
No files matched your search
+2
-1
@@ -12,7 +12,8 @@ GH_APP_INSTALLATION_ID=
|
||||
WEBHOOK_ENABLED=false
|
||||
WEBHOOK_SECRET=
|
||||
WEBHOOK_PATH=/webhooks/github
|
||||
# anyone | read | write. "read" permits repository collaborators with read access.
|
||||
# anyone | read | write. In "read" mode, organization members can also ask on
|
||||
# organization-owned repositories; personal repositories still use collaborator access.
|
||||
WEBHOOK_CHAT_PERMISSION=read
|
||||
WEBHOOK_QUEUE_CONCURRENCY=2
|
||||
WEBHOOK_QUEUE_LIMIT=500
|
||||
|
||||
+1
-1
@@ -135,7 +135,7 @@ GitHub App 的 Webhook URL 配置为 `https://你的域名/webhooks/github`,`W
|
||||
- `WEBHOOK_ENABLED=true` 才启用,默认 `false`。
|
||||
- `WEBHOOK_SECRET`,以及可选的 `WEBHOOK_PATH`(默认 `/webhooks/github`)。
|
||||
- `BOT_NAME`:App 的 login 或 slug,例如 `forumlify[bot]` 同时接受 `@forumlify` 和 `@forumlify[bot]`;`@bot` 始终接受。
|
||||
- `WEBHOOK_CHAT_PERMISSION`:`read`(默认)、`write` 或 `anyone`。`read` 允许具有 read、triage、write、maintain、admin 权限的仓库协作者;`write` 只允许 write、maintain、admin;`anyone` 跳过评论者权限检查,但仍只能访问 App 已安装的仓库。
|
||||
- `WEBHOOK_CHAT_PERMISSION`:`read`(默认)、`write` 或 `anyone`。对于组织拥有的仓库,`read` 还会允许组织成员直接提问,不要求把每个成员单独添加为仓库协作者;个人仓库和组织外用户仍走仓库协作者权限检查。`write` 只允许 write、maintain、admin;`anyone` 跳过评论者权限检查,但仍只能访问 App 已安装的仓库。建议给 App 配置组织级 `Members: read`,这样才能验证私有组织成员身份。
|
||||
- `WEBHOOK_QUEUE_CONCURRENCY` 和 `WEBHOOK_QUEUE_LIMIT` 用于限制后台处理量和内存。
|
||||
|
||||
可以用 `npm run build && npm run webhook` 启动,也可以构建 `docker build -f Dockerfile.webhook -t ghbot-webhook .` 后运行,并传入 App 凭证、Webhook 密钥和 Goose 配置。`GET /healthz` 可作为健康检查。服务会先返回 `202` 再后台调用 Goose;请求使用 HMAC 验签,按 `X-GitHub-Delivery` 去重,后台失败的 delivery 仍可被 GitHub 重试。
|
||||
|
||||
@@ -121,7 +121,7 @@ Set these service environment variables:
|
||||
- `WEBHOOK_ENABLED=true` to opt in; the default is `false`.
|
||||
- `WEBHOOK_SECRET` and optional `WEBHOOK_PATH` (default `/webhooks/github`).
|
||||
- `BOT_NAME`: the App login or slug accepted in mentions, for example `forumlify[bot]` accepts both `@forumlify` and `@forumlify[bot]`; `@bot` is always accepted.
|
||||
- `WEBHOOK_CHAT_PERMISSION`: `read` (default), `write`, or `anyone`. `read` allows repository collaborators with read, triage, write, maintain, or admin permission; `write` allows write, maintain, or admin; `anyone` skips the commenter permission check but still only works in App-installed repositories.
|
||||
- `WEBHOOK_CHAT_PERMISSION`: `read` (default), `write`, or `anyone`. On organization-owned repositories, `read` also allows organization members without adding each member as a repository collaborator; personal repositories and non-members continue through the repository collaborator check. `write` allows write, maintain, or admin; `anyone` skips the commenter permission check but still only works in App-installed repositories. The App should have organization-level `Members: read` permission so private organization membership can be verified.
|
||||
- `WEBHOOK_QUEUE_CONCURRENCY` and `WEBHOOK_QUEUE_LIMIT` bound background work and memory.
|
||||
|
||||
Start it with `npm run build && npm run webhook`, or build `docker build -f Dockerfile.webhook -t ghbot-webhook .` and run it with the App credentials, webhook secret, and Goose variables. `GET /healthz` is available for service probes. GitHub receives `202` before Goose runs; deliveries are HMAC-verified, deduplicated by `X-GitHub-Delivery`, and failed background deliveries remain retryable.
|
||||
|
||||
@@ -185,7 +185,7 @@ export function webhookPermissionAllows(
|
||||
return false;
|
||||
}
|
||||
if (policy === "read") {
|
||||
return ["read", "triage", "write", "maintain", "admin"].includes(permission);
|
||||
return ["organization", "read", "triage", "write", "maintain", "admin"].includes(permission);
|
||||
}
|
||||
return ["write", "maintain", "admin"].includes(permission);
|
||||
}
|
||||
@@ -369,10 +369,25 @@ async function loadReadme(octokit: Octokit, owner: string, repo: string): Promis
|
||||
}
|
||||
}
|
||||
|
||||
async function getCommenterPermission(octokit: Octokit, mention: WebhookMention): Promise<string | null> {
|
||||
export async function getCommenterPermission(octokit: Octokit, mention: WebhookMention): Promise<string | null> {
|
||||
if (config.webhookChatPermission === "anyone") {
|
||||
return "anyone";
|
||||
}
|
||||
|
||||
// An organization-wide App installation should not require every member to
|
||||
// be added as a repository collaborator just to ask the bot a question.
|
||||
// Keep this scoped to read mode; write mode still requires repository write
|
||||
// permission and personal repositories keep the collaborator check below.
|
||||
if (config.webhookChatPermission === "read") {
|
||||
const { data: repository } = await octokit.rest.repos.get({
|
||||
owner: mention.owner,
|
||||
repo: mention.repo
|
||||
});
|
||||
if (repository.owner?.type === "Organization" && await isOrganizationMember(octokit, mention.owner, mention.commenterLogin)) {
|
||||
return "organization";
|
||||
}
|
||||
}
|
||||
|
||||
const { data } = await octokit.rest.repos.getCollaboratorPermissionLevel({
|
||||
owner: mention.owner,
|
||||
repo: mention.repo,
|
||||
@@ -386,6 +401,22 @@ async function getCommenterPermission(octokit: Octokit, mention: WebhookMention)
|
||||
return data.permission ?? null;
|
||||
}
|
||||
|
||||
async function isOrganizationMember(octokit: Octokit, organization: string, login: string): Promise<boolean> {
|
||||
try {
|
||||
await octokit.rest.orgs.checkMembershipForUser({
|
||||
org: organization,
|
||||
username: login
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
// A 404 means the commenter is not a member. A missing Members: read App
|
||||
// permission has the same safe fallback: use repository collaborator
|
||||
// permissions instead of granting organization-wide access.
|
||||
logger.debug({ error, organization, login }, "Webhook commenter is not confirmed as an organization member.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function hasExistingWebhookReply(
|
||||
octokit: Octokit,
|
||||
mention: WebhookMention,
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Readable } from "node:stream";
|
||||
import test from "node:test";
|
||||
import {
|
||||
buildWebhookChatPrompt,
|
||||
getCommenterPermission,
|
||||
parseWebhookMentionEvent,
|
||||
webhookPermissionAllows
|
||||
} from "../src/webhook/processor.js";
|
||||
@@ -138,6 +139,8 @@ test("Webhook event parsing only accepts supported mentions and preserves PR con
|
||||
|
||||
test("Webhook permission policy is explicit", () => {
|
||||
assert.equal(webhookPermissionAllows(null, "anyone"), true);
|
||||
assert.equal(webhookPermissionAllows("organization", "read"), true);
|
||||
assert.equal(webhookPermissionAllows("organization", "write"), false);
|
||||
assert.equal(webhookPermissionAllows("read", "read"), true);
|
||||
assert.equal(webhookPermissionAllows("triage", "read"), true);
|
||||
assert.equal(webhookPermissionAllows("read", "write"), false);
|
||||
@@ -145,6 +148,48 @@ test("Webhook permission policy is explicit", () => {
|
||||
assert.equal(webhookPermissionAllows(null, "read"), false);
|
||||
});
|
||||
|
||||
test("organization members can use webhook chat without repository collaborator access", async () => {
|
||||
const calls: string[] = [];
|
||||
const octokit = {
|
||||
rest: {
|
||||
repos: {
|
||||
get: async () => {
|
||||
calls.push("repo");
|
||||
return { data: { owner: { type: "Organization" } } };
|
||||
},
|
||||
getCollaboratorPermissionLevel: async () => {
|
||||
calls.push("collaborator");
|
||||
return { data: { permission: null } };
|
||||
}
|
||||
},
|
||||
orgs: {
|
||||
checkMembershipForUser: async () => {
|
||||
calls.push("membership");
|
||||
return { status: 204 };
|
||||
}
|
||||
}
|
||||
}
|
||||
} as any;
|
||||
|
||||
const permission = await getCommenterPermission(octokit, {
|
||||
eventName: "issue_comment",
|
||||
action: "created",
|
||||
deliveryId: "delivery-org-member",
|
||||
installationId: 12345,
|
||||
owner: "acme",
|
||||
repo: "demo",
|
||||
issueNumber: 7,
|
||||
targetKind: "issue",
|
||||
sourceCommentId: 88,
|
||||
commentBody: "@bot help",
|
||||
commenterLogin: "alice",
|
||||
replyMode: "conversation"
|
||||
});
|
||||
|
||||
assert.equal(permission, "organization");
|
||||
assert.deepEqual(calls, ["repo", "membership"]);
|
||||
});
|
||||
|
||||
test("Webhook server returns health, verifies signatures, queues asynchronously, and deduplicates deliveries", async () => {
|
||||
const handled: string[] = [];
|
||||
let resolveHandler: (() => void) | undefined;
|
||||
|
||||
Reference in new issue
Block a user