fix: replace use of any type (#1251)

This commit is contained in:
Peter Evans 2022-09-21 15:42:50 +09:00 committed by GitHub
parent 171dd555b9
commit 3f9dbd5a76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 23 deletions

View file

@ -243,8 +243,8 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
}
}
}
} catch (error: any) {
core.setFailed(error.message)
} catch (error) {
core.setFailed(utils.getErrorMessage(error))
} finally {
// Remove auth and restore persisted auth config if it existed
core.startGroup('Restore persisted git credentials')

View file

@ -3,6 +3,7 @@ import * as fs from 'fs'
import {GitCommandManager} from './git-command-manager'
import * as path from 'path'
import {URL} from 'url'
import * as utils from './utils'
export class GitAuthHelper {
private git: GitCommandManager
@ -33,8 +34,8 @@ export class GitAuthHelper {
try {
await this.setExtraheaderConfig(this.persistedExtraheaderConfigValue)
core.info('Persisted git credentials restored')
} catch (e: any) {
core.warning(e)
} catch (e) {
core.warning(utils.getErrorMessage(e))
}
}
}

View file

@ -1,6 +1,7 @@
import * as core from '@actions/core'
import {Inputs} from './create-pull-request'
import {Octokit, OctokitOptions} from './octokit-client'
import * as utils from './utils'
const ERROR_PR_REVIEW_FROM_AUTHOR =
'Review cannot be requested from pull request author'
@ -64,10 +65,9 @@ export class GitHubHelper {
html_url: pull.html_url,
created: true
}
} catch (e: any) {
} catch (e) {
if (
e.message &&
e.message.includes(`A pull request already exists for`)
utils.getErrorMessage(e).includes(`A pull request already exists for`)
) {
core.info(`A pull request already exists for ${headBranch}`)
} else {
@ -169,8 +169,8 @@ export class GitHubHelper {
pull_number: pull.number,
...requestReviewersParams
})
} catch (e: any) {
if (e.message && e.message.includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
} catch (e) {
if (utils.getErrorMessage(e).includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
core.warning(ERROR_PR_REVIEW_FROM_AUTHOR)
} else {
throw e

View file

@ -30,8 +30,8 @@ async function run(): Promise<void> {
core.debug(`Inputs: ${inspect(inputs)}`)
await createPullRequest(inputs)
} catch (error: any) {
core.setFailed(error.message)
} catch (error) {
core.setFailed(utils.getErrorMessage(error))
}
}

View file

@ -134,13 +134,15 @@ export function fileExistsSync(path: string): boolean {
let stats: fs.Stats
try {
stats = fs.statSync(path)
} catch (error: any) {
if (error.code === 'ENOENT') {
} catch (error) {
if (hasErrorCode(error) && error.code === 'ENOENT') {
return false
}
throw new Error(
`Encountered an error when checking whether path '${path}' exists: ${error.message}`
`Encountered an error when checking whether path '${path}' exists: ${getErrorMessage(
error
)}`
)
}
@ -150,3 +152,13 @@ export function fileExistsSync(path: string): boolean {
return false
}
/* eslint-disable @typescript-eslint/no-explicit-any */
function hasErrorCode(error: any): error is {code: string} {
return typeof (error && error.code) === 'string'
}
export function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
}