ci: 添加构建结果评论中的下载链接和获取 artifacts 列表功能

This commit is contained in:
时瑾
2025-12-29 03:14:17 +08:00
parent e6dbc7b9ab
commit e0f88a54f6
3 changed files with 59 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ export interface BuildTarget {
name: string;
status: BuildStatus;
error?: string;
downloadUrl?: string; // Artifact 直接下载链接
}
// ============== 辅助函数 ==============
@@ -70,8 +71,8 @@ export function generateResultComment (
runId: string,
repository: string
): string {
const artifactUrl = `https://github.com/${repository}/actions/runs/${runId}/artifacts`;
const logUrl = `https://github.com/${repository}/actions/runs/${runId}`;
// 链接到 run 详情页,页面底部有 Artifacts 下载区域
const runUrl = `https://github.com/${repository}/actions/runs/${runId}`;
const allSuccess = targets.every(t => t.status === 'success');
const anyCancelled = targets.some(t => t.status === 'cancelled');
@@ -82,8 +83,14 @@ export function generateResultComment (
? '⚪ 构建已取消'
: '❌ 构建失败';
const downloadLink = (status: BuildStatus) =>
status === 'success' ? `[📦 下载](${artifactUrl})` : '—';
const downloadLink = (target: BuildTarget) => {
if (target.status !== 'success') return '—';
if (target.downloadUrl) {
return `[📦 下载](${target.downloadUrl})`;
}
// 回退到 run 详情页
return `[📦 下载](${runUrl}#artifacts)`;
};
const lines: string[] = [
COMMENT_MARKER,
@@ -91,12 +98,12 @@ export function generateResultComment (
'',
'| 构建目标 | 状态 | 下载 |',
'| :--- | :--- | :--- |',
...targets.map(t => `| ${t.name} | ${getStatusIcon(t.status)} | ${downloadLink(t.status)} |`),
...targets.map(t => `| ${t.name} | ${getStatusIcon(t.status)} | ${downloadLink(t)} |`),
'',
'---',
'',
`📝 **提交**: \`${formatSha(prSha)}\``,
`🔗 **构建日志**: [查看详情](${logUrl})`,
`🔗 **构建日志**: [查看详情](${runUrl})`,
];
// 添加错误详情

View File

@@ -24,7 +24,14 @@ export interface Repository {
};
}
// ============== GitHub API Client ==============
export interface Artifact {
id: number;
name: string;
size_in_bytes: number;
archive_download_url: string;
}
// ============== GitHub API Client ==========================
export class GitHubAPI {
private token: string;
@@ -76,6 +83,13 @@ export class GitHubAPI {
}
}
async getRunArtifacts (owner: string, repo: string, runId: string): Promise<Artifact[]> {
const data = await this.request<{ artifacts: Artifact[]; }>(
`/repos/${owner}/${repo}/actions/runs/${runId}/artifacts`
);
return data.artifacts;
}
async createComment (owner: string, repo: string, issueNumber: number, body: string): Promise<void> {
await this.request(`/repos/${owner}/${repo}/issues/${issueNumber}/comments`, {
method: 'POST',