diff --git a/dist/index.js b/dist/index.js index 32bb290..fb0a20b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -127752,102 +127752,90 @@ var external_zlib_default = /*#__PURE__*/__nccwpck_require__.n(external_zlib_); function getStaticBundleSizes(workingDir) { - var manifest = loadBuildManifest(workingDir); + const manifest = loadBuildManifest(workingDir); return getPageSizesFromManifest(manifest, workingDir); } function getPageSizesFromManifest(manifest, workingDir) { - return Object.entries(manifest.pages).map(function (_a) { - var page = _a[0], files = _a[1]; - var size = files - .map(function (filename) { - var fn = external_path_default().join(process.cwd(), workingDir, '.next', filename); - var bytes = external_fs_default().readFileSync(fn); - var gzipped = external_zlib_default().gzipSync(bytes); + return Object.entries(manifest.pages).map(([page, files]) => { + const size = files + .map((filename) => { + const fn = external_path_default().join(process.cwd(), workingDir, '.next', filename); + const bytes = external_fs_default().readFileSync(fn); + const gzipped = external_zlib_default().gzipSync(bytes); return gzipped.byteLength; }) - .reduce(function (s, b) { return s + b; }, 0); - return { page: page, size: size }; + .reduce((s, b) => s + b, 0); + return { page, size }; }); } function loadBuildManifest(workingDir) { - var file = external_fs_default().readFileSync(external_path_default().join(process.cwd(), workingDir, '.next', 'build-manifest.json'), 'utf-8'); + const file = external_fs_default().readFileSync(external_path_default().join(process.cwd(), workingDir, '.next', 'build-manifest.json'), 'utf-8'); return JSON.parse(file); } -function getMarkdownTable(referenceBundleSizes, bundleSizes, name) { - if (referenceBundleSizes === void 0) { referenceBundleSizes = []; } - if (name === void 0) { name = 'Route'; } +function getMarkdownTable(referenceBundleSizes = [], bundleSizes, name = 'Route') { // Produce a Markdown table with each page, its size and difference to default branch - var rows = getPageChangeInfo(referenceBundleSizes, bundleSizes); + const rows = getPageChangeInfo(referenceBundleSizes, bundleSizes); if (rows.length === 0) { - return "".concat(name, ": None found."); + return `${name}: None found.`; } // No diff if reference bundle sizes is empty if (referenceBundleSizes.length === 0) { return formatTableNoDiff(name, rows); } - var significant = getSignificant(rows); + const significant = getSignificant(rows); if (significant.length > 0) { return formatTable(name, significant); } - return "".concat(name, ": No significant changes found"); + return `${name}: No significant changes found`; } function getPageChangeInfo(referenceBundleSizes, bundleSizes) { - var addedAndChanged = bundleSizes.map(function (_a) { - var page = _a.page, size = _a.size; - var referenceSize = referenceBundleSizes.find(function (x) { return x.page === page; }); + const addedAndChanged = bundleSizes.map(({ page, size }) => { + const referenceSize = referenceBundleSizes.find((x) => x.page === page); if (referenceSize) { return { - page: page, + page, type: 'changed', - size: size, + size, diff: size - referenceSize.size, }; } - return { page: page, type: 'added', size: size, diff: size }; - }); - var removed = referenceBundleSizes - .filter(function (_a) { - var page = _a.page; - return !bundleSizes.find(function (x) { return x.page === page; }); - }) - .map(function (_a) { - var page = _a.page; - return ({ page: page, type: 'removed', size: 0, diff: 0 }); + return { page, type: 'added', size, diff: size }; }); + const removed = referenceBundleSizes + .filter(({ page }) => !bundleSizes.find((x) => x.page === page)) + .map(({ page }) => ({ page, type: 'removed', size: 0, diff: 0 })); return addedAndChanged.concat(removed); } function getSignificant(rows) { - return rows.filter(function (_a) { - var type = _a.type, diff = _a.diff; - return type !== 'changed' || diff >= 1000 || diff <= -1000; - }); + return rows.filter(({ type, diff }) => type !== 'changed' || diff >= 1000 || diff <= -1000); } function formatTable(name, rows) { - var rowStrs = rows.map(function (_a) { - var page = _a.page, type = _a.type, size = _a.size, diff = _a.diff; - var diffStr = type === 'changed' ? formatBytes(diff, true) : type; - return "| `".concat(page, "` | ").concat(formatBytes(size), " | ").concat(diffStr, " |"); + const rowStrs = rows.map(({ page, type, size, diff }) => { + const diffStr = type === 'changed' ? formatBytes(diff, true) : type; + return `| \`${page}\` | ${formatBytes(size)} | ${diffStr} |`; }); - return "| ".concat(name, " | Size (gzipped) | Diff |\n | --- | --- | --- |\n ").concat(rowStrs.join('\n')); + return `| ${name} | Size (gzipped) | Diff | + | --- | --- | --- | + ${rowStrs.join('\n')}`; } function formatTableNoDiff(name, rows) { - var rowStrs = rows.map(function (_a) { - var page = _a.page, size = _a.size; - return "| `".concat(page, "` | ").concat(formatBytes(size), " |"); + const rowStrs = rows.map(({ page, size }) => { + return `| \`${page}\` | ${formatBytes(size)} |`; }); - return "| ".concat(name, " | Size (gzipped) |\n | --- | --- |\n ").concat(rowStrs.join('\n')); + return `| ${name} | Size (gzipped) | + | --- | --- | + ${rowStrs.join('\n')}`; } -function formatBytes(bytes, signed) { - if (signed === void 0) { signed = false; } - var sign = signed ? getSign(bytes) : ''; +function formatBytes(bytes, signed = false) { + const sign = signed ? getSign(bytes) : ''; if (bytes === 0) { - return "no change"; + return `no change`; } - var k = 1024; - var dm = 2; - var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - var i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k)); - return "".concat(sign).concat(parseFloat(Math.abs(bytes / Math.pow(k, i)).toFixed(dm)), " ").concat(sizes[i]); + const k = 1024; + const dm = 2; + const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k)); + return `${sign}${parseFloat(Math.abs(bytes / k ** i).toFixed(dm))} ${sizes[i]}`; } function getSign(bytes) { return bytes < 0 ? '-' : '+'; @@ -127855,342 +127843,172 @@ function getSign(bytes) { ;// CONCATENATED MODULE: ./src/text-format.ts function createHtmlComment(content) { - return ""); + return ``; } function getAppNameDelimiter(appName) { return { - start: createHtmlComment("".concat(appName, " start")), - end: createHtmlComment("".concat(appName, " end")), + start: createHtmlComment(`${appName} start`), + end: createHtmlComment(`${appName} end`), }; } -function formatTextFragments() { - var text = []; - for (var _i = 0; _i < arguments.length; _i++) { - text[_i] = arguments[_i]; - } +function formatTextFragments(...text) { return text - .map(function (fragment) { return fragment.trim(); }) + .map((fragment) => fragment.trim()) .filter(Boolean) .join('\n\n'); } -function swapContentPartiallyByDelimiter(_a) { - var existingContent = _a.existingContent, newPartialContent = _a.newPartialContent, delimiterIdentifier = _a.delimiterIdentifier; - var delimiter = getAppNameDelimiter(delimiterIdentifier); - var startIndex = existingContent.indexOf(delimiter.start); - var endIndex = existingContent.indexOf(delimiter.end, startIndex); +function swapContentPartiallyByDelimiter({ existingContent, newPartialContent, delimiterIdentifier, }) { + const delimiter = getAppNameDelimiter(delimiterIdentifier); + const startIndex = existingContent.indexOf(delimiter.start); + const endIndex = existingContent.indexOf(delimiter.end, startIndex); if (startIndex === -1 || endIndex === -1) { return formatTextFragments(existingContent, delimiter.start, newPartialContent, delimiter.end); } - var existingBodyStart = existingContent.substring(0, startIndex); - var existingBodyEnd = existingContent.substring(endIndex + delimiter.end.length); + const existingBodyStart = existingContent.substring(0, startIndex); + const existingBodyEnd = existingContent.substring(endIndex + delimiter.end.length); return formatTextFragments(existingBodyStart, delimiter.start, newPartialContent, delimiter.end, existingBodyEnd); } -function createContentByDelimiter(_a) { - var title = _a.title, content = _a.content, delimiterIdentifier = _a.delimiterIdentifier; - var appNameDelimiter = getAppNameDelimiter(delimiterIdentifier); +function createContentByDelimiter({ title, content, delimiterIdentifier, }) { + const appNameDelimiter = getAppNameDelimiter(delimiterIdentifier); return formatTextFragments(title, appNameDelimiter.start, content, appNameDelimiter.end); } ;// CONCATENATED MODULE: ./src/comments.ts -var __assign = (undefined && undefined.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; -var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (undefined && undefined.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -function createOrUpdateCommentPartially(_a) { - var octokit = _a.octokit, issueNumber = _a.issueNumber, appName = _a.appName, title = _a.title, body = _a.body; - return __awaiter(this, void 0, void 0, function () { - var comments, existingComment, commentBody, newBody, response, newBody, response; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: return [4 /*yield*/, octokit.rest.issues.listComments(__assign(__assign({}, github.context.repo), { issue_number: issueNumber }))]; - case 1: - comments = _b.sent(); - existingComment = comments.data.find(function (comment) { var _a; return (_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes(title); }); - if (!existingComment) return [3 /*break*/, 3]; - commentBody = existingComment.body; - newBody = swapContentPartiallyByDelimiter({ - existingContent: commentBody, - newPartialContent: body, - delimiterIdentifier: appName, - }); - console.log("Updating comment ".concat(existingComment.id)); - return [4 /*yield*/, octokit.rest.issues.updateComment(__assign(__assign({}, github.context.repo), { comment_id: existingComment.id, body: newBody }))]; - case 2: - response = _b.sent(); - console.log("Done with status ".concat(response.status)); - return [3 /*break*/, 5]; - case 3: - newBody = createContentByDelimiter({ - title: title, - content: body, - delimiterIdentifier: appName, - }); - console.log("Creating comment on PR ".concat(issueNumber)); - return [4 /*yield*/, octokit.rest.issues.createComment(__assign(__assign({}, github.context.repo), { issue_number: issueNumber, body: newBody }))]; - case 4: - response = _b.sent(); - console.log("Done with status ".concat(response.status)); - _b.label = 5; - case 5: return [2 /*return*/]; - } +async function createOrUpdateCommentPartially({ octokit, issueNumber, appName, title, body, }) { + const comments = await octokit.rest.issues.listComments(Object.assign(Object.assign({}, github.context.repo), { issue_number: issueNumber })); + const existingComment = comments.data.find((comment) => { var _a; return (_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes(title); }); + if (existingComment) { + const commentBody = existingComment.body; + const newBody = swapContentPartiallyByDelimiter({ + existingContent: commentBody, + newPartialContent: body, + delimiterIdentifier: appName, }); - }); + console.log(`Updating comment ${existingComment.id}`); + const response = await octokit.rest.issues.updateComment(Object.assign(Object.assign({}, github.context.repo), { comment_id: existingComment.id, body: newBody })); + console.log(`Done with status ${response.status}`); + } + else { + const newBody = createContentByDelimiter({ + title, + content: body, + delimiterIdentifier: appName, + }); + console.log(`Creating comment on PR ${issueNumber}`); + const response = await octokit.rest.issues.createComment(Object.assign(Object.assign({}, github.context.repo), { issue_number: issueNumber, body: newBody })); + console.log(`Done with status ${response.status}`); + } } ;// CONCATENATED MODULE: ./src/issue.ts -var issue_assign = (undefined && undefined.__assign) || function () { - issue_assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return issue_assign.apply(this, arguments); -}; -var issue_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var issue_generator = (undefined && undefined.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -var ISSUE_TITLE = 'Current Bundle Sizes'; -function createOrUpdateIssuePartially(_a) { - var octokit = _a.octokit, appName = _a.appName, body = _a.body; - return issue_awaiter(this, void 0, void 0, function () { - var issues, existingIssue, newBody, response, newBody, response; - return issue_generator(this, function (_b) { - switch (_b.label) { - case 0: return [4 /*yield*/, octokit.rest.issues.listForRepo(github.context.repo)]; - case 1: - issues = (_b.sent()).data; - existingIssue = issues.find(function (issue) { return issue.title === ISSUE_TITLE; }); - if (!(existingIssue && existingIssue.body)) return [3 /*break*/, 3]; - newBody = swapContentPartiallyByDelimiter({ - existingContent: existingIssue.body, - newPartialContent: body, - delimiterIdentifier: appName, - }); - console.log("Updating issue ".concat(existingIssue.number, " with latest bundle sizes")); - return [4 /*yield*/, octokit.rest.issues.update(issue_assign(issue_assign({}, github.context.repo), { body: newBody, issue_number: existingIssue.number }))]; - case 2: - response = _b.sent(); - console.log("Issue update response status ".concat(response.status)); - return [3 /*break*/, 5]; - case 3: - newBody = createContentByDelimiter({ - title: '', - content: body, - delimiterIdentifier: appName, - }); - console.log("Creating issue ".concat(ISSUE_TITLE, " to show latest bundle sizes")); - return [4 /*yield*/, octokit.rest.issues.create(issue_assign(issue_assign({}, github.context.repo), { body: newBody, title: ISSUE_TITLE }))]; - case 4: - response = _b.sent(); - console.log("Issue creation response status ".concat(response.status)); - _b.label = 5; - case 5: return [2 /*return*/]; - } +const ISSUE_TITLE = 'Current Bundle Sizes'; +async function createOrUpdateIssuePartially({ octokit, appName, body, }) { + const { data: issues } = await octokit.rest.issues.listForRepo(github.context.repo); + const existingIssue = issues.find((issue) => issue.title === ISSUE_TITLE); + if (existingIssue && existingIssue.body) { + const newBody = swapContentPartiallyByDelimiter({ + existingContent: existingIssue.body, + newPartialContent: body, + delimiterIdentifier: appName, }); - }); + console.log(`Updating issue ${existingIssue.number} with latest bundle sizes`); + const response = await octokit.rest.issues.update(Object.assign(Object.assign({}, github.context.repo), { body: newBody, issue_number: existingIssue.number })); + console.log(`Issue update response status ${response.status}`); + } + else { + const newBody = createContentByDelimiter({ + title: '', + content: body, + delimiterIdentifier: appName, + }); + console.log(`Creating issue ${ISSUE_TITLE} to show latest bundle sizes`); + const response = await octokit.rest.issues.create(Object.assign(Object.assign({}, github.context.repo), { body: newBody, title: ISSUE_TITLE })); + console.log(`Issue creation response status ${response.status}`); + } } // EXTERNAL MODULE: ./node_modules/.pnpm/adm-zip@0.5.10/node_modules/adm-zip/adm-zip.js var adm_zip = __nccwpck_require__(90440); var adm_zip_default = /*#__PURE__*/__nccwpck_require__.n(adm_zip); ;// CONCATENATED MODULE: ./src/download-artifacts.ts -var download_artifacts_assign = (undefined && undefined.__assign) || function () { - download_artifacts_assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return download_artifacts_assign.apply(this, arguments); -}; -var download_artifacts_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var download_artifacts_generator = (undefined && undefined.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; +var __asyncValues = (undefined && undefined.__asyncValues) || function (o) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var m = o[Symbol.asyncIterator], i; + return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); + function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } + function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } +}; + + +const TOTAL_PAGES_LIMIT = 10; +const ARTIFACTS_PER_PAGE_LIMIT = 100; +async function findArtifactForBranch({ octokit, branch, artifactName, }) { + var _a, e_1, _b, _c; + var _d; + const artifactPageIterator = octokit.paginate.iterator(octokit.rest.actions.listArtifactsForRepo, Object.assign(Object.assign({}, github.context.repo), { name: artifactName, per_page: ARTIFACTS_PER_PAGE_LIMIT })); + let pageIndex = 0; + try { + for (var _e = true, artifactPageIterator_1 = __asyncValues(artifactPageIterator), artifactPageIterator_1_1; artifactPageIterator_1_1 = await artifactPageIterator_1.next(), _a = artifactPageIterator_1_1.done, !_a; _e = true) { + _c = artifactPageIterator_1_1.value; + _e = false; + const { data: artifacts } = _c; + for (const artifact of artifacts) { + if (((_d = artifact.workflow_run) === null || _d === void 0 ? void 0 : _d.head_branch) === branch && !artifact.expired) { + return artifact; + } } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; - - -function findArtifactForBranch(_a) { - var octokit = _a.octokit, branch = _a.branch, artifactName = _a.artifactName; - return download_artifacts_awaiter(this, void 0, void 0, function () { - var data, matchingArtifact; - return download_artifacts_generator(this, function (_b) { - switch (_b.label) { - case 0: return [4 /*yield*/, octokit.rest.actions.listArtifactsForRepo(download_artifacts_assign(download_artifacts_assign({}, github.context.repo), { name: artifactName }))]; - case 1: - data = (_b.sent()).data; - matchingArtifact = data.artifacts - .filter(function (artifact) { var _a; return ((_a = artifact.workflow_run) === null || _a === void 0 ? void 0 : _a.head_branch) === branch; }) - .sort(function (a, b) { - var _a, _b; - var aDate = new Date((_a = a.created_at) !== null && _a !== void 0 ? _a : 0); - var bDate = new Date((_b = b.created_at) !== null && _b !== void 0 ? _b : 0); - return bDate.getTime() - aDate.getTime(); - })[0]; - return [2 /*return*/, matchingArtifact !== null && matchingArtifact !== void 0 ? matchingArtifact : null]; + if (pageIndex++ >= TOTAL_PAGES_LIMIT) { + console.log(`Artifact not found in last ${TOTAL_PAGES_LIMIT} pages`); + break; } - }); - }); + } + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (!_e && !_a && (_b = artifactPageIterator_1.return)) await _b.call(artifactPageIterator_1); + } + finally { if (e_1) throw e_1.error; } + } + return null; } -function downloadArtifactAsJson(octokit, branch, artifactName, fileName) { +async function downloadArtifactAsJson(octokit, branch, artifactName, fileName) { var _a; - return download_artifacts_awaiter(this, void 0, void 0, function () { - var bundleSizeArtifact, zip, adm, bundleSizeEntry, e_1; - return download_artifacts_generator(this, function (_b) { - switch (_b.label) { - case 0: - _b.trys.push([0, 3, , 4]); - return [4 /*yield*/, findArtifactForBranch({ - octokit: octokit, - branch: branch, - artifactName: artifactName, - })]; - case 1: - bundleSizeArtifact = _b.sent(); - if (!bundleSizeArtifact) { - console.log("Could not find bundle size artifact on run"); - return [2 /*return*/, null]; - } - // Download a zip of the artifact and find the JSON file - console.log("Downloading artifact ZIP for artifact ".concat(bundleSizeArtifact.id, "...")); - return [4 /*yield*/, octokit.rest.actions.downloadArtifact(download_artifacts_assign(download_artifacts_assign({}, github.context.repo), { artifact_id: bundleSizeArtifact.id, archive_format: 'zip' }))]; - case 2: - zip = _b.sent(); - adm = new (adm_zip_default())(Buffer.from(zip.data)); - bundleSizeEntry = adm.getEntries().find(function (entry) { return entry.entryName === fileName; }); - if (!bundleSizeEntry) { - console.log("Could not find file '".concat(fileName, "' in artifact")); - return [2 /*return*/, null]; - } - // Parse and return the JSON - return [2 /*return*/, { - sha: (_a = bundleSizeArtifact.workflow_run) === null || _a === void 0 ? void 0 : _a.head_sha, - data: JSON.parse(bundleSizeEntry.getData().toString()), - }]; - case 3: - e_1 = _b.sent(); - console.log('Failed to download artifacts', e_1); - return [2 /*return*/, null]; - case 4: return [2 /*return*/]; - } + try { + const bundleSizeArtifact = await findArtifactForBranch({ + octokit, + branch, + artifactName, }); - }); + if (!bundleSizeArtifact) { + console.log(`Could not find bundle size artifact on run`); + return null; + } + // Download a zip of the artifact and find the JSON file + console.log(`Downloading artifact ZIP for artifact ${bundleSizeArtifact.id}...`); + const zip = await octokit.rest.actions.downloadArtifact(Object.assign(Object.assign({}, github.context.repo), { artifact_id: bundleSizeArtifact.id, archive_format: 'zip' })); + // @ts-expect-error zip.data is unknown + const adm = new (adm_zip_default())(Buffer.from(zip.data)); + // @ts-ignore weird any type error from ncc + const bundleSizeEntry = adm.getEntries().find((entry) => entry.entryName === fileName); + if (!bundleSizeEntry) { + console.log(`Could not find file '${fileName}' in artifact`); + return null; + } + // Parse and return the JSON + return { + sha: (_a = bundleSizeArtifact.workflow_run) === null || _a === void 0 ? void 0 : _a.head_sha, + data: JSON.parse(bundleSizeEntry.getData().toString()), + }; + } + catch (e) { + console.log('Failed to download artifacts', e); + return null; + } } // EXTERNAL MODULE: ./node_modules/.pnpm/@actions+artifact@2.1.0/node_modules/@actions/artifact/lib/artifact.js @@ -128198,80 +128016,31 @@ var artifact = __nccwpck_require__(70007); // EXTERNAL MODULE: ./node_modules/.pnpm/tmp@0.2.1/node_modules/tmp/lib/tmp.js var tmp = __nccwpck_require__(38766); ;// CONCATENATED MODULE: ./src/upload-artifacts.ts -var upload_artifacts_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var upload_artifacts_generator = (undefined && undefined.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -function uploadJsonAsArtifact(artifactName, fileName, data) { - return upload_artifacts_awaiter(this, void 0, void 0, function () { - var artifactClient, dir, file, response; - return upload_artifacts_generator(this, function (_a) { - switch (_a.label) { - case 0: - artifactClient = new artifact.DefaultArtifactClient(); - dir = tmp/* dirSync */.op(); - file = tmp/* fileSync */.yd({ name: fileName, dir: dir.name }); - external_fs_.writeFileSync(file.name, JSON.stringify(data, null, 2)); - console.log("Uploading ".concat(file.name)); - return [4 /*yield*/, artifactClient.uploadArtifact(artifactName, [file.name], dir.name)]; - case 1: - response = _a.sent(); - console.log('Artifact uploaded', response); - return [2 /*return*/]; - } - }); - }); +async function uploadJsonAsArtifact(artifactName, fileName, data) { + const artifactClient = new artifact.DefaultArtifactClient(); + const dir = tmp/* dirSync */.op(); + const file = tmp/* fileSync */.yd({ name: fileName, dir: dir.name }); + external_fs_.writeFileSync(file.name, JSON.stringify(data, null, 2)); + console.log(`Uploading ${file.name}`); + const response = await artifactClient.uploadArtifact(artifactName, [file.name], dir.name); + console.log('Artifact uploaded', response); } ;// CONCATENATED MODULE: ./src/create-partial-bundle-info.ts -function createPartialBundleInfo(_a) { - var appName = _a.appName, referenceSha = _a.referenceSha, referenceBundleSizes = _a.referenceBundleSizes, actualBundleSizes = _a.actualBundleSizes; - var title = "### ".concat(appName); - var info = "Compared against ".concat(referenceSha); - var routesTable = getMarkdownTable(referenceBundleSizes, actualBundleSizes, 'Route'); +function createPartialBundleInfo({ appName, referenceSha, referenceBundleSizes, actualBundleSizes, }) { + const title = `### ${appName}`; + const info = `Compared against ${referenceSha}`; + const routesTable = getMarkdownTable(referenceBundleSizes, actualBundleSizes, 'Route'); return formatTextFragments(title, info, routesTable); } -function createPartialReferenceBundleInfo(_a) { - var appName = _a.appName, actualBundleSizes = _a.actualBundleSizes; - var title = "### ".concat(appName); - var routesTable = getMarkdownTable([], actualBundleSizes, 'Route'); +function createPartialReferenceBundleInfo({ appName, actualBundleSizes, }) { + const title = `### ${appName}`; + const routesTable = getMarkdownTable([], actualBundleSizes, 'Route'); return formatTextFragments(title, routesTable); } @@ -128279,57 +128048,10 @@ function createPartialReferenceBundleInfo(_a) { function determineAppName(workingDirectory) { var _a; - return ((_a = workingDirectory === null || workingDirectory === void 0 ? void 0 : workingDirectory.split('/').reverse().find(function (part) { return part; })) !== null && _a !== void 0 ? _a : github.context.repo.repo); + return ((_a = workingDirectory === null || workingDirectory === void 0 ? void 0 : workingDirectory.split('/').reverse().find((part) => part)) !== null && _a !== void 0 ? _a : github.context.repo.repo); } ;// CONCATENATED MODULE: ./src/index.ts -var src_assign = (undefined && undefined.__assign) || function () { - src_assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return src_assign.apply(this, arguments); -}; -var src_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var src_generator = (undefined && undefined.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (g && (g = 0, op[0] && (_ = 0)), _) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; @@ -128339,76 +128061,60 @@ var src_generator = (undefined && undefined.__generator) || function (thisArg, b -var ARTIFACT_NAME_PREFIX = 'next-bundle-analyzer__'; -var FILE_NAME = 'bundle-sizes.json'; -var COMMENT_TITLE = '## Bundle Sizes'; -function run() { +const ARTIFACT_NAME_PREFIX = 'next-bundle-analyzer__'; +const FILE_NAME = 'bundle-sizes.json'; +const COMMENT_TITLE = '## Bundle Sizes'; +async function run() { var _a; - return src_awaiter(this, void 0, void 0, function () { - var workingDir, token, appName, artifactName, octokit, default_branch, issueNumber, referenceBundleSizes, bundleSizes, body, body, e_1; - return src_generator(this, function (_b) { - switch (_b.label) { - case 0: - _b.trys.push([0, 4, , 5]); - workingDir = core.getInput('working-directory'); - token = core.getInput('github-token'); - appName = determineAppName(workingDir); - artifactName = "".concat(ARTIFACT_NAME_PREFIX).concat(appName); - octokit = (0,github.getOctokit)(token); - return [4 /*yield*/, octokit.rest.repos.get(src_assign({}, github.context.repo))]; - case 1: - default_branch = (_b.sent()).data.default_branch; - issueNumber = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number; - console.log("> Downloading bundle sizes from ".concat(default_branch)); - return [4 /*yield*/, downloadArtifactAsJson(octokit, default_branch, artifactName, FILE_NAME)]; - case 2: - referenceBundleSizes = (_b.sent()) || { sha: 'none', data: [] }; - console.log(referenceBundleSizes); - console.log('> Calculating local bundle sizes'); - bundleSizes = getStaticBundleSizes(workingDir); - console.log(bundleSizes); - console.log('> Uploading local bundle sizes'); - return [4 /*yield*/, uploadJsonAsArtifact(artifactName, FILE_NAME, bundleSizes)]; - case 3: - _b.sent(); - if (issueNumber) { - console.log('> Commenting on PR'); - body = createPartialBundleInfo({ - appName: appName, - referenceSha: referenceBundleSizes.sha, - referenceBundleSizes: referenceBundleSizes.data, - actualBundleSizes: bundleSizes, - }); - createOrUpdateCommentPartially({ - octokit: octokit, - issueNumber: issueNumber, - appName: appName, - title: COMMENT_TITLE, - body: body, - }); - } - else if (github.context.ref === "refs/heads/".concat(default_branch)) { - console.log('> Creating/updating bundle size issue'); - body = createPartialReferenceBundleInfo({ - appName: appName, - actualBundleSizes: bundleSizes, - }); - createOrUpdateIssuePartially({ - octokit: octokit, - appName: appName, - body: body, - }); - } - return [3 /*break*/, 5]; - case 4: - e_1 = _b.sent(); - console.log(e_1); - core.setFailed(e_1 === null || e_1 === void 0 ? void 0 : e_1.message); - return [3 /*break*/, 5]; - case 5: return [2 /*return*/]; - } - }); - }); + try { + const workingDir = core.getInput('working-directory'); + const token = core.getInput('github-token'); + const appName = determineAppName(workingDir); + const artifactName = `${ARTIFACT_NAME_PREFIX}${appName}`; + const octokit = (0,github.getOctokit)(token); + const { data: { default_branch }, } = await octokit.rest.repos.get(Object.assign({}, github.context.repo)); + const issueNumber = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number; + console.log(`> Downloading bundle sizes from ${default_branch}`); + const referenceBundleSizes = (await downloadArtifactAsJson(octokit, default_branch, artifactName, FILE_NAME)) || { sha: 'none', data: [] }; + console.log(referenceBundleSizes); + console.log('> Calculating local bundle sizes'); + const bundleSizes = getStaticBundleSizes(workingDir); + console.log(bundleSizes); + console.log('> Uploading local bundle sizes'); + await uploadJsonAsArtifact(artifactName, FILE_NAME, bundleSizes); + if (issueNumber) { + console.log('> Commenting on PR'); + const body = createPartialBundleInfo({ + appName, + referenceSha: referenceBundleSizes.sha, + referenceBundleSizes: referenceBundleSizes.data, + actualBundleSizes: bundleSizes, + }); + createOrUpdateCommentPartially({ + octokit, + issueNumber, + appName, + title: COMMENT_TITLE, + body, + }); + } + else if (github.context.ref === `refs/heads/${default_branch}`) { + console.log('> Creating/updating bundle size issue'); + const body = createPartialReferenceBundleInfo({ + appName, + actualBundleSizes: bundleSizes, + }); + createOrUpdateIssuePartially({ + octokit, + appName, + body, + }); + } + } + catch (e) { + console.log(e); + core.setFailed(e === null || e === void 0 ? void 0 : e.message); + } } run(); diff --git a/src/download-artifacts.ts b/src/download-artifacts.ts index 0f6d7bd..24b54b9 100644 --- a/src/download-artifacts.ts +++ b/src/download-artifacts.ts @@ -4,6 +4,9 @@ import { context } from '@actions/github'; import { Octokit } from './types'; import { PageBundleSizes } from './bundle-size'; +const TOTAL_PAGES_LIMIT = 10; +const ARTIFACTS_PER_PAGE_LIMIT = 100; + async function findArtifactForBranch({ octokit, branch, @@ -13,19 +16,27 @@ async function findArtifactForBranch({ branch: string; artifactName: string; }) { - // TODO: Paginate - const { data } = await octokit.rest.actions.listArtifactsForRepo({ - ...context.repo, - name: artifactName, - }); - const [matchingArtifact] = data.artifacts - .filter((artifact) => artifact.workflow_run?.head_branch === branch) - .sort((a, b) => { - const aDate = new Date(a.created_at ?? 0); - const bDate = new Date(b.created_at ?? 0); - return bDate.getTime() - aDate.getTime(); - }); - return matchingArtifact ?? null; + const artifactPageIterator = octokit.paginate.iterator( + octokit.rest.actions.listArtifactsForRepo, + { + ...context.repo, + name: artifactName, + per_page: ARTIFACTS_PER_PAGE_LIMIT, + }, + ); + let pageIndex = 0; + for await (const { data: artifacts } of artifactPageIterator) { + for (const artifact of artifacts) { + if (artifact.workflow_run?.head_branch === branch && !artifact.expired) { + return artifact; + } + } + if (pageIndex++ >= TOTAL_PAGES_LIMIT) { + console.log(`Artifact not found in last ${TOTAL_PAGES_LIMIT} pages`); + break; + } + } + return null; } export async function downloadArtifactAsJson( diff --git a/tsconfig.json b/tsconfig.json index b4d76ca..efe396f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,9 +7,8 @@ // Project options "allowJs": true, "isolatedModules": true, - "lib": ["dom", "dom.iterable", "esnext"], - "jsx": "preserve", - "target": "es5", + "lib": ["esnext"], + "target": "ES2017", "module": "esnext", "moduleResolution": "node", "noEmit": true,