Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add .prettierrc.json to configure Prettier, reformat code
  • Loading branch information
IvanZosimov committed Dec 19, 2022
1 parent d42abd9 commit 0cad484
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 94 deletions.
12 changes: 12 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,12 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript",
"endOfLine": "auto"
}
24 changes: 12 additions & 12 deletions __mocks__/@actions/github.ts
@@ -1,34 +1,34 @@
export const context = {
payload: {
pull_request: {
number: 123,
},
number: 123
}
},
repo: {
owner: "monalisa",
repo: "helloworld",
},
owner: 'monalisa',
repo: 'helloworld'
}
};

const mockApi = {
rest: {
issues: {
addLabels: jest.fn(),
removeLabel: jest.fn(),
removeLabel: jest.fn()
},
pulls: {
get: jest.fn().mockResolvedValue({}),
listFiles: {
endpoint: {
merge: jest.fn().mockReturnValue({}),
},
},
merge: jest.fn().mockReturnValue({})
}
}
},
repos: {
getContent: jest.fn(),
},
getContent: jest.fn()
}
},
paginate: jest.fn(),
paginate: jest.fn()
};

export const getOctokit = jest.fn().mockImplementation(() => mockApi);
22 changes: 11 additions & 11 deletions __tests__/labeler.test.ts
@@ -1,27 +1,27 @@
import { checkGlobs } from "../src/labeler";
import {checkGlobs} from '../src/labeler';

import * as core from "@actions/core";
import * as core from '@actions/core';

jest.mock("@actions/core");
jest.mock('@actions/core');

beforeAll(() => {
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
return jest.requireActual("@actions/core").getInput(name, options);
jest.spyOn(core, 'getInput').mockImplementation((name, options) => {
return jest.requireActual('@actions/core').getInput(name, options);
});
});

const matchConfig = [{ any: ["*.txt"] }];
const matchConfig = [{any: ['*.txt']}];

