Skip to content

Commit 558ada7

Browse files
committed
log if base is not latest
1 parent 150004e commit 558ada7

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

dev-packages/size-limit-gh-action/index.mjs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ async function run() {
210210
// Else, we run size limit for the current branch, AND fetch it for the comparison branch
211211
let base;
212212
let current;
213+
let baseIsNotLatest = false;
213214

214215
try {
215216
const artifacts = await getArtifactsForBranchAndWorkflow(octokit, {
@@ -231,6 +232,11 @@ async function run() {
231232
});
232233

233234
base = JSON.parse(await fs.readFile(resultsFilePath, { encoding: 'utf8' }));
235+
236+
if (!artifacts.isLatest) {
237+
baseIsNotLatest = true;
238+
core.info('Base artifact is not the latest one. This may lead to incorrect results.');
239+
}
234240
} catch (error) {
235241
core.startGroup('Warning, unable to find base results');
236242
core.error(error);
@@ -254,7 +260,17 @@ async function run() {
254260
isNaN(thresholdNumber) || limit.hasSizeChanges(base, current, thresholdNumber) || sizeLimitComment;
255261

256262
if (shouldComment) {
257-
const body = [SIZE_LIMIT_HEADING, markdownTable(limit.formatResults(base, current))].join('\r\n');
263+
const bodyParts = [SIZE_LIMIT_HEADING];
264+
265+
if (baseIsNotLatest) {
266+
bodyParts.push(
267+
'⚠️ **Warning:** Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results.',
268+
);
269+
}
270+
271+
bodyParts.push(markdownTable(limit.formatResults(base, current)));
272+
273+
const body = bodyParts.join('\r\n');
258274

259275
try {
260276
if (!sizeLimitComment) {
@@ -303,7 +319,7 @@ const DEFAULT_PAGE_LIMIT = 10;
303319
* This is a bit hacky since GitHub Actions currently does not directly
304320
* support downloading artifacts from other workflows
305321
*/
306-
export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, workflowName, branch, artifactName }) {
322+
async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, workflowName, branch, artifactName }) {
307323
core.startGroup(`getArtifactsForBranchAndWorkflow - workflow:"${workflowName}", branch:"${branch}"`);
308324

309325
let repositoryWorkflow = null;
@@ -344,14 +360,13 @@ export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, w
344360
const workflow_id = repositoryWorkflow.id;
345361

346362
let currentPage = 0;
347-
const completedWorkflowRuns = [];
363+
let latestWorkflowRun = null;
348364

349365
for await (const response of octokit.paginate.iterator(octokit.rest.actions.listWorkflowRuns, {
350366
owner,
351367
repo,
352368
workflow_id,
353369
branch,
354-
status: 'completed',
355370
per_page: DEFAULT_PAGE_LIMIT,
356371
event: 'push',
357372
})) {
@@ -364,12 +379,38 @@ export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, w
364379
// Do not allow downloading artifacts from a fork.
365380
const filtered = response.data.filter(workflowRun => workflowRun.head_repository.full_name === `${owner}/${repo}`);
366381

367-
console.log(JSON.stringify(filtered, null, 2));
382+
// Store the first workflow run, to determine if this is the latest one...
383+
if (!latestWorkflowRun) {
384+
latestWorkflowRun = filtered[0];
385+
}
368386

369-
completedWorkflowRuns.push(...filtered);
387+
// Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for
388+
for (const workflowRun of filtered) {
389+
core.info(`Checking artifacts for workflow run: ${workflowRun.html_url}`);
370390

371-
if (completedWorkflowRuns.length) {
372-
break;
391+
const {
392+
data: { artifacts },
393+
} = await octokit.rest.actions.listWorkflowRunArtifacts({
394+
owner,
395+
repo,
396+
run_id: workflowRun.id,
397+
});
398+
399+
if (!artifacts) {
400+
core.warning(
401+
`Unable to fetch artifacts for branch: ${branch}, workflow: ${workflow_id}, workflowRunId: ${workflowRun.id}`,
402+
);
403+
} else {
404+
const foundArtifact = artifacts.find(({ name }) => name === artifactName);
405+
if (foundArtifact) {
406+
core.info(`Found suitable artifact: ${foundArtifact.url}`);
407+
return {
408+
artifact: foundArtifact,
409+
workflowRun,
410+
isLatest: latestWorkflowRun.id === workflowRun.id,
411+
};
412+
}
413+
}
373414
}
374415

375416
if (currentPage > DEFAULT_MAX_PAGES) {
@@ -381,34 +422,6 @@ export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, w
381422
currentPage++;
382423
}
383424

384-
// Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for
385-
for (const workflowRun of completedWorkflowRuns) {
386-
core.info(`Checking artifacts for workflow run: ${workflowRun.html_url}`);
387-
388-
const {
389-
data: { artifacts },
390-
} = await octokit.rest.actions.listWorkflowRunArtifacts({
391-
owner,
392-
repo,
393-
run_id: workflowRun.id,
394-
});
395-
396-
if (!artifacts) {
397-
core.warning(
398-
`Unable to fetch artifacts for branch: ${branch}, workflow: ${workflow_id}, workflowRunId: ${workflowRun.id}`,
399-
);
400-
} else {
401-
const foundArtifact = artifacts.find(({ name }) => name === artifactName);
402-
if (foundArtifact) {
403-
core.info(`Found suitable artifact: ${foundArtifact.url}`);
404-
return {
405-
artifact: foundArtifact,
406-
workflowRun,
407-
};
408-
}
409-
}
410-
}
411-
412425
core.warning(`Artifact not found: ${artifactName}`);
413426
core.endGroup();
414427
return null;

0 commit comments

Comments
 (0)