|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * @license |
| 4 | + * Copyright 2025 Google LLC |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +/* eslint-env node */ |
| 9 | +/* global console, process */ |
| 10 | + |
| 11 | +import { exec } from 'node:child_process'; |
| 12 | +import { promisify } from 'node:util'; |
| 13 | + |
| 14 | +const execAsync = promisify(exec); |
| 15 | + |
| 16 | +async function run(cmd) { |
| 17 | + try { |
| 18 | + const { stdout } = await execAsync(cmd, { |
| 19 | + encoding: 'utf8', |
| 20 | + stdio: ['pipe', 'pipe', 'ignore'], |
| 21 | + }); |
| 22 | + return stdout.trim(); |
| 23 | + } catch (_e) { // eslint-disable-line @typescript-eslint/no-unused-vars |
| 24 | + return null; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const IGNORE_MESSAGES = [ |
| 29 | + 'thank you so much for your contribution to Gemini CLI!', |
| 30 | + "I'm currently reviewing this pull request and will post my feedback shortly.", |
| 31 | + 'This pull request is being closed because it is not currently linked to an issue.', |
| 32 | +]; |
| 33 | + |
| 34 | +const shouldIgnore = (body) => { |
| 35 | + if (!body) return false; |
| 36 | + return IGNORE_MESSAGES.some((msg) => body.includes(msg)); |
| 37 | +}; |
| 38 | + |
| 39 | +async function main() { |
| 40 | + const branch = await run('git branch --show-current'); |
| 41 | + if (!branch) { |
| 42 | + console.error('❌ Could not determine current git branch.'); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + const gqlQuery = `query($branch:String!){repository(name:"gemini-cli",owner:"google-gemini"){pullRequests(headRefName:$branch,first:100){nodes{id,number,state,comments(first:100){nodes{createdAt,isMinimized,minimizedReason,author{login},body,url,authorAssociation}},reviews(first:100){nodes{id,author{login},createdAt,isMinimized,minimizedReason,body,state,comments(first:30){nodes{id,replyTo{id},author{login},createdAt,body,isMinimized,minimizedReason,path,line,startLine,originalLine,originalStartLine}}}}}}}}`; |
| 47 | + |
| 48 | + const [authInfo, diff, commits, rawJson] = await Promise.all([ |
| 49 | + run('gh auth status -a'), |
| 50 | + run('gh pr diff'), |
| 51 | + run( |
| 52 | + 'git fetch && git log origin/main..origin/$(git branch --show-current)', |
| 53 | + ), |
| 54 | + run(`gh api graphql -F branch="${branch}" -f query='${gqlQuery}'`), |
| 55 | + ]); |
| 56 | + |
| 57 | + if (!diff) { |
| 58 | + console.error(`⚠️ No active PR found for branch: ${branch}`); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + |
| 62 | + console.log(`\n# Current GitHub user info:\n\n${authInfo}\n`); |
| 63 | + console.log(`\n# PR diff for current branch: ${branch}\n\n\`\`\``); |
| 64 | + console.log(diff); |
| 65 | + console.log('```'); |
| 66 | + console.log( |
| 67 | + `\n# Commit history (origin/main..origin/${branch})\n\n${commits}`, |
| 68 | + ); |
| 69 | + |
| 70 | + const data = JSON.parse(rawJson || '{}'); |
| 71 | + const prs = data?.data?.repository?.pullRequests?.nodes || []; |
| 72 | + |
| 73 | + // Sort PRs by number descending so we check the newest one first |
| 74 | + prs.sort((a, b) => b.number - a.number); |
| 75 | + |
| 76 | + const pr = prs.find((p) => p.state === 'OPEN') || prs[0]; |
| 77 | + |
| 78 | + if (!pr) { |
| 79 | + console.error('❌ No PR data found.'); |
| 80 | + process.exit(1); |
| 81 | + } |
| 82 | + |
| 83 | + console.log('\n# PR Feedback\n'); |
| 84 | + |
| 85 | + // 1. General PR Comments |
| 86 | + const general = pr.comments.nodes.filter((c) => !shouldIgnore(c.body)); |
| 87 | + if (general.length > 0) { |
| 88 | + console.log('\n💬 GENERAL COMMENTS:'); |
| 89 | + general.forEach((c) => { |
| 90 | + const minimized = c.isMinimized |
| 91 | + ? ` (Minimized: ${c.minimizedReason})` |
| 92 | + : ''; |
| 93 | + console.log( |
| 94 | + `[${c.createdAt}] [${c.author.login}]${minimized}: ${c.body}\n`, |
| 95 | + ); |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + // 2. Process ALL Review Comments into a single Thread Map |
| 100 | + const allInlineComments = pr.reviews.nodes.flatMap((r) => r.comments.nodes); |
| 101 | + const filteredInlines = allInlineComments.filter( |
| 102 | + (c) => !shouldIgnore(c.body), |
| 103 | + ); |
| 104 | + |
| 105 | + console.log('🔍 CODE REVIEWS & INLINE THREADS:'); |
| 106 | + |
| 107 | + // Print Review Summaries First |
| 108 | + pr.reviews.nodes.forEach((review) => { |
| 109 | + if (review.body && !shouldIgnore(review.body)) { |
| 110 | + const icon = review.state === 'APPROVED' ? '✅' : '💬'; |
| 111 | + const minimized = review.isMinimized |
| 112 | + ? ` (Minimized: ${review.minimizedReason})` |
| 113 | + : ''; |
| 114 | + console.log( |
| 115 | + `\n${icon} ${review.state} by ${review.author.login} at ${review.createdAt}${minimized}: "${review.body}"`, |
| 116 | + ); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + // Build and Print Threads |
| 121 | + const topLevelThreads = filteredInlines.filter((c) => !c.replyTo); |
| 122 | + |
| 123 | + const printThread = (parentId, depth = 1) => { |
| 124 | + const indent = ' '.repeat(depth); |
| 125 | + filteredInlines |
| 126 | + .filter((c) => c.replyTo?.id === parentId) |
| 127 | + .forEach((reply) => { |
| 128 | + const minimized = reply.isMinimized |
| 129 | + ? ` (Minimized: ${reply.minimizedReason})` |
| 130 | + : ''; |
| 131 | + console.log( |
| 132 | + `${indent}↳ [${reply.createdAt}] ${reply.author.login}${minimized}: ${reply.body}`, |
| 133 | + ); |
| 134 | + printThread(reply.id, depth + 1); |
| 135 | + }); |
| 136 | + }; |
| 137 | + |
| 138 | + topLevelThreads.forEach((c) => { |
| 139 | + const start = c.startLine || c.originalStartLine; |
| 140 | + const end = c.line || c.originalLine; |
| 141 | + const range = start && end && start !== end ? `${start}-${end}` : end || ''; |
| 142 | + const fileInfo = c.path |
| 143 | + ? `(${c.path}${range ? `:${range}` : ''}) ` |
| 144 | + : range |
| 145 | + ? `(Line ${range}) ` |
| 146 | + : ''; |
| 147 | + const minimized = c.isMinimized ? ` (Minimized: ${c.minimizedReason})` : ''; |
| 148 | + console.log( |
| 149 | + `\n💬 ${minimized}${c.author.login} | ${c.createdAt} ${fileInfo}\n${c.body}`, |
| 150 | + ); |
| 151 | + printThread(c.id); |
| 152 | + }); |
| 153 | + |
| 154 | + console.log('\n'); |
| 155 | +} |
| 156 | + |
| 157 | +main().catch((err) => { |
| 158 | + console.error('❌ Unexpected error:', err); |
| 159 | + process.exit(1); |
| 160 | +}); |
0 commit comments