MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / fitToReleaseBody

Function fitToReleaseBody

scripts/extract-release-notes.mjs:142–172  ·  view source on GitHub ↗
(lines)

Source from the content-addressed store, hash-verified

140const topLevelEntryRe = /^([-*]|\d+\.)\s+/;
141
142function fitToReleaseBody(lines) {
143 const size = (line) => Buffer.byteLength(line, 'utf8') + 1;
144 if (lines.reduce((n, line) => n + size(line), 0) <= MAX_BODY_BYTES) return lines;
145 // Cut only where an entry or a heading starts, outside a code fence, so no
146 // entry is shown in part.
147 const before = [0];
148 const cuts = [];
149 let fenced = false;
150 lines.forEach((line, i) => {
151 if (!fenced && (topLevelEntryRe.test(line) || /^#/.test(line))) cuts.push(i);
152 if (fenceRe.test(line)) fenced = !fenced;
153 before.push(before[i] + size(line));
154 });
155 const entriesFrom = (i) =>
156 lines.slice(i).filter((line) => topLevelEntryRe.test(line)).length;
157 const repo = process.env.GITHUB_REPOSITORY || 'colbymchenry/codegraph';
158 const ref = version && /^\d+\.\d+\.\d+/.test(version) ? `v${version}` : 'main';
159 const pointer = (n) =>
160 `_…and ${n} more ${n === 1 ? 'entry' : 'entries'}: the full list is in ` +
161 `[CHANGELOG.md](https://github.com/${repo}/blob/${ref}/CHANGELOG.md)._`;
162 for (let c = cuts.length - 1; c >= 0; c--) {
163 const kept = lines.slice(0, cuts[c]);
164 // A heading left with nothing under it goes too.
165 while (kept.length > 0 && (/^\s*$/.test(kept[kept.length - 1]) || /^#/.test(kept[kept.length - 1]))) {
166 kept.pop();
167 }
168 const note = pointer(entriesFrom(kept.length));
169 if (before[kept.length] + size('') + size(note) <= MAX_BODY_BYTES) return [...kept, '', note];
170 }
171 return [pointer(entriesFrom(0))];
172}
173
174const body = fitToReleaseBody(out);
175process.stdout.write(body.join('\n'));

Callers 1

Calls 4

pointerFunction · 0.85
entriesFromFunction · 0.85
pushMethod · 0.80
sizeFunction · 0.70

Tested by

no test coverage detected