Skip to content

Commit

Permalink
fix: fallback to author date and current date
Browse files Browse the repository at this point in the history
Signed-off-by: Trim21 <[email protected]>
  • Loading branch information
trim21 committed Nov 19, 2024
1 parent 359e915 commit c8b9761
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

12 changes: 11 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,22 @@ 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,
owner: GitHub.context.repo.owner,
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();
}

0 comments on commit c8b9761

Please sign in to comment.