commiter_date: fix github api request fallback

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-11-19 16:57:38 +01:00
parent 359e915ab3
commit 861d98a3bd
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
2 changed files with 29 additions and 8 deletions

View File

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

View File

@ -119,11 +119,18 @@ async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise
} }
// fallback to github api for commit date // fallback to github api for commit date
const commit = await toolkit.github.octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', { try {
commit_sha: sha, const commit = await toolkit.github.octokit.rest.repos.getCommit({
owner: GitHub.context.repo.owner, owner: GitHub.context.repo.owner,
repo: GitHub.context.repo.repo repo: GitHub.context.repo.repo,
ref: sha
}); });
if (commit.data.commit.committer?.date) {
return new Date(commit.data.committer.date); return new Date(commit.data.commit.committer.date);
}
throw new Error('Committer date not found');
} catch (error) {
core.debug(`Failed to get commit date from GitHub API: ${error.message}`);
return new Date();
}
} }