import * as _ from "lodash" ; import { exec } from "shelljs" ; import { WorkItem } from "./test" ; import { allFixtures , Fixture } from "./fixtures" ; function getChangedFiles ( base , commit ) { let diff = exec ( `git fetch -v origin ${ base } && git diff --name-only origin/${ base }..${ commit }` ) . stdout ; return diff . trim ( ) . split ( "\n" ) ; } export function affectedFixtures ( changedFiles = undefined ) { if ( changedFiles === undefined ) { const { BUILDKITE_PULL_REQUEST_BASE_BRANCH : base , BUILDKITE_COMMIT : commit } = process . env ; return commit === undefined ? allFixtures : affectedFixtures ( getChangedFiles ( base || "master" , commit ) ) ; } changedFiles = _ . reject ( changedFiles , file => _ . endsWith ( file , ".md" ) ) ; const fileDependencies = _ . flatMap ( allFixtures , f => f . language . sourceFiles || [ ] ) ; const allFixturesDirty = _ . some ( changedFiles , f => ! _ . includes ( fileDependencies , f ) ) ; if ( allFixturesDirty ) return allFixtures ; const dirtyFixtures = allFixtures . filter ( fixture => fixture . language . sourceFiles === undefined || _ . some ( changedFiles , f => _ . includes ( fixture . language . sourceFiles , f ) ) ) ; return dirtyFixtures ; } export function divideParallelJobs ( workItems ) { const { BUILDKITE_PARALLEL_JOB : pjob , BUILDKITE_PARALLEL_JOB_COUNT : pcount } = process . env ; if ( pjob === undefined || pcount === undefined ) return workItems ; try { const segment = Math . ceil ( workItems . length / parseFloat ( pcount ) ) ; const start = parseInt ( pjob , 10 ) * segment ; return workItems . slice ( start , start + segment ) ; } catch { return workItems ; } }