Skip to content

Commit

Permalink
fix(scripts-update-release-notes): properly handle git for-each-ref c…
Browse files Browse the repository at this point in the history
…md call to not fail release notes update (#27757)
  • Loading branch information
Hotell authored May 4, 2023
1 parent f0b0574 commit d17c31e
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions scripts/update-release-notes/changelogsAndTags.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as path from 'path';
import * as fs from 'fs-extra';
import { execSync } from 'child_process';
import * as path from 'path';

import { ChangelogJson } from 'beachball';
import * as fs from 'fs-extra';
import { rollup as lernaAliases } from 'lerna-alias';

import { IChangelogEntry } from './types';

const MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
Expand Down Expand Up @@ -49,28 +51,48 @@ export function getTagToChangelogMap(maxAgeDays?: number): Map<string, IChangelo
* @returns List of tags
*/
export function getTags(maxAgeDays?: number): string[] {
const ONE_MEGABYTE = 1024 * 1000;
const TEN_MEGABYTES = ONE_MEGABYTE * 10;
console.log(`Getting tags${maxAgeDays ? ` up to ${maxAgeDays} days old` : ''}...`);

const cmd = 'git for-each-ref --sort=-creatordate --format="%(refname:short) -- %(creatordate)" refs/tags';
try {
const cmd = 'git for-each-ref --sort=-creatordate --format="%(refname:short) -- %(creatordate)" refs/tags';
const gitForEachRefBuffer = execSync(cmd, {
// execSync buffer is by default 1MB (node 16). Our git refs tog is much bigger than that, thus setting for 10MB
maxBuffer: TEN_MEGABYTES,
});
const currentBufferSize = (gitForEachRefBuffer.byteLength / ONE_MEGABYTE).toFixed(2);

let tagsAndDates = execSync(cmd, { cwd: process.cwd() })
.toString()
.split(/\r?\n/g)
.map(tag => tag.split(' -- '))
.filter(arr => arr.length === 2);
console.warn(`
📣 NOTE: "git for-each-ref" current buffer size is ${currentBufferSize}MB.
If this will be more than maxBuffer ${TEN_MEGABYTES}MB this command will fail and you'll have to increase maxBuffer!
`);

if (maxAgeDays) {
const endIndex = tagsAndDates.findIndex(([, date]) => !_isNewEnough(date, maxAgeDays));
if (endIndex !== -1) {
tagsAndDates = tagsAndDates.slice(0, endIndex);
let tagsAndDates = gitForEachRefBuffer
.toString()
.split(/\r?\n/g)
.map(tag => tag.split(' -- '))
.filter(arr => arr.length === 2);

if (maxAgeDays) {
const endIndex = tagsAndDates.findIndex(([, date]) => !_isNewEnough(date, maxAgeDays));
if (endIndex !== -1) {
tagsAndDates = tagsAndDates.slice(0, endIndex);
}
}
}

const tags = tagsAndDates.map(([tag]) => tag);
const tags = tagsAndDates.map(([tag]) => tag);

console.log(`Found ${tags.length} tag(s).\n`);
console.log(`Found ${tags.length} tag(s).\n`);

return tags;
return tags;
} catch (err) {
if (err.code === 'ENOBUFS') {
throw new Error(`maxBuffer ${TEN_MEGABYTES}MB was reached. Increase its size in the codebase`);
}

throw err;
}
}

/**
Expand Down

0 comments on commit d17c31e

Please sign in to comment.