Skip to content

Commit 2408682

Browse files
CfirTsabarildez
authored andcommitted
fix: compatible working-directory+only-new-issues
1 parent 5e67631 commit 2408682

File tree

4 files changed

+214
-15
lines changed

4 files changed

+214
-15
lines changed

dist/post_run/index.js

Lines changed: 84 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/run/index.js

Lines changed: 84 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/run.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { promisify } from "util"
88

99
import { restoreCache, saveCache } from "./cache"
1010
import { installLint, InstallMode } from "./install"
11+
import { alterDiffFile } from "./utils/diffUtils"
1112
import { findLintVersion } from "./version"
1213

1314
const execShellCommand = promisify(exec)
@@ -68,7 +69,7 @@ async function fetchPatch(): Promise<string> {
6869
const tempDir = await createTempDir()
6970
const patchPath = path.join(tempDir, "pull.patch")
7071
core.info(`Writing patch to ${patchPath}`)
71-
await writeFile(patchPath, patch)
72+
await writeFile(patchPath, alterDiffFile(patch))
7273
return patchPath
7374
} catch (err) {
7475
console.warn(`failed to save pull request patch:`, err)
@@ -157,10 +158,6 @@ async function runLint(lintPath: string, patchPath: string): Promise<void> {
157158
const workingDirectory = core.getInput(`working-directory`)
158159
const cmdArgs: ExecOptions = {}
159160
if (workingDirectory) {
160-
if (patchPath) {
161-
// TODO: make them compatible
162-
throw new Error(`options working-directory and only-new-issues aren't compatible`)
163-
}
164161
if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) {
165162
throw new Error(`working-directory (${workingDirectory}) was not a path`)
166163
}

src/utils/diffUtils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as core from "@actions/core"
2+
import * as path from "path"
3+
4+
// If needed alter diff file to be compatible with working directory
5+
export function alterDiffFile(diffFile: string): string {
6+
let workingDirectory = core.getInput(`working-directory`)
7+
if (workingDirectory) {
8+
const workspace = process.env["GITHUB_WORKSPACE"] || ""
9+
const relativeFile = path.relative(workspace, workingDirectory)
10+
workingDirectory = relativeFile
11+
12+
const diffLines = diffFile.split("\n")
13+
let ignore = false
14+
const filteredDiffLines = []
15+
16+
for (const line of diffLines) {
17+
if (line.startsWith("diff --git")) {
18+
if (line.includes(`a/${workingDirectory}/`)) {
19+
ignore = false
20+
filteredDiffLines.push(line.replace(` a/${workingDirectory}/`, " a/").replace(` b/${workingDirectory}/`, " b/"))
21+
} else {
22+
ignore = true
23+
}
24+
} else {
25+
if (!ignore) {
26+
if (line.startsWith(`--- a/${workingDirectory}/`)) {
27+
filteredDiffLines.push(line.replace(`--- a/${workingDirectory}/`, "--- a/"))
28+
} else if (line.startsWith(`+++ a/${workingDirectory}/`)) {
29+
filteredDiffLines.push(line.replace(`+++ a/${workingDirectory}/`, "+++ a/"))
30+
} else if (line.startsWith(`--- b/${workingDirectory}/`)) {
31+
filteredDiffLines.push(line.replace(`--- b/${workingDirectory}/`, "--- b/"))
32+
} else if (line.startsWith(`+++ b/${workingDirectory}/`)) {
33+
filteredDiffLines.push(line.replace(`+++ b/${workingDirectory}/`, "+++ b/"))
34+
} else {
35+
filteredDiffLines.push(line)
36+
}
37+
}
38+
}
39+
}
40+
// Join the modified lines back into a diff string
41+
diffFile = filteredDiffLines.join("\n")
42+
}
43+
return diffFile
44+
}

0 commit comments

Comments
 (0)