|
| 1 | +import { Octokit } from "@octokit/rest"; |
| 2 | +import { RequestError } from "@octokit/request-error"; |
| 3 | +import type { Context, Session } from "koishi"; |
| 4 | +import { Schema } from "koishi"; |
| 5 | + |
| 6 | +export const name = "approve"; |
| 7 | + |
| 8 | +export interface Config {} |
| 9 | + |
| 10 | +export const Config: Schema<Config> = Schema.object({}); |
| 11 | + |
| 12 | +const repoOwner = "Winds-Studio"; |
| 13 | +const repoName = "Leaf"; |
| 14 | +const octokit = new Octokit(); |
| 15 | +const cacheTtlMs = 60_000; |
| 16 | + |
| 17 | +type CacheEntry<T> = { |
| 18 | + expiresAt: number; |
| 19 | + value: T; |
| 20 | + etag?: string; |
| 21 | + lastModified?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +const createCachedFetcher = <T>( |
| 25 | + fetcher: ( |
| 26 | + headers: Record<string, string>, |
| 27 | + ) => Promise<{ value: T; etag?: string; lastModified?: string }>, |
| 28 | +) => { |
| 29 | + let cache: CacheEntry<T> | null = null; |
| 30 | + let inflight: Promise<CacheEntry<T>> | null = null; |
| 31 | + |
| 32 | + return async () => { |
| 33 | + if (cache && Date.now() < cache.expiresAt) { |
| 34 | + return cache; |
| 35 | + } |
| 36 | + |
| 37 | + if (inflight) { |
| 38 | + return inflight; |
| 39 | + } |
| 40 | + |
| 41 | + inflight = (async () => { |
| 42 | + const headers: Record<string, string> = {}; |
| 43 | + if (cache?.etag) { |
| 44 | + headers["If-None-Match"] = cache.etag; |
| 45 | + } |
| 46 | + if (cache?.lastModified) { |
| 47 | + headers["If-Modified-Since"] = cache.lastModified; |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + const result = await fetcher(headers); |
| 52 | + cache = { |
| 53 | + expiresAt: Date.now() + cacheTtlMs, |
| 54 | + value: result.value, |
| 55 | + etag: result.etag, |
| 56 | + lastModified: result.lastModified, |
| 57 | + }; |
| 58 | + return cache; |
| 59 | + } catch (error: unknown) { |
| 60 | + if (error instanceof RequestError && error.status === 304 && cache) { |
| 61 | + cache = { |
| 62 | + ...cache, |
| 63 | + expiresAt: Date.now() + cacheTtlMs, |
| 64 | + }; |
| 65 | + return cache; |
| 66 | + } |
| 67 | + throw error; |
| 68 | + } finally { |
| 69 | + inflight = null; |
| 70 | + } |
| 71 | + })(); |
| 72 | + |
| 73 | + return inflight; |
| 74 | + }; |
| 75 | +}; |
| 76 | + |
| 77 | +const getDefaultBranch = createCachedFetcher(async (headers) => { |
| 78 | + const repoInfo = await octokit.rest.repos.get({ |
| 79 | + owner: repoOwner, |
| 80 | + repo: repoName, |
| 81 | + headers, |
| 82 | + }); |
| 83 | + |
| 84 | + return { |
| 85 | + value: repoInfo.data.default_branch, |
| 86 | + etag: repoInfo.headers.etag, |
| 87 | + lastModified: repoInfo.headers["last-modified"], |
| 88 | + }; |
| 89 | +}); |
| 90 | + |
| 91 | +type CommitInfo = { |
| 92 | + latestSha: string; |
| 93 | + shortSha: string; |
| 94 | + defaultBranch: string; |
| 95 | +}; |
| 96 | + |
| 97 | +const getLatestCommit = createCachedFetcher(async (headers) => { |
| 98 | + const { value: defaultBranch } = await getDefaultBranch(); |
| 99 | + const commitInfo = await octokit.rest.repos.getCommit({ |
| 100 | + owner: repoOwner, |
| 101 | + repo: repoName, |
| 102 | + ref: defaultBranch, |
| 103 | + headers, |
| 104 | + }); |
| 105 | + const latestSha = commitInfo.data.sha; |
| 106 | + |
| 107 | + return { |
| 108 | + value: { |
| 109 | + latestSha, |
| 110 | + shortSha: latestSha.substring(0, 7), |
| 111 | + defaultBranch, |
| 112 | + } satisfies CommitInfo, |
| 113 | + etag: commitInfo.headers.etag, |
| 114 | + lastModified: commitInfo.headers["last-modified"], |
| 115 | + }; |
| 116 | +}); |
| 117 | + |
| 118 | +const handleEvent = async (ctx: Context, session: Session) => { |
| 119 | + if (!session.messageId) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const logger = ctx.logger; |
| 124 | + |
| 125 | + try { |
| 126 | + const { value } = await getLatestCommit(); |
| 127 | + const { latestSha, shortSha } = value; |
| 128 | + |
| 129 | + const applyMessage = (session.content || "").trim(); |
| 130 | + |
| 131 | + if (applyMessage === latestSha || applyMessage === shortSha) { |
| 132 | + await session.bot.handleGuildMemberRequest(session.messageId, true); |
| 133 | + logger.info(`已通过 ${session.userId} 的加群请求,SHA: ${applyMessage}`); |
| 134 | + } else { |
| 135 | + await session.bot.handleGuildMemberRequest(session.messageId, false); |
| 136 | + logger.info( |
| 137 | + `已拒绝 ${session.userId} 的加群请求。错误 SHA: ${applyMessage},期望: ${shortSha} 或 ${latestSha}`, |
| 138 | + ); |
| 139 | + } |
| 140 | + } catch (error) { |
| 141 | + logger.warn(`获取 GitHub 最新 commit 失败: ${error}`); |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +export function apply(ctx: Context, _config: Config) { |
| 146 | + ctx.on("guild-member-request", async (session) => { |
| 147 | + await handleEvent(ctx, session); |
| 148 | + }); |
| 149 | +} |
0 commit comments