Skip to content
Merged
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
83 changes: 34 additions & 49 deletions .github/workflows/pr-check-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ jobs:
with:
script: |
const fs = require('fs');
const path = require('path');

// 获取所有 artifacts
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});

console.log(`Found ${artifacts.data.artifacts.length} artifacts`);
console.log('Found artifacts:', artifacts.data.artifacts.length);

// 下载所有平台的状态文件
for (const artifact of artifacts.data.artifacts) {
if (artifact.name.startsWith('pr-check-state-')) {
console.log(`Downloading artifact: ${artifact.name}`);
console.log('Downloading:', artifact.name);

const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
Expand All @@ -44,7 +41,7 @@ jobs:
archive_format: 'zip',
});

fs.writeFileSync(`${artifact.name}.zip`, Buffer.from(download.data));
fs.writeFileSync(artifact.name + '.zip', Buffer.from(download.data));
}
}

Expand All @@ -66,49 +63,41 @@ jobs:
const path = require('path');

const marker = '<!-- duckcoding-pr-check -->';
const stateMarker = '<!-- duckcoding-pr-check-state:';
const platforms = ['ubuntu-22.04', 'windows-latest', 'macos-arm64', 'macos-x64'];

// 获取 PR 编号
const prNumber = context.payload.workflow_run.pull_requests[0]?.number;
if (!prNumber) {
console.log('No PR number found, skipping comment update');
console.log('No PR number found');
return;
}

console.log(`Updating comment for PR #${prNumber}`);

// 读取所有平台的状态
const defaultState = () =>
Object.fromEntries(
platforms.map((p) => [
p,
{
platform: p,
status: 'pending',
check: 'pending',
fix: 'pending',
recheck: 'pending',
artifact: '',
run_url: ''
}
])
);
console.log('Updating comment for PR', prNumber);

const defaultState = () => Object.fromEntries(
platforms.map(p => [p, {
platform: p,
status: 'pending',
check: 'pending',
fix: 'pending',
recheck: 'pending',
artifact: '',
run_url: ''
}])
);

let state = defaultState();

// 从 artifact 中读取各平台状态
const statesDir = 'states';
if (fs.existsSync(statesDir)) {
for (const platform of platforms) {
const stateFile = path.join(statesDir, `${platform}.json`);
const stateFile = path.join(statesDir, platform + '.json');
if (fs.existsSync(stateFile)) {
try {
const platformState = JSON.parse(fs.readFileSync(stateFile, 'utf8'));
state[platform] = platformState;
console.log(`Loaded state for ${platform}:`, platformState);
console.log('Loaded state for', platform);
} catch (e) {
console.error(`Failed to parse state for ${platform}:`, e);
console.error('Failed to parse state for', platform, e);
}
}
}
Expand All @@ -128,13 +117,11 @@ jobs:
return '❌ Still failing';
};

const detail = (entry) =>
entry.status === 'pending'
? '-'
: `check=${entry.check} / fix=${entry.fix} / recheck=${entry.recheck}`;
const detail = (entry) => entry.status === 'pending' ? '-' :
'check=' + entry.check + ' / fix=' + entry.fix + ' / recheck=' + entry.recheck;

const linkOrDash = (entry) => (entry.run_url ? `[日志](${entry.run_url})` : '-');
const artifactOrDash = (entry) => (entry.status === 'pending' ? '-' : entry.artifact || '-');
const linkOrDash = (entry) => entry.run_url ? '[日志](' + entry.run_url + ')' : '-';
const artifactOrDash = (entry) => entry.status === 'pending' ? '-' : (entry.artifact || '-');

const pLabel = (p) => {
if (p === 'ubuntu-22.04') return 'ubuntu-22.04';
Expand All @@ -145,14 +132,13 @@ jobs:
};

const renderTable = (labelFn, header) => {
const rows = platforms
.map((p) => {
const e = state[p] || defaultState()[p];
return `| ${pLabel(p)} | ${labelFn(e)} | ${detail(e)} | ${artifactOrDash(e)} | ${linkOrDash(e)} |`;
})
.join('\n');
const rows = platforms.map(p => {
const e = state[p] || defaultState()[p];
return '| ' + pLabel(p) + ' | ' + labelFn(e) + ' | ' + detail(e) + ' | ' + artifactOrDash(e) + ' | ' + linkOrDash(e) + ' |';
}).join('\n');

return [
`| ${header.platform} | ${header.status} | ${header.detail} | ${header.artifact} | ${header.link} |`,
'| ' + header.platform + ' | ' + header.status + ' | ' + header.detail + ' | ' + header.artifact + ' | ' + header.link + ' |',
'| --- | --- | --- | --- | --- |',
rows
].join('\n');
Expand Down Expand Up @@ -187,25 +173,24 @@ jobs:
'This comment auto-updates as each platform finishes:',
'If the first `npm run check` fails, run locally: `npm run check:fix` → `npm run check` and commit the fix.',
'If fix still fails, investigate locally and ensure `npm run check` passes before committing.',
'If cross-platform issues can't be reproduced, copy logs to an AI for hints.',
'If cross-platform issues can\'t be reproduced, copy logs to an AI for hints.',
'',
tableEn,
marker,
`${stateMarker}${JSON.stringify(state)} -->`
'<!-- duckcoding-pr-check-state:' + JSON.stringify(state) + ' -->'
].join('\n');

// 查找现有评论
const { data: comments } = await github.rest.issues.listComments({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100
});

const existing = comments.find((c) => c.body.includes(marker));
const existing = comments.find(c => c.body.includes(marker));

if (existing) {
console.log(`Updating existing comment #${existing.id}`);
console.log('Updating existing comment', existing.id);
await github.rest.issues.updateComment({
comment_id: existing.id,
owner: context.repo.owner,
Expand Down