Skip to content

Commit

Permalink
Merge pull request #478 from crazy-max/commit-date-request
Browse files Browse the repository at this point in the history
commiter_date: fix github api request fallback
  • Loading branch information
crazy-max authored Nov 19, 2024
2 parents 359e915 + e01ddd3 commit 8cb0002
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
16 changes: 15 additions & 1 deletion __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
})
)
}
}
}));
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 8cb0002

Please sign in to comment.