Skip to content

Commit 4c538ef

Browse files
committed
streamline & extract
1 parent f7f5bb4 commit 4c538ef

File tree

4 files changed

+138
-135
lines changed

4 files changed

+138
-135
lines changed

dev-packages/size-limit-gh-action/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77

88
overrides: [
99
{
10-
files: ['*.mjs'],
10+
files: ['**/*.mjs'],
1111
extends: ['@sentry-internal/sdk/src/base'],
1212
},
1313
],

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

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import * as glob from '@actions/glob';
1010
import * as io from '@actions/io';
1111
import { markdownTable } from 'markdown-table';
1212

13-
import { SizeLimit } from './utils/size-limit-formatter.mjs';
13+
import { SizeLimit } from './utils/SizeLimitFormatter.mjs';
14+
import { getArtifactsForBranchAndWorkflow } from './utils/getArtifactsForBranchAndWorkflow.mjs';
1415

1516
const SIZE_LIMIT_HEADING = '## size-limit report 📦 ';
1617
const ARTIFACT_NAME = 'size-limit-action';
@@ -206,138 +207,6 @@ async function runSizeLimitOnComparisonBranch() {
206207
await artifactClient.uploadArtifact(ARTIFACT_NAME, files, __dirname);
207208
}
208209

209-
// max pages of workflows to pagination through
210-
const DEFAULT_MAX_PAGES = 50;
211-
// max results per page
212-
const DEFAULT_PAGE_LIMIT = 10;
213-
214-
/**
215-
* Fetch artifacts from a workflow run from a branch
216-
*
217-
* This is a bit hacky since GitHub Actions currently does not directly
218-
* support downloading artifacts from other workflows
219-
*/
220-
/**
221-
* Fetch artifacts from a workflow run from a branch
222-
*
223-
* This is a bit hacky since GitHub Actions currently does not directly
224-
* support downloading artifacts from other workflows
225-
*/
226-
async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, workflowName, branch, artifactName }) {
227-
core.startGroup(`getArtifactsForBranchAndWorkflow - workflow:"${workflowName}", branch:"${branch}"`);
228-
229-
let repositoryWorkflow = null;
230-
231-
// For debugging
232-
const allWorkflows = [];
233-
234-
//
235-
// Find workflow id from `workflowName`
236-
//
237-
for await (const response of octokit.paginate.iterator(octokit.rest.actions.listRepoWorkflows, {
238-
owner,
239-
repo,
240-
})) {
241-
const targetWorkflow = response.data.find(({ name }) => name === workflowName);
242-
243-
allWorkflows.push(...response.data.map(({ name }) => name));
244-
245-
// If not found in responses, continue to search on next page
246-
if (!targetWorkflow) {
247-
continue;
248-
}
249-
250-
repositoryWorkflow = targetWorkflow;
251-
break;
252-
}
253-
254-
if (!repositoryWorkflow) {
255-
core.info(
256-
`Unable to find workflow with name "${workflowName}" in the repository. Found workflows: ${allWorkflows.join(
257-
', ',
258-
)}`,
259-
);
260-
core.endGroup();
261-
return null;
262-
}
263-
264-
const workflow_id = repositoryWorkflow.id;
265-
266-
let currentPage = 0;
267-
let latestWorkflowRun = null;
268-
269-
for await (const response of octokit.paginate.iterator(octokit.rest.actions.listWorkflowRuns, {
270-
owner,
271-
repo,
272-
workflow_id,
273-
branch,
274-
per_page: DEFAULT_PAGE_LIMIT,
275-
event: 'push',
276-
})) {
277-
if (!response.data.length) {
278-
core.warning(`Workflow ${workflow_id} not found in branch ${branch}`);
279-
core.endGroup();
280-
return null;
281-
}
282-
283-
// Do not allow downloading artifacts from a fork.
284-
const filtered = response.data.filter(workflowRun => workflowRun.head_repository.full_name === `${owner}/${repo}`);
285-
286-
// Sort to ensure the latest workflow run is the first
287-
filtered.sort((a, b) => {
288-
return new Date(b.created_at).getTime() - new Date(a.created_at).getTime();
289-
});
290-
291-
// Store the first workflow run, to determine if this is the latest one...
292-
if (!latestWorkflowRun) {
293-
latestWorkflowRun = filtered[0];
294-
}
295-
296-
// Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for
297-
for (const workflowRun of filtered) {
298-
core.info(`Checking artifacts for workflow run: ${workflowRun.html_url}`);
299-
300-
const {
301-
data: { artifacts },
302-
} = await octokit.rest.actions.listWorkflowRunArtifacts({
303-
owner,
304-
repo,
305-
run_id: workflowRun.id,
306-
});
307-
308-
if (!artifacts) {
309-
core.warning(
310-
`Unable to fetch artifacts for branch: ${branch}, workflow: ${workflow_id}, workflowRunId: ${workflowRun.id}`,
311-
);
312-
} else {
313-
const foundArtifact = artifacts.find(({ name }) => name === artifactName);
314-
if (foundArtifact) {
315-
core.info(`Found suitable artifact XXX: ${foundArtifact.url}`);
316-
return {
317-
artifact: foundArtifact,
318-
workflowRun,
319-
isLatest: latestWorkflowRun.id === workflowRun.id,
320-
};
321-
} else {
322-
core.info(`No artifact found for ${artifactName}, trying next workflow run...`);
323-
}
324-
}
325-
}
326-
327-
if (currentPage > DEFAULT_MAX_PAGES) {
328-
core.warning(`Workflow ${workflow_id} not found in branch: ${branch}`);
329-
core.endGroup();
330-
return null;
331-
}
332-
333-
currentPage++;
334-
}
335-
336-
core.warning(`Artifact not found: ${artifactName}`);
337-
core.endGroup();
338-
return null;
339-
}
340-
341210
run();
342211

