From 002017fde17deded64720bc6cbc47d0ad702ccbf Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Mon, 23 Mar 2026 23:50:40 +0530 Subject: [PATCH 1/3] make a separate bot comment --- SyncPRSummary/action.yml | 59 ++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/SyncPRSummary/action.yml b/SyncPRSummary/action.yml index c99abfc..396336f 100644 --- a/SyncPRSummary/action.yml +++ b/SyncPRSummary/action.yml @@ -1,13 +1,13 @@ name: Sync PR Summary -description: Update the managed PR summary block from workflow outputs and bot comments. +description: Consolidate workflow outputs and bot comments into a single managed bot comment on the PR. inputs: docs-content: - description: Documentation preview content to inject into the PR summary. + description: Documentation preview content to inject into the managed bot comment. required: false default: '' perf-content: - description: Performance summary content to inject into the PR summary. + description: Performance summary content to inject into the managed bot comment. required: false default: '' @@ -38,16 +38,11 @@ runs: }, ]; - function extractManagedBlock(body) { + function extractManagedSections(body) { const start = body.indexOf(START_MARKER); const end = body.indexOf(END_MARKER); - if (start === -1 || end === -1 || end < start) return ""; - return body.slice(start + START_MARKER.length, end).trim(); - } - - function extractManagedSections(body) { - const managedBlock = extractManagedBlock(body); - if (!managedBlock) return new Map(); + if (start === -1 || end === -1 || end < start) return new Map(); + const managedBlock = body.slice(start + START_MARKER.length, end).trim(); const sections = new Map(); const sectionRegex = /^### (.+)$/gm; @@ -62,27 +57,10 @@ runs: return sections; } - function replaceManagedBlock(body, managedBlock) { - const start = body.indexOf(START_MARKER); - const end = body.indexOf(END_MARKER); - - if (start === -1 && end === -1) { - if (!body) return managedBlock; - if (body.endsWith("\n\n")) return `${body}${managedBlock}`; - if (body.endsWith("\n")) return `${body}\n${managedBlock}`; - return `${body}\n\n${managedBlock}`; - } - if (start !== -1 && (end === -1 || end < start)) return `${body.slice(0, start)}${managedBlock}`; - if (start === -1 && end !== -1) return `${managedBlock}${body.slice(end + END_MARKER.length)}`; - return `${body.slice(0, start)}${managedBlock}${body.slice(end + END_MARKER.length)}`; - } - const prNumber = context.payload.pull_request.number; const owner = context.repo.owner; const repo = context.repo.repo; - const pr = await github.rest.pulls.get({ owner, repo, pull_number: prNumber }); - const comments = await github.paginate(github.rest.issues.listComments, { owner, repo, @@ -90,8 +68,7 @@ runs: per_page: 100, }); - const currentBody = pr.data.body || ""; - const existingSections = extractManagedSections(currentBody); + let managedComment = null; const latestByMarker = new Map(); for (const comment of comments) { @@ -99,6 +76,7 @@ runs: const user = comment.user?.login; const app = comment.performed_via_github_app?.slug; if (user !== "github-actions[bot]" && app !== "github-actions") continue; + if (!managedComment && body.includes(START_MARKER)) managedComment = comment; for (const { marker, title } of sectionsConfig) { if (!marker) continue; if (!body.startsWith(marker)) continue; @@ -107,23 +85,28 @@ runs: } } + const existingSections = managedComment + ? extractManagedSections(managedComment.body || "") + : new Map(); + const sections = sectionsConfig.map(({ marker, title, fallback, input }) => { if (input) return `### ${title}\n\n${input.trim()}`; return latestByMarker.get(marker) || existingSections.get(title) || `### ${title}\n\n${fallback}`; }); - const managedBlock = [ + const newBody = [ START_MARKER, "## Automated PR Summary", - "", ...sections, END_MARKER, ].join("\n\n"); - const updatedBody = replaceManagedBlock(currentBody, managedBlock); - if (updatedBody === currentBody) { - core.info("PR summary already up to date."); - return; + if (managedComment) { + if (managedComment.body === newBody) { + core.info("PR summary already up to date."); + return; + } + await github.rest.issues.updateComment({ owner, repo, comment_id: managedComment.id, body: newBody }); + } else { + await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: newBody }); } - - await github.rest.pulls.update({ owner, repo, pull_number: prNumber, body: updatedBody }); From 69f63ecccdee5d17a6ae28b616287fa7cbf221bb Mon Sep 17 00:00:00 2001 From: Shravan Goswami Date: Tue, 24 Mar 2026 01:01:23 +0530 Subject: [PATCH 2/3] clean code --- SyncPRSummary/action.yml | 106 +++++++++++++++------------------------ 1 file changed, 40 insertions(+), 66 deletions(-) diff --git a/SyncPRSummary/action.yml b/SyncPRSummary/action.yml index cf6ea62..a0f6d67 100644 --- a/SyncPRSummary/action.yml +++ b/SyncPRSummary/action.yml @@ -22,91 +22,65 @@ runs: github-token: ${{ github.token }} script: | const START_MARKER = ""; - const END_MARKER = ""; + const END_MARKER = ""; - const sectionsConfig = [ + const SECTIONS = [ { - title: "Documentation Preview", - fallback: "_Pending. No docs preview comment found yet._", - input: process.env.INPUT_DOCS_CONTENT, + key: "docs", + title: "Documentation Preview", + input: (process.env.INPUT_DOCS_CONTENT ?? "").trim(), + fallback: "_Pending. No docs preview available yet._", }, { - marker: "", - title: "Performance", - fallback: "_Pending. No performance comment found yet._", - input: process.env.INPUT_PERF_CONTENT, + key: "perf", + title: "Performance", + input: (process.env.INPUT_PERF_CONTENT ?? "").trim(), + fallback: "_Pending. No performance results available yet._", }, ]; - function extractManagedSections(body) { - const start = body.indexOf(START_MARKER); - const end = body.indexOf(END_MARKER); - if (start === -1 || end === -1 || end < start) return new Map(); - const managedBlock = body.slice(start + START_MARKER.length, end).trim(); + function readSection(body, key) { + const open = ``; + const close = ``; + const s = body.indexOf(open); + const e = body.indexOf(close); + if (s === -1 || e === -1 || e <= s) return null; + return body.slice(s + open.length, e).trim(); + } - const sections = new Map(); - const sectionRegex = /^### (.+)$/gm; - const matches = [...managedBlock.matchAll(sectionRegex)]; - for (let i = 0; i < matches.length; i += 1) { - const title = matches[i][1]; - const contentStart = matches[i].index + matches[i][0].length; - const contentEnd = i + 1 < matches.length ? matches[i + 1].index : managedBlock.length; - const content = managedBlock.slice(contentStart, contentEnd).trim(); - if (content) sections.set(title, `### ${title}\n\n${content}`); - } - return sections; + function buildBody(resolvedSections) { + const inner = resolvedSections + .map(({ key, title, content }) => + `\n### ${title}\n\n${content}\n`) + .join("\n\n"); + return `${START_MARKER}\n## CI Summary — GitHub Actions\n\n${inner}\n${END_MARKER}`; } + const { owner, repo } = context.repo; const prNumber = context.payload.pull_request.number; - const owner = context.repo.owner; - const repo = context.repo.repo; - const comments = await github.paginate(github.rest.issues.listComments, { - owner, - repo, - issue_number: prNumber, - per_page: 100, + const allComments = await github.paginate(github.rest.issues.listComments, { + owner, repo, issue_number: prNumber, per_page: 100, }); - let managedComment = null; - const latestByMarker = new Map(); - - for (const comment of comments) { - const body = comment.body || ""; - const user = comment.user?.login; - const app = comment.performed_via_github_app?.slug; - if (user !== "github-actions[bot]" && app !== "github-actions") continue; - if (!managedComment && body.includes(START_MARKER)) managedComment = comment; - for (const { marker, title } of sectionsConfig) { - if (!marker) continue; - if (!body.startsWith(marker)) continue; - const content = body.slice(marker.length).trim(); - if (content) latestByMarker.set(marker, `### ${title}\n\n${content}`); - } - } + const managedComment = allComments.find((c) => { + const user = c.user?.login; + const app = c.performed_via_github_app?.slug; + const isBot = user === "github-actions[bot]" || app === "github-actions"; + return isBot && (c.body || "").includes(START_MARKER); + }); - const existingSections = managedComment - ? extractManagedSections(managedComment.body || "") - : new Map(); + const managedBody = managedComment?.body ?? ""; - const sections = sectionsConfig.map(({ marker, title, fallback, input }) => { - if (input) return `### ${title}\n\n${input.trim()}`; - return latestByMarker.get(marker) || existingSections.get(title) || `### ${title}\n\n${fallback}`; - }); + const resolvedSections = SECTIONS.map(({ key, title, input, fallback }) => ({ + key, + title, + content: input || readSection(managedBody, key) || fallback, + })); - const newBody = [ - START_MARKER, - "## CI Summary — GitHub Actions", - "", - ...sections, - END_MARKER, - ].join("\n\n"); + const newBody = buildBody(resolvedSections); if (managedComment) { - if (managedComment.body === newBody) { - core.info("PR summary already up to date."); - return; - } await github.rest.issues.updateComment({ owner, repo, comment_id: managedComment.id, body: newBody }); } else { await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: newBody }); From e3786a2ca405fa2c157c8c2028e2dc4915d6bc1e Mon Sep 17 00:00:00 2001 From: Shravan Goswami <123811742+shravanngoswamii@users.noreply.github.com> Date: Thu, 26 Mar 2026 21:40:33 +0530 Subject: [PATCH 3/3] Fix SyncPRSummary action bot comments scanning and redundant API calls (#28) Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- SyncPRSummary/action.yml | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/SyncPRSummary/action.yml b/SyncPRSummary/action.yml index a0f6d67..bed68b1 100644 --- a/SyncPRSummary/action.yml +++ b/SyncPRSummary/action.yml @@ -33,6 +33,7 @@ runs: }, { key: "perf", + marker: "", title: "Performance", input: (process.env.INPUT_PERF_CONTENT ?? "").trim(), fallback: "_Pending. No performance results available yet._", @@ -72,16 +73,36 @@ runs: const managedBody = managedComment?.body ?? ""; - const resolvedSections = SECTIONS.map(({ key, title, input, fallback }) => ({ + const latestByMarker = new Map(); + for (const comment of allComments) { + const body = comment.body || ""; + const user = comment.user?.login; + const app = comment.performed_via_github_app?.slug; + const isBot = user === "github-actions[bot]" || app === "github-actions"; + if (!isBot) continue; + + for (const { marker, title } of SECTIONS) { + if (!marker) continue; + if (!body.startsWith(marker)) continue; + const content = body.slice(marker.length).trim(); + if (content) latestByMarker.set(marker, content); + } + } + + const resolvedSections = SECTIONS.map(({ key, marker, title, input, fallback }) => ({ key, title, - content: input || readSection(managedBody, key) || fallback, + content: input || latestByMarker.get(marker) || readSection(managedBody, key) || fallback, })); const newBody = buildBody(resolvedSections); if (managedComment) { - await github.rest.issues.updateComment({ owner, repo, comment_id: managedComment.id, body: newBody }); + if (managedComment.body !== newBody) { + await github.rest.issues.updateComment({ owner, repo, comment_id: managedComment.id, body: newBody }); + } else { + core.info("PR summary already up to date."); + } } else { await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: newBody }); }