2022-04-23 18:41:14 +02:00
|
|
|
import { execaCommand } from 'execa'
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2022-02-19 18:30:29 +01:00
|
|
|
import { Challenge } from './Challenge.js'
|
|
|
|
import { Solution } from './Solution.js'
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2022-02-19 18:30:29 +01: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
|
|
|
|
2021-12-07 12:11:54 +01:00
|
|
|
const dockerRegex = /templates\/docker\/(c|cpp|cs|dart|java|javascript|python|rust|typescript)\/(.*)/
|
2021-11-09 16:45:42 +01:00
|
|
|
|
2022-02-19 18:30:29 +01: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 {
|
|
|
|
isContinuousIntegration: boolean
|
2021-06-30 15:23:58 +02:00
|
|
|
base?: string
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class GitAffected implements GitAffectedOptions {
|
|
|
|
public isContinuousIntegration: boolean
|
2021-06-30 15:23:58 +02:00
|
|
|
public base?: string
|
2021-06-25 12:46:01 +02:00
|
|
|
|
|
|
|
constructor (options: GitAffectedOptions) {
|
|
|
|
this.isContinuousIntegration = options.isContinuousIntegration
|
2021-06-30 15:23:58 +02:00
|
|
|
this.base = options.base
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public parseGitOutput (output: string): string[] {
|
|
|
|
return output
|
|
|
|
.split('\n')
|
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter((line) => line.length > 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getFilesUsingBaseAndHead (
|
|
|
|
base: string,
|
|
|
|
head: string
|
|
|
|
): Promise<string[]> {
|
2021-06-25 13:02:34 +02:00
|
|
|
try {
|
2022-04-23 18:41:14 +02:00
|
|
|
const { stdout } = await execaCommand(
|
2021-06-25 13:02:34 +02:00
|
|
|
`git diff --name-only --relative ${base} ${head}`
|
|
|
|
)
|
|
|
|
return this.parseGitOutput(stdout)
|
|
|
|
} catch {
|
|
|
|
return []
|
|
|
|
}
|
2021-06-25 12:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getUncommittedFiles (): Promise<string[]> {
|
|
|
|
return await this.getFilesUsingBaseAndHead('HEAD', '.')
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getLatestPushedCommit (): Promise<string> {
|
|
|
|
const latestCommit = this.isContinuousIntegration ? '~1' : ''
|
2022-04-23 18:41:14 +02:00
|
|
|
const { stdout } = await execaCommand(`git rev-parse origin/master${latestCommit}`)
|
2021-06-25 12:46:01 +02:00
|
|
|
return stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getUnpushedFiles (): Promise<string[]> {
|
|
|
|
return await this.getFilesUsingBaseAndHead(
|
|
|
|
await this.getLatestPushedCommit(),
|
|
|
|
'.'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:11:54 +01:00
|
|
|
public async getAffectedSolutionsFromFiles (files: string[]): 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) => {
|
|
|
|
const [,, programmingLanguageName] = filePath.replaceAll('\\', '/').split('/')
|
|
|
|
return programmingLanguageName
|
|
|
|
})
|
2021-11-10 18:57:10 +01:00
|
|
|
const affectedInputOutput = files.filter((filePath) => {
|
|
|
|
return inputOutputRegex.test(filePath)
|
|
|
|
})
|
2021-11-30 21:42:43 +01:00
|
|
|
const affectedChallengesFromInputOutput = affectedInputOutput.map((filePath) => {
|
2021-11-10 18:57:10 +01:00
|
|
|
const [, challengeName] = filePath.replaceAll('\\', '/').split('/')
|
|
|
|
return new Challenge({ name: challengeName })
|
|
|
|
})
|
2021-11-09 16:45:42 +01:00
|
|
|
const solutionsChallenges = await Solution.getManyByPaths(affectedSolutionsPaths)
|
|
|
|
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
|
|
|
|
|
|
|
public async getAffectedSolutionsFromGit (): Promise<Solution[]> {
|
|
|
|
let files = [
|
|
|
|
...(await this.getUnpushedFiles()),
|
|
|
|
...(await this.getUncommittedFiles())
|
|
|
|
]
|
|
|
|
if (this.base != null) {
|
|
|
|
files.push(...(await this.getFilesUsingBaseAndHead(this.base, '.')))
|
|
|
|
}
|
|
|
|
files = Array.from(new Set(files))
|
|
|
|
return await this.getAffectedSolutionsFromFiles(files)
|
|
|
|
}
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|