Skip to content

Commit 40fd916

Browse files
fix(COD-6990): get the modified files compared to HEAD^1 (#265)
1 parent 7fd105a commit 40fd916

2 files changed

Lines changed: 8 additions & 21 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function runAnalysis() {
4848
// Only pass modified files for PR "new" scans — this optimises scanning to only changed files
4949
let modifiedFiles: string | undefined
5050
if (currBranch !== '' && target === 'new') {
51-
modifiedFiles = getModifiedFiles()
51+
modifiedFiles = await getModifiedFiles()
5252
if (modifiedFiles) {
5353
info(`Modified files for optimised scanning: ${modifiedFiles}`)
5454
}

src/util.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { error, getInput, info, isDebug } from '@actions/core'
22
import { context } from '@actions/github'
3-
import { spawn, spawnSync } from 'child_process'
3+
import { spawn } from 'child_process'
44
import { existsSync, readFileSync, mkdirSync, writeFileSync } from 'fs'
55
import * as os from 'os'
66
import * as path from 'path'
7+
import { simpleGit } from 'simple-git'
78

89
// Gather GITHUB_* and CI env vars for the lacework iac binary to read directly
910
function gatherGitHubEnvVars(): string[] {
@@ -115,29 +116,15 @@ export function generateUILink() {
115116
return url
116117
}
117118

118-
export function getModifiedFiles(): string | undefined {
119-
const eventPath = process.env.GITHUB_EVENT_PATH
120-
if (!eventPath) return undefined
121-
122-
let eventData: any
119+
export async function getModifiedFiles(): Promise<string | undefined> {
123120
try {
124-
eventData = JSON.parse(readFileSync(eventPath, 'utf8'))
121+
const diff = await simpleGit().diff(['--name-only', 'HEAD^1...HEAD'])
122+
const files = diff.trim().split('\n').filter(Boolean).join(',')
123+
return files || undefined
125124
} catch (e) {
126-
info(`Failed to parse GitHub event file: ${e}`)
127-
return undefined
128-
}
129-
130-
const baseSha = eventData.pull_request?.base?.sha
131-
if (!baseSha) return undefined
132-
133-
const result = spawnSync('git', ['diff', '--name-only', `${baseSha}...HEAD`])
134-
if (result.status !== 0) {
135-
info(`Failed to get modified files: ${result.stderr?.toString()}`)
125+
info(`Failed to get modified files: ${e}`)
136126
return undefined
137127
}
138-
139-
const files = result.stdout.toString().trim().split('\n').filter(Boolean).join(',')
140-
return files || undefined
141128
}
142129

143130
export function shouldRunIaCScanner(modifiedFiles: string): boolean {

0 commit comments

Comments
 (0)