Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude internal files from autofix commits #1074

Merged
merged 2 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/red-llamas-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'skuba': patch
---

lint: Exclude internal files from autofix commits

`skuba lint` now avoids committing the following internal files in a [GitHub autofix](https://seek-oss.github.io/skuba/docs/deep-dives/github.html#github-autofixes):

- `.npmrc`
- `Dockerfile-incunabulum`
12 changes: 11 additions & 1 deletion src/api/git/commitAllChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ import git from 'isomorphic-git';

import { commit } from './commit';
import type { Identity } from './commit';
import type { ChangedFile } from './getChangedFiles';
import { getChangedFiles } from './getChangedFiles';

interface CommitAllParameters {
dir: string;
message: string;
author?: Identity;
committer?: Identity;

/**
* File changes to exclude from the commit.
*
* Defaults to `[]` (no exclusions).
*/
ignore?: ChangedFile[];
}

/**
Expand All @@ -18,10 +26,12 @@ interface CommitAllParameters {
export const commitAllChanges = async ({
dir,
message,

author,
committer,
ignore,
}: CommitAllParameters): Promise<string | undefined> => {
const changedFiles = await getChangedFiles({ dir });
const changedFiles = await getChangedFiles({ dir, ignore });

if (!changedFiles.length) {
return;
Expand Down
48 changes: 48 additions & 0 deletions src/api/git/getChangedFiles.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,54 @@ it('should return files which were deleted', async () => {
expect(files).toStrictEqual([{ path: newFileName, state: 'deleted' }]);
});

it('should exclude file changes based on ignore parameter', async () => {
await Promise.all([
fs.promises.writeFile('a', '1'),
fs.promises.writeFile('b', '1'),
]);

await git.add({ fs, dir, filepath: ['a', 'b'] });
await git.commit({
fs,
dir,
message: 'initial commit',
author,
});

await Promise.all([
fs.promises.rm('a'),
fs.promises.writeFile('b', '2'),
fs.promises.writeFile('c', '2'),
]);

const files = await getChangedFiles({
dir,
ignore: [
{
path: 'c',
// c file change is not matched
state: 'modified',
},
{
path: 'a',
// a file change is not matched
state: 'added',
},
{
// b file change is matched and therefore ignored
path: 'b',
state: 'modified',
},
],
});

expect(files).toStrictEqual([
// b file change is matched and therefore ignored
{ path: 'a', state: 'deleted' },
{ path: 'c', state: 'added' },
]);
});

it('should return an empty array if no files were changed', async () => {
await fs.promises.writeFile(newFileName, '');
await git.add({ fs, dir, filepath: newFileName });
Expand Down
17 changes: 16 additions & 1 deletion src/api/git/getChangedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export interface ChangedFile {
}
interface ChangedFilesParameters {
dir: string;

/**
* File changes to exclude from the result.
*
* Defaults to `[]` (no exclusions).
*/
ignore?: ChangedFile[];
}

