forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease-pr-coderabbit-gate.js
More file actions
133 lines (113 loc) · 3.74 KB
/
release-pr-coderabbit-gate.js
File metadata and controls
133 lines (113 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Posts a single "@coderabbit review" comment on release PRs, embedding the
* release review prompt. Designed to run with:
* - permissions: contents: read, pull-requests: write
*
* Safety:
* - Only runs for maintainer-authored PRs (MEMBER/OWNER)
* - Dedupe via hidden marker comment
*/
const fs = require("fs");
const path = require("path");
const MARKER = "<!-- coderabbit-release-gate: v1 -->";
function loadPrompt() {
const promptPath = path.join(
process.env.GITHUB_WORKSPACE || ".",
".github/coderabbit/release-pr-prompt.md"
);
try {
const content = fs.readFileSync(promptPath, "utf8").trim();
if (!content) {
throw new Error("Release prompt file is empty");
}
return content;
} catch (error) {
throw new Error(`Failed to load release prompt from ${promptPath}: ${error.message}`);
}
}
async function commentAlreadyExists({ github, owner, repo, issue_number }) {
try {
// Pull a bounded number of recent comments to avoid pagination complexity.
const { data } = await github.rest.issues.listComments({
owner,
repo,
issue_number,
per_page: 100,
});
return data.some((c) => typeof c.body === "string" && c.body.includes(MARKER));
}
catch (error) {
console.error(`Error checking for existing comments: ${error.message}`);
return false; // Fail open: allow posting if check fails
}
}
function buildBody({ prompt, baseRef, headRef, baseLooksLikeTag }) {
// Keep it human-friendly but compact; instructions are collapsible.
const lines = [
"@coderabbitai review",
"",
MARKER,
"",
`This is a **release-gate** review request for diff **${baseRef} → ${headRef}**.`,
"",
];
if (!baseLooksLikeTag) {
lines.push(
"⚠️ Warning: The base ref does not look like a release tag. For a full release diff, set base to the previous tag (e.g., release-v0.1.10).",
""
);
}
lines.push(
"<details>",
"<summary>CodeRabbit release review instructions</summary>",
"",
prompt,
"",
"</details>",
);
return lines.join("\n");
}
module.exports = async ({ github, context }) => {
try {
const owner = context.repo.owner;
const repo = context.repo.repo;
const pr = context.payload.pull_request;
if (!pr) {
console.log("No pull_request payload; exiting.");
return;
}
// Safety: only maintainers
const assoc = pr.author_association;
if (!(assoc === "MEMBER" || assoc === "OWNER")) {
console.log(`author_association=${assoc}; skipping.`);
return;
}
const title = pr.title || "";
if (!title.toLowerCase().startsWith("chore: release v")) {
console.log("Not a release PR title; skipping.");
return;
}
const baseRef = pr.base?.ref || "";
const headRef = pr.head?.ref || "";
// Optional sanity check: base should look like a tag. If it doesn't, still comment but warn.
const baseLooksLikeTag = baseRef.startsWith("release-v") && /\d+\.\d+\.\d+/.test(baseRef);
const issue_number = pr.number;
if (await commentAlreadyExists({ github, owner, repo, issue_number })) {
console.log("Marker comment already exists; not posting again.");
return;
}
const prompt = loadPrompt();
const body = buildBody({ prompt, baseRef, headRef, baseLooksLikeTag });
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
console.log("Posted CodeRabbit release-gate comment.");
console.log(`PR #${issue_number} (${headRef} → ${baseRef})`);
} catch (error) {
console.error(`Error in release PR coderabbit gate: ${error.message}`);
console.log(`PR #${issue_number || 'unknown'} (${headRef || '?'} → ${baseRef || '?'})`);
}
};