Fix lint findings

This commit is contained in:
Alex Miller 2024-09-11 13:04:01 +12:00
parent 00461c6632
commit 19fa779d55
6 changed files with 34 additions and 28 deletions

View File

@ -1,7 +1,7 @@
import {describe, expect, test} from 'vitest'
import {Filter} from '../src/filter'
import {File, ChangeStatus} from '../src/file'
import {Filter} from '../src/filter'
describe('yaml filter parsing tests', () => {
test('throws if yaml is not a dictionary', () => {
@ -26,7 +26,7 @@ describe('matching tests', () => {
const yaml = `
src: "src/**/*.js"
`
let filter = new Filter(yaml)
const filter = new Filter(yaml)
const files = modified(['src/app/module/file.js'])
const match = filter.match(files)
expect(match.src).toEqual(files)

View File

@ -1,10 +1,10 @@
import {describe, expect, test} from 'vitest'
import * as git from '../src/git'
import {ChangeStatus} from '../src/file'
import * as git from '../src/git'
describe('parsing output of the git diff command', () => {
test('parseGitDiffOutput returns files with correct change status', async () => {
test('parseGitDiffOutput returns files with correct change status', () => {
const files = git.parseGitDiffOutput(
'A\u0000LICENSE\u0000' + 'M\u0000src/index.ts\u0000' + 'D\u0000src/main.ts\u0000'
)

View File

@ -3,12 +3,12 @@ import {exec as execImpl, ExecOptions} from '@actions/exec'
// Wraps original exec() function
// Returns exit code and whole stdout/stderr
export default async function exec(commandLine: string, args?: string[], options?: ExecOptions): Promise<ExecResult> {
options = options || {}
options = options ?? {}
let stdout = ''
let stderr = ''
options.listeners = {
stdout: (data: Buffer) => (stdout += data.toString()),
stderr: (data: Buffer) => (stderr += data.toString())
stdout: (data: Buffer): string => (stdout += data.toString()),
stderr: (data: Buffer): string => (stderr += data.toString())
}
const code = await execImpl(commandLine, args, options)
return {code, stdout, stderr}

View File

@ -1,11 +1,10 @@
import * as jsyaml from 'js-yaml'
import micromatch from 'micromatch'
import {File} from './file'
// Type definition of object we expect to load from YAML
interface FilterYaml {
[name: string]: FilterItemYaml
}
type FilterYaml = Record<string, FilterItemYaml>
type FilterItemYaml =
| string // Filename pattern, e.g. "path/to/*.js"
| FilterItemYaml[] // Supports referencing another rule via YAML anchor
@ -15,12 +14,10 @@ const MatchOptions: micromatch.Options = {
dot: true
}
export interface FilterResults {
[key: string]: File[]
}
export type FilterResults = Partial<Record<string, File[]>>
export class Filter {
rules: {[key: string]: string[]} = {}
rules: Record<string, string[]> = {}
// Creates instance of Filter and load rules from YAML if it's provided
constructor(yaml?: string) {
@ -47,9 +44,9 @@ export class Filter {
match(files: File[]): FilterResults {
const result: FilterResults = {}
const filesMap = files.reduce((result, x) => {
result.set(x.filename, x)
return result
const filesMap = files.reduce((fileResult, x) => {
fileResult.set(x.filename, x)
return fileResult
}, new Map<string, File>())
for (const [key, patterns] of Object.entries(this.rules)) {

View File

@ -1,5 +1,6 @@
import exec from './exec'
import * as core from '@actions/core'
import exec from './exec'
import {File, ChangeStatus} from './file'
export const NULL_SHA = '0000000000000000000000000000000000000000'
@ -215,7 +216,7 @@ async function getLocalRef(shortName: string): Promise<string | undefined> {
const output = (await exec('git', ['show-ref', shortName], {ignoreReturnCode: true})).stdout
const refs = output
.split(/\r?\n/g)
.map(l => l.match(/refs\/(?:(?:heads)|(?:tags)|(?:remotes\/origin))\/(.*)$/))
.map(l => /refs\/(?:(?:heads)|(?:tags)|(?:remotes\/origin))\/(.*)$/.exec(l))
.filter(match => match !== null && match[1] === shortName)
.map(match => match?.[0] ?? '') // match can't be null here but compiler doesn't understand that
@ -260,7 +261,7 @@ function fixStdOutNullTermination(): void {
core.info('')
}
const statusMap: {[char: string]: ChangeStatus} = {
const statusMap: Record<string, ChangeStatus> = {
A: ChangeStatus.Added,
C: ChangeStatus.Copied,
D: ChangeStatus.Deleted,

View File

@ -1,13 +1,14 @@
import * as fs from 'fs'
import * as fs from 'node:fs'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {PullRequest, PushEvent} from '@octokit/webhooks-types'
import {Filter, FilterResults} from './filter'
import {File, ChangeStatus} from './file'
import {Filter, FilterResults} from './filter'
import * as git from './git'
import {backslashEscape, shellEscape} from './list-format/shell-escape'
import {csvEscape} from './list-format/csv-escape'
import {backslashEscape, shellEscape} from './list-format/shell-escape'
type ExportFormat = 'none' | 'csv' | 'json' | 'shell' | 'escape'
@ -95,7 +96,10 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
}
async function getChangedFilesFromGit(base: string, head: string, initialFetchDepth: number): Promise<File[]> {
const defaultBranch = github.context.payload.repository?.default_branch
const defaultBranch =
typeof github.context.payload.repository?.default_branch === 'string'
? github.context.payload.repository.default_branch
: ''
const beforeSha = github.context.eventName === 'push' ? (github.context.payload as PushEvent).before : null
@ -174,6 +178,7 @@ async function getChangedFilesFromApi(token: string, prNumber: PullRequest): Pro
page
})
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (response.status !== 200) {
throw new Error(`Fetching list of changed files from GitHub API failed with error code ${response.status}`)
}
@ -195,8 +200,9 @@ async function getChangedFilesFromApi(token: string, prNumber: PullRequest): Pro
status: ChangeStatus.Added
})
files.push({
// 'previous_filename' for some unknown reason isn't in the type definition or documentation
filename: (<any>row).previous_filename as string,
// Existing behaviour, possibly a bug
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
filename: row.previous_filename!,
status: ChangeStatus.Deleted
})
} else {
@ -220,6 +226,8 @@ function exportResults(results: FilterResults, format: ExportFormat): void {
core.info('Results:')
const changes = []
for (const [key, files] of Object.entries(results)) {
if (!files) continue
const value = files.length > 0
core.startGroup(`Filter ${key} = ${value}`)
if (files.length > 0) {
@ -241,7 +249,7 @@ function exportResults(results: FilterResults, format: ExportFormat): void {
core.endGroup()
}
if (results['changes'] === undefined) {
if (results.changes === undefined) {
const changesJson = JSON.stringify(changes)
core.info(`Changes output set to ${changesJson}`)
core.setOutput('changes', changesJson)
@ -270,4 +278,4 @@ function isExportFormat(value: string): value is ExportFormat {
return ['none', 'csv', 'shell', 'json', 'escape'].includes(value)
}
run()
;((): Promise<void> => run())()