From 789d7a604e0db1e170a6f3b8d64decbb9836d738 Mon Sep 17 00:00:00 2001 From: Iceflower Date: Mon, 15 Jan 2024 23:50:17 +0100 Subject: [PATCH] [ts] Use double quotes --- typescript/.prettierrc.json | 2 +- typescript/package-lock.json | 61 +++++++++++++++--------------- typescript/package.json | 1 + typescript/src/commit-validator.ts | 22 +++++------ typescript/src/commit.ts | 22 +++++------ typescript/src/gh-utils.ts | 43 +++++++++++---------- typescript/src/main.ts | 22 +++++------ typescript/src/utils.ts | 14 +++---- typescript/src/validation.ts | 24 +++++------- 9 files changed, 105 insertions(+), 106 deletions(-) diff --git a/typescript/.prettierrc.json b/typescript/.prettierrc.json index b3107a2..cce3d2b 100644 --- a/typescript/.prettierrc.json +++ b/typescript/.prettierrc.json @@ -3,7 +3,7 @@ "tabWidth": 4, "useTabs": false, "semi": false, - "singleQuote": true, + "singleQuote": false, "trailingComma": "none", "bracketSpacing": false, "arrowParens": "avoid" diff --git a/typescript/package-lock.json b/typescript/package-lock.json index 9bcc386..5de981a 100644 --- a/typescript/package-lock.json +++ b/typescript/package-lock.json @@ -19,6 +19,7 @@ "@vercel/ncc": "^0.38.1", "eslint": "^8.56.0", "eslint-plugin-github": "^4.10.1", + "eslint-plugin-prettier": "^5.1.3", "js-yaml": "^4.1.0", "prettier": "3.2.2", "typescript": "^5.3.3" @@ -1443,36 +1444,6 @@ "eslint": "^8.0.1" } }, - "node_modules/eslint-plugin-github/node_modules/eslint-plugin-prettier": { - "version": "5.1.3", - "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-5.1.3.tgz", - "integrity": "sha512-C9GCVAs4Eq7ZC/XFQHITLiHJxQngdtraXaM+LoUFoFp/lHNl2Zn8f3WQbe9HvTBBQ9YnKFB0/2Ajdqwo5D1EAw==", - "dev": true, - "dependencies": { - "prettier-linter-helpers": "^1.0.0", - "synckit": "^0.8.6" - }, - "engines": { - "node": "^14.18.0 || >=16.0.0" - }, - "funding": { - "url": "https://opencollective.com/eslint-plugin-prettier" - }, - "peerDependencies": { - "@types/eslint": ">=8.0.0", - "eslint": ">=8.0.0", - "eslint-config-prettier": "*", - "prettier": ">=3.0.0" - }, - "peerDependenciesMeta": { - "@types/eslint": { - "optional": true - }, - "eslint-config-prettier": { - "optional": true - } - } - }, "node_modules/eslint-plugin-i18n-text": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/eslint-plugin-i18n-text/-/eslint-plugin-i18n-text-1.0.1.tgz", @@ -1582,6 +1553,36 @@ "node": ">=5.0.0" } }, + "node_modules/eslint-plugin-prettier": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-5.1.3.tgz", + "integrity": "sha512-C9GCVAs4Eq7ZC/XFQHITLiHJxQngdtraXaM+LoUFoFp/lHNl2Zn8f3WQbe9HvTBBQ9YnKFB0/2Ajdqwo5D1EAw==", + "dev": true, + "dependencies": { + "prettier-linter-helpers": "^1.0.0", + "synckit": "^0.8.6" + }, + "engines": { + "node": "^14.18.0 || >=16.0.0" + }, + "funding": { + "url": "https://opencollective.com/eslint-plugin-prettier" + }, + "peerDependencies": { + "@types/eslint": ">=8.0.0", + "eslint": ">=8.0.0", + "eslint-config-prettier": "*", + "prettier": ">=3.0.0" + }, + "peerDependenciesMeta": { + "@types/eslint": { + "optional": true + }, + "eslint-config-prettier": { + "optional": true + } + } + }, "node_modules/eslint-rule-documentation": { "version": "1.0.23", "resolved": "https://registry.npmjs.org/eslint-rule-documentation/-/eslint-rule-documentation-1.0.23.tgz", diff --git a/typescript/package.json b/typescript/package.json index ce65681..8844ce8 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -34,6 +34,7 @@ "@typescript-eslint/parser": "^6.19.0", "@vercel/ncc": "^0.38.1", "eslint": "^8.56.0", + "eslint-plugin-prettier": "^5.1.3", "eslint-plugin-github": "^4.10.1", "js-yaml": "^4.1.0", "prettier": "3.2.2", diff --git a/typescript/src/commit-validator.ts b/typescript/src/commit-validator.ts index eec6186..29fc68e 100644 --- a/typescript/src/commit-validator.ts +++ b/typescript/src/commit-validator.ts @@ -1,17 +1,17 @@ -import {Commit} from './commit' +import {Commit} from "./commit" export enum Status { - Failure = 'Failure', - Warning = 'Warning', - Ok = 'Correct' + Failure = "Failure", + Warning = "Warning", + Ok = "Correct" } export class Result { status: Status = Status.Failure - message = '' + message = "" commit: Commit | undefined = undefined - constructor(status: Status, message = '', commit: Commit | undefined = undefined) { + constructor(status: Status, message = "", commit: Commit | undefined = undefined) { this.status = status this.message = message this.commit = commit @@ -35,9 +35,9 @@ export class Result { if (this.commit !== undefined) { msg += ` | ${this.commit.hexsha} - ${this.commit.summary()}` } - if (this.message !== '') { + if (this.message !== "") { if (this.commit === undefined) { - msg += ' |' + msg += " |" } msg += `\n : ${this.message}` } @@ -52,9 +52,9 @@ export class CommitValidator { } static split_message(message: string): [string, string] { - const res: string[] = message.split('\n', 1) + const res: string[] = message.split("\n", 1) if (res.length === 1) { - return [res[0], ''] + return [res[0], ""] } return [res[0], res[1]] } @@ -71,6 +71,6 @@ export class CommitValidator { } validate_message(_summary: string, _description: string): Result { - return new Result(Status.Ok, '') + return new Result(Status.Ok, "") } } diff --git a/typescript/src/commit.ts b/typescript/src/commit.ts index b60825a..4ce2659 100644 --- a/typescript/src/commit.ts +++ b/typescript/src/commit.ts @@ -5,9 +5,9 @@ export class User { constructor(data: object) { type key = keyof typeof data - this.email = data['email' as key] - this.name = data['name' as key] - this.username = data['username' as key] + this.email = data["email" as key] + this.name = data["name" as key] + this.username = data["username" as key] } } @@ -21,22 +21,22 @@ export class Commit { constructor(commit: object, sha = "", timestamp = "") { type key = keyof typeof commit - this.author = new User(commit['author' as key]) - this.committer = new User(commit['committer' as key]) - this.distinct = commit['distinct' as key] ?? false - this.hexsha = commit['id' as key] ?? sha - const timestamp_raw = commit['timestamp' as key] ?? timestamp + this.author = new User(commit["author" as key]) + this.committer = new User(commit["committer" as key]) + this.distinct = commit["distinct" as key] ?? false + this.hexsha = commit["id" as key] ?? sha + const timestamp_raw = commit["timestamp" as key] ?? timestamp if (timestamp_raw !== undefined) { this.timestamp = new Date(timestamp_raw) } - this.message = commit['message' as key] + this.message = commit["message" as key] } // return empty string if message is undefined summary(): string { if (this.message === undefined) { - return '' + return "" } - return this.message.split('\n', 1)[0] + return this.message.split("\n", 1)[0] } } diff --git a/typescript/src/gh-utils.ts b/typescript/src/gh-utils.ts index 9b40a2e..ff6cca6 100644 --- a/typescript/src/gh-utils.ts +++ b/typescript/src/gh-utils.ts @@ -1,21 +1,24 @@ // GitHub support utility. import {resolve} from "path" -import {GitHub} from "@actions/github/lib/utils"; -import * as github from "@actions/github"; -import * as core from "@actions/core"; -import fs from "fs"; -import {Commit} from "./commit"; -import {Result, Status} from "./commit-validator"; -import {pathToFileURL} from "url"; +import {GitHub} from "@actions/github/lib/utils" +import * as github from "@actions/github" +import * as core from "@actions/core" +import fs from "fs" +import {Commit} from "./commit" +import {Result, Status} from "./commit-validator" +import {pathToFileURL} from "url" // return html url to validator file and local filepath to downloaded file -export async function download_validator_file(validator_file: string, octokit: InstanceType): Promise<[string, string]> { +export async function download_validator_file( + validator_file: string, + octokit: InstanceType +): Promise<[string, string]> { const response = await octokit.rest.repos.getContent({ path: validator_file, owner: github.context.repo.owner, repo: github.context.repo.repo, - ref: github.context.sha, + ref: github.context.sha }) if (response.status !== 200) { core.error(JSON.stringify(response.data)) @@ -30,17 +33,17 @@ export async function download_validator_file(validator_file: string, octokit: I core.setFailed(`download of '${response.url}' failed`) return ["", ""] } - const buffer = Buffer.from(response.data.content, 'base64').toString('utf-8') + const buffer = Buffer.from(response.data.content, "base64").toString("utf-8") const output_path = pathToFileURL(resolve("./validator.mjs")) fs.writeFileSync(output_path, buffer) return [response.data.html_url || "", output_path.toString()] } export async function get_commit_creation(octokit: InstanceType): Promise { - const response = await octokit.request('GET /repos/{owner}/{repo}/git/commits/{commit_sha}', { + const response = await octokit.request("GET /repos/{owner}/{repo}/git/commits/{commit_sha}", { owner: github.context.repo.owner, repo: github.context.repo.repo, - commit_sha: github.context.payload.pull_request?.base.sha, + commit_sha: github.context.payload.pull_request?.base.sha }) if (response.status !== 200) { core.error(JSON.stringify(response.data)) @@ -55,16 +58,16 @@ export async function get_commit_creation(octokit: InstanceType): export async function get_commits(octokit: InstanceType): Promise { const commits: Commit[] = [] switch (github.context.eventName) { - case 'pull_request': { + case "pull_request": { const pages = Math.floor(github.context.payload.pull_request?.commits / 100) + 1 for (let page = 1; page <= pages; page++) { - const response = await octokit.request('GET /repos/{owner}/{repo}/commits', { + const response = await octokit.request("GET /repos/{owner}/{repo}/commits", { owner: github.context.payload.pull_request?.head.repo.owner.login, repo: github.context.payload.pull_request?.head.repo.name, sha: github.context.payload.pull_request?.head.ref, since: await get_commit_creation(octokit), per_page: 100, - page, + page }) if (response.status !== 200) { core.error(JSON.stringify(response.data)) @@ -80,15 +83,15 @@ export async function get_commits(octokit: InstanceType): Promise } break } - case 'push': + case "push": default: { - if ('commits' in github.context.payload && github.context.payload['commits'].length > 0) { - for (const commit of github.context.payload['commits']) { + if ("commits" in github.context.payload && github.context.payload["commits"].length > 0) { + for (const commit of github.context.payload["commits"]) { commits.push(new Commit(commit)) } // on tags or if commits was empty - } else if ('head_commit' in github.context.payload) { - commits.push(new Commit(github.context.payload['head_commit'])) + } else if ("head_commit" in github.context.payload) { + commits.push(new Commit(github.context.payload["head_commit"])) } } } diff --git a/typescript/src/main.ts b/typescript/src/main.ts index cc3654d..cc52808 100644 --- a/typescript/src/main.ts +++ b/typescript/src/main.ts @@ -1,25 +1,25 @@ -import * as core from '@actions/core' -import * as github from '@actions/github' -import {CommitValidator, Result} from './commit-validator' +import * as core from "@actions/core" +import * as github from "@actions/github" +import {CommitValidator, Result} from "./commit-validator" import {GitHub} from "@actions/github/lib/utils" -import * as utils from './utils' -import * as gh_utils from './gh-utils' +import * as utils from "./utils" +import * as gh_utils from "./gh-utils" async function run(): Promise { try { - const validator_file: string = core.getInput('validator_file') - const validator_name: string = core.getInput('validator') - const options: string[] = core.getMultilineInput('options') - const access_token: string = core.getInput('access_token') + const validator_file: string = core.getInput("validator_file") + const validator_name: string = core.getInput("validator") + const options: string[] = core.getMultilineInput("options") + const access_token: string = core.getInput("access_token") // just to be sure core.setSecret(access_token) core.debug(JSON.stringify(github.context)) - if (validator_file !== '' && validator_name !== '') { + if (validator_file !== "" && validator_name !== "") { core.setFailed("Please provide only 'validator' or 'validator_file'!") return } - if (validator_file === '' && validator_name === '') { + if (validator_file === "" && validator_name === "") { core.setFailed("Please provide either 'validator' or 'validator_file'!") return } diff --git a/typescript/src/utils.ts b/typescript/src/utils.ts index 078b787..aef62df 100644 --- a/typescript/src/utils.ts +++ b/typescript/src/utils.ts @@ -1,19 +1,19 @@ -import {Commit} from './commit' -import {CommitValidator, Result, Status} from './commit-validator' -import * as validation from './validation' +import {Commit} from "./commit" +import {CommitValidator, Result, Status} from "./commit-validator" +import * as validation from "./validation" export async function get_shipped_validator_cls(validator: string): Promise { switch (validator.toLowerCase()) { - case 'simpletag': + case "simpletag": return validation.SimpleTag - case 'regex': + case "regex": return validation.RegEx default: - throw Error('Invalid validator name!') + throw Error("Invalid validator name!") } } -const _importDynamic = new Function('modulePath', 'return import(modulePath)') +const _importDynamic = new Function("modulePath", "return import(modulePath)") export async function import_validator_cls(validator_file: string): Promise { const validation_mod = await _importDynamic(validator_file) diff --git a/typescript/src/validation.ts b/typescript/src/validation.ts index ed88519..43ed932 100644 --- a/typescript/src/validation.ts +++ b/typescript/src/validation.ts @@ -1,30 +1,27 @@ -import {CommitValidator, Result, Status} from './commit-validator' +import {CommitValidator, Result, Status} from "./commit-validator" export class SimpleTag extends CommitValidator { - private static readonly rx_parser: RegExp = new RegExp('^\\[(.*)] (.*)$') - private static readonly rx_category: RegExp = new RegExp('^\\*|(?:[a-z0-9]{2,}[ |-]?)+$') - private static readonly rx_description: RegExp = new RegExp('^[A-Z0-9]\\S*(?:\\s\\S*)+[^.!?,\\s]$') + private static readonly rx_parser: RegExp = new RegExp("^\\[(.*)] (.*)$") + private static readonly rx_category: RegExp = new RegExp("^\\*|(?:[a-z0-9]{2,}[ |-]?)+$") + private static readonly rx_description: RegExp = new RegExp("^[A-Z0-9]\\S*(?:\\s\\S*)+[^.!?,\\s]$") validate_message(summary: string, _description: string): Result { const match = SimpleTag.rx_parser.exec(summary) if (match === null) { - return new Result( - Status.Failure, - 'Summary has invalid format. It should be \'[] \'' - ) + return new Result(Status.Failure, "Summary has invalid format. It should be '[] '") } if (!SimpleTag.rx_category.test(match[1])) { return new Result( Status.Failure, "Invalid category tag. It should be either a single '*' or completely lowercase " + - "letters or numbers, at least 2 characters long, other allowed characters are: '|', '-' and spaces." + "letters or numbers, at least 2 characters long, other allowed characters are: '|', '-' and spaces." ) } if (!SimpleTag.rx_description.test(match[2])) { return new Result( Status.Failure, - 'Invalid description. It should start with an uppercase letter or number, ' + - 'should be not to short and should not end with a punctuation.' + "Invalid description. It should start with an uppercase letter or number, " + + "should be not to short and should not end with a punctuation." ) } return new Result(Status.Ok) @@ -50,10 +47,7 @@ export class RegEx extends CommitValidator { validate_message(summary: string, description: string): Result { if (this.rx_summary !== undefined && !this.rx_summary.test(summary)) { - return new Result( - Status.Failure, - `Invalid summary, does not match RegEx '${this.rx_summary.source}'` - ) + return new Result(Status.Failure, `Invalid summary, does not match RegEx '${this.rx_summary.source}'`) } if (this.rx_description !== undefined && !this.rx_description.test(description)) { return new Result(