style: ๐Ÿ’„ lint error

This commit is contained in:
marocchino 2021-05-31 15:32:58 +09:00
parent 7874cc39cf
commit 6ee941ecc7
No known key found for this signature in database
GPG key ID: AFF521DBDB122570
6 changed files with 154 additions and 155 deletions

View file

@ -3,7 +3,6 @@
"tabWidth": 2, "tabWidth": 2,
"useTabs": false, "useTabs": false,
"semi": false, "semi": false,
"singleQuote": true,
"trailingComma": "none", "trailingComma": "none",
"bracketSpacing": false, "bracketSpacing": false,
"arrowParens": "avoid" "arrowParens": "avoid"

View file

@ -3,49 +3,49 @@ import {
createComment, createComment,
updateComment, updateComment,
deleteComment 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() warning: jest.fn()
})) }))
const repo = { const repo = {
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment' repo: "sticky-pull-request-comment"
} }
it('findPreviousComment', async () => { it("findPreviousComment", async () => {
const comment = { const comment = {
user: { user: {
login: 'github-actions[bot]' login: "github-actions[bot]"
}, },
body: 'previous message\n<!-- Sticky Pull Request Comment -->' body: "previous message\n<!-- Sticky Pull Request Comment -->"
} }
const commentWithCustomHeader = { const commentWithCustomHeader = {
user: { user: {
login: 'github-actions[bot]' login: "github-actions[bot]"
}, },
body: 'previous message\n<!-- Sticky Pull Request CommentTypeA -->' body: "previous message\n<!-- Sticky Pull Request CommentTypeA -->"
} }
const headerFirstComment = { const headerFirstComment = {
user: { user: {
login: 'github-actions[bot]' login: "github-actions[bot]"
}, },
body: '<!-- Sticky Pull Request CommentLegacyComment -->\nheader first message' body: "<!-- Sticky Pull Request CommentLegacyComment -->\nheader first message"
} }
const otherComments = [ const otherComments = [
{ {
user: { user: {
login: 'some-user' login: "some-user"
}, },
body: 'lgtm' body: "lgtm"
}, },
{ {
user: { user: {
login: 'github-actions[bot]' login: "github-actions[bot]"
}, },
body: 'previous message\n<!-- Sticky Pull Request CommentTypeB -->' body: "previous message\n<!-- Sticky Pull Request CommentTypeB -->"
} }
] ]
const octokit: any = { const octokit: any = {
@ -65,21 +65,21 @@ it('findPreviousComment', async () => {
} }
} }
expect(await findPreviousComment(octokit, repo, 123, '')).toBe(comment) expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment)
expect(await findPreviousComment(octokit, repo, 123, 'TypeA')).toBe( expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe(
commentWithCustomHeader commentWithCustomHeader
) )
expect(await findPreviousComment(octokit, repo, 123, 'LegacyComment')).toBe( expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe(
headerFirstComment headerFirstComment
) )
expect(octokit.rest.issues.listComments).toBeCalledWith({ expect(octokit.rest.issues.listComments).toBeCalledWith({
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment', repo: "sticky-pull-request-comment",
issue_number: 123 issue_number: 123
}) })
}) })
describe('updateComment', () => { describe("updateComment", () => {
let octokit let octokit
beforeEach(() => { beforeEach(() => {
@ -92,51 +92,51 @@ describe('updateComment', () => {
} }
}) })
it('with comment body', async () => { it("with comment body", async () => {
expect( expect(
await updateComment(octokit, repo, 456, 'hello there', '') await updateComment(octokit, repo, 456, "hello there", "")
).toBeUndefined() ).toBeUndefined()
expect(octokit.rest.issues.updateComment).toBeCalledWith({ expect(octokit.rest.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment', repo: "sticky-pull-request-comment",
body: 'hello there\n<!-- Sticky Pull Request Comment -->' body: "hello there\n<!-- Sticky Pull Request Comment -->"
}) })
expect( expect(
await updateComment(octokit, repo, 456, 'hello there', 'TypeA') await updateComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined() ).toBeUndefined()
expect(octokit.rest.issues.updateComment).toBeCalledWith({ expect(octokit.rest.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment', repo: "sticky-pull-request-comment",
body: 'hello there\n<!-- Sticky Pull Request CommentTypeA -->' body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
}) })
expect( expect(
await updateComment( await updateComment(
octokit, octokit,
repo, repo,
456, 456,
'hello there', "hello there",
'TypeA', "TypeA",
'hello there\n<!-- Sticky Pull Request CommentTypeA -->' "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
) )
).toBeUndefined() ).toBeUndefined()
expect(octokit.rest.issues.updateComment).toBeCalledWith({ expect(octokit.rest.issues.updateComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment', repo: "sticky-pull-request-comment",
body: 'hello there\n<!-- Sticky Pull Request CommentTypeA -->\nhello there' body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->\nhello there"
}) })
}) })
it('without comment body and previous body', async () => { it("without comment body and previous body", async () => {
expect(await updateComment(octokit, repo, 456, '', '')).toBeUndefined() expect(await updateComment(octokit, repo, 456, "", "")).toBeUndefined()
expect(octokit.rest.issues.updateComment).not.toBeCalled() 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 let octokit
beforeEach(() => { beforeEach(() => {
@ -149,34 +149,34 @@ describe('createComment', () => {
} }
}) })
it('with comment body or previousBody', async () => { it("with comment body or previousBody", async () => {
expect( expect(
await createComment(octokit, repo, 456, 'hello there', '') await createComment(octokit, repo, 456, "hello there", "")
).toBeUndefined() ).toBeUndefined()
expect(octokit.rest.issues.createComment).toBeCalledWith({ expect(octokit.rest.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment', repo: "sticky-pull-request-comment",
body: 'hello there\n<!-- Sticky Pull Request Comment -->' body: "hello there\n<!-- Sticky Pull Request Comment -->"
}) })
expect( expect(
await createComment(octokit, repo, 456, 'hello there', 'TypeA') await createComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined() ).toBeUndefined()
expect(octokit.rest.issues.createComment).toBeCalledWith({ expect(octokit.rest.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment', repo: "sticky-pull-request-comment",
body: 'hello there\n<!-- Sticky Pull Request CommentTypeA -->' body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
}) })
}) })
it('without comment body and previousBody', async () => { it("without comment body and previousBody", async () => {
expect(await createComment(octokit, repo, 456, '', '')).toBeUndefined() expect(await createComment(octokit, repo, 456, "", "")).toBeUndefined()
expect(octokit.rest.issues.createComment).not.toBeCalled() 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 = { const octokit: any = {
rest: { rest: {
issues: { issues: {
@ -187,7 +187,7 @@ it('deleteComment', async () => {
expect(await deleteComment(octokit, repo, 456)).toBeUndefined() expect(await deleteComment(octokit, repo, 456)).toBeUndefined()
expect(octokit.rest.issues.deleteComment).toBeCalledWith({ expect(octokit.rest.issues.deleteComment).toBeCalledWith({
comment_id: 456, comment_id: 456,
owner: 'marocchino', owner: "marocchino",
repo: 'sticky-pull-request-comment' repo: "sticky-pull-request-comment"
}) })
}) })

View file

@ -1,131 +1,131 @@
beforeEach(() => { beforeEach(() => {
process.env['GITHUB_REPOSITORY'] = 'marocchino/stick-pull-request-comment' process.env["GITHUB_REPOSITORY"] = "marocchino/stick-pull-request-comment"
process.env['INPUT_NUMBER'] = '123' process.env["INPUT_NUMBER"] = "123"
process.env['INPUT_APPEND'] = 'false' process.env["INPUT_APPEND"] = "false"
process.env['INPUT_RECREATE'] = 'false' process.env["INPUT_RECREATE"] = "false"
process.env['INPUT_DELETE'] = 'false' process.env["INPUT_DELETE"] = "false"
process.env['INPUT_GITHUB_TOKEN'] = 'some-token' process.env["INPUT_GITHUB_TOKEN"] = "some-token"
}) })
afterEach(() => { afterEach(() => {
jest.resetModules() jest.resetModules()
delete process.env['GITHUB_REPOSITORY'] delete process.env["GITHUB_REPOSITORY"]
delete process.env['INPUT_REPO'] delete process.env["INPUT_REPO"]
delete process.env['INPUT_HEADER'] delete process.env["INPUT_HEADER"]
delete process.env['INPUT_MESSAGE'] delete process.env["INPUT_MESSAGE"]
delete process.env['INPUT_NUMBER'] delete process.env["INPUT_NUMBER"]
delete process.env['INPUT_APPEND'] delete process.env["INPUT_APPEND"]
delete process.env['INPUT_RECREATE'] delete process.env["INPUT_RECREATE"]
delete process.env['INPUT_DELETE'] delete process.env["INPUT_DELETE"]
delete process.env['INPUT_GITHUB_TOKEN'] delete process.env["INPUT_GITHUB_TOKEN"]
delete process.env['INPUT_PATH'] delete process.env["INPUT_PATH"]
}) })
test('repo', () => { test("repo", () => {
process.env['INPUT_REPO'] = 'other' process.env["INPUT_REPO"] = "other"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'other'}, repo: {owner: "marocchino", repo: "other"},
body: '', body: "",
header: '', header: "",
append: false, append: false,
recreate: false, recreate: false,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
test('header', () => { test("header", () => {
process.env['INPUT_HEADER'] = 'header' process.env["INPUT_HEADER"] = "header"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: '', body: "",
header: 'header', header: "header",
append: false, append: false,
recreate: false, recreate: false,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
test('append', () => { test("append", () => {
process.env['INPUT_APPEND'] = 'true' process.env["INPUT_APPEND"] = "true"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: '', body: "",
header: '', header: "",
append: true, append: true,
recreate: false, recreate: false,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
test('recreate', () => { test("recreate", () => {
process.env['INPUT_RECREATE'] = 'true' process.env["INPUT_RECREATE"] = "true"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: '', body: "",
header: '', header: "",
append: false, append: false,
recreate: true, recreate: true,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
test('delete', () => { test("delete", () => {
process.env['INPUT_DELETE'] = 'true' process.env["INPUT_DELETE"] = "true"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: '', body: "",
header: '', header: "",
append: false, append: false,
recreate: false, recreate: false,
deleteOldComment: true, deleteOldComment: true,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
describe('path', () => { describe("path", () => {
test('when exists return content of a file', () => { test("when exists return content of a file", () => {
process.env['INPUT_PATH'] = './__tests__/assets/result' process.env["INPUT_PATH"] = "./__tests__/assets/result"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: 'hi there\n', body: "hi there\n",
header: '', header: "",
append: false, append: false,
recreate: false, recreate: false,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
test('when not exists return null string', () => { test("when not exists return null string", () => {
process.env['INPUT_PATH'] = './__tests__/assets/not_exists' process.env["INPUT_PATH"] = "./__tests__/assets/not_exists"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: '', body: "",
header: '', header: "",
append: false, append: false,
recreate: false, recreate: false,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })
}) })
test('message', () => { test("message", () => {
process.env['INPUT_MESSAGE'] = 'hello there' process.env["INPUT_MESSAGE"] = "hello there"
expect(require('../src/config')).toMatchObject({ expect(require("../src/config")).toMatchObject({
pullRequestNumber: expect.any(Number), pullRequestNumber: expect.any(Number),
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'}, repo: {owner: "marocchino", repo: "stick-pull-request-comment"},
body: 'hello there', body: "hello there",
header: '', header: "",
append: false, append: false,
recreate: false, recreate: false,
deleteOldComment: false, deleteOldComment: false,
githubToken: 'some-token' githubToken: "some-token"
}) })
}) })

View file

@ -1,5 +1,5 @@
import * as core from '@actions/core' import * as core from "@actions/core"
import {GitHub} from '@actions/github/lib/utils' import {GitHub} from "@actions/github/lib/utils"
function headerComment(header: String): string { function headerComment(header: String): string {
return `<!-- Sticky Pull Request Comment${header} -->` return `<!-- Sticky Pull Request Comment${header} -->`
@ -33,7 +33,7 @@ export async function updateComment(
previousBody?: string previousBody?: string
): Promise<void> { ): Promise<void> {
if (!body && !previousBody) if (!body && !previousBody)
return core.warning('Comment body cannot be blank') return core.warning("Comment body cannot be blank")
await octokit.rest.issues.updateComment({ await octokit.rest.issues.updateComment({
...repo, ...repo,
@ -55,7 +55,7 @@ export async function createComment(
previousBody?: string previousBody?: string
): Promise<void> { ): Promise<void> {
if (!body && !previousBody) if (!body && !previousBody)
return core.warning('Comment body cannot be blank') return core.warning("Comment body cannot be blank")
await octokit.rest.issues.createComment({ await octokit.rest.issues.createComment({
...repo, ...repo,

View file

@ -1,37 +1,37 @@
import * as core from '@actions/core' import * as core from "@actions/core"
import {context} from '@actions/github' import {context} from "@actions/github"
import {readFileSync} from 'fs' import {readFileSync} from "fs"
export const pullRequestNumber = export const pullRequestNumber =
context?.payload?.pull_request?.number || context?.payload?.pull_request?.number ||
+core.getInput('number', {required: false}) +core.getInput("number", {required: false})
export const repo = buildRepo() export const repo = buildRepo()
export const header = core.getInput('header', {required: false}) export const header = core.getInput("header", {required: false})
export const append = core.getBooleanInput('append', {required: true}) export const append = core.getBooleanInput("append", {required: true})
export const recreate = core.getBooleanInput('recreate', {required: true}) export const recreate = core.getBooleanInput("recreate", {required: true})
export const deleteOldComment = core.getBooleanInput('delete', {required: true}) export const deleteOldComment = core.getBooleanInput("delete", {required: true})
export const githubToken = core.getInput('GITHUB_TOKEN', {required: true}) export const githubToken = core.getInput("GITHUB_TOKEN", {required: true})
export const body = buildBody() export const body = buildBody()
function buildRepo(): {repo: string; owner: string} { function buildRepo(): {repo: string; owner: string} {
return { return {
owner: context.repo.owner, 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 { function buildBody(): string {
const path = core.getInput('path', {required: false}) const path = core.getInput("path", {required: false})
if (path) { if (path) {
try { try {
return readFileSync(path, 'utf-8') return readFileSync(path, "utf-8")
} catch (error) { } catch (error) {
core.setFailed(error.message) core.setFailed(error.message)
return '' return ""
} }
} else { } else {
return core.getInput('message', {required: false}) return core.getInput("message", {required: false})
} }
} }

View file

@ -1,11 +1,11 @@
import * as core from '@actions/core' import * as core from "@actions/core"
import * as github from '@actions/github' import * as github from "@actions/github"
import { import {
findPreviousComment, findPreviousComment,
createComment, createComment,
updateComment, updateComment,
deleteComment deleteComment
} from './comment' } from "./comment"
import { import {
pullRequestNumber, pullRequestNumber,
repo, repo,
@ -15,21 +15,21 @@ import {
recreate, recreate,
deleteOldComment, deleteOldComment,
githubToken githubToken
} from './config' } from "./config"
async function run(): Promise<undefined> { async function run(): Promise<undefined> {
if (isNaN(pullRequestNumber) || pullRequestNumber < 1) { if (isNaN(pullRequestNumber) || pullRequestNumber < 1) {
core.info('no pull request numbers given: skip step') core.info("no pull request numbers given: skip step")
return return
} }
try { try {
if (!deleteOldComment && !body) { 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) { 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) const octokit = github.getOctokit(githubToken)