Skip to content

Commit 861d98a

Browse files
committed
commiter_date: fix github api request fallback
Signed-off-by: CrazyMax <[email protected]>
1 parent 359e915 commit 861d98a

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

__mocks__/@actions/github.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,19 @@ export const context = {
205205
};
206206

207207
export const getOctokit = jest.fn(() => ({
208-
request: () => Promise.resolve({data: {committer: {date: '2024-11-13T13:42:28Z'}}})
208+
rest: {
209+
repos: {
210+
getCommit: jest.fn(() =>
211+
Promise.resolve({
212+
data: {
213+
commit: {
214+
committer: {
215+
date: '2024-11-13T13:42:28Z'
216+
}
217+
}
218+
}
219+
})
220+
)
221+
}
222+
}
209223
}));

src/context.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,18 @@ async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise
119119
}
120120

121121
// fallback to github api for commit date
122-
const commit = await toolkit.github.octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
123-
commit_sha: sha,
124-
owner: GitHub.context.repo.owner,
125-
repo: GitHub.context.repo.repo
126-
});
127-
128-
return new Date(commit.data.committer.date);
122+
try {
123+
const commit = await toolkit.github.octokit.rest.repos.getCommit({
124+
owner: GitHub.context.repo.owner,
125+
repo: GitHub.context.repo.repo,
126+
ref: sha
127+
});
128+
if (commit.data.commit.committer?.date) {
129+
return new Date(commit.data.commit.committer.date);
130+
}
131+
throw new Error('Committer date not found');
132+
} catch (error) {
133+
core.debug(`Failed to get commit date from GitHub API: ${error.message}`);
134+
return new Date();
135+
}
129136
}

0 commit comments

Comments
 (0)