From 861d98a3bdeb4420f7cb62a8417e3e311ca450cf Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 19 Nov 2024 16:57:38 +0100 Subject: [PATCH] commiter_date: fix github api request fallback Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __mocks__/@actions/github.ts | 16 +++++++++++++++- src/context.ts | 21 ++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/__mocks__/@actions/github.ts b/__mocks__/@actions/github.ts index 14df365..a36ff9c 100644 --- a/__mocks__/@actions/github.ts +++ b/__mocks__/@actions/github.ts @@ -205,5 +205,19 @@ export const context = { }; 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' + } + } + } + }) + ) + } + } })); diff --git a/src/context.ts b/src/context.ts index f2b7348..7d69c53 100644 --- a/src/context.ts +++ b/src/context.ts @@ -119,11 +119,18 @@ async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise } // fallback to github api for commit date - const commit = await toolkit.github.octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', { - commit_sha: sha, - owner: GitHub.context.repo.owner, - repo: GitHub.context.repo.repo - }); - - return new Date(commit.data.committer.date); + try { + const commit = await toolkit.github.octokit.rest.repos.getCommit({ + owner: GitHub.context.repo.owner, + repo: GitHub.context.repo.repo, + ref: sha + }); + if (commit.data.commit.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(); + } }