dsh-inbox-bridge 2.18.0 → 2.18.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.en.md +3 -2
- package/README.md +2 -1
- package/docs/configuration.md +4 -2
- package/docs/how-it-works.md +1 -1
- package/docs/index.json +3 -3
- package/index.js +11 -1
- package/package.json +2 -2
- package/scripts/clear-inbox.mjs +215 -37
- package/scripts/clear-inbox.test.mjs +72 -0
- package/scripts/inbox-bridge.mjs +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
## 2.18.2
|
|
2
|
+
|
|
3
|
+
- 归档再加一道行级防护:每行都按它自己的链接核对仓库,明确属于别的仓库的行一律不动;一行都认不出来时宁可什么都不归档,也不会把整个账号的收件箱带走。搜索视图的 `repo:` 限定不再是唯一保险。
|
|
4
|
+
## 2.18.1
|
|
5
|
+
|
|
6
|
+
- 归档只碰订阅的仓库:按仓库逐个打开 GitHub 的通知搜索视图(`repo:owner/name`),只归档这些视图里的线程。以前它选中整个收件箱,会把账号里其它仓库的通知一起归档。
|
|
7
|
+
- 没有订阅时不再归档任何东西(`--all` 才动整个收件箱),所以后台轮询不会替别人做决定。
|
|
8
|
+
- 登录窗口结束后的那次归档也带上同一份订阅。
|
|
9
|
+
- 登录现在能留住:GitHub 的会话 cookie 是会话级的,插件在浏览器配置里打开「恢复上次会话」,不再一次运行后就被登出。
|
|
10
|
+
- `--check` 与 `doctor` 的登录痕迹改看 `Default/Network/Cookies`(Chromium 96 之后的位置)。
|
|
1
11
|
## 2.18.0
|
|
2
12
|
|
|
3
13
|
- 归档不再让你去终端登录:把「消息清理」切到归档,插件自己弹出登录窗口;会话失效而选项仍在归档上时,下一次归档失败会再开一次(signInCooldownMs 默认 6 小时,防打扰)。
|
package/README.en.md
CHANGED
|
@@ -91,7 +91,7 @@ To see what it can actually see: `node scripts/inbox-bridge.mjs doctor` walks th
|
|
|
91
91
|
| `delivery` | `quiet` | `quiet` queues only; `wakeup` may start a turn while the session is idle |
|
|
92
92
|
| `wakeOn` | empty | roles allowed to wake under `wakeup`; empty never wakes |
|
|
93
93
|
| `clear` | `repo` | mark read after a poll: `repo` / `off`. Only the subscribed repositories, never the whole account |
|
|
94
|
-
| `sweep` | off | archive the
|
|
94
|
+
| `sweep` | off | archive the threads of the **repositories you subscribe to** into Done on turns that found something; the plugin opens a sign-in window when one is missing |
|
|
95
95
|
|
|
96
96
|
The panel's "message clearing" choice sets both: **off** (neither), **read** (clear the blue dot), **archive** (mark read and move to Done).
|
|
97
97
|
|
|
@@ -101,7 +101,8 @@ Full configuration, CLI and state files: [configuration](docs/configuration.md).
|
|
|
101
101
|
|
|
102
102
|
- **Read-only.** It does not merge, comment, or change anything.
|
|
103
103
|
- **No turn by default.** Only `delivery: wakeup` with a matching `wakeOn` role, enough idle time, and room in both budgets may start one; any gate that fails falls back to queueing.
|
|
104
|
-
- **
|
|
104
|
+
- **Archiving needs one browser sign-in, but you do not type a command for it.** GitHub has no archive API; `scripts/clear-inbox.mjs` drives a local Chromium-based browser (Edge, Chrome or Chromium). Switching "message clearing" to archive opens the sign-in window by itself, and it opens again if the session expires.
|
|
105
|
+
- **Archiving touches only the repositories you subscribe to.** It opens GitHub's notification search per repository (`repo:owner/name`) and archives what is in that view; with no subscription it archives nothing. The whole inbox needs an explicit `--all`.
|
|
105
106
|
- **State is local.** It lives under `.work/` in the working directory and is not synced across machines.
|
|
106
107
|
|
|
107
108
|
## Further reading
|
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ dsh plugin add dsh-inbox-bridge
|
|
|
91
91
|
| `delivery` | `quiet` | `quiet` 只入队;`wakeup` 可在会话空闲时主动开启一轮 |
|
|
92
92
|
| `wakeOn` | 空 | `wakeup` 下允许唤醒的角色白名单;为空则永不唤醒 |
|
|
93
93
|
| `clear` | `repo` | 将订阅仓库里读过的通知标为已读:`repo` / `off`。只清订阅的仓库,不动整个账号 |
|
|
94
|
-
| `sweep` | 关 |
|
|
94
|
+
| `sweep` | 关 | 有发现的回合把**订阅的仓库**的通知归档到 Done;缺登录时插件自己弹窗口让你登录一次 |
|
|
95
95
|
|
|
96
96
|
面板里的「消息清理」一档同时管这两件事:**关闭**(都不做)、**已读**(清蓝点)、**归档**(标已读并存进 Done)。
|
|
97
97
|
|
|
@@ -102,6 +102,7 @@ dsh plugin add dsh-inbox-bridge
|
|
|
102
102
|
- **只读。** 不合并、不评论、不修改仓库。
|
|
103
103
|
- **默认不主动开启对话。** 只有显式设置 `delivery: wakeup` 且命中 `wakeOn`、会话足够空闲、两个预算都还有余额时才会;任何一道门不过,就退回只排队。
|
|
104
104
|
- **Done 归档依赖一次浏览器登录,但不用你敲命令。** GitHub 没有归档 API,`scripts/clear-inbox.mjs` 通过本机浏览器(Edge / Chrome / Chromium)完成。把「消息清理」切到归档,插件会自己弹出登录窗口;会话失效时会再弹一次。
|
|
105
|
+
- **归档只碰你订阅的仓库。** 它按仓库逐个打开 GitHub 的通知搜索视图(`repo:owner/name`),只归档这些视图里的线程;没有订阅就什么都不归档。整个收件箱要显式 `--all` 才会动。
|
|
105
106
|
- **状态在本机。** 存放在工作目录的 `.work/` 下,不跨机器同步。
|
|
106
107
|
|
|
107
108
|
## 更多
|
package/docs/configuration.md
CHANGED
|
@@ -42,7 +42,9 @@ GitHub 的 Done 只有网页端点,所以归档要用插件自己的浏览器
|
|
|
42
42
|
- 之后会话失效,而选项还停在归档上——下一次归档失败时,插件自己再弹一次窗口(两次之间至少隔 `signInCooldownMs`)。
|
|
43
43
|
- 还没登录时卡片里会写着一行「归档还没登录 GitHub」,旁边有「打开登录窗口」按钮。
|
|
44
44
|
|
|
45
|
-
窗口是独立进程,宿主的轮询不会被它挡住。命令行里 `scripts/clear-inbox.mjs --check` 只回答「登录了没」(没登录时退出码 2),`--login` 打开窗口等你登录,`--dry-run`
|
|
45
|
+
窗口是独立进程,宿主的轮询不会被它挡住。命令行里 `scripts/clear-inbox.mjs --check` 只回答「登录了没」(没登录时退出码 2),`--login` 打开窗口等你登录,`--dry-run` 只报数量,`--repo owner/name`(可重复,插件通过 `INBOX_REPO` 传)指定范围,`--all` 才是整个收件箱。
|
|
46
|
+
|
|
47
|
+
登录状态能活下来,靠的是插件在浏览器配置里打开「恢复上次会话」——GitHub 的会话 cookie 本身是会话级的,Chromium 默认在关闭时丢掉它。
|
|
46
48
|
|
|
47
49
|
## 全部配置项
|
|
48
50
|
|
|
@@ -66,7 +68,7 @@ GitHub 的 Done 只有网页端点,所以归档要用插件自己的浏览器
|
|
|
66
68
|
| `identity` | `{ kind: "user" }` | 用哪个身份读 GitHub,见下 |
|
|
67
69
|
| `roles` | `[]` | 只接收这些角色的事件;空 = 全收 |
|
|
68
70
|
| `clear` | `"repo"` | 把这一轮读过的通知线程标为已读:`repo` 只清订阅的仓库,`off` 不碰收件箱。整个账号不在范围内——清别人的仓库属于人的决定,不是后台轮询的决定。旧配置里的 `"all"` 按 `"repo"` 载入。GitHub 没有「完成」API,所以清的是未读角标,历史仍在收件箱里 |
|
|
69
|
-
| `sweep` | `null` | 抓到东西后把收件箱归档进 Done:`{ args: ["scripts/clear-inbox.mjs"], when: "events" }`。`when: "events"` 只在有发现的回合跑,`"always"`
|
|
71
|
+
| `sweep` | `null` | 抓到东西后把收件箱归档进 Done:`{ args: ["scripts/clear-inbox.mjs"], when: "events" }`。`when: "events"` 只在有发现的回合跑,`"always"` 每轮都跑。**只归档订阅的仓库**(按仓库逐个走 `repo:` 搜索视图),没有订阅时什么都不归档;整个收件箱要显式 `--all`。归档要有插件的浏览器配置里的一次登录;没有就让面板替你打开窗口,见下 |
|
|
70
72
|
| `signInCooldownMs` | `21600000`(6 小时) | 归档缺登录时,两次「替你打开登录窗口」之间至少隔多久。长到不打扰,短到会话失效后的下一个事件还能恢复 |
|
|
71
73
|
| `debug` | `false` | 把每个边界事件写进 `plugin.log`,用来排查「没轮询」还是「轮询了没结果」 |
|
|
72
74
|
| `poll` | 插件自带 CLI 的 `poll` | 换掉抓取命令:`{ command, args }`,只要能输出约定的 JSON,来源可以不是 GitHub |
|
package/docs/how-it-works.md
CHANGED
|
@@ -36,7 +36,7 @@ DSH 插件 (index.js) 调度与投递:什么时候去取、取回来给
|
|
|
36
36
|
7. 投递:`quiet` 时注入给空闲的会话、等它下一轮;`wakeup` 时先过四道门。
|
|
37
37
|
8. 只把**真正投出去**的 key 标 delivered。没投出去的下次还有机会,不会被误标。
|
|
38
38
|
9. 把这些线程标为已读(`clear`,默认只清订阅的仓库;整个账号不在范围内)。GitHub 没有「完成」的公开接口,所以清掉的是未读角标,收件箱里的历史还在——在你自己的界面上一切照旧可查。
|
|
39
|
-
10. 如果配了 `sweep`,在**这一轮真的抓到东西**时顺手把它们归档进 Done(`scripts/clear-inbox.mjs` 用一次浏览器完成,约 3–5
|
|
39
|
+
10. 如果配了 `sweep`,在**这一轮真的抓到东西**时顺手把它们归档进 Done(`scripts/clear-inbox.mjs` 用一次浏览器完成,约 3–5 秒)。它按仓库逐个打开 GitHub 的通知搜索视图(`repo:owner/name`),只归档订阅的这些视图里的线程;没有订阅就一条也不归档(整个收件箱要显式 `--all`)。没抓到就什么也不做,空回合不花这笔钱。
|
|
40
40
|
|
|
41
41
|
## 页面怎么读这份状态
|
|
42
42
|
|
package/docs/index.json
CHANGED
|
@@ -50,12 +50,12 @@
|
|
|
50
50
|
},
|
|
51
51
|
"versions": [
|
|
52
52
|
{
|
|
53
|
-
"version": "2.18.
|
|
54
|
-
"publishTimestamp":
|
|
53
|
+
"version": "2.18.2",
|
|
54
|
+
"publishTimestamp": 1794916295,
|
|
55
55
|
"source": {
|
|
56
56
|
"type": "git",
|
|
57
57
|
"url": "https://github.com/c3115644151/dsh-inbox-bridge",
|
|
58
|
-
"commit": "
|
|
58
|
+
"commit": "3a01898a1cbaa3b043fd674aa6c4ba91c7accd0a"
|
|
59
59
|
},
|
|
60
60
|
"engines": {
|
|
61
61
|
"dsh": ">=0.1.0"
|
package/index.js
CHANGED
|
@@ -480,7 +480,17 @@ export function apply(ctx, config) {
|
|
|
480
480
|
try {
|
|
481
481
|
const child = spawn(process.execPath, [script, "--login", "--json"], {
|
|
482
482
|
cwd: options.cwd,
|
|
483
|
-
env: {
|
|
483
|
+
env: {
|
|
484
|
+
...process.env,
|
|
485
|
+
// The window finishes the sweep after a sign-in; give it the same
|
|
486
|
+
// subscription the poll uses, or it would not know what to archive.
|
|
487
|
+
...(options.repos?.length > 0
|
|
488
|
+
? { INBOX_REPO: options.repos.join(",") }
|
|
489
|
+
: options.repo
|
|
490
|
+
? { INBOX_REPO: options.repo }
|
|
491
|
+
: {}),
|
|
492
|
+
ELECTRON_RUN_AS_NODE: "1",
|
|
493
|
+
},
|
|
484
494
|
// Detached so a twenty-minute sign-in never holds a poll open; visible
|
|
485
495
|
// because there has to be a window to sign in.
|
|
486
496
|
detached: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-inbox-bridge",
|
|
3
|
-
"version": "2.18.
|
|
3
|
+
"version": "2.18.2",
|
|
4
4
|
"displayName": "GitHub inbox",
|
|
5
5
|
"description": "DSH 插件:让 agent 像维护者一样盯着你的仓库——按会话订阅仓库,取回与你有关、需要你处理的事件(失败的 CI、待评审的 PR、指派给你的 issue、@你的评论、开着的安全告警),在自己的回合送进上下文;会话空闲又遇到要紧的事时也能主动开一轮,让 agent 自己去修、去审、去接。",
|
|
6
6
|
"type": "module",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"screenshots.json"
|
|
46
46
|
],
|
|
47
47
|
"scripts": {
|
|
48
|
-
"test": "node --test index.test.mjs scripts/github-inbox.test.mjs scripts/inbox-bridge.test.mjs",
|
|
48
|
+
"test": "node --test index.test.mjs scripts/github-inbox.test.mjs scripts/inbox-bridge.test.mjs scripts/clear-inbox.test.mjs",
|
|
49
49
|
"market": "node scripts/build-market-index.mjs",
|
|
50
50
|
"market:check": "node scripts/build-market-index.mjs --check",
|
|
51
51
|
"market:release": "node scripts/build-market-index.mjs --check --release"
|
package/scripts/clear-inbox.mjs
CHANGED
|
@@ -15,13 +15,31 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { spawn } from "node:child_process";
|
|
18
|
-
import { existsSync, mkdirSync, rmSync } from "node:fs";
|
|
19
|
-
import { join, resolve } from "node:path";
|
|
18
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
19
|
+
import { dirname, join, resolve } from "node:path";
|
|
20
|
+
import { pathToFileURL } from "node:url";
|
|
20
21
|
|
|
21
22
|
const DEFAULT_PORT = 9333;
|
|
22
23
|
const NOTIFICATIONS = "https://github.com/notifications";
|
|
23
24
|
const PROFILE_REL = join(".work", "browser-profile");
|
|
24
25
|
|
|
26
|
+
/** The notification view for one repository, or the whole inbox for `null`. */
|
|
27
|
+
export function inboxUrlFor(repo) {
|
|
28
|
+
return repo ? `${NOTIFICATIONS}?query=${encodeURIComponent(`repo:${repo}`)}` : NOTIFICATIONS;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Which notification views to archive. The default is the subscription the
|
|
33
|
+
* plugin watches — never the whole account, because a background poll must not
|
|
34
|
+
* decide what to do with another repository's mail.
|
|
35
|
+
*/
|
|
36
|
+
export function archiveTargets(options = {}) {
|
|
37
|
+
if (options.all) return { ok: true, targets: [null] };
|
|
38
|
+
const repos = options.repos ?? [];
|
|
39
|
+
if (repos.length === 0) return { ok: false, reason: "no-subscription", targets: [] };
|
|
40
|
+
return { ok: true, targets: repos };
|
|
41
|
+
}
|
|
42
|
+
|
|
25
43
|
const WINDOWS_CANDIDATES = [
|
|
26
44
|
"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
|
|
27
45
|
"C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe",
|
|
@@ -59,14 +77,28 @@ function onPath(command) {
|
|
|
59
77
|
return dirs.some((dir) => dir && names.some((name) => existsSync(join(dir, name))));
|
|
60
78
|
}
|
|
61
79
|
|
|
62
|
-
function parseArgs(argv = []) {
|
|
63
|
-
const options = {
|
|
80
|
+
export function parseArgs(argv = []) {
|
|
81
|
+
const options = {
|
|
82
|
+
json: false,
|
|
83
|
+
login: false,
|
|
84
|
+
dryRun: false,
|
|
85
|
+
check: false,
|
|
86
|
+
all: false,
|
|
87
|
+
repos: [],
|
|
88
|
+
port: DEFAULT_PORT,
|
|
89
|
+
keepOpen: false,
|
|
90
|
+
};
|
|
64
91
|
for (const argument of argv) {
|
|
65
92
|
if (argument === "--json") options.json = true;
|
|
66
93
|
else if (argument === "--login") options.login = true;
|
|
67
94
|
else if (argument === "--dry-run") options.dryRun = true;
|
|
68
|
-
else if (argument === "--check") options.check = true;
|
|
69
|
-
else if (argument === "--
|
|
95
|
+
else if (argument === "--check") options.check = true;
|
|
96
|
+
else if (argument === "--all") options.all = true;
|
|
97
|
+
else if (argument === "--repo") options.expectRepo = true;
|
|
98
|
+
else if (options.expectRepo) {
|
|
99
|
+
options.repos.push(...argument.split(/[,\s]+/u).filter(Boolean));
|
|
100
|
+
options.expectRepo = false;
|
|
101
|
+
} else if (argument === "--keep-open") options.keepOpen = true;
|
|
70
102
|
else if (argument === "--port") options.expectPort = true;
|
|
71
103
|
else if (options.expectPort) {
|
|
72
104
|
options.port = Number(argument);
|
|
@@ -75,6 +107,14 @@ else if (argument === "--check") options.check = true;
|
|
|
75
107
|
else throw new Error(`unknown option: ${argument}`);
|
|
76
108
|
}
|
|
77
109
|
if (options.expectPort) throw new Error("`--port` needs a value");
|
|
110
|
+
if (options.expectRepo) throw new Error("`--repo` needs a value");
|
|
111
|
+
// The plugin passes its subscription in INBOX_REPO, so a configured sweep is
|
|
112
|
+
// scoped without repeating the list in `sweep.args`.
|
|
113
|
+
const fromEnv = (process.env.INBOX_REPO ?? "").split(/[,\s]+/u).filter(Boolean);
|
|
114
|
+
const repos = options.repos.length > 0 ? options.repos : fromEnv;
|
|
115
|
+
options.repos = repos.filter((repo) => /^[\w.-]+\/[\w.-]+$/u.test(repo));
|
|
116
|
+
const bad = repos.filter((repo) => !/^[\w.-]+\/[\w.-]+$/u.test(repo));
|
|
117
|
+
if (bad.length > 0) throw new Error(`not an owner/name repository: ${bad.join(", ")}`);
|
|
78
118
|
return options;
|
|
79
119
|
}
|
|
80
120
|
|
|
@@ -137,17 +177,58 @@ async function connect(wsUrl) {
|
|
|
137
177
|
}
|
|
138
178
|
|
|
139
179
|
/**
|
|
140
|
-
* Runs in the page. Selects
|
|
141
|
-
* reports what happened. Retries the
|
|
180
|
+
* Runs in the page. Selects the rows that belong to `repo` (everything when
|
|
181
|
+
* `repo` is null) and presses Done, then reports what happened. Retries the
|
|
182
|
+
* click because the toolbar renders lazily.
|
|
183
|
+
*
|
|
184
|
+
* The view is already narrowed by a `repo:` search, but that alone is a thin
|
|
185
|
+
* guarantee: if GitHub ever ignores the qualifier, the page would hand us the
|
|
186
|
+
* whole account and this script would archive it. So each row is also checked
|
|
187
|
+
* against its own links — a row that clearly belongs to another repository is
|
|
188
|
+
* left alone. A row whose repository cannot be read is kept, because the view
|
|
189
|
+
* is the only evidence available at that point.
|
|
142
190
|
*/
|
|
143
|
-
const
|
|
144
|
-
const
|
|
191
|
+
export const sweepScript = (repo) => `(async () => {
|
|
192
|
+
const boxes = () => [...document.querySelectorAll('input[type=checkbox][name="notification_ids[]"]')];
|
|
193
|
+
const owner = ${JSON.stringify(repo ?? null)};
|
|
194
|
+
const pathOf = (link) => {
|
|
195
|
+
try { return new URL(link.href).pathname; } catch { return ""; }
|
|
196
|
+
};
|
|
197
|
+
const repoPaths = (node) =>
|
|
198
|
+
[...node.querySelectorAll('a[href]')]
|
|
199
|
+
.map(pathOf)
|
|
200
|
+
.filter((path) => /^\\/[^/]+\\/[^/]+\\//.test(path));
|
|
201
|
+
const rowOf = (box) => {
|
|
202
|
+
let node = box;
|
|
203
|
+
let found = null;
|
|
204
|
+
for (let depth = 0; depth < 10 && node; depth += 1) {
|
|
205
|
+
const count = node.querySelectorAll?.('input[type=checkbox][name="notification_ids[]"]').length ?? 0;
|
|
206
|
+
if (count > 1) break;
|
|
207
|
+
if (count === 1 && repoPaths(node).length > 0) found = node;
|
|
208
|
+
node = node.parentElement;
|
|
209
|
+
}
|
|
210
|
+
return found;
|
|
211
|
+
};
|
|
212
|
+
const foreign = (box) => {
|
|
213
|
+
if (!owner) return false;
|
|
214
|
+
const row = rowOf(box);
|
|
215
|
+
if (!row) return false;
|
|
216
|
+
const paths = repoPaths(row);
|
|
217
|
+
if (paths.length === 0) return false;
|
|
218
|
+
return !paths.some((path) => path.startsWith('/' + owner + '/'));
|
|
219
|
+
};
|
|
145
220
|
const started = Date.now();
|
|
146
|
-
const
|
|
221
|
+
const all = boxes();
|
|
222
|
+
const before = all.length;
|
|
147
223
|
if (before === 0) {
|
|
148
|
-
return { archived: 0, before: 0, after: 0, empty: true };
|
|
224
|
+
return { archived: 0, before: 0, after: 0, skipped: 0, empty: true };
|
|
225
|
+
}
|
|
226
|
+
const mine = all.filter((box) => !foreign(box));
|
|
227
|
+
const skipped = before - mine.length;
|
|
228
|
+
if (mine.length === 0) {
|
|
229
|
+
return { archived: 0, before, after: before, skipped, unscoped: true };
|
|
149
230
|
}
|
|
150
|
-
for (const box of
|
|
231
|
+
for (const box of mine) box.click();
|
|
151
232
|
await new Promise((r) => setTimeout(r, 700));
|
|
152
233
|
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
153
234
|
const done = [...document.querySelectorAll('button')].find((b) => b.textContent.trim() === 'Done');
|
|
@@ -159,9 +240,9 @@ const SWEEP = `(async () => {
|
|
|
159
240
|
}
|
|
160
241
|
for (let waited = 0; waited < 30; waited += 1) {
|
|
161
242
|
await new Promise((r) => setTimeout(r, 300));
|
|
162
|
-
if (
|
|
243
|
+
if (boxes().length === 0) break;
|
|
163
244
|
}
|
|
164
|
-
return { archived: before -
|
|
245
|
+
return { archived: before - boxes().length, before, after: boxes().length, skipped, empty: false, ms: Date.now() - started };
|
|
165
246
|
})()`;
|
|
166
247
|
|
|
167
248
|
async function main(argv) {
|
|
@@ -169,8 +250,10 @@ async function main(argv) {
|
|
|
169
250
|
if (options.help) {
|
|
170
251
|
console.log(
|
|
171
252
|
[
|
|
172
|
-
"clear-inbox — archive
|
|
253
|
+
"clear-inbox — archive the GitHub notification threads of the repositories you watch, then close the browser",
|
|
173
254
|
"",
|
|
255
|
+
" --repo <owner/name> archive only this repository's threads (repeatable; defaults to INBOX_REPO)",
|
|
256
|
+
" --all archive the whole notification inbox instead of one repository at a time",
|
|
174
257
|
" --json machine-readable summary",
|
|
175
258
|
" --dry-run report how many threads would be archived",
|
|
176
259
|
" --check only say whether this profile is signed in (exit 2 if not)",
|
|
@@ -184,14 +267,36 @@ async function main(argv) {
|
|
|
184
267
|
|
|
185
268
|
const root = process.cwd();
|
|
186
269
|
const profileDir = resolve(root, PROFILE_REL);
|
|
270
|
+
// Chromium keeps cookies under Network/ since v96; older builds used Default/.
|
|
271
|
+
const cookieFiles = [
|
|
272
|
+
join(profileDir, "Default", "Network", "Cookies"),
|
|
273
|
+
join(profileDir, "Default", "Cookies"),
|
|
274
|
+
];
|
|
187
275
|
// A profile with no cookie database has never been signed in. Answering that
|
|
188
276
|
// here keeps `--check` from launching a browser just to learn it is useless.
|
|
189
|
-
if (options.check && !
|
|
277
|
+
if (options.check && !cookieFiles.some((file) => existsSync(file))) {
|
|
190
278
|
if (options.json) console.log(JSON.stringify({ ok: false, reason: "sign-in-required" }));
|
|
191
279
|
else console.log("not signed in: archiving cannot run yet");
|
|
192
280
|
return 2;
|
|
193
281
|
}
|
|
194
282
|
mkdirSync(profileDir, { recursive: true });
|
|
283
|
+
// GitHub's session cookie is a session cookie, and Chromium only keeps those
|
|
284
|
+
// across restarts when the profile restores its last session. Without this the
|
|
285
|
+
// sign-in survives exactly one run — which looks like "it forgot me again".
|
|
286
|
+
try {
|
|
287
|
+
const file = join(profileDir, "Default", "Preferences");
|
|
288
|
+
let prefs = {};
|
|
289
|
+
try {
|
|
290
|
+
prefs = JSON.parse(readFileSync(file, "utf8"));
|
|
291
|
+
} catch {
|
|
292
|
+
prefs = {};
|
|
293
|
+
}
|
|
294
|
+
prefs.session = { ...(prefs.session ?? {}), restore_on_startup: 1 };
|
|
295
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
296
|
+
writeFileSync(file, JSON.stringify(prefs), "utf8");
|
|
297
|
+
} catch {
|
|
298
|
+
// A profile we cannot pre-seed still works for one run.
|
|
299
|
+
}
|
|
195
300
|
const browser = findBrowser();
|
|
196
301
|
const base = `http://127.0.0.1:${options.port}`;
|
|
197
302
|
const headless = !options.login;
|
|
@@ -285,15 +390,6 @@ async function main(argv) {
|
|
|
285
390
|
}
|
|
286
391
|
}
|
|
287
392
|
|
|
288
|
-
if (options.dryRun) {
|
|
289
|
-
const rows = await read(
|
|
290
|
-
`document.querySelectorAll('input[type=checkbox][name="notification_ids[]"]').length`,
|
|
291
|
-
);
|
|
292
|
-
if (options.json) console.log(JSON.stringify({ ok: true, dryRun: true, rows }));
|
|
293
|
-
else console.log(`would archive ${rows} thread(s)`);
|
|
294
|
-
return 0;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
393
|
// `--check` answers one question and touches nothing: is this profile
|
|
298
394
|
// signed in? The panel and `doctor` need that without archiving anything.
|
|
299
395
|
if (options.check) {
|
|
@@ -305,11 +401,90 @@ async function main(argv) {
|
|
|
305
401
|
return 0;
|
|
306
402
|
}
|
|
307
403
|
|
|
308
|
-
|
|
309
|
-
|
|
404
|
+
// One view per subscribed repository: GitHub's notification search takes
|
|
405
|
+
// `repo:`, and Done applies to the rows in view — so this archives what
|
|
406
|
+
// the plugin watches instead of everything the account ever received.
|
|
407
|
+
const plan = archiveTargets(options);
|
|
408
|
+
if (!plan.ok) {
|
|
409
|
+
if (options.json) console.log(JSON.stringify({ ok: false, reason: plan.reason }));
|
|
410
|
+
else
|
|
411
|
+
console.error(
|
|
412
|
+
"nothing archived: pass --repo owner/name (or INBOX_REPO), or --all for the whole inbox",
|
|
413
|
+
);
|
|
414
|
+
return 2;
|
|
415
|
+
}
|
|
416
|
+
const targets = plan.targets;
|
|
417
|
+
const urlFor = inboxUrlFor;
|
|
418
|
+
|
|
419
|
+
const waitForList = async () => {
|
|
420
|
+
const deadline = Date.now() + 30_000;
|
|
421
|
+
let view = null;
|
|
422
|
+
while (Date.now() < deadline) {
|
|
423
|
+
view = await read(`(() => ({
|
|
424
|
+
ready: document.readyState,
|
|
425
|
+
rows: document.querySelectorAll('input[type=checkbox][name="notification_ids[]"]').length,
|
|
426
|
+
caughtUp: document.body.innerText.includes('All caught up'),
|
|
427
|
+
signIn: /\\/login|\\/session/.test(location.pathname),
|
|
428
|
+
}))()`);
|
|
429
|
+
if (view.signIn) return view;
|
|
430
|
+
if (view.ready === "complete" && (view.rows > 0 || view.caughtUp)) return view;
|
|
431
|
+
await sleep(400);
|
|
432
|
+
}
|
|
433
|
+
return view;
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
const unsorted = async () => {
|
|
437
|
+
if (options.json) console.log(JSON.stringify({ ok: false, reason: "sign-in-required" }));
|
|
438
|
+
else
|
|
439
|
+
console.error(
|
|
440
|
+
"not signed in: run `node scripts/clear-inbox.mjs --login` once, sign in, and it will finish the sweep",
|
|
441
|
+
);
|
|
442
|
+
return 2;
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
if (options.dryRun) {
|
|
446
|
+
const perRepo = [];
|
|
447
|
+
let total = 0;
|
|
448
|
+
for (const repo of targets) {
|
|
449
|
+
await devtools.send("Page.navigate", { url: urlFor(repo) });
|
|
450
|
+
const view = await waitForList();
|
|
451
|
+
if (view?.signIn) return await unsorted();
|
|
452
|
+
perRepo.push({ repo, rows: view?.rows ?? 0 });
|
|
453
|
+
total += view?.rows ?? 0;
|
|
454
|
+
}
|
|
455
|
+
if (options.json) console.log(JSON.stringify({ ok: true, dryRun: true, rows: total, perRepo }));
|
|
456
|
+
else console.log(`would archive ${total} thread(s) (${perRepo.map((each) => `${each.repo ?? "everything"}: ${each.rows}`).join(", ")})`);
|
|
457
|
+
return 0;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const started = Date.now();
|
|
461
|
+
const perRepo = [];
|
|
462
|
+
for (const repo of targets) {
|
|
463
|
+
await devtools.send("Page.navigate", { url: urlFor(repo) });
|
|
464
|
+
const view = await waitForList();
|
|
465
|
+
if (view?.signIn) return await unsorted();
|
|
466
|
+
const result = await read(sweepScript(repo));
|
|
467
|
+
perRepo.push({ repo, archived: result.archived ?? 0, before: result.before ?? 0, skipped: result.skipped ?? 0 });
|
|
468
|
+
if (result.unscoped) perRepo[perRepo.length - 1].unscoped = true;
|
|
469
|
+
}
|
|
470
|
+
const archived = perRepo.reduce((sum, each) => sum + each.archived, 0);
|
|
471
|
+
const skipped = perRepo.reduce((sum, each) => sum + (each.skipped ?? 0), 0);
|
|
472
|
+
const summary = {
|
|
473
|
+
ok: true,
|
|
474
|
+
archived,
|
|
475
|
+
skipped,
|
|
476
|
+
repos: perRepo.map((each) => each.repo).filter(Boolean),
|
|
477
|
+
perRepo,
|
|
478
|
+
ms: Date.now() - started,
|
|
479
|
+
empty: archived === 0 && perRepo.every((each) => each.before === 0),
|
|
480
|
+
};
|
|
310
481
|
if (options.json) console.log(JSON.stringify(summary));
|
|
311
|
-
else if (
|
|
312
|
-
else
|
|
482
|
+
else if (summary.empty) console.log("nothing to archive in the repositories you watch");
|
|
483
|
+
else {
|
|
484
|
+
const guarded = skipped > 0 ? `, left ${skipped} thread(s) of other repositories alone` : "";
|
|
485
|
+
const confused = perRepo.some((each) => each.unscoped) ? "; the page did not name the rows it showed, so nothing was archived" : "";
|
|
486
|
+
console.log(`archived ${archived} thread(s) in ${Math.round(summary.ms / 100) / 10}s${guarded}${confused}`);
|
|
487
|
+
}
|
|
313
488
|
return 0;
|
|
314
489
|
} finally {
|
|
315
490
|
devtools.close();
|
|
@@ -340,10 +515,13 @@ async function main(argv) {
|
|
|
340
515
|
}
|
|
341
516
|
}
|
|
342
517
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
);
|
|
518
|
+
// Importable for tests; only a direct run drives a browser.
|
|
519
|
+
if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
|
|
520
|
+
main(process.argv.slice(2)).then(
|
|
521
|
+
(code) => process.exit(code ?? 0),
|
|
522
|
+
(error) => {
|
|
523
|
+
console.error(`clear-inbox: ${error.message}`);
|
|
524
|
+
process.exit(1);
|
|
525
|
+
},
|
|
526
|
+
);
|
|
527
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { archiveTargets, inboxUrlFor, parseArgs, sweepScript } from "./clear-inbox.mjs";
|
|
4
|
+
|
|
5
|
+
test("the sweep archives the repositories the plugin watches, one view each", () => {
|
|
6
|
+
const plan = archiveTargets({ repos: ["owner/one", "owner/two"] });
|
|
7
|
+
assert.deepEqual(plan.targets, ["owner/one", "owner/two"]);
|
|
8
|
+
assert.equal(plan.ok, true);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("a sweep with no subscription refuses instead of taking the whole account", () => {
|
|
12
|
+
const plan = archiveTargets({ repos: [] });
|
|
13
|
+
assert.equal(plan.ok, false);
|
|
14
|
+
assert.equal(plan.reason, "no-subscription");
|
|
15
|
+
assert.deepEqual(plan.targets, []);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("`--all` is the one way to archive the whole inbox", () => {
|
|
19
|
+
assert.deepEqual(archiveTargets({ all: true }).targets, [null]);
|
|
20
|
+
assert.deepEqual(archiveTargets({ all: true, repos: ["owner/one"] }).targets, [null]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("each repository is a notification search view", () => {
|
|
24
|
+
assert.equal(
|
|
25
|
+
inboxUrlFor("owner/one"),
|
|
26
|
+
"https://github.com/notifications?query=repo%3Aowner%2Fone",
|
|
27
|
+
);
|
|
28
|
+
assert.equal(inboxUrlFor(null), "https://github.com/notifications");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("--repo is repeatable and also accepts a comma-joined list", () => {
|
|
32
|
+
const one = parseArgs(["--repo", "owner/one", "--repo", "owner/two"]);
|
|
33
|
+
assert.deepEqual(one.repos, ["owner/one", "owner/two"]);
|
|
34
|
+
const joined = parseArgs(["--repo", "owner/one,owner/two"]);
|
|
35
|
+
assert.deepEqual(joined.repos, ["owner/one", "owner/two"]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("a malformed repository name is refused rather than half-used", () => {
|
|
39
|
+
assert.throws(() => parseArgs(["--repo", "not-a-repo"]), /not an owner\/name repository/u);
|
|
40
|
+
assert.throws(() => parseArgs(["--repo"]), /`--repo` needs a value/u);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("the plugin's subscription arrives through INBOX_REPO", () => {
|
|
44
|
+
process.env.INBOX_REPO = "owner/one,owner/two";
|
|
45
|
+
try {
|
|
46
|
+
assert.deepEqual(parseArgs(["--json"]).repos, ["owner/one", "owner/two"]);
|
|
47
|
+
// An explicit --repo wins over the environment, so a manual run can narrow it.
|
|
48
|
+
assert.deepEqual(parseArgs(["--repo", "owner/three"]).repos, ["owner/three"]);
|
|
49
|
+
assert.equal(parseArgs(["--all"]).all, true);
|
|
50
|
+
} finally {
|
|
51
|
+
delete process.env.INBOX_REPO;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("--check and --dry-run are separate switches", () => {
|
|
56
|
+
const both = parseArgs(["--check", "--dry-run"]);
|
|
57
|
+
assert.equal(both.check, true);
|
|
58
|
+
assert.equal(both.dryRun, true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("the page script refuses rows that belong to another repository", () => {
|
|
62
|
+
// The search view is narrowed by `repo:`, but the page script checks each row
|
|
63
|
+
// against its own links too: if GitHub ignored the qualifier, an unfiltered
|
|
64
|
+
// row must never be archived. The guard is generated per repository, so this
|
|
65
|
+
// test keeps it from being removed without a trace.
|
|
66
|
+
const scoped = sweepScript("owner/repo");
|
|
67
|
+
assert.match(scoped, /const owner = "owner\/repo";/u);
|
|
68
|
+
assert.match(scoped, /\.startsWith\('\/' \+ owner \+ '\/'\)/u);
|
|
69
|
+
assert.match(scoped, /unscoped: true/u);
|
|
70
|
+
// `--all` is the only mode that keeps every row.
|
|
71
|
+
assert.match(sweepScript(null), /const owner = null;/u);
|
|
72
|
+
});
|
package/scripts/inbox-bridge.mjs
CHANGED
|
@@ -565,21 +565,23 @@ export function describeDoctor(root, options = {}) {
|
|
|
565
565
|
}
|
|
566
566
|
|
|
567
567
|
if (runtime?.sweep && runtime.sweep !== "off" && runtime.sweep !== null) {
|
|
568
|
-
const
|
|
568
|
+
const profile = join(resolve(root), ".work", "browser-profile", "Default");
|
|
569
|
+
// Chromium keeps cookies under Network/ since v96; older builds used Default/.
|
|
570
|
+
const cookies = [join(profile, "Network", "Cookies"), join(profile, "Cookies")];
|
|
569
571
|
const cli = join(dirname(fileURLToPath(import.meta.url)), "clear-inbox.mjs");
|
|
570
|
-
if (existsSync(
|
|
572
|
+
if (cookies.some((file) => existsSync(file))) {
|
|
571
573
|
add(
|
|
572
574
|
"sweep",
|
|
573
575
|
"ok",
|
|
574
576
|
"清理收件箱用的浏览器配置里有登录痕迹",
|
|
575
|
-
"
|
|
577
|
+
`这只是痕迹,不代表会话还有效;真失效时插件会自己再开一次登录窗口(node "${cli}" --check 能直接问)`,
|
|
576
578
|
);
|
|
577
579
|
} else {
|
|
578
580
|
add(
|
|
579
581
|
"sweep",
|
|
580
582
|
"warn",
|
|
581
583
|
"清理收件箱还没有登录过",
|
|
582
|
-
|
|
584
|
+
`把面板的「消息清理」切到归档,插件会自己弹出登录窗口;命令行也行:node "${cli}" --login`,
|
|
583
585
|
);
|
|
584
586
|
}
|
|
585
587
|
}
|