-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathbot-pr-gfi-review-triage.js
More file actions
102 lines (76 loc) · 2.38 KB
/
bot-pr-gfi-review-triage.js
File metadata and controls
102 lines (76 loc) · 2.38 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
/**
* This script automatically requests triage reviews on PRs labeled as "Good First Issue" or "Beginner".
* It runs on when a pr added with label "Good First Issue" or "Beginner" and posts a comment mentioing triage reviewers.
*
* safty measures:
* - Only runs on PRs (not issues).
* - Only run for once per pr.
* - it does not run on new commit push but can run on pr label update
*/
const fs = require("fs");
const https = require("https");
function fail(message) {
console.error(`${message}`);
process.exit(1);
}
function info(message) {
console.log(`${message}`);
}
const eventPath = process.env.GITHUB_EVENT_PATH;
const repository = process.env.GITHUB_REPOSITORY;
const token = process.env.GITHUB_TOKEN;
if (!eventPath) fail("GITHUB_EVENT_PATH is not set.");
if (!repository) fail("GITHUB_REPOSITORY is not set.");
if (!token) fail("GITHUB_TOKEN is not set.");
let event;
try {
const payload = fs.readFileSync(eventPath, "utf8");
event = JSON.parse(payload);
} catch (err) {
fail(`Failed to read GitHub event payload: ${err.message}`);
}
const prNumber = event?.pull_request?.number;
if (!prNumber) {
info("No pull request found in event payload. Exiting.");
process.exit(0);
}
const [owner, repo] = repository.split("/");
if (!owner || !repo) {
fail("Invalid GITHUB_REPOSITORY format. Expected owner/repo.");
}
const commentBody =
"Requesting triage review from: @hiero-ledger/hiero-sdk-python-triage";
info(`Preparing to comment on PR #${prNumber} in ${owner}/${repo}`);
const data = JSON.stringify({ body: commentBody });
const options = {
hostname: "api.github.com",
path: `/repos/${owner}/${repo}/issues/${prNumber}/comments`,
method: "POST",
headers: {
"User-Agent": "triage-review-bot",
"Authorization": `Bearer ${token}`,
"Accept": "application/vnd.github+json",
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data),
},
};
const req = https.request(options, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
info("Comment posted successfully.");
} else {
console.error(`Error: GitHub API error (${res.statusCode})`);
console.error(body);
process.exit(1);
}
});
});
req.on("error", (err) => {
fail(`Request failed: ${err.message}`);
});
req.write(data);
req.end();