Skip to content

ci: Fix size limit comparison #11282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 181 additions & 8 deletions dev-packages/size-limit-gh-action/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-lines */
import { promises as fs } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
Expand All @@ -7,11 +8,10 @@ import * as core from '@actions/core';
import { exec } from '@actions/exec';
import { context, getOctokit } from '@actions/github';
import * as glob from '@actions/glob';
import * as io from '@actions/io';
import bytes from 'bytes';
import { markdownTable } from 'markdown-table';

import download from 'github-fetch-workflow-artifact';

const SIZE_LIMIT_HEADING = '## size-limit report 📦 ';
const ARTIFACT_NAME = 'size-limit-action';
const RESULTS_FILE = 'size-limit-results.json';
Expand Down Expand Up @@ -229,20 +229,28 @@ async function run() {
let current;

try {
// Ignore failures here as it is likely that this only happens when introducing size-limit
// and this has not been run on the main branch yet
await download(octokit, {
const artifacts = await getArtifactsForBranchAndWorkflow(octokit, {
...repo,
artifactName: ARTIFACT_NAME,
branch: comparisonBranch,
downloadPath: __dirname,
workflowEvent: 'push',
workflowName: `${process.env.GITHUB_WORKFLOW || ''}`,
});

if (!artifacts) {
throw new Error('No artifacts found');
}

await downloadOtherWorkflowArtifact(octokit, {
...repo,
artifactName: ARTIFACT_NAME,
artifactId: artifacts.artifact.id,
downloadPath: __dirname,
});

base = JSON.parse(await fs.readFile(resultsFilePath, { encoding: 'utf8' }));
} catch (error) {
core.startGroup('Warning, unable to find base results');
core.debug(error);
core.error(error);
core.endGroup();
}

Expand Down Expand Up @@ -295,4 +303,169 @@ async function run() {
}
}

// max pages of workflows to pagination through
const DEFAULT_MAX_PAGES = 50;
// max results per page
const DEFAULT_PAGE_LIMIT = 10;

/**
* Fetch artifacts from a workflow run from a branch
*
* This is a bit hacky since GitHub Actions currently does not directly
* support downloading artifacts from other workflows
*/
/**
* Fetch artifacts from a workflow run from a branch
*
* This is a bit hacky since GitHub Actions currently does not directly
* support downloading artifacts from other workflows
*/
export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, workflowName, branch, artifactName }) {
core.startGroup(`getArtifactsForBranchAndWorkflow - workflow:"${workflowName}", branch:"${branch}"`);

let repositoryWorkflow = null;

// For debugging
const allWorkflows = [];

//
// Find workflow id from `workflowName`
//
for await (const response of octokit.paginate.iterator(octokit.rest.actions.listRepoWorkflows, {
owner,
repo,
})) {
const targetWorkflow = response.data.find(({ name }) => name === workflowName);

allWorkflows.push(...response.data.map(({ name }) => name));

// If not found in responses, continue to search on next page
if (!targetWorkflow) {
continue;
}

repositoryWorkflow = targetWorkflow;
break;
}

if (!repositoryWorkflow) {
core.info(
`Unable to find workflow with name "${workflowName}" in the repository. Found workflows: ${allWorkflows.join(
', ',
)}`,
);
core.endGroup();
return null;
}

const workflow_id = repositoryWorkflow.id;

let currentPage = 0;
const completedWorkflowRuns = [];

for await (const response of octokit.paginate.iterator(octokit.rest.actions.listWorkflowRuns, {
owner,
repo,
workflow_id,
branch,
status: 'completed',
per_page: DEFAULT_PAGE_LIMIT,
event: 'push',
})) {
if (!response.data.length) {
core.warning(`Workflow ${workflow_id} not found in branch ${branch}`);
core.endGroup();
return null;
}

// Do not allow downloading artifacts from a fork.
completedWorkflowRuns.push(
...response.data.filter(workflowRun => workflowRun.head_repository.full_name === `${owner}/${repo}`),
);

if (completedWorkflowRuns.length) {
break;
}

if (currentPage > DEFAULT_MAX_PAGES) {
core.warning(`Workflow ${workflow_id} not found in branch: ${branch}`);
core.endGroup();
return null;
}

currentPage++;
}

// Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for
for (const workflowRun of completedWorkflowRuns) {
core.info(`Checking artifacts for workflow run: ${workflowRun.html_url}`);

const {
data: { artifacts },
} = await octokit.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: workflowRun.id,
});

if (!artifacts) {
core.warning(
`Unable to fetch artifacts for branch: ${branch}, workflow: ${workflow_id}, workflowRunId: ${workflowRun.id}`,
);
} else {
const foundArtifact = artifacts.find(({ name }) => name === artifactName);
if (foundArtifact) {
core.info(`Found suitable artifact: ${foundArtifact.url}`);
return {
artifact: foundArtifact,
workflowRun,
};
}
}
}

core.warning(`Artifact not found: ${artifactName}`);
core.endGroup();
return null;
}

run();

/**
* Use GitHub API to fetch artifact download url, then
* download and extract artifact to `downloadPath`
*/
async function downloadOtherWorkflowArtifact(octokit, { owner, repo, artifactId, artifactName, downloadPath }) {
const artifact = await octokit.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifactId,
archive_format: 'zip',
});

// Make sure output path exists
try {
await io.mkdirP(downloadPath);
} catch {
// ignore errors
}

const downloadFile = path.resolve(downloadPath, `${artifactName}.zip`);

await exec('wget', [
'-nv',
'--retry-connrefused',
'--waitretry=1',
'--read-timeout=20',
'--timeout=15',
'-t',
'0',
'-O',
downloadFile,
artifact.url,
]);

await exec('unzip', ['-q', '-d', downloadPath, downloadFile], {
silent: true,
});
}
4 changes: 2 additions & 2 deletions dev-packages/size-limit-gh-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"@actions/artifact": "1.1.2",
"@actions/core": "1.10.1",
"@actions/exec": "1.1.1",
"@actions/github": "6.0.0",
"@actions/io": "1.1.3",
"@actions/github": "^5.0.0",
"@actions/glob": "0.4.0",
"bytes": "3.1.2",
"github-fetch-workflow-artifact": "2.0.0",
"markdown-table": "3.0.3"
},
"volta": {
Expand Down
Loading