22
33import { execFileSync } from "node:child_process" ;
44import { readFileSync , writeFileSync } from "node:fs" ;
5+ import { pathToFileURL } from "node:url" ;
56
67const repo = "openclaw/openclaw" ;
78const commitAssociationQueryBatchSize = 20 ;
@@ -221,6 +222,42 @@ function referencesIn(text) {
221222 return references ;
222223}
223224
225+ function referenceLabelsIn ( text ) {
226+ const labels = [ ] ;
227+ for ( const match of text . matchAll (
228+ / (?< ! [ A - Z a - z 0 - 9 _ . & - ] ) (?: (?< owner > [ A - Z a - z 0 - 9 _ . - ] + ) \/ (?< name > [ A - Z a - z 0 - 9 _ . - ] + ) ) ? # (?< number > \d + ) / g,
229+ ) ) {
230+ const qualifiedRepository = match . groups ?. owner
231+ ? `${ match . groups . owner } /${ match . groups . name } `
232+ : undefined ;
233+ labels . push (
234+ ! qualifiedRepository || qualifiedRepository . toLowerCase ( ) === repo
235+ ? `#${ match . groups ?. number } `
236+ : `${ qualifiedRepository } #${ match . groups ?. number } ` ,
237+ ) ;
238+ }
239+ return labels ;
240+ }
241+
242+ export function renderContributionRecordEntry ( entry ) {
243+ const references = [ ] ;
244+ appendUnique ( references , referenceLabelsIn ( entry . title ) ) ;
245+ appendUnique (
246+ references ,
247+ ( entry . priorReferences ?? [ ] ) . map ( ( number ) => `#${ number } ` ) ,
248+ ) ;
249+ appendUnique ( references , entry . externalReferences ?? [ ] ) ;
250+ for ( const issue of entry . linkedIssues ) {
251+ appendUnique ( references , [ `#${ issue . number } ` ] ) ;
252+ }
253+ const related = references . length > 0 ? ` Related ${ references . join ( ", " ) } .` : "" ;
254+ const attribution =
255+ entry . thanks . length > 0
256+ ? ` Thanks ${ entry . thanks . map ( ( handle ) => `@${ handle } ` ) . join ( " and " ) } .`
257+ : "" ;
258+ return `- **PR #${ entry . number } **${ related } ${ attribution } ` ;
259+ }
260+
224261function closingReferencesIn ( text ) {
225262 const references = [ ] ;
226263 for ( const match of text . matchAll (
@@ -255,26 +292,38 @@ function handlesIn(text) {
255292 ) ;
256293}
257294
258- function relatedReferencesIn ( line ) {
259- const related = line . match ( / \b R e l a t e d ( (?: # \d + ) (?: , # \d + ) * ) \. / ) ;
260- return related ? referencesIn ( related [ 1 ] ) : [ ] ;
295+ function externalReferencesIn ( text ) {
296+ return referenceLabelsIn ( text ) . filter ( ( reference ) => ! reference . startsWith ( "#" ) ) ;
297+ }
298+
299+ function appendUnique ( values , additions ) {
300+ const seen = new Set ( values . map ( ( value ) => value . toLowerCase ( ) ) ) ;
301+ for ( const value of additions ) {
302+ const key = value . toLowerCase ( ) ;
303+ if ( ! seen . has ( key ) ) {
304+ values . push ( value ) ;
305+ seen . add ( key ) ;
306+ }
307+ }
261308}
262309
263310function addContributionRecordEntry ( entries , key , entry ) {
264311 const existing = entries . get ( key ) ;
265312 if ( ! existing ) {
266313 entries . set ( key , {
267314 ...entry ,
315+ externalReferences : [ ...( entry . externalReferences ?? [ ] ) ] ,
268316 references : [ ...entry . references ] ,
269317 thanks : [ ...entry . thanks ] ,
270318 } ) ;
271319 return ;
272320 }
321+ appendUnique ( existing . externalReferences , entry . externalReferences ?? [ ] ) ;
273322 appendReferences ( existing . references , entry . references ) ;
274323 addHandles ( existing . thanks , entry . thanks ) ;
275324}
276325
277- function contributionRecordFor ( section ) {
326+ export function contributionRecordFor ( section ) {
278327 const result = { legacyIssues : new Map ( ) , pullRequests : new Map ( ) } ;
279328 const recordStart = section . source . search ( / \n # # # C o m p l e t e c o n t r i b u t i o n (?: l e d g e r | r e c o r d ) \r ? $ / m) ;
280329 if ( recordStart < 0 ) {
@@ -301,8 +350,10 @@ function contributionRecordFor(section) {
301350 const number = explicitRecord ?. [ 1 ] ?? legacyRecord ?. [ 1 ] ;
302351 if ( number ) {
303352 const value = Number ( number ) ;
353+ const metadata = explicitRecord ? line . slice ( explicitRecord [ 0 ] . length ) : line ;
304354 addContributionRecordEntry ( result . pullRequests , value , {
305- references : relatedReferencesIn ( line ) ,
355+ externalReferences : externalReferencesIn ( metadata ) ,
356+ references : referencesIn ( metadata ) . filter ( ( reference ) => reference !== value ) ,
306357 thanks : handlesIn ( line ) ,
307358 } ) ;
308359 }
@@ -345,6 +396,7 @@ function withoutRevertedContributionRecords(record, revertedReferences) {
345396 }
346397 addContributionRecordEntry ( filtered . pullRequests , number , {
347398 ...entry ,
399+ externalReferences : entry . externalReferences ,
348400 references : entry . references . filter ( ( reference ) => ! revertedReferences . has ( reference ) ) ,
349401 } ) ;
350402 }
@@ -1153,7 +1205,7 @@ function mergeIssues(...groups) {
11531205 return [ ...entries . values ( ) ] ;
11541206}
11551207
1156- function ledgerFor (
1208+ export function ledgerFor (
11571209 base ,
11581210 target ,
11591211 references ,
@@ -1203,6 +1255,8 @@ function ledgerFor(
12031255 const issues = entries . filter ( ( entry ) => entry . type === "Issue" ) ;
12041256 const legacyIssues = legacyIssuesByPullRequest ( priorRecord , nodes ) ;
12051257 const records = pullRequests . map ( ( entry ) => {
1258+ const priorEntry = priorRecord . pullRequests . get ( entry . number ) ;
1259+ const priorReferences = priorEntry ?. references ?? [ ] ;
12061260 const titleIssues = issueEntries ( referencesIn ( entry . title ) , nodes ) ;
12071261 const closingIssues = issueEntries (
12081262 entry . closingIssuesReferences ?. nodes . map ( ( issue ) => issue . number ) ?? [ ] ,
@@ -1212,41 +1266,31 @@ function ledgerFor(
12121266 titleIssues ,
12131267 closingIssues ,
12141268 relationships . issuesByPullRequest . get ( entry . number ) ?? [ ] ,
1269+ issueEntries ( priorReferences , nodes ) ,
12151270 issueEntries ( legacyIssues . get ( entry . number ) ?? [ ] , nodes , priorRecord . legacyIssues ) ,
12161271 ) ;
1217- const titleIssueNumbers = new Set ( titleIssues . map ( ( issue ) => issue . number ) ) ;
1218- const relatedIssues = linkedIssues . filter ( ( issue ) => ! titleIssueNumbers . has ( issue . number ) ) ;
12191272 const thanks = [ ...entry . thanks ] ;
1273+ addHandles ( thanks , priorEntry ?. thanks ?? [ ] ) ;
12201274 for ( const issue of linkedIssues ) {
12211275 addHandles ( thanks , issue . thanks ) ;
12221276 }
12231277 return {
12241278 ...entry ,
12251279 ...editorialClassification ( entry . title ) ,
1280+ externalReferences : priorEntry ?. externalReferences ?? [ ] ,
12261281 linkedIssues,
1227- relatedIssues ,
1282+ priorReferences ,
12281283 thanks,
12291284 } ;
12301285 } ) ;
1231- const renderEntry = ( entry ) => {
1232- const attribution =
1233- entry . thanks . length > 0
1234- ? ` Thanks ${ entry . thanks . map ( ( handle ) => `@${ handle } ` ) . join ( " and " ) } .`
1235- : "" ;
1236- const relatedIssues =
1237- entry . relatedIssues . length > 0
1238- ? ` Related ${ entry . relatedIssues . map ( ( issue ) => `#${ issue . number } ` ) . join ( ", " ) } .`
1239- : "" ;
1240- return `- **PR #${ entry . number } ** ${ withSentenceEnding ( entry . title ) } ${ relatedIssues } ${ attribution } ` ;
1241- } ;
12421286 const ledger = [
12431287 "### Complete contribution record" ,
12441288 "" ,
12451289 `This audited record covers the complete ${ base } ..${ target } history: ${ records . length } merged PRs. The generation manifest also supplies direct commits as editorial input; the grouped notes above prioritize user impact.` ,
12461290 "" ,
12471291 "#### Pull requests" ,
12481292 "" ,
1249- ...records . map ( ( entry ) => renderEntry ( entry ) ) ,
1293+ ...records . map ( ( entry ) => renderContributionRecordEntry ( entry ) ) ,
12501294 ] . join ( "\n" ) ;
12511295 return {
12521296 entries,
@@ -1267,7 +1311,7 @@ function replaceLedger(changelog, section, ledger, pullRequests, directCommits)
12671311 return `${ changelog . slice ( 0 , section . start ) } ${ replacement } ${ changelog . slice ( section . end ) } ` ;
12681312}
12691313
1270- function ledgerChecks ( section , pullRequests , nodes , directCommits ) {
1314+ export function ledgerChecks ( section , pullRequests , nodes , directCommits ) {
12711315 const errors = [ ] ;
12721316 if ( / @ u n d e f i n e d \b / i. test ( section . source ) ) {
12731317 errors . push ( "release section contains invalid @undefined contributor credit" ) ;
@@ -1311,6 +1355,25 @@ function ledgerChecks(section, pullRequests, nodes, directCommits) {
13111355 errors . push ( `missing Thanks @${ handle } for #${ entry . number } ` ) ;
13121356 }
13131357 }
1358+ const expectedReferences = [ ] ;
1359+ appendUnique ( expectedReferences , referenceLabelsIn ( entry . title ) ) ;
1360+ appendUnique (
1361+ expectedReferences ,
1362+ entry . priorReferences . map ( ( number ) => `#${ number } ` ) ,
1363+ ) ;
1364+ appendUnique ( expectedReferences , entry . externalReferences ) ;
1365+ appendUnique (
1366+ expectedReferences ,
1367+ entry . linkedIssues . map ( ( issue ) => `#${ issue . number } ` ) ,
1368+ ) ;
1369+ const actualReferences = new Set (
1370+ referenceLabelsIn ( line ) . map ( ( reference ) => reference . toLowerCase ( ) ) ,
1371+ ) ;
1372+ for ( const reference of expectedReferences ) {
1373+ if ( ! actualReferences . has ( reference . toLowerCase ( ) ) ) {
1374+ errors . push ( `missing ${ reference } on contribution record for PR #${ entry . number } ` ) ;
1375+ }
1376+ }
13141377 }
13151378 const editorialProse = section . source . slice ( 0 , ledgerStart ) ;
13161379 for ( const entry of pullRequests ) {
@@ -1402,6 +1465,8 @@ function manifestFor(options, source, ledger, directCommitRecords) {
14021465 type : entry . type ,
14031466 editorialEligible : entry . editorialEligible ,
14041467 thanks : entry . thanks ,
1468+ externalReferences : entry . externalReferences ,
1469+ relatedReferences : [ ...new Set ( [ ...entry . priorReferences , ...referencesIn ( entry . title ) ] ) ] ,
14051470 linkedIssues : entry . linkedIssues . map ( ( issue ) => ( {
14061471 number : issue . number ,
14071472 title : issue . title ,
@@ -1620,4 +1685,6 @@ function main() {
16201685 }
16211686}
16221687
1623- main ( ) ;
1688+ if ( process . argv [ 1 ] && import . meta. url === pathToFileURL ( process . argv [ 1 ] ) . href ) {
1689+ main ( ) ;
1690+ }
0 commit comments