|
| 1 | +// @ts-check |
| 2 | +/// <reference types="@actions/github-script" /> |
| 3 | + |
| 4 | +const { getErrorMessage, isRateLimitError } = require("./error_helpers.cjs"); |
| 5 | +const { resolveExecutionOwnerRepo } = require("./repo_helpers.cjs"); |
| 6 | +const { sanitizeContent } = require("./sanitize_content.cjs"); |
| 7 | + |
| 8 | +const ISSUE_TITLE = "[aw] agentic status report"; |
| 9 | +const REPORT_COUNT = 1000; |
| 10 | +const HEADING_DEMOTION_LEVELS = 2; |
| 11 | +const DEFAULT_REPORT_OUTPUT_DIR = "./.cache/gh-aw/activity-report-logs"; |
| 12 | + |
| 13 | +/** @typedef {{ key: string, heading: string, startDate: string, optionalOnRateLimit: boolean }} ActivityRange */ |
| 14 | + |
| 15 | +/** @type {ActivityRange[]} */ |
| 16 | +const REPORT_RANGES = [ |
| 17 | + { key: "24h", heading: "Last 24 hours", startDate: "-1d", optionalOnRateLimit: false }, |
| 18 | + { key: "7d", heading: "Last 7 days", startDate: "-1w", optionalOnRateLimit: false }, |
| 19 | +]; |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {string} text |
| 23 | + * @returns {boolean} |
| 24 | + */ |
| 25 | +function hasRateLimitText(text) { |
| 26 | + return /\bapi rate limit\b|\brate limit exceeded\b|\bsecondary rate limit\b|\b429\b/i.test(text); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Run the logs command for a configured report range. |
| 31 | + * |
| 32 | + * @param {string} bin |
| 33 | + * @param {string[]} prefixArgs |
| 34 | + * @param {string} repoSlug |
| 35 | + * @param {ActivityRange} range |
| 36 | + * @param {string} outputDir |
| 37 | + * @returns {Promise<{ heading: string, body: string }>} |
| 38 | + */ |
| 39 | +async function runRangeReport(bin, prefixArgs, repoSlug, range, outputDir) { |
| 40 | + const args = [...prefixArgs, "logs", "--repo", repoSlug, "--start-date", range.startDate, "--count", String(REPORT_COUNT), "--output", outputDir, "--format", "markdown"]; |
| 41 | + core.info(`Running: ${bin} ${args.join(" ")}`); |
| 42 | + |
| 43 | + try { |
| 44 | + const result = await exec.getExecOutput(bin, args, { ignoreReturnCode: true }); |
| 45 | + const output = `${result.stdout || ""}\n${result.stderr || ""}`.trim(); |
| 46 | + const rateLimited = hasRateLimitText(output); |
| 47 | + |
| 48 | + if (result.exitCode === 0 && result.stdout.trim()) { |
| 49 | + return { |
| 50 | + heading: range.heading, |
| 51 | + body: normalizeReportMarkdown(sanitizeContent(result.stdout.trim())), |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + if (rateLimited && range.optionalOnRateLimit) { |
| 56 | + core.warning(`Skipping ${range.heading} report due to GitHub API rate limiting`); |
| 57 | + return { |
| 58 | + heading: range.heading, |
| 59 | + body: "_Skipped due to GitHub API rate limiting._", |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + if (rateLimited) { |
| 64 | + return { |
| 65 | + heading: range.heading, |
| 66 | + body: "_Could not generate this section due to GitHub API rate limiting._", |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + heading: range.heading, |
| 72 | + body: `_Report command failed (exit code ${result.exitCode})._\n\n\`\`\`\n${sanitizeContent(output || "No command output was captured.")}\n\`\`\``, |
| 73 | + }; |
| 74 | + } catch (error) { |
| 75 | + const errorMessage = getErrorMessage(error); |
| 76 | + const rateLimited = isRateLimitError(error) || hasRateLimitText(errorMessage); |
| 77 | + |
| 78 | + if (rateLimited && range.optionalOnRateLimit) { |
| 79 | + core.warning(`Skipping ${range.heading} report due to GitHub API rate limiting`); |
| 80 | + return { |
| 81 | + heading: range.heading, |
| 82 | + body: "_Skipped due to GitHub API rate limiting._", |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + if (rateLimited) { |
| 87 | + return { |
| 88 | + heading: range.heading, |
| 89 | + body: "_Could not generate this section due to GitHub API rate limiting._", |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + heading: range.heading, |
| 95 | + body: `_Report command failed: ${sanitizeContent(errorMessage)}_`, |
| 96 | + }; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Normalize report markdown for issue rendering. |
| 102 | + * Demotes headings so top-level report headings start at H3. |
| 103 | + * |
| 104 | + * @param {string} markdown |
| 105 | + * @returns {string} |
| 106 | + */ |
| 107 | +function normalizeReportMarkdown(markdown) { |
| 108 | + return markdown.replace(/^(#{1,6})\s+/gm, (_, hashes) => { |
| 109 | + const headingLevel = hashes.length; |
| 110 | + const demotedHeadingLevel = Math.min(6, headingLevel + HEADING_DEMOTION_LEVELS); |
| 111 | + return `${"#".repeat(demotedHeadingLevel)} `; |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Generate an agentic workflow activity report issue. |
| 117 | + * @returns {Promise<void>} |
| 118 | + */ |
| 119 | +async function main() { |
| 120 | + const cmdPrefixStr = process.env.GH_AW_CMD_PREFIX || "gh aw"; |
| 121 | + const reportOutputDir = process.env.GH_AW_ACTIVITY_REPORT_OUTPUT_DIR || DEFAULT_REPORT_OUTPUT_DIR; |
| 122 | + const [bin, ...prefixArgs] = cmdPrefixStr.split(" ").filter(Boolean); |
| 123 | + const { owner, repo } = resolveExecutionOwnerRepo(); |
| 124 | + const repoSlug = `${owner}/${repo}`; |
| 125 | + |
| 126 | + core.info(`Generating agentic workflow activity report for ${repoSlug}`); |
| 127 | + |
| 128 | + const sections = []; |
| 129 | + for (const range of REPORT_RANGES) { |
| 130 | + sections.push(await runRangeReport(bin, prefixArgs, repoSlug, range, reportOutputDir)); |
| 131 | + } |
| 132 | + |
| 133 | + const headerLines = ["### Agentic workflow activity report", "", `Repository: \`${repoSlug}\``, `Generated at: ${new Date().toISOString()}`, ""]; |
| 134 | + const sectionLines = sections.flatMap(section => ["<details>", `<summary>${section.heading}</summary>`, "", section.body, "", "</details>", ""]); |
| 135 | + const body = [...headerLines, ...sectionLines].join("\n"); |
| 136 | + |
| 137 | + const createdIssue = await github.rest.issues.create({ |
| 138 | + owner, |
| 139 | + repo, |
| 140 | + title: ISSUE_TITLE, |
| 141 | + body, |
| 142 | + labels: ["agentic-workflows"], |
| 143 | + }); |
| 144 | + |
| 145 | + core.info(`Created issue #${createdIssue.data.number}: ${createdIssue.data.html_url}`); |
| 146 | +} |
| 147 | + |
| 148 | +module.exports = { main, hasRateLimitText, runRangeReport, normalizeReportMarkdown }; |
0 commit comments