Skip to content

Commit

Permalink
fix: compatible working-directory+only-new-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
CfirTsabari authored and ldez committed Aug 14, 2023
1 parent 5e67631 commit 2408682
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 15 deletions.
89 changes: 84 additions & 5 deletions dist/post_run/index.js

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

89 changes: 84 additions & 5 deletions dist/run/index.js

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

7 changes: 2 additions & 5 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { promisify } from "util"

import { restoreCache, saveCache } from "./cache"
import { installLint, InstallMode } from "./install"
import { alterDiffFile } from "./utils/diffUtils"
import { findLintVersion } from "./version"

const execShellCommand = promisify(exec)
Expand Down Expand Up @@ -68,7 +69,7 @@ async function fetchPatch(): Promise<string> {
const tempDir = await createTempDir()
const patchPath = path.join(tempDir, "pull.patch")
core.info(`Writing patch to ${patchPath}`)
await writeFile(patchPath, patch)
await writeFile(patchPath, alterDiffFile(patch))
return patchPath
} catch (err) {
console.warn(`failed to save pull request patch:`, err)
Expand Down Expand Up @@ -157,10 +158,6 @@ async function runLint(lintPath: string, patchPath: string): Promise<void> {
const workingDirectory = core.getInput(`working-directory`)
const cmdArgs: ExecOptions = {}
if (workingDirectory) {
if (patchPath) {
// TODO: make them compatible
throw new Error(`options working-directory and only-new-issues aren't compatible`)
}
if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) {
throw new Error(`working-directory (${workingDirectory}) was not a path`)
}
Expand Down
44 changes: 44 additions & 0 deletions src/utils/diffUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as core from "@actions/core"
import * as path from "path"

// If needed alter diff file to be compatible with working directory
export function alterDiffFile(diffFile: string): string {
let workingDirectory = core.getInput(`working-directory`)
if (workingDirectory) {
const workspace = process.env["GITHUB_WORKSPACE"] || ""
const relativeFile = path.relative(workspace, workingDirectory)
workingDirectory = relativeFile

const diffLines = diffFile.split("\n")
let ignore = false
const filteredDiffLines = []

for (const line of diffLines) {
if (line.startsWith("diff --git")) {
if (line.includes(`a/${workingDirectory}/`)) {
ignore = false
filteredDiffLines.push(line.replace(` a/${workingDirectory}/`, " a/").replace(` b/${workingDirectory}/`, " b/"))
} else {
ignore = true
}
} else {
if (!ignore) {
if (line.startsWith(`--- a/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`--- a/${workingDirectory}/`, "--- a/"))
} else if (line.startsWith(`+++ a/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`+++ a/${workingDirectory}/`, "+++ a/"))
} else if (line.startsWith(`--- b/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`--- b/${workingDirectory}/`, "--- b/"))
} else if (line.startsWith(`+++ b/${workingDirectory}/`)) {
filteredDiffLines.push(line.replace(`+++ b/${workingDirectory}/`, "+++ b/"))
} else {
filteredDiffLines.push(line)
}
}
}
}
// Join the modified lines back into a diff string
diffFile = filteredDiffLines.join("\n")
}
return diffFile
}

0 comments on commit 2408682

Please sign in to comment.