Compare commits

..

5 Commits

Author SHA1 Message Date
shamoon
37409ee564 Fancier comment 2025-09-25 00:46:00 -07:00
shamoon
8d53c9cd36 Try unauthenticated 2025-09-25 00:46:00 -07:00
shamoon
f8189abd81 Remove codecov 2025-09-25 00:46:00 -07:00
shamoon
f203a79dbf Try this 2025-09-25 00:46:00 -07:00
shamoon
84ff073695 Try manual Codecov comments 2025-09-25 00:46:00 -07:00
3 changed files with 454 additions and 5 deletions

View File

@@ -322,6 +322,455 @@ jobs:
run: cd src-ui && pnpm exec playwright install
- name: Run Playwright e2e tests
run: cd src-ui && pnpm exec playwright test --shard ${{ matrix.shard-index }}/${{ matrix.shard-count }}
codecov-comment:
name: "Codecov PR Comment"
runs-on: ubuntu-24.04
needs:
- tests-backend
- tests-frontend
- tests-frontend-e2e
if: github.event_name == 'pull_request'
permissions:
contents: read
pull-requests: write
steps:
- name: Gather pull request context
id: pr
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
if (!pr) {
core.info('No associated pull request. Skipping.');
core.setOutput('shouldRun', 'false');
return;
}
core.setOutput('shouldRun', 'true');
core.setOutput('prNumber', pr.number.toString());
core.setOutput('headSha', pr.head.sha);
- name: Fetch Codecov coverage
id: coverage
if: steps.pr.outputs.shouldRun == 'true'
uses: actions/github-script@v7
env:
COMMIT_SHA: ${{ steps.pr.outputs.headSha }}
PR_NUMBER: ${{ steps.pr.outputs.prNumber }}
with:
script: |
const commitSha = process.env.COMMIT_SHA;
const prNumber = process.env.PR_NUMBER;
const owner = context.repo.owner;
const repo = context.repo.repo;
const service = 'gh';
const baseUrl = `https://api.codecov.io/api/v2/${service}/${owner}/repos/${repo}`;
const commitUrl = `${baseUrl}/commits/${commitSha}`;
const maxAttempts = 20;
const waitMs = 15000;
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
let data;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
core.info(`Fetching Codecov report (attempt ${attempt}/${maxAttempts})`);
let response;
try {
response = await fetch(commitUrl, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
});
} catch (error) {
core.warning(`Codecov fetch failed: ${error}. Waiting before retrying.`);
await sleep(waitMs);
continue;
}
if (response.status === 404) {
core.info('Report not ready yet (404). Waiting before retrying.');
await sleep(waitMs);
continue;
}
if ([429, 500, 502, 503, 504].includes(response.status)) {
const text = await response.text().catch(() => '');
core.info(`Codecov API transient error ${response.status}: ${text}. Waiting before retrying.`);
await sleep(waitMs);
continue;
}
if (!response.ok) {
const text = await response.text().catch(() => '');
core.warning(`Codecov API returned ${response.status}: ${text}. Skipping comment.`);
core.setOutput('shouldComment', 'false');
return;
}
data = await response.json().catch((error) => {
core.warning(`Failed to parse Codecov response: ${error}.`);
return undefined;
});
if (data && Object.keys(data).length > 0) {
break;
}
core.info('Report payload empty. Waiting before retrying.');
await sleep(waitMs);
}
if (!data && prNumber) {
core.info('Attempting to retrieve coverage from PR endpoint.');
const prUrl = `${baseUrl}/pulls/${prNumber}`;
let prResponse;
try {
prResponse = await fetch(prUrl, {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
});
} catch (error) {
core.warning(`Codecov PR fetch failed: ${error}.`);
}
if (prResponse) {
if ([429, 500, 502, 503, 504].includes(prResponse.status)) {
const text = await prResponse.text().catch(() => '');
core.info(`Codecov PR endpoint transient error ${prResponse.status}: ${text}.`);
} else if (!prResponse.ok) {
const text = await prResponse.text().catch(() => '');
core.warning(`Codecov PR endpoint returned ${prResponse.status}: ${text}.`);
} else {
const prData = await prResponse.json().catch((error) => {
core.warning(`Failed to parse Codecov PR response: ${error}.`);
return undefined;
});
if (prData?.latest_report) {
data = { report: prData.latest_report };
} else if (prData?.head_totals) {
const headTotals = prData.head_totals;
const baseTotals = prData.base_totals;
let compareTotals;
if (baseTotals && headTotals) {
const headCoverage = Number(headTotals.coverage);
const baseCoverage = Number(baseTotals.coverage);
if (Number.isFinite(headCoverage) && Number.isFinite(baseCoverage)) {
compareTotals = {
base_coverage: baseCoverage,
coverage_change: headCoverage - baseCoverage,
};
}
}
data = {
report: {
totals: headTotals,
compare: compareTotals ? { totals: compareTotals } : undefined,
totals_by_flag: [],
},
head_totals: headTotals,
base_totals: baseTotals,
};
} else {
data = prData;
}
}
}
}
if (!data) {
core.warning('Unable to retrieve Codecov report after multiple attempts.');
core.setOutput('shouldComment', 'false');
return;
}
const toNumber = (value) => {
if (value === null || value === undefined || value === '') {
return undefined;
}
const num = Number(value);
return Number.isFinite(num) ? num : undefined;
};
const reportData = data.report || data;
const totals = reportData.totals ?? data.head_totals ?? data.totals;
if (!totals) {
core.warning('Codecov response does not contain coverage totals.');
core.setOutput('shouldComment', 'false');
return;
}
let compareTotals = reportData.compare?.totals ?? data.compare?.totals;
if (!compareTotals && data.base_totals) {
const baseCoverageValue = toNumber(data.base_totals.coverage);
if (baseCoverageValue !== undefined) {
const headCoverageValue = toNumber((data.head_totals ?? {}).coverage);
compareTotals = {
base_coverage: baseCoverageValue,
coverage_change:
headCoverageValue !== undefined ? headCoverageValue - baseCoverageValue : undefined,
};
}
}
const coverage = toNumber(totals.coverage);
const baseCoverage = toNumber(compareTotals?.base_coverage ?? compareTotals?.base);
let delta = toNumber(
compareTotals?.coverage_change ??
compareTotals?.coverage_diff ??
totals.delta ??
totals.diff ??
totals.change,
);
if (delta === undefined && coverage !== undefined && baseCoverage !== undefined) {
delta = coverage - baseCoverage;
}
const formatPercent = (value) => {
if (value === undefined) return '—';
return `${value.toFixed(2)}%`;
};
const formatDelta = (value) => {
if (value === undefined) return '—';
const sign = value >= 0 ? '+' : '';
return `${sign}${value.toFixed(2)}%`;
};
const shortSha = commitSha.slice(0, 7);
const reportBaseUrl = `https://app.codecov.io/gh/${owner}/${repo}`;
const commitReportUrl = `${reportBaseUrl}/commit/${commitSha}?src=pr&el=comment`;
const prReportUrl = prNumber
? `${reportBaseUrl}/pull/${prNumber}?src=pr&el=comment`
: commitReportUrl;
const findBaseCommitSha = () =>
data?.report?.compare?.base_commitid ??
data?.report?.compare?.base?.commitid ??
data?.report?.base_commitid ??
data?.compare?.base_commitid ??
data?.compare?.base?.commitid ??
data?.base_commitid ??
data?.base?.commitid;
const baseCommitSha = findBaseCommitSha();
const baseCommitUrl = baseCommitSha
? `${reportBaseUrl}/commit/${baseCommitSha}?src=pr&el=comment`
: undefined;
const baseShortSha = baseCommitSha ? baseCommitSha.slice(0, 7) : undefined;
const lines = ['<!-- codecov-coverage-comment -->'];
lines.push(`## [Codecov](${prReportUrl}) Report`);
lines.push('');
if (coverage !== undefined) {
lines.push(`:white_check_mark: Project coverage for \`${shortSha}\` is ${formatPercent(coverage)}.`);
} else {
lines.push(':warning: Coverage for the head commit is unavailable.');
}
if (baseCoverage !== undefined) {
const changeEmoji = delta === undefined ? ':grey_question:' : delta >= 0 ? ':white_check_mark:' : ':small_red_triangle_down:';
const baseCoverageText = `Base${baseShortSha ? ` \`${baseShortSha}\`` : ''} ${formatPercent(baseCoverage)}`;
const baseLink = baseCommitUrl ? `[${baseCoverageText}](${baseCommitUrl})` : baseCoverageText;
const changeText =
delta !== undefined
? `${baseLink} (${formatDelta(delta)})`
: `${baseLink} (change unknown)`;
lines.push(`${changeEmoji} ${changeText}.`);
}
lines.push(`:clipboard: [View full report on Codecov](${commitReportUrl}).`);
const normalizeTotals = (value) => {
if (!value) return undefined;
if (value.totals && typeof value.totals === 'object') return value.totals;
return value;
};
const headTotals = normalizeTotals(totals) ?? {};
const baseTotals =
normalizeTotals(data.base_totals) ??
normalizeTotals(reportData.base_totals) ??
normalizeTotals(reportData.compare?.base_totals) ??
normalizeTotals(reportData.compare?.base);
const formatInteger = (value) => {
if (value === undefined) return '—';
return value.toLocaleString('en-US');
};
const formatIntegerDelta = (value) => {
if (value === undefined) return '—';
const sign = value >= 0 ? '+' : '';
return `${sign}${value.toLocaleString('en-US')}`;
};
const getInteger = (value) => {
const num = toNumber(value);
return Number.isFinite(num) ? Math.round(num) : undefined;
};
const metrics = [];
metrics.push({
label: 'Coverage',
base: baseCoverage,
head: coverage,
diff: delta,
format: formatPercent,
formatDiff: formatDelta,
});
const pushIntegerMetric = (label, headValueRaw, baseValueRaw) => {
const headValue = getInteger(headValueRaw);
const baseValue = getInteger(baseValueRaw);
if (headValue === undefined && baseValue === undefined) {
return;
}
const diff = headValue !== undefined && baseValue !== undefined ? headValue - baseValue : undefined;
metrics.push({
label,
base: baseValue,
head: headValue,
diff,
format: formatInteger,
formatDiff: formatIntegerDelta,
});
};
pushIntegerMetric('Files', headTotals.files, baseTotals?.files);
pushIntegerMetric('Lines', headTotals.lines, baseTotals?.lines);
pushIntegerMetric('Branches', headTotals.branches, baseTotals?.branches);
pushIntegerMetric('Hits', headTotals.hits, baseTotals?.hits);
pushIntegerMetric('Misses', headTotals.misses, baseTotals?.misses);
const hasMetricData = metrics.some((metric) => metric.base !== undefined || metric.head !== undefined);
if (hasMetricData) {
lines.push('');
lines.push('<details><summary>Coverage summary</summary>');
lines.push('');
lines.push('| Metric | Base | Head | Δ |');
lines.push('| --- | --- | --- | --- |');
for (const metric of metrics) {
const baseValue = metric.base !== undefined ? metric.format(metric.base) : '—';
const headValue = metric.head !== undefined ? metric.format(metric.head) : '—';
const diffValue = metric.diff !== undefined ? metric.formatDiff(metric.diff) : '—';
lines.push(`| ${metric.label} | ${baseValue} | ${headValue} | ${diffValue} |`);
}
lines.push('');
lines.push('</details>');
}
const normalizeEntries = (raw) => {
if (!raw) return [];
if (Array.isArray(raw)) return raw;
if (typeof raw === 'object') {
return Object.entries(raw).map(([name, totals]) => ({ name, ...(typeof totals === 'object' ? totals : { coverage: totals }) }));
}
return [];
};
const buildTableRows = (entries) => {
const rows = [];
for (const entry of entries) {
const label = entry.flag ?? entry.name ?? entry.component ?? entry.id;
const entryTotals = entry.totals ?? entry;
const entryCoverage = toNumber(entryTotals?.coverage);
if (!label || entryCoverage === undefined) {
continue;
}
const entryDelta = toNumber(
entryTotals?.coverage_change ??
entryTotals?.coverage_diff ??
entryTotals?.delta ??
entryTotals?.diff ??
entryTotals?.change,
);
const coverageText = entryCoverage !== undefined ? `\`${formatPercent(entryCoverage)}\`` : '—';
const deltaText = entryDelta !== undefined ? `\`${formatDelta(entryDelta)}\`` : '—';
rows.push(`| ${label} | ${coverageText} | ${deltaText} |`);
}
return rows;
};
const componentEntries = normalizeEntries(reportData.components ?? data.components);
const flagEntries = normalizeEntries(reportData.totals_by_flag ?? data.totals_by_flag);
if (componentEntries.length) {
const componentsLink = prNumber
? `${reportBaseUrl}/pull/${prNumber}/components?src=pr&el=components`
: `${commitReportUrl}`;
const componentRows = buildTableRows(componentEntries);
if (componentRows.length) {
lines.push('');
lines.push(`[Components report](${componentsLink})`);
lines.push('');
lines.push('| Component | Coverage | Δ |');
lines.push('| --- | --- | --- |');
lines.push(...componentRows);
}
}
if (flagEntries.length) {
const flagsLink = prNumber
? `${reportBaseUrl}/pull/${prNumber}/flags?src=pr&el=flags`
: `${commitReportUrl}`;
const flagRows = buildTableRows(flagEntries);
if (flagRows.length) {
lines.push('');
lines.push(`[Flags report](${flagsLink})`);
lines.push('');
lines.push('| Flag | Coverage | Δ |');
lines.push('| --- | --- | --- |');
lines.push(...flagRows);
}
}
const commentBody = lines.join('\n');
const shouldComment = coverage !== undefined;
core.setOutput('shouldComment', shouldComment ? 'true' : 'false');
if (shouldComment) {
core.setOutput('commentBody', commentBody);
}
- name: Upsert coverage comment
if: steps.pr.outputs.shouldRun == 'true' && steps.coverage.outputs.shouldComment == 'true'
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ steps.pr.outputs.prNumber }}
COMMENT_BODY: ${{ steps.coverage.outputs.commentBody }}
with:
script: |
const prNumber = Number(process.env.PR_NUMBER);
const body = process.env.COMMENT_BODY;
const marker = '<!-- codecov-coverage-comment -->';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
});
const existing = comments.find((comment) => comment.body?.includes(marker));
if (existing) {
core.info(`Updating existing coverage comment (id: ${existing.id}).`);
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
core.info('Creating new coverage comment.');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}
frontend-bundle-analysis:
name: "Frontend Bundle Analysis"
runs-on: ubuntu-24.04

