Skip to content

Commit

Permalink
Use command line git diff --name-only base..head for diff strategy (#…
Browse files Browse the repository at this point in the history
…57)

* Loop through pages of files to handle more than 300 files

* Remove comment about defaulting to true when > 290 files

* Don't return true if files > 290

* Reimplement diff strategy to handle 300 files better

* Test

* Update dist folder so I can test

* Remove console.logs that were used for debugging

* feat: handle more than 300 files

Fix with git fetch

Co-authored-by: Saurav Aggarwal <[email protected]>

* feat: more than 300 files compared (fix build)

---------

Co-authored-by: Saurav Aggarwal <[email protected]>
  • Loading branch information
cwbusacker and saurav0705 authored Mar 18, 2024
1 parent 2f355f1 commit 09a4b8b
Show file tree
Hide file tree
Showing 8 changed files with 1,379 additions and 29 deletions.
1,317 changes: 1,304 additions & 13 deletions 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.

24 changes: 24 additions & 0 deletions dist/licenses.txt

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@actions/artifact": "^1.1.1",
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@actions/exec": "^1.1.1",
"@types/js-yaml": "^4.0.5",
"adm-zip": "^0.5.10",
"octokit": "^2.0.19",
Expand Down
4 changes: 2 additions & 2 deletions src/get-diff-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const getDiffFiles = async (
): Promise<ReturnTypeOfDiffFiles> => {
if (!hash[`${prevSha}--${currSha}`]) {
hash[`${prevSha}--${currSha}`] = await getFileDiffFromGithub({
base: prevSha,
head: currSha
baseSha: prevSha,
headSha: currSha
})
}
return Promise.resolve(hash[`${prevSha}--${currSha}`])
Expand Down
55 changes: 47 additions & 8 deletions src/github/get-diff-from-commit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as exec from '@actions/exec'
import {github} from './client'

type DiffFilesType = {
base: string
head: string
baseSha: string
headSha: string
}

export type ReturnTypeOfDiffFiles = {
Expand All @@ -11,18 +12,56 @@ export type ReturnTypeOfDiffFiles = {
}

export const getFileDiffFromGithub = async ({
base,
head
baseSha,
headSha
}: DiffFilesType): Promise<ReturnTypeOfDiffFiles> => {
console.log(`fetching file diff for ${base} ${head}`)
const base = await exec.getExecOutput('git' , ['fetch', 'origin', baseSha])
const head = await exec.getExecOutput('git' , ['fetch', 'origin', headSha])
const diff = await exec.getExecOutput(
'git',
[
'diff',
'--name-only',
'--ignore-submodules=all',
`--diff-filter=ACDMRTUX`,
`${baseSha}..${headSha}`
],
{
cwd: '.',
ignoreReturnCode: true,
silent: false
}
)

if (!diff.exitCode || !base.exitCode || !head.exitCode) {
const errors: string[] = []

if(!diff.exitCode){
errors.push( `Failed to get diff files between ${base}..${head} Exit code: ${diff.exitCode}. Due to error ${diff.stderr}`)
}

if(!base.exitCode){
errors.push( `Failed to fetch base: ${base} commit Exit code: ${base.exitCode}. Due to error ${base.stderr}`)
}

if(!head.exitCode){
errors.push( `Failed to fetch head: ${head} commit Exit code: ${head.exitCode}. Due to error ${head.stderr}`)
}

throw new Error(errors.join("\n"))
}

const allFiles = diff.stdout.split('\n').filter(Boolean)

// Get the html_url from this only
const resp = await github.client.rest.repos.compareCommits({
...github.getRequestConfig(),
base,
head
base: baseSha,
head: headSha
})

return {
files: resp.data.files?.map(item => item.filename) ?? [],
files: allFiles,
url: resp.data.html_url
}
}
2 changes: 0 additions & 2 deletions src/post-comment-on-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const makeSummaryForItem = (item: ArtifactFinalResponseStatus): string => {
${item.diffFiles ? '- Diff Files:\n' : ''}
${item.diffFiles?.map(file => `\`${file}\``).join('\n') ?? ''}
</details>
\`Note\`: If Changed Files > \`290\` check will by deafult return true
`
}

Expand Down
3 changes: 0 additions & 3 deletions src/regex-match-for-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ export type ArtifactFinalResponseStatus = ArtifactReponseTypeWithFileDiff & {
}

export const matchFile = (files: string[], pattern: string): boolean => {
if (files.length > 290) {
return true
}
const isMatch = picomatch(pattern)
for (const file of files) {
if (isMatch(file)) {
Expand Down

0 comments on commit 09a4b8b

Please sign in to comment.