@gtheys/pi-pr-digest 0.0.0-stage → 0.2.0

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/README.md CHANGED
@@ -1,3 +1,46 @@
1
- # Temporary Holding Version
1
+ # @gtheys/pi-pr-digest
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Pi extension that gives the agent a digest of your outstanding GitHub PRs in
4
+ an org: which have human comments, who reviewed them, and which are silently
5
+ waiting for reviewers. Uses the `gh` CLI (must be installed and
6
+ authenticated).
7
+
8
+ ## What it provides
9
+
10
+ - **`pr_digest` tool** — lists an author's open PRs in an org via
11
+ `gh search prs`, fetches comments/reviews per PR, filters out bot activity
12
+ (logins ending in `[bot]`, `sonarqubecloud`,
13
+ `copilot-pull-request-reviewer`, `github-actions`, `dependabot`, `codecov`,
14
+ plus configured `botLogins`), and returns per-PR `humanCommenters`,
15
+ `humanReviews`, and `hasHumanComments`.
16
+ - **`/pr-digest [org] [author]` command** — runs the digest and has the agent
17
+ report PRs with human comments, then render a reviewer-request markdown
18
+ table for the silent ones ("asking for a second reviewer" / "we need 2
19
+ reviewers").
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ pi install @gtheys/pi-pr-digest
25
+ ```
26
+
27
+ ## Configuration
28
+
29
+ Global config lives at `~/.pi/agent/pi-pr-digest/config.json`.
30
+
31
+ | Option | Type | Default | Description |
32
+ |--------|------|---------|-------------|
33
+ | `org` | string | `Salary-Hero` | GitHub org/owner to search open PRs in |
34
+ | `author` | string | `@me` | GitHub author login to search PRs for |
35
+ | `botLogins` | string[] | `[]` | Extra logins to treat as bots (their comments/reviews don't count as human activity) |
36
+
37
+ ```json
38
+ {
39
+ "$schema": "https://raw.githubusercontent.com/gtheys/pi-my-rifle-ext/main/packages/pi-pr-digest/config.schema.json",
40
+ "org": "Salary-Hero",
41
+ "author": "@me",
42
+ "botLogins": []
43
+ }
44
+ ```
45
+
46
+ Tool parameters `org` / `author` override config per call.
@@ -0,0 +1,20 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "org": {
5
+ "type": "string",
6
+ "description": "GitHub org or owner to search open PRs in (e.g. 'Salary-Hero'). Default 'Salary-Hero'."
7
+ },
8
+ "author": {
9
+ "type": "string",
10
+ "description": "GitHub author login to search PRs for, or '@me'. Default '@me'."
11
+ },
12
+ "botLogins": {
13
+ "type": "array",
14
+ "items": {
15
+ "type": "string"
16
+ },
17
+ "description": "Extra logins to treat as bots (their comments/reviews don't count as human activity). Logins ending in '[bot]' are always ignored. Built-in bots: sonarqubecloud, copilot-pull-request-reviewer, github-actions, dependabot, codecov."
18
+ }
19
+ }
20
+ }
package/index.ts ADDED
@@ -0,0 +1,301 @@
1
+ import { execFile } from 'node:child_process'
2
+ import { promises as fs } from 'node:fs'
3
+ import path from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+ import { promisify } from 'node:util'
6
+ import {
7
+ CONFIG_DIR_NAME,
8
+ type ExtensionAPI,
9
+ getAgentDir,
10
+ } from '@earendil-works/pi-coding-agent'
11
+ import { Type } from 'typebox'
12
+ import { Value } from 'typebox/value'
13
+
14
+ const execFileAsync = promisify(execFile)
15
+
16
+ // AIDEV-NOTE: TypeBox schema is the source of truth for config shape.
17
+ // config.schema.json (checked in) is regenerated from this at startup if
18
+ // missing. Global path: getAgentDir()/pi-pr-digest/config.json.
19
+ // Project override: <cwd>/CONFIG_DIR_NAME/pi-pr-digest/config.json.
20
+ const PrDigestConfigSchema = Type.Object({
21
+ org: Type.Optional(
22
+ Type.String({
23
+ description:
24
+ "GitHub org or owner to search open PRs in (e.g. 'Salary-Hero'). Default 'Salary-Hero'.",
25
+ }),
26
+ ),
27
+ author: Type.Optional(
28
+ Type.String({
29
+ description:
30
+ "GitHub author login to search PRs for, or '@me'. Default '@me'.",
31
+ }),
32
+ ),
33
+ botLogins: Type.Optional(
34
+ Type.Array(Type.String(), {
35
+ description:
36
+ "Extra logins to treat as bots (their comments/reviews don't count as human activity). Logins ending in '[bot]' are always ignored. Built-in bots: sonarqubecloud, copilot-pull-request-reviewer, github-actions, dependabot, codecov.",
37
+ }),
38
+ ),
39
+ })
40
+
41
+ type PrDigestConfig = {
42
+ org?: string
43
+ author?: string
44
+ botLogins?: string[]
45
+ }
46
+
47
+ const DEFAULT_ORG = 'Salary-Hero'
48
+ // AIDEV-NOTE: keep in sync with the botLogins schema description above.
49
+ export const BUILTIN_BOTS = new Set([
50
+ 'sonarqubecloud',
51
+ 'copilot-pull-request-reviewer',
52
+ 'github-actions',
53
+ 'dependabot',
54
+ 'codecov',
55
+ ])
56
+
57
+ const GLOBAL_CONFIG_PATH = path.join(
58
+ getAgentDir(),
59
+ 'pi-pr-digest',
60
+ 'config.json',
61
+ )
62
+
63
+ function projectConfigPath(cwd: string): string {
64
+ return path.join(cwd, CONFIG_DIR_NAME, 'pi-pr-digest', 'config.json')
65
+ }
66
+
67
+ async function readConfigFile(file: string): Promise<PrDigestConfig> {
68
+ try {
69
+ const raw = await fs.readFile(file, 'utf8')
70
+ const parsed: unknown = JSON.parse(raw)
71
+ if (!Value.Check(PrDigestConfigSchema, parsed)) return {}
72
+ return parsed
73
+ } catch (error: unknown) {
74
+ if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') return {}
75
+ const msg = error instanceof Error ? error.message : String(error)
76
+ throw new Error(`Failed to read pi-pr-digest config ${file}: ${msg}`)
77
+ }
78
+ }
79
+
80
+ // AIDEV-NOTE: precedence is explicit tool param > project config > global
81
+ // config > built-in default.
82
+ async function resolveConfig(
83
+ cwd: string,
84
+ paramOrg?: string,
85
+ paramAuthor?: string,
86
+ ): Promise<{ org: string; author: string; bots: Set<string> }> {
87
+ const global = await readConfigFile(GLOBAL_CONFIG_PATH)
88
+ const project = await readConfigFile(projectConfigPath(cwd))
89
+ const org = paramOrg || project.org || global.org || DEFAULT_ORG
90
+ const author = paramAuthor || project.author || global.author || '@me'
91
+ const bots = new Set(BUILTIN_BOTS)
92
+ for (const login of global.botLogins || []) bots.add(login)
93
+ for (const login of project.botLogins || []) bots.add(login)
94
+ return { org, author, bots }
95
+ }
96
+
97
+ interface GhSearchEntry {
98
+ number: number
99
+ title: string
100
+ url: string
101
+ repository: { name: string; nameWithOwner: string }
102
+ }
103
+
104
+ interface GhReview {
105
+ author: { login: string }
106
+ state: string
107
+ }
108
+
109
+ interface GhComment {
110
+ author: { login: string }
111
+ }
112
+
113
+ interface GhPrDetails {
114
+ url: string
115
+ title: string
116
+ body: string
117
+ comments: GhComment[]
118
+ reviews: GhReview[]
119
+ }
120
+
121
+ export interface DigestPr {
122
+ number: number
123
+ repo: string
124
+ title: string
125
+ url: string
126
+ humanCommenters: string[]
127
+ humanReviews: Array<{ author: string; state: string }>
128
+ hasHumanComments: boolean
129
+ }
130
+
131
+ export function isBot(login: string, bots: Set<string>): boolean {
132
+ return login.endsWith('[bot]') || bots.has(login)
133
+ }
134
+
135
+ // AIDEV-NOTE: pure so the bot-filter/classification rules stay unit-testable
136
+ // without shelling out to gh.
137
+ export function classifyActivity(
138
+ comments: GhComment[],
139
+ reviews: GhReview[],
140
+ bots: Set<string>,
141
+ ): Pick<DigestPr, 'humanCommenters' | 'humanReviews' | 'hasHumanComments'> {
142
+ const humanCommenters = [
143
+ ...new Set(
144
+ comments
145
+ .map((c) => c.author.login)
146
+ .filter((login) => !isBot(login, bots)),
147
+ ),
148
+ ]
149
+ const humanReviews = reviews
150
+ .filter((r) => !isBot(r.author.login, bots))
151
+ .map((r) => ({ author: r.author.login, state: r.state }))
152
+ return {
153
+ humanCommenters,
154
+ humanReviews,
155
+ hasHumanComments: humanCommenters.length > 0,
156
+ }
157
+ }
158
+
159
+ async function ghJson<T>(args: string[]): Promise<T> {
160
+ const env = { ...process.env, GH_PAGER: 'cat' }
161
+ const { stdout } = await execFileAsync('gh', args, {
162
+ env,
163
+ maxBuffer: 16 * 1024 * 1024,
164
+ })
165
+ return JSON.parse(stdout) as T
166
+ }
167
+
168
+ export async function collectDigest(
169
+ org: string,
170
+ author: string,
171
+ limit: number,
172
+ bots: Set<string>,
173
+ ): Promise<DigestPr[]> {
174
+ const search = await ghJson<GhSearchEntry[]>([
175
+ 'search',
176
+ 'prs',
177
+ '--author',
178
+ author,
179
+ '--owner',
180
+ org,
181
+ '--state=open',
182
+ '--limit',
183
+ String(limit),
184
+ '--json',
185
+ 'repository,number,title,url',
186
+ ])
187
+ const out: DigestPr[] = []
188
+ for (const pr of search) {
189
+ const details = await ghJson<GhPrDetails>([
190
+ 'pr',
191
+ 'view',
192
+ pr.url,
193
+ '--json',
194
+ 'url,title,body,comments,reviews',
195
+ ])
196
+ const activity = classifyActivity(details.comments, details.reviews, bots)
197
+ out.push({
198
+ number: pr.number,
199
+ repo: pr.repository.nameWithOwner,
200
+ title: pr.title,
201
+ url: pr.url,
202
+ ...activity,
203
+ })
204
+ }
205
+ return out
206
+ }
207
+
208
+ export default function (pi: ExtensionAPI) {
209
+ // Scaffold config.schema.json next to this file when missing.
210
+ pi.on('session_start', async (event) => {
211
+ if (event.reason !== 'startup') return
212
+ const schemaPath = path.join(
213
+ path.dirname(fileURLToPath(import.meta.url)),
214
+ 'config.schema.json',
215
+ )
216
+ try {
217
+ await fs.access(schemaPath)
218
+ } catch {
219
+ await fs.writeFile(
220
+ schemaPath,
221
+ JSON.stringify(PrDigestConfigSchema, null, 2),
222
+ 'utf-8',
223
+ )
224
+ }
225
+ })
226
+
227
+ pi.registerTool({
228
+ name: 'pr_digest',
229
+ label: 'GitHub PR Digest',
230
+ description:
231
+ "List a GitHub author's open PRs in an org (via the gh CLI) with human comment and review status. Bot activity (logins ending in [bot], sonarqubecloud, copilot-pull-request-reviewer, github-actions, dependabot, codecov, plus configured botLogins) is filtered out. Returns one entry per PR with humanCommenters, humanReviews (author + state), and hasHumanComments.",
232
+ promptSnippet:
233
+ "Digest of an author's open PRs in a GitHub org with human comment/review status (bots filtered).",
234
+ promptGuidelines: [
235
+ 'Use this to answer "my outstanding PRs", "PRs waiting for review", "who reviewed my PRs".',
236
+ 'PRs with hasHumanComments=true: report who commented and review states. PRs without: they go in a reviewer-request table — one human approval means "asking for a second reviewer", zero human reviews means "we need 2 reviewers".',
237
+ 'Write a 1-line plain-text description per silent PR from its title when building the table.',
238
+ ],
239
+ parameters: Type.Object({
240
+ org: Type.Optional(
241
+ Type.String({
242
+ description:
243
+ 'GitHub org/owner to search (default from config, else Salary-Hero)',
244
+ }),
245
+ ),
246
+ author: Type.Optional(
247
+ Type.String({
248
+ description: "PR author login or '@me' (default from config)",
249
+ }),
250
+ ),
251
+ limit: Type.Optional(
252
+ Type.Integer({
253
+ description: 'Max PRs to return (default 50)',
254
+ minimum: 1,
255
+ maximum: 100,
256
+ }),
257
+ ),
258
+ }),
259
+ async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
260
+ const cfg = await resolveConfig(ctx.cwd, params.org, params.author)
261
+ const limit = params.limit ?? 50
262
+ const prs = await collectDigest(cfg.org, cfg.author, limit, cfg.bots)
263
+ const withComments = prs.filter((p) => p.hasHumanComments)
264
+ const silent = prs.filter((p) => !p.hasHumanComments)
265
+ return {
266
+ content: [
267
+ {
268
+ type: 'text' as const,
269
+ text: JSON.stringify(
270
+ {
271
+ org: cfg.org,
272
+ author: cfg.author,
273
+ total: prs.length,
274
+ withComments,
275
+ silent,
276
+ },
277
+ null,
278
+ 2,
279
+ ),
280
+ },
281
+ ],
282
+ details: { org: cfg.org, author: cfg.author, prs },
283
+ }
284
+ },
285
+ })
286
+
287
+ pi.registerCommand('pr-digest', {
288
+ description:
289
+ 'Digest of your outstanding PRs in the configured GitHub org: who commented/reviewed, and a reviewer-request table for silent PRs. Usage: /pr-digest [org] [author]',
290
+ // AIDEV-NOTE: hands off to the agent via sendUserMessage so the run goes
291
+ // through the pr_digest tool call (same pattern as pi-teams-transcript
292
+ // commands) instead of duplicating the fetch logic here.
293
+ handler: async (args, ctx) => {
294
+ const [argOrg, argAuthor] = args.trim().split(/\s+/).filter(Boolean)
295
+ const cfg = await resolveConfig(ctx.cwd, argOrg, argAuthor)
296
+ pi.sendUserMessage(
297
+ `Call the pr_digest tool with org="${cfg.org}", author="${cfg.author}". Report PRs with human comments (who commented, review states), then a markdown table of the silent ones: PR link, 1-line description, human reviewers with state, and the ask ("asking for a second reviewer" if exactly one human approval, "we need 2 reviewers" if none).`,
298
+ )
299
+ },
300
+ })
301
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@gtheys/pi-pr-digest",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Pi extension — digest of your outstanding GitHub PRs in an org: human comment/review status and reviewer-request table data",
5
+ "type": "module",
6
+ "scripts": {
7
+ "test": "bun test"
8
+ },
9
+ "keywords": [
10
+ "pi-package"
11
+ ],
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/gtheys/pi-my-rifle-ext.git",
16
+ "directory": "packages/pi-pr-digest"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "files": [
22
+ "index.ts",
23
+ "config.schema.json",
24
+ "README.md"
25
+ ],
26
+ "pi": {
27
+ "extensions": [
28
+ "./index.ts"
29
+ ]
30
+ },
31
+ "peerDependencies": {
32
+ "@earendil-works/pi-coding-agent": "*",
33
+ "@earendil-works/pi-tui": "*",
34
+ "typebox": "*"
35
+ }
36
+ }