@kaitranntt/ccs 8.1.0-dev.7 → 8.1.0-dev.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
|
|
4
|
+
const ISSUE_REF_PATTERN = /#([0-9]+)/g;
|
|
5
|
+
const ACTION_VERB_PATTERN = /\b(fixes|closes|resolves|refs?)\b(.*)/gi;
|
|
6
|
+
const RESOLVE_VERB_PATTERN = /\b(fixes|closes|resolves)\b(.*)/gi;
|
|
7
|
+
const PR_REF_PATTERN = /(?:Merge pull request #|\(#)([0-9]+)/g;
|
|
8
|
+
const STABLE_TAG_PATTERN = /^v[0-9]+\.[0-9]+\.[0-9]+$/;
|
|
9
|
+
|
|
10
|
+
export function extractIssueNumbers(text, { includeRefs = true } = {}) {
|
|
11
|
+
const pattern = includeRefs ? ACTION_VERB_PATTERN : RESOLVE_VERB_PATTERN;
|
|
12
|
+
const issues = new Set();
|
|
13
|
+
let actionMatch;
|
|
14
|
+
|
|
15
|
+
pattern.lastIndex = 0;
|
|
16
|
+
while ((actionMatch = pattern.exec(text || '')) !== null) {
|
|
17
|
+
const tail = actionMatch[2] || '';
|
|
18
|
+
let issueMatch;
|
|
19
|
+
ISSUE_REF_PATTERN.lastIndex = 0;
|
|
20
|
+
while ((issueMatch = ISSUE_REF_PATTERN.exec(tail)) !== null) {
|
|
21
|
+
issues.add(Number(issueMatch[1]));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return [...issues].sort((a, b) => a - b);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function extractPrNumbers(text) {
|
|
29
|
+
const prs = new Set();
|
|
30
|
+
let match;
|
|
31
|
+
|
|
32
|
+
PR_REF_PATTERN.lastIndex = 0;
|
|
33
|
+
while ((match = PR_REF_PATTERN.exec(text || '')) !== null) {
|
|
34
|
+
prs.add(Number(match[1]));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return [...prs].sort((a, b) => a - b);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function planIssueCleanup({ releaseIssues, resolvedIssues, issueStates }) {
|
|
41
|
+
const resolved = new Set(resolvedIssues);
|
|
42
|
+
return releaseIssues.map((number) => {
|
|
43
|
+
const state = issueStates.get(number) || { labels: [], state: 'UNKNOWN' };
|
|
44
|
+
const labels = new Set(state.labels);
|
|
45
|
+
const wasReleasedDev = labels.has('released-dev');
|
|
46
|
+
const shouldClose = state.state === 'OPEN' && (wasReleasedDev || resolved.has(number));
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
number,
|
|
50
|
+
removeLabels: ['released-dev', 'pending-release'],
|
|
51
|
+
addReleasedLabel: shouldClose,
|
|
52
|
+
close: shouldClose,
|
|
53
|
+
reason: wasReleasedDev ? 'promoted from dev to stable' : 'resolved by stable release',
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getStableReleaseContext({ env = process.env, exec = runCommand } = {}) {
|
|
59
|
+
const repo = env.GITHUB_REPOSITORY;
|
|
60
|
+
if (!repo) throw new Error('GITHUB_REPOSITORY is required');
|
|
61
|
+
|
|
62
|
+
const version = JSON.parse(readFileSync('package.json', 'utf8')).version;
|
|
63
|
+
const currentTag = `v${version}`;
|
|
64
|
+
const releaseBody = exec('gh', [
|
|
65
|
+
'release',
|
|
66
|
+
'view',
|
|
67
|
+
currentTag,
|
|
68
|
+
'--repo',
|
|
69
|
+
repo,
|
|
70
|
+
'--json',
|
|
71
|
+
'body',
|
|
72
|
+
'--jq',
|
|
73
|
+
'.body',
|
|
74
|
+
]);
|
|
75
|
+
const tags = exec('git', ['tag', '-l', 'v[0-9]*.[0-9]*.[0-9]*', '--sort=-v:refname'])
|
|
76
|
+
.split('\n')
|
|
77
|
+
.map((tag) => tag.trim())
|
|
78
|
+
.filter((tag) => STABLE_TAG_PATTERN.test(tag) && tag !== currentTag);
|
|
79
|
+
const previousStableTag = tags[0] || '';
|
|
80
|
+
const range = previousStableTag ? `${previousStableTag}..HEAD~1` : 'HEAD~50..HEAD~1';
|
|
81
|
+
const commitText = exec('git', ['log', range, '--pretty=format:%s%n%b'], { optional: true });
|
|
82
|
+
|
|
83
|
+
return { repo, version, currentTag, releaseBody, range, commitText };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function buildReleaseIssueSet({ releaseBody, commitText, prText }) {
|
|
87
|
+
const releaseIssues = new Set([
|
|
88
|
+
...extractIssueNumbers(releaseBody, { includeRefs: true }),
|
|
89
|
+
...extractIssueNumbers(commitText, { includeRefs: true }),
|
|
90
|
+
...extractIssueNumbers(prText, { includeRefs: true }),
|
|
91
|
+
]);
|
|
92
|
+
const resolvedIssues = new Set([
|
|
93
|
+
...extractIssueNumbers(releaseBody, { includeRefs: false }),
|
|
94
|
+
...extractIssueNumbers(commitText, { includeRefs: false }),
|
|
95
|
+
...extractIssueNumbers(prText, { includeRefs: false }),
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
releaseIssues: [...releaseIssues].sort((a, b) => a - b),
|
|
100
|
+
resolvedIssues: [...resolvedIssues].sort((a, b) => a - b),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function runCommand(command, args, { optional = false } = {}) {
|
|
105
|
+
const result = spawnSync(command, args, { encoding: 'utf8' });
|
|
106
|
+
if (result.status !== 0) {
|
|
107
|
+
if (optional) return '';
|
|
108
|
+
throw new Error(
|
|
109
|
+
`${command} ${args.join(' ')} failed: ${(result.stderr || result.stdout || '').trim()}`
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return result.stdout.trim();
|
|
113
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildReleaseIssueSet,
|
|
3
|
+
extractPrNumbers,
|
|
4
|
+
getStableReleaseContext,
|
|
5
|
+
planIssueCleanup,
|
|
6
|
+
runCommand,
|
|
7
|
+
} from './stable-release-issue-cleanup-lib.mjs';
|
|
8
|
+
|
|
9
|
+
function gh(args, options) {
|
|
10
|
+
return runCommand('gh', args, options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function fetchPrText(repo, commitText) {
|
|
14
|
+
let prText = '';
|
|
15
|
+
for (const prNumber of extractPrNumbers(commitText)) {
|
|
16
|
+
const details = gh(
|
|
17
|
+
[
|
|
18
|
+
'pr',
|
|
19
|
+
'view',
|
|
20
|
+
String(prNumber),
|
|
21
|
+
'--repo',
|
|
22
|
+
repo,
|
|
23
|
+
'--json',
|
|
24
|
+
'title,body',
|
|
25
|
+
'--jq',
|
|
26
|
+
'.title + "\n" + (.body // "")',
|
|
27
|
+
],
|
|
28
|
+
{ optional: true }
|
|
29
|
+
);
|
|
30
|
+
if (details) prText += `\n${details}`;
|
|
31
|
+
}
|
|
32
|
+
return prText;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fetchIssueStates(repo, issueNumbers) {
|
|
36
|
+
const states = new Map();
|
|
37
|
+
for (const number of issueNumbers) {
|
|
38
|
+
const raw = gh(
|
|
39
|
+
[
|
|
40
|
+
'issue',
|
|
41
|
+
'view',
|
|
42
|
+
String(number),
|
|
43
|
+
'--repo',
|
|
44
|
+
repo,
|
|
45
|
+
'--json',
|
|
46
|
+
'state,labels',
|
|
47
|
+
'--jq',
|
|
48
|
+
'{state:.state,labels:[.labels[].name]}',
|
|
49
|
+
],
|
|
50
|
+
{ optional: true }
|
|
51
|
+
);
|
|
52
|
+
if (!raw) continue;
|
|
53
|
+
states.set(number, JSON.parse(raw));
|
|
54
|
+
}
|
|
55
|
+
return states;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function ensureReleasedLabel(repo) {
|
|
59
|
+
gh(
|
|
60
|
+
[
|
|
61
|
+
'label',
|
|
62
|
+
'create',
|
|
63
|
+
'released',
|
|
64
|
+
'--color',
|
|
65
|
+
'ededed',
|
|
66
|
+
'--description',
|
|
67
|
+
'Fix available in stable npm channel',
|
|
68
|
+
'--repo',
|
|
69
|
+
repo,
|
|
70
|
+
],
|
|
71
|
+
{ optional: true }
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function applyAction(repo, action) {
|
|
76
|
+
console.log(`Cleaning release state on issue #${action.number}`);
|
|
77
|
+
for (const label of action.removeLabels) {
|
|
78
|
+
gh(['issue', 'edit', String(action.number), '--remove-label', label, '--repo', repo], {
|
|
79
|
+
optional: true,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!action.close) return;
|
|
84
|
+
|
|
85
|
+
gh(['issue', 'edit', String(action.number), '--add-label', 'released', '--repo', repo], {
|
|
86
|
+
optional: true,
|
|
87
|
+
});
|
|
88
|
+
gh(
|
|
89
|
+
[
|
|
90
|
+
'issue',
|
|
91
|
+
'close',
|
|
92
|
+
String(action.number),
|
|
93
|
+
'--comment',
|
|
94
|
+
'[bot] Closing issue because this fix/feature is now in stable release (@latest).',
|
|
95
|
+
'--repo',
|
|
96
|
+
repo,
|
|
97
|
+
],
|
|
98
|
+
{ optional: true }
|
|
99
|
+
);
|
|
100
|
+
console.log(`Closed issue #${action.number}: ${action.reason}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function main() {
|
|
104
|
+
const context = getStableReleaseContext();
|
|
105
|
+
console.log(`Checking stable release issue lifecycle for ${context.currentTag}`);
|
|
106
|
+
console.log(`Checking commits in range: ${context.range}`);
|
|
107
|
+
|
|
108
|
+
const prText = fetchPrText(context.repo, context.commitText);
|
|
109
|
+
const { releaseIssues, resolvedIssues } = buildReleaseIssueSet({
|
|
110
|
+
releaseBody: context.releaseBody,
|
|
111
|
+
commitText: context.commitText,
|
|
112
|
+
prText,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (releaseIssues.length === 0) {
|
|
116
|
+
console.log(`No release-scoped issues found for ${context.currentTag}`);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
ensureReleasedLabel(context.repo);
|
|
121
|
+
const issueStates = fetchIssueStates(context.repo, releaseIssues);
|
|
122
|
+
for (const action of planIssueCleanup({ releaseIssues, resolvedIssues, issueStates })) {
|
|
123
|
+
applyAction(context.repo, action);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
128
|
+
try {
|
|
129
|
+
main();
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.error(error);
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
}
|