Skip to content

Commit

Permalink
ci: fix linting ci check for deleted files (#2167)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Sep 13, 2023
1 parent be07b0c commit 1ab954d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .buildkite/scripts/steps/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ void (async () => {
await exec('yarn build:ts');

if (bkEnv.isPullRequest && !hasLintConfigChanges) {
const filesToLint = changes.files.filter('**/*.ts?(x)').join(' ');
const filesToLint = changes.files.byType('DELETED', true).filter('**/*.ts?(x)');

if (filesToLint.length > 0) {
startGroup(`Running eslint checks - ${filesToLint.length} files`);

await exec('yarn lint:it', {
args: filesToLint,
args: filesToLint.join(' '),
});
}
} else {
Expand Down
55 changes: 44 additions & 11 deletions .buildkite/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,45 @@ export const defaultGHOptions = {
};

class FilesContext {
private _files: string[] = [];
private _files: FileDiff[] = [];
private _initialized: boolean = false;

constructor(files?: FileDiff[]) {
if (files) {
this._files = files;
this._initialized = true;
}
}

async init() {
this._files = await getFileDiffs();
if (!this._initialized) {
this._files = await getFileDiffs();
this._initialized = true;
}
}

get names(): readonly string[] {
return this._files;
return this._files.map(({ path }) => path);
}

/**
* Returns new FileContext by given changeType(s)
*/
byType(type: FileDiff['changeType'], negate?: boolean): FilesContext;
byType(types: FileDiff['changeType'][], negate?: boolean): FilesContext;
byType(typeArg: FileDiff['changeType'] | FileDiff['changeType'][], negate: boolean = false): FilesContext {
const changeTypes = Array.isArray(typeArg) ? typeArg : [typeArg];

if (changeTypes.length === 0) {
console.warn('No changeType(s) provided');
}

const filesByType = this._files.filter(({ changeType }) => {
const match = changeTypes.includes(changeType);
return negate ? !match : match;
});

return new FilesContext(filesByType);
}

/**
Expand Down Expand Up @@ -272,6 +303,11 @@ export const updateCheckStatus = async (
}
};

interface FileDiff {
path: string;
changeType: 'ADDED' | 'DELETED' | 'RENAMED' | 'COPIED' | 'MODIFIED' | 'CHANGED';
}

interface GLQPullRequestFiles {
repository: {
pullRequest: {
Expand All @@ -281,24 +317,20 @@ interface GLQPullRequestFiles {
endCursor: string;
hasNextPage: boolean;
};
nodes: [
{
path: string;
},
];
nodes: FileDiff[];
};
};
};
}

export async function getFileDiffs(): Promise<string[]> {
export async function getFileDiffs(): Promise<FileDiff[]> {
if (!bkEnv.isPullRequest) return [];

const prNumber = bkEnv.pullRequestNumber;
if (!prNumber) throw new Error(`Failed to set status, no prNumber available`);

try {
const files: string[] = [];
const files: FileDiff[] = [];
let hasNextPage = true;
let after = '';

Expand All @@ -315,6 +347,7 @@ export async function getFileDiffs(): Promise<string[]> {
}
nodes {
path
changeType
}
}
}
Expand All @@ -323,7 +356,7 @@ export async function getFileDiffs(): Promise<string[]> {
).repository.pullRequest.files;
hasNextPage = response.pageInfo.hasNextPage;
after = `, after: "${response.pageInfo.endCursor}"`;
files.push(...response.nodes.map(({ path }) => path));
files.push(...response.nodes);
}

return files;
Expand Down

0 comments on commit 1ab954d

Please sign in to comment.