From 6ee941ecc788c910f892202ab8d9ebfc92b36f41 Mon Sep 17 00:00:00 2001 From: marocchino Date: Mon, 31 May 2021 15:32:58 +0900 Subject: [PATCH] =?UTF-8?q?style:=20=F0=9F=92=84=20lint=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc.json | 1 - __tests__/comment.test.ts | 112 ++++++++++++++--------------- __tests__/config.test.ts | 146 +++++++++++++++++++------------------- src/comment.ts | 8 +-- src/config.ts | 28 ++++---- src/main.ts | 14 ++-- 6 files changed, 154 insertions(+), 155 deletions(-) diff --git a/.prettierrc.json b/.prettierrc.json index c34bafc..ec9e821 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -3,7 +3,6 @@ "tabWidth": 2, "useTabs": false, "semi": false, - "singleQuote": true, "trailingComma": "none", "bracketSpacing": false, "arrowParens": "avoid" diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 5979952..2bb44f8 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -3,49 +3,49 @@ import { createComment, updateComment, deleteComment -} from '../src/comment' +} from "../src/comment" -import * as core from '@actions/core' +import * as core from "@actions/core" -jest.mock('@actions/core', () => ({ +jest.mock("@actions/core", () => ({ warning: jest.fn() })) const repo = { - owner: 'marocchino', - repo: 'sticky-pull-request-comment' + owner: "marocchino", + repo: "sticky-pull-request-comment" } -it('findPreviousComment', async () => { +it("findPreviousComment", async () => { const comment = { user: { - login: 'github-actions[bot]' + login: "github-actions[bot]" }, - body: 'previous message\n' + body: "previous message\n" } const commentWithCustomHeader = { user: { - login: 'github-actions[bot]' + login: "github-actions[bot]" }, - body: 'previous message\n' + body: "previous message\n" } const headerFirstComment = { user: { - login: 'github-actions[bot]' + login: "github-actions[bot]" }, - body: '\nheader first message' + body: "\nheader first message" } const otherComments = [ { user: { - login: 'some-user' + login: "some-user" }, - body: 'lgtm' + body: "lgtm" }, { user: { - login: 'github-actions[bot]' + login: "github-actions[bot]" }, - body: 'previous message\n' + body: "previous message\n" } ] const octokit: any = { @@ -65,21 +65,21 @@ it('findPreviousComment', async () => { } } - expect(await findPreviousComment(octokit, repo, 123, '')).toBe(comment) - expect(await findPreviousComment(octokit, repo, 123, 'TypeA')).toBe( + expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment) + expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe( commentWithCustomHeader ) - expect(await findPreviousComment(octokit, repo, 123, 'LegacyComment')).toBe( + expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe( headerFirstComment ) expect(octokit.rest.issues.listComments).toBeCalledWith({ - owner: 'marocchino', - repo: 'sticky-pull-request-comment', + owner: "marocchino", + repo: "sticky-pull-request-comment", issue_number: 123 }) }) -describe('updateComment', () => { +describe("updateComment", () => { let octokit beforeEach(() => { @@ -92,51 +92,51 @@ describe('updateComment', () => { } }) - it('with comment body', async () => { + it("with comment body", async () => { expect( - await updateComment(octokit, repo, 456, 'hello there', '') + await updateComment(octokit, repo, 456, "hello there", "") ).toBeUndefined() expect(octokit.rest.issues.updateComment).toBeCalledWith({ comment_id: 456, - owner: 'marocchino', - repo: 'sticky-pull-request-comment', - body: 'hello there\n' + owner: "marocchino", + repo: "sticky-pull-request-comment", + body: "hello there\n" }) expect( - await updateComment(octokit, repo, 456, 'hello there', 'TypeA') + await updateComment(octokit, repo, 456, "hello there", "TypeA") ).toBeUndefined() expect(octokit.rest.issues.updateComment).toBeCalledWith({ comment_id: 456, - owner: 'marocchino', - repo: 'sticky-pull-request-comment', - body: 'hello there\n' + owner: "marocchino", + repo: "sticky-pull-request-comment", + body: "hello there\n" }) expect( await updateComment( octokit, repo, 456, - 'hello there', - 'TypeA', - 'hello there\n' + "hello there", + "TypeA", + "hello there\n" ) ).toBeUndefined() expect(octokit.rest.issues.updateComment).toBeCalledWith({ comment_id: 456, - owner: 'marocchino', - repo: 'sticky-pull-request-comment', - body: 'hello there\n\nhello there' + owner: "marocchino", + repo: "sticky-pull-request-comment", + body: "hello there\n\nhello there" }) }) - it('without comment body and previous body', async () => { - expect(await updateComment(octokit, repo, 456, '', '')).toBeUndefined() + it("without comment body and previous body", async () => { + expect(await updateComment(octokit, repo, 456, "", "")).toBeUndefined() expect(octokit.rest.issues.updateComment).not.toBeCalled() - expect(core.warning).toBeCalledWith('Comment body cannot be blank') + expect(core.warning).toBeCalledWith("Comment body cannot be blank") }) }) -describe('createComment', () => { +describe("createComment", () => { let octokit beforeEach(() => { @@ -149,34 +149,34 @@ describe('createComment', () => { } }) - it('with comment body or previousBody', async () => { + it("with comment body or previousBody", async () => { expect( - await createComment(octokit, repo, 456, 'hello there', '') + await createComment(octokit, repo, 456, "hello there", "") ).toBeUndefined() expect(octokit.rest.issues.createComment).toBeCalledWith({ issue_number: 456, - owner: 'marocchino', - repo: 'sticky-pull-request-comment', - body: 'hello there\n' + owner: "marocchino", + repo: "sticky-pull-request-comment", + body: "hello there\n" }) expect( - await createComment(octokit, repo, 456, 'hello there', 'TypeA') + await createComment(octokit, repo, 456, "hello there", "TypeA") ).toBeUndefined() expect(octokit.rest.issues.createComment).toBeCalledWith({ issue_number: 456, - owner: 'marocchino', - repo: 'sticky-pull-request-comment', - body: 'hello there\n' + owner: "marocchino", + repo: "sticky-pull-request-comment", + body: "hello there\n" }) }) - it('without comment body and previousBody', async () => { - expect(await createComment(octokit, repo, 456, '', '')).toBeUndefined() + it("without comment body and previousBody", async () => { + expect(await createComment(octokit, repo, 456, "", "")).toBeUndefined() expect(octokit.rest.issues.createComment).not.toBeCalled() - expect(core.warning).toBeCalledWith('Comment body cannot be blank') + expect(core.warning).toBeCalledWith("Comment body cannot be blank") }) }) -it('deleteComment', async () => { +it("deleteComment", async () => { const octokit: any = { rest: { issues: { @@ -187,7 +187,7 @@ it('deleteComment', async () => { expect(await deleteComment(octokit, repo, 456)).toBeUndefined() expect(octokit.rest.issues.deleteComment).toBeCalledWith({ comment_id: 456, - owner: 'marocchino', - repo: 'sticky-pull-request-comment' + owner: "marocchino", + repo: "sticky-pull-request-comment" }) }) diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts index d8b80fa..c928ed1 100644 --- a/__tests__/config.test.ts +++ b/__tests__/config.test.ts @@ -1,131 +1,131 @@ beforeEach(() => { - process.env['GITHUB_REPOSITORY'] = 'marocchino/stick-pull-request-comment' - process.env['INPUT_NUMBER'] = '123' - process.env['INPUT_APPEND'] = 'false' - process.env['INPUT_RECREATE'] = 'false' - process.env['INPUT_DELETE'] = 'false' - process.env['INPUT_GITHUB_TOKEN'] = 'some-token' + process.env["GITHUB_REPOSITORY"] = "marocchino/stick-pull-request-comment" + process.env["INPUT_NUMBER"] = "123" + process.env["INPUT_APPEND"] = "false" + process.env["INPUT_RECREATE"] = "false" + process.env["INPUT_DELETE"] = "false" + process.env["INPUT_GITHUB_TOKEN"] = "some-token" }) afterEach(() => { jest.resetModules() - delete process.env['GITHUB_REPOSITORY'] - delete process.env['INPUT_REPO'] - delete process.env['INPUT_HEADER'] - delete process.env['INPUT_MESSAGE'] - delete process.env['INPUT_NUMBER'] - delete process.env['INPUT_APPEND'] - delete process.env['INPUT_RECREATE'] - delete process.env['INPUT_DELETE'] - delete process.env['INPUT_GITHUB_TOKEN'] - delete process.env['INPUT_PATH'] + delete process.env["GITHUB_REPOSITORY"] + delete process.env["INPUT_REPO"] + delete process.env["INPUT_HEADER"] + delete process.env["INPUT_MESSAGE"] + delete process.env["INPUT_NUMBER"] + delete process.env["INPUT_APPEND"] + delete process.env["INPUT_RECREATE"] + delete process.env["INPUT_DELETE"] + delete process.env["INPUT_GITHUB_TOKEN"] + delete process.env["INPUT_PATH"] }) -test('repo', () => { - process.env['INPUT_REPO'] = 'other' - expect(require('../src/config')).toMatchObject({ +test("repo", () => { + process.env["INPUT_REPO"] = "other" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'other'}, - body: '', - header: '', + repo: {owner: "marocchino", repo: "other"}, + body: "", + header: "", append: false, recreate: false, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) -test('header', () => { - process.env['INPUT_HEADER'] = 'header' - expect(require('../src/config')).toMatchObject({ +test("header", () => { + process.env["INPUT_HEADER"] = "header" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: '', - header: 'header', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "", + header: "header", append: false, recreate: false, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) -test('append', () => { - process.env['INPUT_APPEND'] = 'true' - expect(require('../src/config')).toMatchObject({ +test("append", () => { + process.env["INPUT_APPEND"] = "true" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: '', - header: '', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "", + header: "", append: true, recreate: false, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) -test('recreate', () => { - process.env['INPUT_RECREATE'] = 'true' - expect(require('../src/config')).toMatchObject({ +test("recreate", () => { + process.env["INPUT_RECREATE"] = "true" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: '', - header: '', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "", + header: "", append: false, recreate: true, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) -test('delete', () => { - process.env['INPUT_DELETE'] = 'true' - expect(require('../src/config')).toMatchObject({ +test("delete", () => { + process.env["INPUT_DELETE"] = "true" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: '', - header: '', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "", + header: "", append: false, recreate: false, deleteOldComment: true, - githubToken: 'some-token' + githubToken: "some-token" }) }) -describe('path', () => { - test('when exists return content of a file', () => { - process.env['INPUT_PATH'] = './__tests__/assets/result' - expect(require('../src/config')).toMatchObject({ +describe("path", () => { + test("when exists return content of a file", () => { + process.env["INPUT_PATH"] = "./__tests__/assets/result" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: 'hi there\n', - header: '', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "hi there\n", + header: "", append: false, recreate: false, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) - test('when not exists return null string', () => { - process.env['INPUT_PATH'] = './__tests__/assets/not_exists' - expect(require('../src/config')).toMatchObject({ + test("when not exists return null string", () => { + process.env["INPUT_PATH"] = "./__tests__/assets/not_exists" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: '', - header: '', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "", + header: "", append: false, recreate: false, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) }) -test('message', () => { - process.env['INPUT_MESSAGE'] = 'hello there' - expect(require('../src/config')).toMatchObject({ +test("message", () => { + process.env["INPUT_MESSAGE"] = "hello there" + expect(require("../src/config")).toMatchObject({ pullRequestNumber: expect.any(Number), - repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, - body: 'hello there', - header: '', + repo: {owner: "marocchino", repo: "stick-pull-request-comment"}, + body: "hello there", + header: "", append: false, recreate: false, deleteOldComment: false, - githubToken: 'some-token' + githubToken: "some-token" }) }) diff --git a/src/comment.ts b/src/comment.ts index 852d544..42e36c8 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -1,5 +1,5 @@ -import * as core from '@actions/core' -import {GitHub} from '@actions/github/lib/utils' +import * as core from "@actions/core" +import {GitHub} from "@actions/github/lib/utils" function headerComment(header: String): string { return `` @@ -33,7 +33,7 @@ export async function updateComment( previousBody?: string ): Promise { if (!body && !previousBody) - return core.warning('Comment body cannot be blank') + return core.warning("Comment body cannot be blank") await octokit.rest.issues.updateComment({ ...repo, @@ -55,7 +55,7 @@ export async function createComment( previousBody?: string ): Promise { if (!body && !previousBody) - return core.warning('Comment body cannot be blank') + return core.warning("Comment body cannot be blank") await octokit.rest.issues.createComment({ ...repo, diff --git a/src/config.ts b/src/config.ts index 35f8152..77de887 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,37 +1,37 @@ -import * as core from '@actions/core' -import {context} from '@actions/github' -import {readFileSync} from 'fs' +import * as core from "@actions/core" +import {context} from "@actions/github" +import {readFileSync} from "fs" export const pullRequestNumber = context?.payload?.pull_request?.number || - +core.getInput('number', {required: false}) + +core.getInput("number", {required: false}) export const repo = buildRepo() -export const header = core.getInput('header', {required: false}) -export const append = core.getBooleanInput('append', {required: true}) -export const recreate = core.getBooleanInput('recreate', {required: true}) -export const deleteOldComment = core.getBooleanInput('delete', {required: true}) -export const githubToken = core.getInput('GITHUB_TOKEN', {required: true}) +export const header = core.getInput("header", {required: false}) +export const append = core.getBooleanInput("append", {required: true}) +export const recreate = core.getBooleanInput("recreate", {required: true}) +export const deleteOldComment = core.getBooleanInput("delete", {required: true}) +export const githubToken = core.getInput("GITHUB_TOKEN", {required: true}) export const body = buildBody() function buildRepo(): {repo: string; owner: string} { return { owner: context.repo.owner, - repo: core.getInput('repo', {required: false}) || context.repo.repo + repo: core.getInput("repo", {required: false}) || context.repo.repo } } function buildBody(): string { - const path = core.getInput('path', {required: false}) + const path = core.getInput("path", {required: false}) if (path) { try { - return readFileSync(path, 'utf-8') + return readFileSync(path, "utf-8") } catch (error) { core.setFailed(error.message) - return '' + return "" } } else { - return core.getInput('message', {required: false}) + return core.getInput("message", {required: false}) } } diff --git a/src/main.ts b/src/main.ts index b013aae..4057391 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,11 @@ -import * as core from '@actions/core' -import * as github from '@actions/github' +import * as core from "@actions/core" +import * as github from "@actions/github" import { findPreviousComment, createComment, updateComment, deleteComment -} from './comment' +} from "./comment" import { pullRequestNumber, repo, @@ -15,21 +15,21 @@ import { recreate, deleteOldComment, githubToken -} from './config' +} from "./config" async function run(): Promise { if (isNaN(pullRequestNumber) || pullRequestNumber < 1) { - core.info('no pull request numbers given: skip step') + core.info("no pull request numbers given: skip step") return } try { if (!deleteOldComment && !body) { - throw new Error('Either message or path input is required') + throw new Error("Either message or path input is required") } if (deleteOldComment && recreate) { - throw new Error('delete and recreate cannot be both set to true') + throw new Error("delete and recreate cannot be both set to true") } const octokit = github.getOctokit(githubToken)