fix: fallback to author date and current date

Signed-off-by: Trim21 <trim21.me@gmail.com>
This commit is contained in:
Trim21 2024-11-19 23:51:15 +08:00
parent 359e915ab3
commit c8b97615be
No known key found for this signature in database
GPG Key ID: 809F01CFB0A797FB
4 changed files with 14 additions and 4 deletions

View File

@ -205,5 +205,5 @@ export const context = {
};
export const getOctokit = jest.fn(() => ({
request: () => Promise.resolve({data: {committer: {date: '2024-11-13T13:42:28Z'}}})
request: () => Promise.resolve({data: {commit: {committer: {date: '2024-11-13T13:42:28Z'}}}})
}));

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -118,6 +118,8 @@ async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise
}
}
core.debug(`fetch commit ${sha} metadata from ${GitHub.context.repo.owner}/${GitHub.context.repo.repo}`);
// fallback to github api for commit date
const commit = await toolkit.github.octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
commit_sha: sha,
@ -125,5 +127,13 @@ async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise
repo: GitHub.context.repo.repo
});
return new Date(commit.data.committer.date);
const apiCommitDate = commit.data.commit.committer?.date ?? commit.data.commit.author?.date;
if (apiCommitDate) {
return new Date(apiCommitDate);
}
core.debug(`failed to find commit date, ${JSON.stringify(commit.data)}`);
core.warning(`failed to get commit date for ${sha}, fallback to current date`);
return new Date();
}