Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
improve diff files filtering logic (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
harjotgill authored Apr 21, 2023
1 parent 96d81ca commit 3891f38
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
30 changes: 23 additions & 7 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions src/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,46 @@ export const codeReview = async (
core.info(`Will review from commit: ${highest_reviewed_commit_id}`)
}

// get the list of files changed between the highest reviewed commit
// and the latest (head) commit
// use octokit.pulls.compareCommits to get the list of files changed
// between the highest reviewed commit and the latest (head) commit
const diff = await octokit.repos.compareCommits({
// Fetch the diff between the highest reviewed commit and the latest commit of the PR branch
const incrementalDiff = await octokit.repos.compareCommits({
owner: repo.owner,
repo: repo.repo,
base: highest_reviewed_commit_id,
head: context.payload.pull_request.head.sha
})

const {files, commits} = diff.data
// Fetch the diff between the target branch's base commit and the latest commit of the PR branch
const targetBranchDiff = await octokit.repos.compareCommits({
owner: repo.owner,
repo: repo.repo,
base: context.payload.pull_request.base.sha,
head: context.payload.pull_request.head.sha
})

const incrementalFiles = incrementalDiff.data.files
const targetBranchFiles = targetBranchDiff.data.files

if (!incrementalFiles || !targetBranchFiles) {
core.warning(`Skipped: files data is missing`)
return
}
// Filter out any file that is not changed compared to the target branch
const files = incrementalFiles.filter(incrementalChange =>
targetBranchFiles.some(
changeRelativeToTargetBranch =>
changeRelativeToTargetBranch.filename === incrementalChange.filename
)
)

if (!files) {
core.warning(`Skipped: diff.data.files is null`)
core.warning(`Skipped: files is null`)
return
}

const commits = incrementalDiff.data.commits

if (!commits) {
core.warning(`Skipped: commits is null`)
return
}

Expand Down

0 comments on commit 3891f38

Please sign in to comment.