describe("checkGlobs", () => {
it("returns true when our pattern does match changed files", () => {
const changedFiles = ["foo.txt", "bar.txt"];
describe('checkGlobs', () => {
it('returns true when our pattern does match changed files', () => {
const changedFiles = ['foo.txt', 'bar.txt'];
const result = checkGlobs(changedFiles, matchConfig);

expect(result).toBeTruthy();
});

it("returns false when our pattern does not match changed files", () => {
const changedFiles = ["foo.docx"];
it('returns false when our pattern does not match changed files', () => {
const changedFiles = ['foo.docx'];
const result = checkGlobs(changedFiles, matchConfig);

expect(result).toBeFalsy();
Expand Down
92 changes: 46 additions & 46 deletions __tests__/main.test.ts
@@ -1,100 +1,100 @@
import { run } from "../src/labeler";
import * as github from "@actions/github";
import * as core from "@actions/core";
import {run} from '../src/labeler';
import * as github from '@actions/github';
import * as core from '@actions/core';

const fs = jest.requireActual("fs");
const fs = jest.requireActual('fs');

jest.mock("@actions/core");
jest.mock("@actions/github");
jest.mock('@actions/core');
jest.mock('@actions/github');

const gh = github.getOctokit("_");
const addLabelsMock = jest.spyOn(gh.rest.issues, "addLabels");
const removeLabelMock = jest.spyOn(gh.rest.issues, "removeLabel");
const reposMock = jest.spyOn(gh.rest.repos, "getContent");
const paginateMock = jest.spyOn(gh, "paginate");
const getPullMock = jest.spyOn(gh.rest.pulls, "get");
const gh = github.getOctokit('_');
const addLabelsMock = jest.spyOn(gh.rest.issues, 'addLabels');
const removeLabelMock = jest.spyOn(gh.rest.issues, 'removeLabel');
const reposMock = jest.spyOn(gh.rest.repos, 'getContent');
const paginateMock = jest.spyOn(gh, 'paginate');
const getPullMock = jest.spyOn(gh.rest.pulls, 'get');

const yamlFixtures = {
"only_pdfs.yml": fs.readFileSync("__tests__/fixtures/only_pdfs.yml"),
'only_pdfs.yml': fs.readFileSync('__tests__/fixtures/only_pdfs.yml')
};

afterAll(() => jest.restoreAllMocks());

describe("run", () => {
it("adds labels to PRs that match our glob patterns", async () => {
usingLabelerConfigYaml("only_pdfs.yml");
mockGitHubResponseChangedFiles("foo.pdf");
describe('run', () => {
it('adds labels to PRs that match our glob patterns', async () => {
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.pdf');

await run();

expect(removeLabelMock).toHaveBeenCalledTimes(0);
expect(addLabelsMock).toHaveBeenCalledTimes(1);
expect(addLabelsMock).toHaveBeenCalledWith({
owner: "monalisa",
repo: "helloworld",
owner: 'monalisa',
repo: 'helloworld',
issue_number: 123,
labels: ["touched-a-pdf-file"],
labels: ['touched-a-pdf-file']
});
});

it("does not add labels to PRs that do not match our glob patterns", async () => {
usingLabelerConfigYaml("only_pdfs.yml");
mockGitHubResponseChangedFiles("foo.txt");
it('does not add labels to PRs that do not match our glob patterns', async () => {
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.txt');

await run();

expect(removeLabelMock).toHaveBeenCalledTimes(0);
expect(addLabelsMock).toHaveBeenCalledTimes(0);
});

it("(with sync-labels: true) it deletes preexisting PR labels that no longer match the glob pattern", async () => {
it('(with sync-labels: true) it deletes preexisting PR labels that no longer match the glob pattern', async () => {
let mockInput = {
"repo-token": "foo",
"configuration-path": "bar",
"sync-labels": true,
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': true
};

jest
.spyOn(core, "getInput")
.spyOn(core, 'getInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);

usingLabelerConfigYaml("only_pdfs.yml");
mockGitHubResponseChangedFiles("foo.txt");
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.txt');
getPullMock.mockResolvedValue(<any>{
data: {
labels: [{ name: "touched-a-pdf-file" }],
},
labels: [{name: 'touched-a-pdf-file'}]
}
});

await run();

expect(addLabelsMock).toHaveBeenCalledTimes(0);
expect(removeLabelMock).toHaveBeenCalledTimes(1);
expect(removeLabelMock).toHaveBeenCalledWith({
owner: "monalisa",
repo: "helloworld",
owner: 'monalisa',
repo: 'helloworld',
issue_number: 123,
name: "touched-a-pdf-file",
name: 'touched-a-pdf-file'
});
});

it("(with sync-labels: false) it issues no delete calls even when there are preexisting PR labels that no longer match the glob pattern", async () => {
it('(with sync-labels: false) it issues no delete calls even when there are preexisting PR labels that no longer match the glob pattern', async () => {
let mockInput = {
"repo-token": "foo",
"configuration-path": "bar",
"sync-labels": false,
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': false
};

jest
.spyOn(core, "getInput")
.spyOn(core, 'getInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);

usingLabelerConfigYaml("only_pdfs.yml");
mockGitHubResponseChangedFiles("foo.txt");
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.txt');
getPullMock.mockResolvedValue(<any>{
data: {
labels: [{ name: "touched-a-pdf-file" }],
},
labels: [{name: 'touched-a-pdf-file'}]
}
});

await run();
Expand All @@ -106,11 +106,11 @@ describe("run", () => {

function usingLabelerConfigYaml(fixtureName: keyof typeof yamlFixtures): void {
reposMock.mockResolvedValue(<any>{
data: { content: yamlFixtures[fixtureName], encoding: "utf8" },
data: {content: yamlFixtures[fixtureName], encoding: 'utf8'}
});
}

function mockGitHubResponseChangedFiles(...files: string[]): void {
const returnValue = files.map((f) => ({ filename: f }));
const returnValue = files.map(f => ({filename: f}));
paginateMock.mockReturnValue(<any>returnValue);
}

0 comments on commit 0cad484

Please sign in to comment.