343212
/**

dev-packages/size-limit-gh-action/utils/size-limit-formatter.mjs renamed to dev-packages/size-limit-gh-action/utils/SizeLimitFormatter.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const EmptyResult = {
77
size: 0,
88
};
99

10-
export class SizeLimit {
10+
export class SizeLimitFormatter {
1111
formatBytes(size) {
1212
return bytes.format(size, { unitSeparator: ' ' });
1313
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import * as core from '@actions/core';
2+
3+
// max pages of workflows to pagination through
4+
const DEFAULT_MAX_PAGES = 50;
5+
// max results per page
6+
const DEFAULT_PAGE_LIMIT = 10;
7+
8+
/**
9+
* Fetch artifacts from a workflow run from a branch
10+
*
11+
* This is a bit hacky since GitHub Actions currently does not directly
12+
* support downloading artifacts from other workflows
13+
*/
14+
/**
15+
* Fetch artifacts from a workflow run from a branch
16+
*
17+
* This is a bit hacky since GitHub Actions currently does not directly
18+
* support downloading artifacts from other workflows
19+
*/
20+
export async function getArtifactsForBranchAndWorkflow(octokit, { owner, repo, workflowName, branch, artifactName }) {
21+
core.startGroup(`getArtifactsForBranchAndWorkflow - workflow:"${workflowName}", branch:"${branch}"`);
22+
23+
let repositoryWorkflow = null;
24+
25+
// For debugging
26+
const allWorkflows = [];
27+
28+
//
29+
// Find workflow id from `workflowName`
30+
//
31+
for await (const response of octokit.paginate.iterator(octokit.rest.actions.listRepoWorkflows, {
32+
owner,
33+
repo,
34+
})) {
35+
const targetWorkflow = response.data.find(({ name }) => name === workflowName);
36+
37+
allWorkflows.push(...response.data.map(({ name }) => name));
38+
39+
// If not found in responses, continue to search on next page
40+
if (!targetWorkflow) {
41+
continue;
42+
}
43+
44+
repositoryWorkflow = targetWorkflow;
45+
break;
46+
}
47+
48+
if (!repositoryWorkflow) {
49+
core.info(
50+
`Unable to find workflow with name "${workflowName}" in the repository. Found workflows: ${allWorkflows.join(
51+
', ',
52+
)}`,
53+
);
54+
core.endGroup();
55+
return null;
56+
}
57+
58+
const workflow_id = repositoryWorkflow.id;
59+
60+
let currentPage = 0;
61+
let latestWorkflowRun = null;
62+
63+
for await (const response of octokit.paginate.iterator(octokit.rest.actions.listWorkflowRuns, {
64+
owner,
65+
repo,
66+
workflow_id,
67+
branch,
68+
per_page: DEFAULT_PAGE_LIMIT,
69+
event: 'push',
70+
})) {
71+
if (!response.data.length) {
72+
core.warning(`Workflow ${workflow_id} not found in branch ${branch}`);
73+
core.endGroup();
74+
return null;
75+
}
76+
77+
// Do not allow downloading artifacts from a fork.
78+
const filtered = response.data.filter(workflowRun => workflowRun.head_repository.full_name === `${owner}/${repo}`);
79+
80+
// Sort to ensure the latest workflow run is the first
81+
filtered.sort((a, b) => {
82+
return new Date(b.created_at).getTime() - new Date(a.created_at).getTime();
83+
});
84+
85+
// Store the first workflow run, to determine if this is the latest one...
86+
if (!latestWorkflowRun) {
87+
latestWorkflowRun = filtered[0];
88+
}
89+
90+
// Search through workflow artifacts until we find a workflow run w/ artifact name that we are looking for
91+
for (const workflowRun of filtered) {
92+
core.info(`Checking artifacts for workflow run: ${workflowRun.html_url}`);
93+
94+
const {
95+
data: { artifacts },
96+
} = await octokit.rest.actions.listWorkflowRunArtifacts({
97+
owner,
98+
repo,
99+
run_id: workflowRun.id,
100+
});
101+
102+
if (!artifacts) {
103+
core.warning(
104+
`Unable to fetch artifacts for branch: ${branch}, workflow: ${workflow_id}, workflowRunId: ${workflowRun.id}`,
105+
);
106+
} else {
107+
const foundArtifact = artifacts.find(({ name }) => name === artifactName);
108+
if (foundArtifact) {
109+
core.info(`Found suitable artifact: ${foundArtifact.url}`);
110+
core.endGroup();
111+
return {
112+
artifact: foundArtifact,
113+
workflowRun,
114+
isLatest: latestWorkflowRun.id === workflowRun.id,
115+
};
116+
} else {
117+
core.info(`No artifact found for ${artifactName}, trying next workflow run...`);
118+
}
119+
}
120+
}
121+
122+
if (currentPage > DEFAULT_MAX_PAGES) {
123+
core.warning(`Workflow ${workflow_id} not found in branch: ${branch}`);
124+
core.endGroup();
125+
return null;
126+
}
127+
128+
currentPage++;
129+
}
130+
131+
core.warning(`Artifact not found: ${artifactName}`);
132+
core.endGroup();
133+
return null;
134+
}

0 commit comments

Comments
 (0)