Skip to content

Commit 3f9dbd5

Browse files
authored
fix: replace use of any type (#1251)
1 parent 171dd55 commit 3f9dbd5

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

dist/index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ function createPullRequest(inputs) {
452452
}
453453
}
454454
catch (error) {
455-
core.setFailed(error.message);
455+
core.setFailed(utils.getErrorMessage(error));
456456
}
457457
finally {
458458
// Remove auth and restore persisted auth config if it existed
@@ -507,6 +507,7 @@ const core = __importStar(__nccwpck_require__(2186));
507507
const fs = __importStar(__nccwpck_require__(7147));
508508
const path = __importStar(__nccwpck_require__(1017));
509509
const url_1 = __nccwpck_require__(7310);
510+
const utils = __importStar(__nccwpck_require__(918));
510511
class GitAuthHelper {
511512
constructor(git) {
512513
this.extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***';
@@ -531,7 +532,7 @@ class GitAuthHelper {
531532
core.info('Persisted git credentials restored');
532533
}
533534
catch (e) {
534-
core.warning(e);
535+
core.warning(utils.getErrorMessage(e));
535536
}
536537
}
537538
});
@@ -942,6 +943,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
942943
exports.GitHubHelper = void 0;
943944
const core = __importStar(__nccwpck_require__(2186));
944945
const octokit_client_1 = __nccwpck_require__(5040);
946+
const utils = __importStar(__nccwpck_require__(918));
945947
const ERROR_PR_REVIEW_FROM_AUTHOR = 'Review cannot be requested from pull request author';
946948
class GitHubHelper {
947949
constructor(token) {
@@ -976,8 +978,7 @@ class GitHubHelper {
976978
};
977979
}
978980
catch (e) {
979-
if (e.message &&
980-
e.message.includes(`A pull request already exists for`)) {
981+
if (utils.getErrorMessage(e).includes(`A pull request already exists for`)) {
981982
core.info(`A pull request already exists for ${headBranch}`);
982983
}
983984
else {
@@ -1040,7 +1041,7 @@ class GitHubHelper {
10401041
yield this.octokit.rest.pulls.requestReviewers(Object.assign(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { pull_number: pull.number }), requestReviewersParams));
10411042
}
10421043
catch (e) {
1043-
if (e.message && e.message.includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
1044+
if (utils.getErrorMessage(e).includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
10441045
core.warning(ERROR_PR_REVIEW_FROM_AUTHOR);
10451046
}
10461047
else {
@@ -1124,7 +1125,7 @@ function run() {
11241125
yield (0, create_pull_request_1.createPullRequest)(inputs);
11251126
}
11261127
catch (error) {
1127-
core.setFailed(error.message);
1128+
core.setFailed(utils.getErrorMessage(error));
11281129
}
11291130
});
11301131
}
@@ -1192,7 +1193,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
11921193
return result;
11931194
};
11941195
Object.defineProperty(exports, "__esModule", ({ value: true }));
1195-
exports.fileExistsSync = exports.parseDisplayNameEmail = exports.randomString = exports.secondsSinceEpoch = exports.getRemoteUrl = exports.getRemoteDetail = exports.getRepoPath = exports.getStringAsArray = exports.getInputAsArray = void 0;
1196+
exports.getErrorMessage = exports.fileExistsSync = exports.parseDisplayNameEmail = exports.randomString = exports.secondsSinceEpoch = exports.getRemoteUrl = exports.getRemoteDetail = exports.getRepoPath = exports.getStringAsArray = exports.getInputAsArray = void 0;
11961197
const core = __importStar(__nccwpck_require__(2186));
11971198
const fs = __importStar(__nccwpck_require__(7147));
11981199
const path = __importStar(__nccwpck_require__(1017));
@@ -1293,17 +1294,27 @@ function fileExistsSync(path) {
12931294
stats = fs.statSync(path);
12941295
}
12951296
catch (error) {
1296-
if (error.code === 'ENOENT') {
1297+
if (hasErrorCode(error) && error.code === 'ENOENT') {
12971298
return false;
12981299
}
1299-
throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error.message}`);
1300+
throw new Error(`Encountered an error when checking whether path '${path}' exists: ${getErrorMessage(error)}`);
13001301
}
13011302
if (!stats.isDirectory()) {
13021303
return true;
13031304
}
13041305
return false;
13051306
}
13061307
exports.fileExistsSync = fileExistsSync;
1308+
/* eslint-disable @typescript-eslint/no-explicit-any */
1309+
function hasErrorCode(error) {
1310+
return typeof (error && error.code) === 'string';
1311+
}
1312+
function getErrorMessage(error) {
1313+
if (error instanceof Error)
1314+
return error.message;
1315+
return String(error);
1316+
}
1317+
exports.getErrorMessage = getErrorMessage;
13071318

13081319

13091320
/***/ }),

src/create-pull-request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
243243
}
244244
}
245245
}
246-
} catch (error: any) {
247-
core.setFailed(error.message)
246+
} catch (error) {
247+
core.setFailed(utils.getErrorMessage(error))
248248
} finally {
249249
// Remove auth and restore persisted auth config if it existed
250250
core.startGroup('Restore persisted git credentials')

src/git-auth-helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as fs from 'fs'
33
import {GitCommandManager} from './git-command-manager'
44
import * as path from 'path'
55
import {URL} from 'url'
6+
import * as utils from './utils'
67

78
export class GitAuthHelper {
89
private git: GitCommandManager
@@ -33,8 +34,8 @@ export class GitAuthHelper {
3334
try {
3435
await this.setExtraheaderConfig(this.persistedExtraheaderConfigValue)
3536
core.info('Persisted git credentials restored')
36-
} catch (e: any) {
37-
core.warning(e)
37+
} catch (e) {
38+
core.warning(utils.getErrorMessage(e))
3839
}
3940
}
4041
}

src/github-helper.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core'
22
import {Inputs} from './create-pull-request'
33
import {Octokit, OctokitOptions} from './octokit-client'
4+
import * as utils from './utils'
45

56
const ERROR_PR_REVIEW_FROM_AUTHOR =
67
'Review cannot be requested from pull request author'
@@ -64,10 +65,9 @@ export class GitHubHelper {
6465
html_url: pull.html_url,
6566
created: true
6667
}
67-
} catch (e: any) {
68+
} catch (e) {
6869
if (
69-
e.message &&
70-
e.message.includes(`A pull request already exists for`)
70+
utils.getErrorMessage(e).includes(`A pull request already exists for`)
7171
) {
7272
core.info(`A pull request already exists for ${headBranch}`)
7373
} else {
@@ -169,8 +169,8 @@ export class GitHubHelper {
169169
pull_number: pull.number,
170170
...requestReviewersParams
171171
})
172-
} catch (e: any) {
173-
if (e.message && e.message.includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
172+
} catch (e) {
173+
if (utils.getErrorMessage(e).includes(ERROR_PR_REVIEW_FROM_AUTHOR)) {
174174
core.warning(ERROR_PR_REVIEW_FROM_AUTHOR)
175175
} else {
176176
throw e

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ async function run(): Promise<void> {
3030
core.debug(`Inputs: ${inspect(inputs)}`)
3131

3232
await createPullRequest(inputs)
33-
} catch (error: any) {
34-
core.setFailed(error.message)
33+
} catch (error) {
34+
core.setFailed(utils.getErrorMessage(error))
3535
}
3636
}
3737

src/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ export function fileExistsSync(path: string): boolean {
134134
let stats: fs.Stats
135135
try {
136136
stats = fs.statSync(path)
137-
} catch (error: any) {
138-
if (error.code === 'ENOENT') {
137+
} catch (error) {
138+
if (hasErrorCode(error) && error.code === 'ENOENT') {
139139
return false
140140
}
141141

142142
throw new Error(
143-
`Encountered an error when checking whether path '${path}' exists: ${error.message}`
143+
`Encountered an error when checking whether path '${path}' exists: ${getErrorMessage(
144+
error
145+
)}`
144146
)
145147
}
146148

@@ -150,3 +152,13 @@ export function fileExistsSync(path: string): boolean {
150152

151153
return false
152154
}
155+
156+
/* eslint-disable @typescript-eslint/no-explicit-any */
157+
function hasErrorCode(error: any): error is {code: string} {
158+
return typeof (error && error.code) === 'string'
159+
}
160+
161+
export function getErrorMessage(error: unknown) {
162+
if (error instanceof Error) return error.message
163+
return String(error)
164+
}

0 commit comments

Comments
 (0)