Repository navigation
Expand file tree
/
Copy pathgithub-pr-utils.js
More file actions
156 lines (142 loc) · 4.06 KB
/
Copy pathgithub-pr-utils.js
File metadata and controls
156 lines (142 loc) · 4.06 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* github-pr-utils.js — GitHub PR detection via GitHub CLI.
*
* Implements PR detection using `gh pr list` with error handling
* for missing gh CLI, auth failures, and rate limits.
*
* @module scripts/lib/github-pr-utils
*/
import { spawnSync } from 'child_process';
const OPEN_PR_LIMIT = 250;
/**
* Check whether the GitHub CLI can be invoked successfully.
*
* @returns {boolean} Whether gh --version succeeds.
*/
export function isGhAvailable() {
const result = spawnSync('gh', ['--version'], { encoding: 'utf8' });
return result.status === 0;
}
/**
* Get open pull request head branch names via the GitHub CLI.
* Missing CLI, failed requests, and lists reaching the 250-PR limit return
* null, not an empty Set; only a successful confirmed empty list is empty.
*
* @returns {Set<string>|null} Open PR branch names, or null when unverified.
*/
export function getOpenPRs() {
if (!isGhAvailable()) {
console.warn('⚠️ GitHub CLI (gh) not found. Open-PR verification is unavailable.');
return null;
}
const result = spawnSync(
'gh',
[
'pr',
'list',
'--state',
'open',
'--limit',
String(OPEN_PR_LIMIT),
'--json',
'headRefName',
'--jq',
'.[].headRefName',
],
{
encoding: 'utf8',
env: {
...process.env,
GH_TOKEN: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
},
}
);
if (result.status !== 0) {
const stderr = (result.stderr || '').trim();
if (stderr.includes('rate limit')) {
console.warn('⚠️ GitHub API rate limit reached. Open-PR verification is unavailable.');
} else if (stderr.includes('unauthorized') || stderr.includes('not authenticated')) {
console.warn(
'⚠️ GitHub auth failed (set GH_TOKEN or GITHUB_TOKEN). Open-PR verification is unavailable.'
);
} else {
console.warn(`⚠️ Could not fetch open PRs: ${stderr || 'unknown error'}`);
}
return null;
}
const output = (result.stdout || '').trim();
const prBranches = output
.split('\n')
.map((b) => b.trim())
.filter(Boolean);
if (prBranches.length >= OPEN_PR_LIMIT) {
console.warn(
`⚠️ Open PR list truncated at ${OPEN_PR_LIMIT}. Open-PR verification is unavailable.`
);
return null;
}
return new Set(prBranches);
}
/**
* Check a branch against confirmed open PR names, fetching them when omitted.
*
* @param {string} branch - Branch name to check.
* @param {Set<string>} openPRs - Previously verified names, if available.
* @returns {boolean} Whether the branch has an open PR.
* @throws {Error} When open-PR verification is unavailable.
*/
export function hasOpenPR(branch, openPRs) {
const cached = openPRs === undefined ? getOpenPRs() : openPRs;
if (!(cached instanceof Set)) {
throw new Error('Open-PR verification is unavailable; deletion is unsafe');
}
return cached.has(branch);
}
/**
* Fetch open PR head names, titles, and authors via the GitHub CLI.
* Returns null if the CLI is missing, the query fails, the JSON is invalid,
* or the result reaches the 250-PR limit; an empty successful list is [].
*
* @returns {*} Parsed JSON (normally an array), or null when unavailable.
*/
export function getOpenPRDetails() {
if (!isGhAvailable()) {
return null;
}
const result = spawnSync(
'gh',
[
'pr',
'list',
'--state',
'open',
'--limit',
String(OPEN_PR_LIMIT),
'--json',
'headRefName,title,author',
],
{
encoding: 'utf8',
env: {
...process.env,
GH_TOKEN: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
},
}
);
if (result.status !== 0) {
return null;
}
try {
const output = (result.stdout || '').trim();
const details = output ? JSON.parse(output) : [];
if (Array.isArray(details) && details.length >= OPEN_PR_LIMIT) {
console.warn(
`⚠️ Open PR details truncated at ${OPEN_PR_LIMIT}. Open-PR verification is unavailable.`
);
return null;
}
return details;
} catch {
return null;
}
}