-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move git commands in common folder out of review
- Loading branch information
Showing
14 changed files
with
130 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { exec } from "child_process"; | ||
|
||
import { getGitHubEnvVariables } from "../../config"; | ||
|
||
export const getChangesFileLinesCommand = ( | ||
isCi: boolean, | ||
fileName: string | ||
): string => { | ||
if (isCi) { | ||
const { githubSha, baseSha } = getGitHubEnvVariables(); | ||
return `git diff -U0 --diff-filter=AMT ${baseSha} ${githubSha} ${fileName}`; | ||
} | ||
return `git diff -U0 --diff-filter=AMT --cached ${fileName}`; | ||
}; | ||
|
||
export const getChangedFileLines = async ( | ||
isCi: boolean, | ||
fileName: string | ||
): Promise<string> => { | ||
const commandString = getChangesFileLinesCommand(isCi, fileName); | ||
|
||
return new Promise((resolve, reject) => { | ||
exec(commandString, (error, stdout, stderr) => { | ||
if (error) { | ||
reject(new Error(`Failed to execute command. Error: ${error.message}`)); | ||
} else if (stderr) { | ||
reject(new Error(`Command execution error: ${stderr}`)); | ||
} else { | ||
const changedLines = stdout | ||
.split("\n") | ||
.filter((line) => line.startsWith("+") || line.startsWith("-")) | ||
.filter((line) => !line.startsWith("---") && !line.startsWith("+++")) | ||
.join("\n"); | ||
resolve(changedLines); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { exec } from "child_process"; | ||
import { join } from "path"; | ||
|
||
import { getGitHubEnvVariables } from "../../config"; | ||
|
||
export const getChangedFilesNamesCommand = (isCi: boolean): string => { | ||
if (isCi) { | ||
const { githubSha, baseSha } = getGitHubEnvVariables(); | ||
return `git diff --name-only --diff-filter=AMT ${baseSha} ${githubSha}`; | ||
} | ||
return "git diff --name-only --diff-filter=AMT --cached"; | ||
}; | ||
|
||
export const getChangedFilesNames = async ( | ||
isCi: boolean | ||
): Promise<string[]> => { | ||
const commandString = getChangedFilesNamesCommand(isCi); | ||
|
||
return new Promise((resolve, reject) => { | ||
exec(commandString, (error, stdout, stderr) => { | ||
if (error) { | ||
reject(new Error(`Failed to execute command. Error: ${error.message}`)); | ||
} else if (stderr) { | ||
reject(new Error(`Command execution error: ${stderr}`)); | ||
} else { | ||
const files = stdout | ||
.split("\n") | ||
.filter((fileName) => fileName.trim() !== "") | ||
.map((fileName) => join(process.cwd(), fileName.trim())); | ||
resolve(files); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { readFile } from "fs/promises"; | ||
import { getChangedFileLines } from "./getChangedFileLines"; | ||
import { getChangedFilesNames } from "./getChangedFilesNames"; | ||
import { File } from "../types/File"; | ||
|
||
export const getFilesWithChanges = async (isCi: boolean): Promise<File[]> => { | ||
try { | ||
const fileNames = await getChangedFilesNames(isCi); | ||
|
||
const files = await Promise.all( | ||
fileNames.map(async (fileName) => { | ||
const fileContent = await readFile(fileName, "utf8"); | ||
const changedLines = await getChangedFileLines(isCi, fileName); | ||
|
||
return { fileName, fileContent, changedLines }; | ||
}) | ||
); | ||
|
||
return files; | ||
} catch (error) { | ||
throw new Error(`Failed to get files with changes: ${error}`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface File { | ||
fileName: string; | ||
fileContent: string; | ||
changedLines: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { File } from "./File"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.