【错误:"request sandbox failed err:Cannot read properties of undefined (reading '0')"】

// 在这里,您可以通过 'params' 获取节点中的输入变量,并通过 'ret' 输出结果
// 'params' 已经被正确地注入到环境中
async function main({ arguments }: Args): Promise<Output> {
const {
gogsUrl, // 你的 Gogs 服务器地址,例如:https://gogs.your-company.com
owner, // 仓库所属用户名或组织名
repo, // 仓库名称
personalToken, // Gogs 个人访问令牌
startDate, // 开始日期 (由上游节点传入,格式:YYYY-MM-DD)
endDate // 结束日期 (由上游节点传入,格式:YYYY-MM-DD)
} = arguments[0];
// 构建 API URL - 获取仓库的提交记录 [citation:1]
// Gogs API 端点格式:/api/v1/repos/{owner}/{repo}/commits
const apiUrl = `${gogsUrl}/api/v1/repos/${owner}/${repo}/commits`;
// 构建请求参数(获取指定时间范围内的提交)
// 注意:Gogs API 支持 since 和 until 参数来过滤时间范围
const params = new URLSearchParams({
// 由于 API 返回的是所有分支的提交,你可能需要根据实际情况调整
// 如果只需要某个分支,可以添加:sha: 'branch-name'
});
// 添加时间过滤参数(格式需要是 ISO 8601)
// Gogs API 接受 since 和 until 参数 [citation:1]
if (startDate) {
params.append('since', `${startDate}T00:00:00Z`);
}
if (endDate) {
params.append('until', `${endDate}T23:59:59Z`);
}
const urlWithParams = `${apiUrl}?${params.toString()}`;
// 发送请求到 Gogs API
try {
const response = await fetch(urlWithParams, {
method: 'GET',
headers: {
// Gogs 支持使用个人访问令牌进行认证 [citation:5]
'Authorization': `token ${personalToken}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`Gogs API 请求失败,状态码:${response.status} - ${response.statusText}`);
}
const commits = await response.json();
// 将 Gogs 返回的提交数据转换为周报可用的格式
const worklogs = commits.map(commit => {
// Gogs 返回的 commit 对象结构示例 [citation:1]
return {
date: commit.commit.author.date.split('T')[0], // 提取日期部分
author: commit.commit.author.name,
email: commit.commit.author.email,
message: commit.commit.message,
hash: commit.sha.substring(0, 7), // 短哈希
url: commit.html_url,
// 可以从 commit.message 中提取更多信息,比如:
// - 如果是符合 Conventional Commits 规范的消息,可以解析出类型和范围
// - 可以提取关联的 issue 编号等
};
});
// 添加一些统计信息
const result = {
success: true,
summary: `共获取 ${worklogs.length} 条提交记录`,
worklogs: worklogs,
// 按日期分组,方便后续处理
groupedByDate: worklogs.reduce((groups, log) => {
const date = log.date;
if (!groups[date]) {
groups[date] = [];
}
groups[date].push(log);
return groups;
}, {}),
// 统计文件变更(如果需要更详细的数据,可以额外调用 /commits/{sha} 获取)
stats: {
totalCommits: worklogs.length,
dateRange: `${startDate} 至 ${endDate}`
}
};
// 如果没有获取到数据,可以返回提示
if (worklogs.length === 0) {
result.message = '本周暂无提交记录';
}
return result;
} catch (error) {
console.error('获取 Gogs 提交记录失败:', error);
// 返回错误信息,让工作流可以处理错误情况
return {
success: false,
error: error.message,
worklogs: []
};
}
}