const mapState = (
Expand All @@ -40,6 +47,8 @@ const mapState = (
*/
export const getChangedFiles = async ({
dir,

ignore = [],
}: ChangedFilesParameters): Promise<ChangedFile[]> => {
const allFiles = await git.statusMatrix({ fs, dir });
return allFiles
Expand All @@ -49,5 +58,11 @@ export const getChangedFiles = async ({
row[WORKDIR] !== UNMODIFIED ||
row[STAGE] !== UNMODIFIED,
)
.map((row) => ({ path: row[FILEPATH], state: mapState(row) }));
.map((row) => ({ path: row[FILEPATH], state: mapState(row) }))
.filter(
(changedFile) =>
!ignore.some(
(i) => i.path === changedFile.path && i.state === changedFile.state,
),
);
};
1 change: 1 addition & 0 deletions src/api/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { commit } from './commit';
export { commitAllChanges } from './commitAllChanges';
export { currentBranch } from './currentBranch';
export type { ChangedFile } from './getChangedFiles';
export { getChangedFiles } from './getChangedFiles';
export { getHeadCommitId, getHeadCommitMessage } from './log';
export { getOwnerAndRepo } from './remote';
Expand Down
19 changes: 14 additions & 5 deletions src/api/github/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
import fs from 'fs-extra';

import * as Git from '../git';
import type { ChangedFile } from '../git/getChangedFiles';

import { apiTokenFromEnvironment } from './environment';

Expand All @@ -29,6 +28,13 @@ interface UploadAllFileChangesParams {
* The headline of the commit message
*/
messageHeadline: string;

/**
* File changes to exclude from the upload.
*
* Defaults to `[]` (no exclusions).
*/
ignore?: Git.ChangedFile[];
/**
* The body of the commit message
*/
Expand All @@ -52,15 +58,18 @@ interface UploadAllFileChangesParams {
* specified.
*/
export const uploadAllFileChanges = async ({
dir,
branch,
dir,
messageHeadline,

ignore,
messageBody,
updateLocal = false,
}: UploadAllFileChangesParams): Promise<string | undefined> => {
const changedFiles = await Git.getChangedFiles({ dir });
const changedFiles = await Git.getChangedFiles({ dir, ignore });

if (!changedFiles.length) {
return undefined;
return;
}

const fileChanges = await readFileChanges(changedFiles);
Expand Down Expand Up @@ -102,7 +111,7 @@ export interface FileChanges {
* https://docs.github.com/en/graphql/reference/input-objects#filechanges
*/
export const readFileChanges = async (
changedFiles: ChangedFile[],
changedFiles: Git.ChangedFile[],
): Promise<FileChanges> => {
const { added, deleted } = changedFiles.reduce<{
added: string[];
Expand Down
25 changes: 22 additions & 3 deletions src/cli/lint/autofix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as GitHub from '../../api/github';
import { runESLint } from '../../cli/adapter/eslint';
import { runPrettier } from '../../cli/adapter/prettier';

import { autofix } from './autofix';
import { AUTOFIX_IGNORE_FILES, autofix } from './autofix';

jest.mock('simple-git');
jest.mock('../../api/git');
Expand Down Expand Up @@ -155,6 +155,13 @@ describe('autofix', () => {

expectAutofixCommit();

expect(Git.commitAllChanges).toHaveBeenNthCalledWith(1, {
dir: expect.any(String),
message: 'Run `skuba format`',

ignore: AUTOFIX_IGNORE_FILES,
});

expect(push).toHaveBeenNthCalledWith(1);

expect(stdout()).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -197,6 +204,14 @@ describe('autofix', () => {
).resolves.toBeUndefined();

expectAutofixCommit({ eslint: false });

expect(Git.commitAllChanges).toHaveBeenNthCalledWith(1, {
dir: expect.any(String),
message: 'Run `skuba format`',

ignore: AUTOFIX_IGNORE_FILES,
});

expect(push).toHaveBeenNthCalledWith(1);

// We should only run Prettier
Expand Down Expand Up @@ -357,9 +372,11 @@ describe('autofix', () => {

expectAutofixCommit();
expect(GitHub.uploadAllFileChanges).toHaveBeenNthCalledWith(1, {
dir: expect.any(String),
branch: 'dev',
dir: expect.any(String),
messageHeadline: 'Run `skuba format`',

ignore: AUTOFIX_IGNORE_FILES,
});

// We should run both ESLint and Prettier
Expand All @@ -382,9 +399,11 @@ describe('autofix', () => {

expectAutofixCommit({ eslint: false });
expect(GitHub.uploadAllFileChanges).toHaveBeenNthCalledWith(1, {
dir: expect.any(String),
branch: 'dev',
dir: expect.any(String),
messageHeadline: 'Run `skuba format`',

ignore: AUTOFIX_IGNORE_FILES,
});

// We should only run Prettier
Expand Down
17 changes: 16 additions & 1 deletion src/cli/lint/autofix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import type { Input } from './types';

const AUTOFIX_COMMIT_MESSAGE = 'Run `skuba format`';

export const AUTOFIX_IGNORE_FILES: Git.ChangedFile[] = [
{
path: '.npmrc',
state: 'added',
},
{
path: 'Dockerfile-incunabulum',
state: 'added',
},
];

const shouldPush = async ({
currentBranch,
dir,
Expand Down Expand Up @@ -100,6 +111,8 @@ export const autofix = async (params: AutofixParameters): Promise<void> => {
const ref = await Git.commitAllChanges({
dir,
message: AUTOFIX_COMMIT_MESSAGE,

ignore: AUTOFIX_IGNORE_FILES,
});

if (!ref) {
Expand All @@ -122,9 +135,11 @@ export const autofix = async (params: AutofixParameters): Promise<void> => {

const ref = await throwOnTimeout(
GitHub.uploadAllFileChanges({
dir,
branch: currentBranch,
dir,
messageHeadline: AUTOFIX_COMMIT_MESSAGE,

ignore: AUTOFIX_IGNORE_FILES,
}),
{ s: 30 },
);
Expand Down