2023-10-23 23:16:24 +02:00
|
|
|
import { execaCommand } from "execa"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { Challenge } from "./Challenge.js"
|
|
|
|
import { Solution } from "./Solution.js"
|
|
|
|
import { parseCommandOutput } from "../utils/parseCommandOutput.js"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
const solutionsRegex =
|
|
|
|
/challenges\/[\S\s]*\/solutions\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/[\S\s]*\/(.*).(c|cpp|cs|dart|java|js|py|rs|ts)/
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
const dockerRegex =
|
|
|
|
/templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/(.*)/
|
2021-11-09 16:45:42 +01:00
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
const inputOutputRegex =
|
|
|
|
/challenges\/[\S\s]*\/test\/(.*)\/(input.txt|output.txt)/
|
2021-11-10 18:57:10 +01:00
|
|
|
|
2021-06-25 12:46:01 +02:00
|
|
|
export interface GitAffectedOptions {
|
2021-06-30 15:23:58 +02:00
|
|
|
base?: string
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class GitAffected implements GitAffectedOptions {
|
2021-06-30 15:23:58 +02:00
|
|
|
public base?: string
|
2021-06-25 12:46:01 +02:00
|
|
|
|
2022-09-22 16:16:21 +02:00
|
|
|
constructor(options: GitAffectedOptions = {}) {
|
2021-06-30 15:23:58 +02:00
|
|
|
this.base = options.base
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
public async getFilesUsingBaseAndHead(
|
2021-06-25 12:46:01 +02:00
|
|
|
base: string,
|
2023-10-23 23:16:24 +02:00
|
|
|
head: string,
|
2021-06-25 12:46:01 +02:00
|
|
|
): Promise<string[]> {
|
2021-06-25 13:02:34 +02:00
|
|
|
try {
|
2022-04-23 18:41:14 +02:00
|
|
|
const { stdout } = await execaCommand(
|
2023-10-23 23:16:24 +02:00
|
|
|
`git diff --name-only --relative ${base} ${head}`,
|
2021-06-25 13:02:34 +02:00
|
|
|
)
|
2022-09-22 16:16:21 +02:00
|
|
|
return parseCommandOutput(stdout)
|
2021-06-25 13:02:34 +02:00
|
|
|
} catch {
|
|
|
|
return []
|
|
|
|
}
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
public async getUncommittedFiles(): Promise<string[]> {
|
2023-10-23 23:16:24 +02:00
|
|
|
return await this.getFilesUsingBaseAndHead("HEAD", ".")
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
public async getLatestPushedCommit(): Promise<string> {
|
2023-10-23 23:16:24 +02:00
|
|
|
const latestCommit = this.base != null ? "~1" : ""
|
2022-04-24 20:27:51 +02:00
|
|
|
const { stdout } = await execaCommand(
|
2023-10-23 23:16:24 +02:00
|
|
|
`git rev-parse origin/master${latestCommit}`,
|
2022-04-24 20:27:51 +02:00
|
|
|
)
|
2021-06-25 12:46:01 +02:00
|
|
|
return stdout
|
|
|
|
}
|
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
public async getUnpushedFiles(): Promise<string[]> {
|
2021-06-25 12:46:01 +02:00
|
|
|
return await this.getFilesUsingBaseAndHead(
|
|
|
|
await this.getLatestPushedCommit(),
|
2023-10-23 23:16:24 +02:00
|
|
|
".",
|
2021-06-25 12:46:01 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
public async getAffectedSolutionsFromFiles(
|
2023-10-23 23:16:24 +02:00
|
|
|
files: string[],
|
2022-04-24 20:27:51 +02:00
|
|
|
): Promise<Solution[]> {
|
2021-06-25 12:46:01 +02:00
|
|
|
const affectedSolutionsPaths = files.filter((filePath) => {
|
|
|
|
return solutionsRegex.test(filePath)
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
2021-11-09 16:45:42 +01:00
|
|
|
const affectedDockerPaths = files.filter((filePath) => {
|
|
|
|
return dockerRegex.test(filePath)
|
|
|
|
})
|
|
|
|
const affectedLanguages = affectedDockerPaths.map((filePath) => {
|
2022-04-24 20:27:51 +02:00
|
|
|
const [, , programmingLanguageName] = filePath
|
2023-10-23 23:16:24 +02:00
|
|
|
.replaceAll("\\", "/")
|
|
|
|
.split("/")
|
2023-01-10 23:15:36 +01:00
|
|
|
if (programmingLanguageName == null) {
|
2023-10-23 23:16:24 +02:00
|
|
|
throw new Error("programmingLanguageName is null")
|
2023-01-10 23:15:36 +01:00
|
|
|
}
|
2021-11-09 16:45:42 +01:00
|
|
|
return programmingLanguageName
|
|
|
|
})
|
2021-11-10 18:57:10 +01:00
|
|
|
const affectedInputOutput = files.filter((filePath) => {
|
|
|
|
return inputOutputRegex.test(filePath)
|
|
|
|
})
|
2022-04-24 20:27:51 +02:00
|
|
|
const affectedChallengesFromInputOutput = affectedInputOutput.map(
|
|
|
|
(filePath) => {
|
2023-10-23 23:16:24 +02:00
|
|
|
const [, challengeName] = filePath.replaceAll("\\", "/").split("/")
|
2023-01-10 23:15:36 +01:00
|
|
|
if (challengeName == null) {
|
2023-10-23 23:16:24 +02:00
|
|
|
throw new Error("challengeName is null")
|
2023-01-10 23:15:36 +01:00
|
|
|
}
|
2022-04-24 20:27:51 +02:00
|
|
|
return new Challenge({ name: challengeName })
|
2023-10-23 23:16:24 +02:00
|
|
|
},
|
2022-04-24 20:27:51 +02:00
|
|
|
)
|
|
|
|
const solutionsChallenges = await Solution.getManyByPaths(
|
2023-10-23 23:16:24 +02:00
|
|
|
affectedSolutionsPaths,
|
2022-04-24 20:27:51 +02:00
|
|
|
)
|
2023-10-23 23:16:24 +02:00
|
|
|
const solutionsDocker =
|
|
|
|
await Solution.getManyByProgrammingLanguages(affectedLanguages)
|
2021-11-30 21:42:43 +01:00
|
|
|
const solutions: Solution[] = [...solutionsDocker, ...solutionsChallenges]
|
|
|
|
for (const challenge of affectedChallengesFromInputOutput) {
|
|
|
|
const solutionsByChallenge = await Solution.getManyByChallenge(challenge)
|
|
|
|
solutions.push(...solutionsByChallenge)
|
2021-11-10 18:57:10 +01:00
|
|
|
}
|
2021-11-30 17:13:37 +01:00
|
|
|
const solutionsUnique: Solution[] = []
|
|
|
|
for (const solution of solutions) {
|
|
|
|
const isAlreadyIncluded = solutionsUnique.some((solutionUnique) => {
|
|
|
|
return solutionUnique.path === solution.path
|
|
|
|
})
|
|
|
|
if (!isAlreadyIncluded) {
|
|
|
|
solutionsUnique.push(solution)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return solutionsUnique
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|
2021-12-07 12:11:54 +01:00
|
|
|
|
2022-04-24 20:27:51 +02:00
|
|
|
public async getAffectedSolutionsFromGit(): Promise<Solution[]> {
|
2021-12-07 12:11:54 +01:00
|
|
|
let files = [
|
|
|
|
...(await this.getUnpushedFiles()),
|
2023-10-23 23:16:24 +02:00
|
|
|
...(await this.getUncommittedFiles()),
|
2021-12-07 12:11:54 +01:00
|
|
|
]
|
|
|
|
if (this.base != null) {
|
2023-10-23 23:16:24 +02:00
|
|
|
files.push(...(await this.getFilesUsingBaseAndHead(this.base, ".")))
|
2021-12-07 12:11:54 +01:00
|
|
|
}
|
|
|
|
files = Array.from(new Set(files))
|
|
|
|
return await this.getAffectedSolutionsFromFiles(files)
|
|
|
|
}
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|