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 committed Jul 10, 2023
1 parent a422a81 commit b174c03
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
Expand Up @@ -66544,6 +66544,7 @@ const tmp_1 = __nccwpck_require__(8517);
const util_1 = __nccwpck_require__(3837);
const cache_1 = __nccwpck_require__(4810);
const install_1 = __nccwpck_require__(1649);
const diffUtils_1 = __nccwpck_require__(3617);
const version_1 = __nccwpck_require__(1946);
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
const writeFile = (0, util_1.promisify)(fs.writeFile);
Expand Down Expand Up @@ -66600,7 +66601,7 @@ function fetchPatch() {
const tempDir = yield createTempDir();
const patchPath = path.join(tempDir, "pull.patch");
core.info(`Writing patch to ${patchPath}`);
yield writeFile(patchPath, patch);
yield writeFile(patchPath, (0, diffUtils_1.alterDiffFile)(patch));
return patchPath;
}
catch (err) {
Expand Down Expand Up @@ -66669,10 +66670,6 @@ function runLint(lintPath, patchPath) {
const workingDirectory = core.getInput(`working-directory`);
const cmdArgs = {};
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 Expand Up @@ -66798,6 +66795,88 @@ function isValidEvent() {
exports.isValidEvent = isValidEvent;


/***/ }),

/***/ 3617:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.alterDiffFile = void 0;
const core = __importStar(__nccwpck_require__(2186));
const path = __importStar(__nccwpck_require__(1017));
// If needed alter diff file to be compatible with working directory
function alterDiffFile(diffFile) {
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;
}
exports.alterDiffFile = alterDiffFile;


/***/ }),

/***/ 1946:
Expand Down
89 changes: 84 additions & 5 deletions dist/run/index.js
Expand Up @@ -66544,6 +66544,7 @@ const tmp_1 = __nccwpck_require__(8517);
const util_1 = __nccwpck_require__(3837);
const cache_1 = __nccwpck_require__(4810);
const install_1 = __nccwpck_require__(1649);
const diffUtils_1 = __nccwpck_require__(3617);
const version_1 = __nccwpck_require__(1946);
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
const writeFile = (0, util_1.promisify)(fs.writeFile);
Expand Down Expand Up @@ -66600,7 +66601,7 @@ function fetchPatch() {
const tempDir = yield createTempDir();
const patchPath = path.join(tempDir, "pull.patch");
core.info(`Writing patch to ${patchPath}`);
yield writeFile(patchPath, patch);
yield writeFile(patchPath, (0, diffUtils_1.alterDiffFile)(patch));
return patchPath;
}
catch (err) {
Expand Down Expand Up @@ -66669,10 +66670,6 @@ function runLint(lintPath, patchPath) {
const workingDirectory = core.getInput(`working-directory`);
const cmdArgs = {};
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 Expand Up @@ -66798,6 +66795,88 @@ function isValidEvent() {
exports.isValidEvent = isValidEvent;


/***/ }),

/***/ 3617:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.alterDiffFile = void 0;
const core = __importStar(__nccwpck_require__(2186));
const path = __importStar(__nccwpck_require__(1017));
// If needed alter diff file to be compatible with working directory
function alterDiffFile(diffFile) {
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;
}
exports.alterDiffFile = alterDiffFile;


/***/ }),

/***/ 1946:
Expand Down
7 changes: 2 additions & 5 deletions src/run.ts
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
@@ -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 b174c03

Please sign in to comment.