Skip to content

Commit 47ff131

Browse files
fix: using compareCommits for push event commit query (wagoid#801)
* fix: fixed logic in push event API request to only get commits from before to after event * fix: getting events from first push using push event payload
1 parent a2bc521 commit 47ff131

File tree

3 files changed

+352
-96
lines changed

3 files changed

+352
-96
lines changed

src/action.mjs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const pullRequestTargetEvent = 'pull_request_target'
1212
const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]
1313

1414
const { GITHUB_EVENT_NAME } = process.env
15+
const FIRST_COMMIT_SHA = '0000000000000000000000000000000000000000'
1516

1617
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
1718

@@ -22,13 +23,30 @@ const getCommitDepth = () => {
2223
return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0)
2324
}
2425

25-
const getPushEventCommits = () => {
26-
const mappedCommits = eventContext.payload.commits.map((commit) => ({
27-
message: commit.message,
28-
hash: commit.id,
29-
}))
26+
const getPushEventCommits = async () => {
27+
const octokit = getOctokit(getInput('token'))
28+
const { owner, repo } = eventContext.issue
29+
const { before, after } = eventContext.payload
30+
31+
if (before === FIRST_COMMIT_SHA) {
32+
return eventContext.payload.commits.map((commit) => ({
33+
message: commit.message,
34+
hash: commit.id,
35+
}))
36+
}
3037

31-
return mappedCommits
38+
const { data: comparison } = await octokit.rest.repos.compareCommits({
39+
owner,
40+
repo,
41+
head: after,
42+
base: before,
43+
per_page: 100,
44+
})
45+
46+
return comparison.commits.map((commit) => ({
47+
message: commit.commit.message,
48+
hash: commit.sha,
49+
}))
3250
}
3351

3452
const getPullRequestEventCommits = async () => {

0 commit comments

Comments
 (0)