From 7153d6ace2764342ae5df6411d5b079b2b0f3de7 Mon Sep 17 00:00:00 2001 From: Jimmy Bosse Date: Fri, 21 Jun 2024 17:53:23 -0400 Subject: [PATCH] linting --- .github/linters/.eslintrc.yml | 10 ++- .prettierrc.json | 2 +- __tests__/fogbogz-client.test.js | 74 +++++++++------- __tests__/index.test.js | 23 ++--- __tests__/main.test.js | 112 +++++++++++++----------- __tests__/planview-client.test.js | 136 +++++++++++++++++++----------- src/fogbugz-client.js | 19 ++--- src/index.js | 7 +- src/main.js | 32 ++++--- src/planview-client.js | 18 ++-- 10 files changed, 249 insertions(+), 184 deletions(-) diff --git a/.github/linters/.eslintrc.yml b/.github/linters/.eslintrc.yml index 48bc2ee..bb61122 100644 --- a/.github/linters/.eslintrc.yml +++ b/.github/linters/.eslintrc.yml @@ -45,7 +45,11 @@ rules: "import/no-namespace": "off", "no-console": "off", "no-unused-vars": "off", - "prettier/prettier": "error", - "semi": "off", - "quotes": ["error", "double", {"allowTemplateLiterals": true }] + "prettier/prettier": ["error", + { + "singleQuote": false, + "avoidEscape": false, + "semi": true, + }], + "semi": "off" } diff --git a/.prettierrc.json b/.prettierrc.json index a378146..b2154c2 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,5 +1,5 @@ { - "printWidth": 80, + "printWidth": 100, "tabWidth": 2, "useTabs": false, "semi": false, diff --git a/__tests__/fogbogz-client.test.js b/__tests__/fogbogz-client.test.js index c0764d6..7648942 100644 --- a/__tests__/fogbogz-client.test.js +++ b/__tests__/fogbogz-client.test.js @@ -23,49 +23,53 @@ describe("createCase", () => { const category = "My Category"; beforeEach(() => { - jest.clearAllMocks() + jest.clearAllMocks(); }); it("posts to the FogBugz API", async () => { - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); await fogbugzClient.createCase(title, project, text, category); - expect(postMock) - .toHaveBeenCalledWith( - "https://example.com", - expect.anything(), - expect.objectContaining({ - headers: { - "Accept": "application/json", - } - }) - ); + expect(postMock).toHaveBeenCalledWith( + "https://example.com", + expect.anything(), + expect.objectContaining({ + headers: { + Accept: "application/json" + } + }) + ); }); it("posts the correct payload", async () => { - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); await fogbugzClient.createCase(title, project, text, category); - expect(postMock) - .toHaveBeenCalledWith( - expect.anything(), - expect.objectContaining({ - token: "sometoken", - cmd: "new", - sTitle: title, - sProject: project, - sCategory: category, - sEvent: text, - fRichText: 1 - }), - expect.anything() - ); + expect(postMock).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + token: "sometoken", + cmd: "new", + sTitle: title, + sProject: project, + sCategory: category, + sEvent: text, + fRichText: 1 + }), + expect.anything() + ); }); it("returns the case number if case is created", async () => { - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); const result = await fogbugzClient.createCase(title, project, text, category); @@ -81,8 +85,10 @@ describe("createCase", () => { warnings: ["warning"], data: {} } - } - postMock.mockImplementation((_url, _payload, _config) => { return invalidResponse; }); + }; + postMock.mockImplementation((_url, _payload, _config) => { + return invalidResponse; + }); const result = await fogbugzClient.createCase(title, project, text, category); @@ -93,7 +99,9 @@ describe("createCase", () => { it("returns an error if the response is not 200", async () => { validResponse.status = 500; - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); const result = await fogbugzClient.createCase(title, project, text, category); @@ -103,7 +111,9 @@ describe("createCase", () => { }); it("returns an error if there is an eunexpected error", async () => { - postMock.mockImplementation((_url, _payload, _config) => { throw new Error("just no"); }); + postMock.mockImplementation((_url, _payload, _config) => { + throw new Error("just no"); + }); const result = await fogbugzClient.createCase(title, project, text, category); diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 0b20ef9..8863885 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -1,18 +1,13 @@ -/** - * Unit tests for the action's entrypoint, src/index.js - */ +const { run } = require("../src/main"); -const { run } = require('../src/main') - -// Mock the action's entrypoint -jest.mock('../src/main', () => ({ +jest.mock("../ src / main", () => ({ run: jest.fn() -})) +})); -describe('index', () => { - it('calls run when imported', async () => { - require('../src/index') +describe("index", () => { + it("calls run when imported", async () => { + require("../src/index"); - expect(run).toHaveBeenCalled() - }) -}) + expect(run).toHaveBeenCalled(); + }); +}); diff --git a/__tests__/main.test.js b/__tests__/main.test.js index 02c02d1..ce37435 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -1,7 +1,7 @@ /** - * Unit tests for the action's main functionality, src/main.js + * Unit tests for the action"s main functionality, src/main.js */ -const core = require('@actions/core'); +const core = require("@actions/core"); const github = require("@actions/github"); const FogBugzClient = require("../src/fogbugz-client"); @@ -12,16 +12,16 @@ const PlanviewClient = require("../src/planview-client"); jest.mock("../src/planview-client", () => jest.fn()); const createCard = jest.fn(); -const dependabotPr = require("./dependabot_pr_sample.json"); -const regularPr = require("./regular_pr_sample.json"); -const syncPr = require("./sync_pr_sample.json"); -const notAPr = require("./not_a_pr_sample.json"); -const main = require('../src/main'); +const dependabotPr = require("./dependabot_pr_sample"); +const regularPr = require("./regular_pr_sample"); +const syncPr = require("./sync_pr_sample"); +const notAPr = require("./not_a_pr_sample"); +const main = require("../src/main"); -const debugMock = jest.spyOn(core, 'debug').mockImplementation(); -const getInputMock = jest.spyOn(core, 'getInput').mockImplementation(); -const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(); -const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation(); +const debugMock = jest.spyOn(core, "debug").mockImplementation(); +const getInputMock = jest.spyOn(core, "getInput").mockImplementation(); +const setFailedMock = jest.spyOn(core, "setFailed").mockImplementation(); +const setOutputMock = jest.spyOn(core, "setOutput").mockImplementation(); const invalidFogBugzResponse = { success: false, error: "hi" }; const validFogBugzResponse = { success: true, case: { ixBug: 123 } }; @@ -29,7 +29,7 @@ const validFogBugzResponse = { success: true, case: { ixBug: 123 } }; const invalidPlanviewResponse = { success: false, result: "hi" }; const validPlanviewResponse = { success: true, data: { id: 42 } }; -describe('action', () => { +describe("action", () => { let debugMessages = []; beforeEach(() => { @@ -37,44 +37,44 @@ describe('action', () => { debugMessages = []; debugMock.mockImplementation(msg => { - debugMessages.push(msg) + debugMessages.push(msg); }); getInputMock.mockImplementation(name => { switch (name) { case "fogbugz_api_url": - return "https://my.fb.com/api" + return "https://my.fb.com/api"; case "fogbugz_token": - return "myfbtoken" + return "myfbtoken"; case "fogbugz_project": - return "My Project" + return "My Project"; case "fogbugz_subproject": - return "My Subproject" + return "My Subproject"; case "fogbugz_category": - return "My Category" + return "My Category"; case "planview_api_url": - return "https://my.pv.com/io" + return "https://my.pv.com/io"; case "planview_auth": - return "myplanviewauth" + return "myplanviewauth"; case "planview_board_id": - return 123456 + return 123456; case "planview_lane_id": - return 654321 + return 654321; case "planview_type_id": - return 987654 + return 987654; case "users": - return "dependabot[bot]" + return "dependabot[bot]"; default: - return "" + return ""; } }); FogBugzClient.mockImplementation((_baseUrl, _token) => { - return { createCase: createCase }; + return { createCase }; }); PlanviewClient.mockImplementation((_baseUrl, _auth) => { - return { createCard: createCard }; + return { createCard }; }); }); @@ -125,9 +125,14 @@ describe('action', () => { await main.run(); - expect(createCase).toHaveBeenCalledWith("My Subproject - Dependabot PR title", "My Project", "Dependabot PR body", "My Category"); + expect(createCase).toHaveBeenCalledWith( + "My Subproject - Dependabot PR title", + "My Project", + "Dependabot PR body", + "My Category" + ); expect(debugMessages).toContain("Creating FB case for My Subproject - Dependabot PR title"); - expect(debugMessages).toContain("fbt_result: \"myresult\""); + expect(debugMessages).toContain('fbt_result: "myresult"'); }); it("fails if the FogBugz case isn't created", async () => { @@ -165,9 +170,15 @@ describe('action', () => { await main.run(); - expect(createCard).toHaveBeenCalledWith(123456, 654321, 987654, - "My Subproject - Dependabot PR title", 123, "https://github.com/MyOrg/my_repo/pull/123"); - expect(debugMessages).toContain("pvc_result: \"myresult\""); + expect(createCard).toHaveBeenCalledWith( + 123456, + 654321, + 987654, + "My Subproject - Dependabot PR title", + 123, + "https://github.com/MyOrg/my_repo/pull/123" + ); + expect(debugMessages).toContain('pvc_result: "myresult"'); }); it("fails if the card can't be created", async () => { @@ -190,8 +201,10 @@ describe('action', () => { }); it("handles and unecpected exception", async () => { - error = new Error("just no"); - getInputMock.mockImplementation(_name => { throw error }); + const error = new Error("just no"); + getInputMock.mockImplementation(_name => { + throw error; + }); await main.run(); @@ -203,32 +216,37 @@ describe('action', () => { getInputMock.mockImplementation(name => { switch (name) { case "fogbugz_api_url": - return "https://my.fb.com/api" + return "https://my.fb.com/api"; case "fogbugz_token": - return "myfbtoken" + return "myfbtoken"; case "fogbugz_project": - return "My Project" + return "My Project"; case "fogbugz_category": - return "My Category" + return "My Category"; case "planview_api_url": - return "https://my.pv.com/io" + return "https://my.pv.com/io"; case "planview_auth": - return "myplanviewauth" + return "myplanviewauth"; case "planview_board_id": - return 123456 + return 123456; case "planview_lane_id": - return 654321 + return 654321; case "planview_type_id": - return 987654 + return 987654; case "users": - return "dependabot[bot]" + return "dependabot[bot]"; default: - return "" + return ""; } }); await main.run(); - expect(createCase).toHaveBeenCalledWith("Dependabot PR title", "My Project", "Dependabot PR body", "My Category"); + expect(createCase).toHaveBeenCalledWith( + "Dependabot PR title", + "My Project", + "Dependabot PR body", + "My Category" + ); }); -}) +}); diff --git a/__tests__/planview-client.test.js b/__tests__/planview-client.test.js index 8fa20c2..18f37ee 100644 --- a/__tests__/planview-client.test.js +++ b/__tests__/planview-client.test.js @@ -14,61 +14,71 @@ describe("createCase", () => { const boardId = 123; const laneId = 234; const typeId = 456; - const title = "My Title" + const title = "My Title"; const customId = 999; const pr_url = "https://github.com/some_pr_url"; beforeEach(() => { - jest.clearAllMocks() + jest.clearAllMocks(); }); it("posts to the Planview API", async () => { - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); await planviewClient.createCard(boardId, laneId, typeId, title, customId, pr_url); - expect(postMock) - .toHaveBeenCalledWith( - "https://example.com/card", - expect.anything(), - expect.objectContaining({ - headers: { - "Accept": "application/json", - "Content-Type": "application/json", - "Authorization": "bearer somebase64Auth" - } - }) - ); + expect(postMock).toHaveBeenCalledWith( + "https://example.com/card", + expect.anything(), + expect.objectContaining({ + headers: { + Accept: "application/json", + "Content-Type": "application/json", + Authorization: "bearer somebase64Auth" + } + }) + ); }); it("posts the correct payload", async () => { - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); await planviewClient.createCard(boardId, laneId, typeId, title, customId, pr_url); - expect(postMock) - .toHaveBeenCalledWith( - expect.anything(), - expect.objectContaining({ - boardId: "123", - laneId: "234", - typeId: "456", - title: "My Title", - customId: "999", - externalLink: { - label: "GitHub PR", - url: "https://github.com/some_pr_url" - } - }), - expect.anything() - ); + expect(postMock).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + boardId: "123", + laneId: "234", + typeId: "456", + title: "My Title", + customId: "999", + externalLink: { + label: "GitHub PR", + url: "https://github.com/some_pr_url" + } + }), + expect.anything() + ); }); it("returns the case number if case is created", async () => { - postMock.mockImplementation((_url, _payload, _config) => { return validResponse; }); - - const result = await planviewClient - .createCard(boardId, laneId, typeId, title, customId, pr_url); + postMock.mockImplementation((_url, _payload, _config) => { + return validResponse; + }); + + const result = await planviewClient.createCard( + boardId, + laneId, + typeId, + title, + customId, + pr_url + ); expect(result.success).toEqual(true); expect(result.data).toEqual(validResponse.data); @@ -78,11 +88,19 @@ describe("createCase", () => { const invalidResponse = { status: 200, data: {} - } - postMock.mockImplementation((_url, _payload, _config) => { return invalidResponse; }); - - const result = await planviewClient - .createCard(boardId, laneId, typeId, title, customId, pr_url); + }; + postMock.mockImplementation((_url, _payload, _config) => { + return invalidResponse; + }); + + const result = await planviewClient.createCard( + boardId, + laneId, + typeId, + title, + customId, + pr_url + ); expect(result.success).toEqual(false); expect(result.result).toBe(invalidResponse); @@ -92,22 +110,38 @@ describe("createCase", () => { const invalidResponse = { status: 500, data: {} - } - postMock.mockImplementation((_url, _payload, _config) => { return invalidResponse; }); - - const result = await planviewClient - .createCard(boardId, laneId, typeId, title, customId, pr_url); + }; + postMock.mockImplementation((_url, _payload, _config) => { + return invalidResponse; + }); + + const result = await planviewClient.createCard( + boardId, + laneId, + typeId, + title, + customId, + pr_url + ); expect(result.success).toEqual(false); expect(result.result).toBe(invalidResponse); }); it("returns an error if there is an eunexpected error", async () => { - error = new Error("just no") - postMock.mockImplementation((_url, _payload, _config) => { throw error; }); - - const result = await planviewClient - .createCard(boardId, laneId, typeId, title, customId, pr_url); + const error = new Error("just no"); + postMock.mockImplementation((_url, _payload, _config) => { + throw error; + }); + + const result = await planviewClient.createCard( + boardId, + laneId, + typeId, + title, + customId, + pr_url + ); expect(result.success).toEqual(false); expect(result.error).toBe(error); diff --git a/src/fogbugz-client.js b/src/fogbugz-client.js index ae99044..8673198 100644 --- a/src/fogbugz-client.js +++ b/src/fogbugz-client.js @@ -1,15 +1,15 @@ const axios = require("axios"); -const FogBugzClient = function (baseUrl, token) { +const FogBugzClient = (baseUrl, token) => { const config = { headers: { - "Accept": "application/json", + Accept: "application/json" } - } + }; const getCreateCasePayload = (title, project, text, category) => { return { - token: token, + token, cmd: "new", sTitle: title, sProject: project, @@ -19,7 +19,7 @@ const FogBugzClient = function (baseUrl, token) { }; }; - const parseResponse = (result) => { + const parseResponse = result => { if (result.status === 200 && result.data.errors.length === 0) { return { success: true, @@ -30,11 +30,11 @@ const FogBugzClient = function (baseUrl, token) { success: false, errors: result.data.errors, warnings: result.data.warnings - } + }; } }; - const parseException = (e) => { + const parseException = e => { return { success: false, errors: [e.message] @@ -49,10 +49,9 @@ const FogBugzClient = function (baseUrl, token) { } catch (e) { return parseException(e); } - } - return { - createCase: createCase }; + + return { createCase }; }; module.exports = FogBugzClient; diff --git a/src/index.js b/src/index.js index 69d5712..0878dfa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,3 @@ -/** - * The entrypoint for the action. - */ -const { run } = require('./main') +const { run } = require("./main"); -run() +run(); diff --git a/src/main.js b/src/main.js index a085440..b97eddd 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,4 @@ -const core = require('@actions/core'); +const core = require("@actions/core"); const github = require("@actions/github"); const FogBugzClient = require("./fogbugz-client"); const PlanviewClient = require("./planview-client"); @@ -9,12 +9,15 @@ async function run() { try { // Get the JSON webhook payload for the event that triggered the workflow const payload = github.context.payload; - const users = core.getInput("users").split(",").map(element => element.trim()); + const users = core + .getInput("users") + .split(",") + .map(element => element.trim()); if (payload.action !== "opened") { core.debug(`pr was ${payload.action} so not running`); return; } - if (!!!payload.pull_request) { + if (!payload.pull_request) { core.debug(`not a pr so not running`); return; } @@ -25,8 +28,8 @@ async function run() { const fbc = new FogBugzClient(core.getInput("fogbugz_api_url"), core.getInput("fogbugz_token")); const project = core.getInput("fogbugz_project"); const subproject = core.getInput("fogbugz_subproject"); - const title = !!subproject - ? subproject + " - " + payload.pull_request.title + const title = subproject + ? `${subproject} - ${payload.pull_request.title}` : payload.pull_request.title; const text = payload.pull_request.body; const category = core.getInput("fogbugz_category"); @@ -35,12 +38,22 @@ async function run() { core.debug(`fbt_result: ${JSON.stringify(fbt_result)}`); if (fbt_result.success) { core.setOutput("fogbugz_id", fbt_result.case.ixBug); - const pvc = new PlanviewClient(core.getInput("planview_api_url"), core.getInput("planview_auth")); + const pvc = new PlanviewClient( + core.getInput("planview_api_url"), + core.getInput("planview_auth") + ); const boardId = core.getInput("planview_board_id"); const laneId = core.getInput("planview_lane_id"); const typeId = core.getInput("planview_type_id"); core.debug(`Creating Planview card for ${fbt_result.case.ixBug}`); - const pvc_result = await pvc.createCard(boardId, laneId, typeId, title, fbt_result.case.ixBug, payload.pull_request.html_url); + const pvc_result = await pvc.createCard( + boardId, + laneId, + typeId, + title, + fbt_result.case.ixBug, + payload.pull_request.html_url + ); core.debug(`pvc_result: ${JSON.stringify(pvc_result)}`); pvc_result.success ? core.setOutput("planview_id", pvc_result.data.id) @@ -48,12 +61,9 @@ async function run() { } else { core.setFailed(fbt_result); } - } catch (error) { core.setFailed(error); } } -module.exports = { - run -} +module.exports = { run }; diff --git a/src/planview-client.js b/src/planview-client.js index ab68049..4cb9a47 100644 --- a/src/planview-client.js +++ b/src/planview-client.js @@ -1,12 +1,12 @@ const axios = require("axios"); -const PlanviewClient = function (base_url, base64Auth) { - const cardUrl = base_url + "/card"; +const PlanviewClient = (base_url, base64Auth) => { + const cardUrl = `${base_url}/card`; const config = { headers: { - "Accept": "application/json", + Accept: "application/json", "Content-Type": "application/json", - "Authorization": "bearer " + base64Auth + Authorization: `bearer ${base64Auth}` } }; @@ -24,7 +24,7 @@ const PlanviewClient = function (base_url, base64Auth) { }; }; - const parseResponse = (result) => { + const parseResponse = result => { if (result.status === 200 && !!result.data.id) { return { success: true, @@ -33,12 +33,12 @@ const PlanviewClient = function (base_url, base64Auth) { } else { return { success: false, - result: result + result }; } }; - const parseException = (e) => { + const parseException = e => { return { success: false, error: e @@ -55,9 +55,7 @@ const PlanviewClient = function (base_url, base64Auth) { } }; - return { - createCard: createCard - }; + return { createCard }; }; module.exports = PlanviewClient;