View File

@@ -30,7 +30,7 @@ dependencies = [
"django-cachalot~=2.8.0",
"django-celery-results~=2.6.0",
"django-compression-middleware~=0.5.0",
"django-cors-headers~=4.9.0",
"django-cors-headers~=4.8.0",
"django-extensions~=4.1",
"django-filter~=25.1",
"django-guardian~=3.1.2",

8
uv.lock generated
View File

@@ -730,15 +730,15 @@ wheels = [
[[package]]
name = "django-cors-headers"
version = "4.9.0"
version = "4.8.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "asgiref", marker = "sys_platform == 'darwin' or sys_platform == 'linux'" },
{ name = "django", marker = "sys_platform == 'darwin' or sys_platform == 'linux'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/21/39/55822b15b7ec87410f34cd16ce04065ff390e50f9e29f31d6d116fc80456/django_cors_headers-4.9.0.tar.gz", hash = "sha256:fe5d7cb59fdc2c8c646ce84b727ac2bca8912a247e6e68e1fb507372178e59e8", size = 21458, upload-time = "2025-09-18T10:40:52.326Z" }
sdist = { url = "https://files.pythonhosted.org/packages/89/8e/6225441edcfe179bf4861e9e67489e33375e0b66316c8d7b9edaae863d37/django_cors_headers-4.8.0.tar.gz", hash = "sha256:0a12a2efcd59a3cea741e44db8ab589e929949de5bc4cdf35a29c6ae77297686", size = 21425, upload-time = "2025-09-08T15:58:05.34Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/30/d8/19ed1e47badf477d17fb177c1c19b5a21da0fd2d9f093f23be3fb86c5fab/django_cors_headers-4.9.0-py3-none-any.whl", hash = "sha256:15c7f20727f90044dcee2216a9fd7303741a864865f0c3657e28b7056f61b449", size = 12809, upload-time = "2025-09-18T10:40:50.843Z" },
{ url = "https://files.pythonhosted.org/packages/ac/b3/29ef49d6ff7800f323f3d98cde7777b3cfdda133de8feea84cffafea4578/django_cors_headers-4.8.0-py3-none-any.whl", hash = "sha256:3b883f4c6d07848673218456a5e070d8ab51f97341c1f27d0242ca167e7272ab", size = 12804, upload-time = "2025-09-08T15:58:03.882Z" },
]
[[package]]
@@ -2182,7 +2182,7 @@ requires-dist = [
{ name = "django-cachalot", specifier = "~=2.8.0" },
{ name = "django-celery-results", specifier = "~=2.6.0" },
{ name = "django-compression-middleware", specifier = "~=0.5.0" },
{ name = "django-cors-headers", specifier = "~=4.9.0" },
{ name = "django-cors-headers", specifier = "~=4.8.0" },
{ name = "django-extensions", specifier = "~=4.1" },
{ name = "django-filter", specifier = "~=25.1" },
{ name = "django-guardian", specifier = "~=3.1.2" },