Skip to content

Commit

Permalink
[ts] Use double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
IceflowRE committed Jan 15, 2024
1 parent 9896a46 commit 789d7a6
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 106 deletions.
2 changes: 1 addition & 1 deletion typescript/.prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tabWidth": 4,
"useTabs": false,
"semi": false,
"singleQuote": true,
"singleQuote": false,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid"
Expand Down
61 changes: 31 additions & 30 deletions typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 11 additions & 11 deletions typescript/src/commit-validator.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`
}
Expand All @@ -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]]
}
Expand All @@ -71,6 +71,6 @@ export class CommitValidator {
}

validate_message(_summary: string, _description: string): Result {
return new Result(Status.Ok, '')
return new Result(Status.Ok, "")
}
}
22 changes: 11 additions & 11 deletions typescript/src/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand All @@ -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]
}
}
43 changes: 23 additions & 20 deletions typescript/src/gh-utils.ts
Original file line number Diff line number Diff line change
@@ -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<typeof GitHub>): Promise<[string, string]> {
export async function download_validator_file(
validator_file: string,
octokit: InstanceType<typeof GitHub>
): 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))
Expand All @@ -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<typeof GitHub>): Promise<string> {
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))
Expand All @@ -55,16 +58,16 @@ export async function get_commit_creation(octokit: InstanceType<typeof GitHub>):
export async function get_commits(octokit: InstanceType<typeof GitHub>): Promise<Commit[]> {
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))
Expand All @@ -80,15 +83,15 @@ export async function get_commits(octokit: InstanceType<typeof GitHub>): 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"]))
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions typescript/src/main.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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
}
Expand Down
14 changes: 7 additions & 7 deletions typescript/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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<typeof CommitValidator> {
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<typeof CommitValidator> {
const validation_mod = await _importDynamic(validator_file)
Expand Down
Loading

0 comments on commit 789d7a6

Please sign in to comment.