✨ feat(vscode): 支持讨论评论引用与 macOS 系统级新评论桌面提醒

This commit is contained in:
cruldra committed 2026-09-29 04:23:57 +08:00
1 parent 540c212bad
commit a2c3c1f919
13 files changed
+638 -302

No files matched your search

+11 -213
View File
@@ -24,91 +24,23 @@
import type { IncomingMessage, Server, ServerResponse } from 'node:http'
import type { Event } from 'vscode'
import type { WebhookEvent } from './eventParser'
import { Buffer } from 'node:buffer'
import { createHmac, timingSafeEqual } from 'node:crypto'
import { createServer } from 'node:http'
import { EventEmitter } from 'vscode'
import { logger } from '../logging/logger'
import { parseEvent } from './eventParser'
export type WebhookEvent = PrWebhookEvent | IssueWebhookEvent | IssueCommentWebhookEvent | PushWebhookEvent
export interface PrWebhookEvent {
/** Discriminator: this event came from a `pull_request` payload. */
kind: 'pr'
/**
* Issue number from the legacy `/webhook/:n` path. `undefined` when the
* canonical `/webhook` route was used — the coordinator then resolves the
* issue via PR body / branch heuristics.
*/
issueNumber: number | undefined
/**
* The `action` field from the `pull_request` payload (e.g. `opened`,
* `reopened`, `synchronize`, `closed`, `edited`). The server fires this
* event on every `pull_request` action whose payload parses correctly;
* downstream code dispatches on `action` to decide what to do.
*/
action: string
/** PR number as string. */
pr: string
/** Head branch ref. */
branch: string
/** Browser URL to the PR. */
htmlUrl: string
/** PR title (best-effort; empty string if missing). */
title: string
/** PR body / description — used by the coordinator for `Closes #N` parsing. */
body: string
/** Raw JSON payload, in case downstream needs more fields. */
raw: unknown
}
export interface IssueWebhookEvent {
/** Discriminator: this event came from an `issues` payload. */
kind: 'issue'
/**
* The `action` field from the `issues` payload (`opened` / `closed` /
* `edited` / `reopened` / `deleted`...). The server fires for every
* action; the coordinator decides which ones it actually handles.
*/
action: string
/** Issue number. */
issueNumber: number
/** Issue title (best-effort). */
title: string
/** Issue body — used to extract the `<!-- spx:nonce=... -->` marker. */
body: string
/** Browser URL to the issue. */
htmlUrl: string
/** Raw JSON payload. */
raw: unknown
}
export interface IssueCommentWebhookEvent {
/** Discriminator: this event came from an `issue_comment` payload. */
kind: 'issue_comment'
/** Action: 'created' / 'edited' / 'deleted'. */
action: string
/**
* Issue number — gitea uses the same number for issues and PRs. If the
* issue is actually a PR, {@link prNumber} is also set.
*/
issueNumber: number
/** PR number when the comment is on a PR; otherwise undefined. */
prNumber: number | undefined
/** Comment body — caller scans for the `<!-- spx:review=1 -->` marker. */
commentBody: string
/** Browser URL to the comment. */
commentHtmlUrl: string
/** Raw JSON payload. */
raw: unknown
}
export interface PushWebhookEvent {
kind: 'push'
/** 被推送的分支名(从 ref `refs/heads/<branch>` 提取)。 */
branch: string
raw: unknown
}
export type {
IssueCommentWebhookEvent,
IssueWebhookEvent,
PrWebhookEvent,
PushWebhookEvent,
WebhookEvent,
} from './eventParser'
export { parseEvent } from './eventParser'
export class WebhookServer {
private server?: Server
@@ -345,137 +277,3 @@ export class WebhookServer {
}
}
}
/**
* Validate and normalise a gitea pull_request webhook payload. Returns null
* for missing fields or non-actionable actions; the HTTP handler still
* responds 200 in both cases. `issueNumber` is forwarded as-is — it's
* `undefined` when the request came in on the canonical `/webhook` route
* (the coordinator resolves it from `body` / `branch` in that case).
*/
function parseEvent(
eventHeader: string,
issueNumber: number | undefined,
raw: unknown,
): WebhookEvent | null {
if (!raw || typeof raw !== 'object')
return null
const obj = raw as {
action?: unknown
pull_request?: unknown
issue?: unknown
comment?: unknown
ref?: unknown
}
if (eventHeader === 'push') {
// gitea push 事件无 `action`,顶层带 `ref: "refs/heads/<branch>"`。
if (typeof obj.ref !== 'string')
return null
const branch = obj.ref.replace(/^refs\/heads\//, '')
if (!branch)
return null
return { kind: 'push', branch, raw }
}
if (typeof obj.action !== 'string')
return null
if (eventHeader === 'issue_comment') {
const issue = obj.issue
const comment = obj.comment
if (!issue || typeof issue !== 'object')
return null
if (!comment || typeof comment !== 'object')
return null
const issueObj = issue as {
number?: unknown
pull_request?: unknown
}
const commentObj = comment as {
body?: unknown
html_url?: unknown
}
const num = typeof issueObj.number === 'number' ? issueObj.number : Number(issueObj.number)
if (!Number.isFinite(num))
return null
// gitea sets issue.pull_request (non-null object) when the issue is a PR.
const prNumber = issueObj.pull_request && typeof issueObj.pull_request === 'object'
? num
: undefined
const commentBody = typeof commentObj.body === 'string' ? commentObj.body : ''
const commentHtmlUrl = typeof commentObj.html_url === 'string' ? commentObj.html_url : ''
return {
kind: 'issue_comment',
action: obj.action,
issueNumber: num,
prNumber,
commentBody,
commentHtmlUrl,
raw,
}
}
if (eventHeader === 'issues') {
const issue = obj.issue
if (!issue || typeof issue !== 'object')
return null
const issueObj = issue as {
number?: unknown
html_url?: unknown
title?: unknown
body?: unknown
}
const num = typeof issueObj.number === 'number' ? issueObj.number : Number(issueObj.number)
if (!Number.isFinite(num))
return null
const htmlUrl = typeof issueObj.html_url === 'string' ? issueObj.html_url : ''
const title = typeof issueObj.title === 'string' ? issueObj.title : ''
const body = typeof issueObj.body === 'string' ? issueObj.body : ''
return {
kind: 'issue',
action: obj.action,
issueNumber: num,
title,
body,
htmlUrl,
raw,
}
}
// Default path: pull_request payload (preserves legacy behaviour for any
// event header value, since gitea historically may not set the header).
const pr = obj.pull_request
if (!pr || typeof pr !== 'object')
return null
const prObj = pr as {
number?: unknown
html_url?: unknown
head?: unknown
title?: unknown
body?: unknown
}
const num = typeof prObj.number === 'number' ? prObj.number : Number(prObj.number)
if (!Number.isFinite(num))
return null
const htmlUrl = typeof prObj.html_url === 'string' ? prObj.html_url : ''
const head = prObj.head as { ref?: unknown } | undefined
const branch = head && typeof head.ref === 'string' ? head.ref : ''
const title = typeof prObj.title === 'string' ? prObj.title : ''
const body = typeof prObj.body === 'string' ? prObj.body : ''
if (!branch || !htmlUrl)
return null
return {
kind: 'pr',
issueNumber,
action: obj.action,
pr: String(num),
branch,
htmlUrl,
title,
body,
raw,
}
}