Skip to content

Commit bba4c33

Browse files
fix: types of octokit
1 parent 574a3e3 commit bba4c33

File tree

8 files changed

+73
-56
lines changed

8 files changed

+73
-56
lines changed

__tests__/api-helper1.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable no-magic-numbers */
22
import nock from 'nock';
33
import path from 'path';
4-
import {Utils} from '../src';
54
import {
5+
getOctokit,
66
disableNetConnect,
77
testEnv,
88
getContext,
@@ -13,9 +13,10 @@ import {
1313
exportVariableCalledWith,
1414
} from '@technote-space/github-action-test-helper';
1515
import {Logger} from '@technote-space/github-action-log-helper';
16-
import {GitCreateCommitResponseData} from '@octokit/types';
16+
import {components} from '@octokit/openapi-types';
1717
import {ApiHelper} from '../src';
1818

19+
type GitCreateCommitResponseData = components['schemas']['git-commit'];
1920
const rootDir = path.resolve(__dirname, 'fixtures');
2021
const context = getContext({
2122
ref: 'refs/heads/test',
@@ -32,7 +33,7 @@ const context = getContext({
3233
number: 123,
3334
},
3435
});
35-
const octokit = Utils.getOctokit('test-token');
36+
const octokit = getOctokit();
3637
const logger = new Logger();
3738

3839
const createCommitResponse: GitCreateCommitResponseData = {
@@ -162,6 +163,7 @@ describe('ApiHelper', () => {
162163

163164
const commit = await helper.createCommit('test commit message', {
164165
sha: 'tree-sha',
166+
truncated: true,
165167
tree: [],
166168
url: '',
167169
});
@@ -198,6 +200,7 @@ describe('ApiHelper', () => {
198200
}), logger);
199201
const commit = await helper.createCommit('test commit message', {
200202
sha: 'tree-sha',
203+
truncated: true,
201204
tree: [],
202205
url: '',
203206
});

__tests__/api-helper2.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable no-magic-numbers */
22
import nock from 'nock';
33
import path from 'path';
4-
import {Utils} from '../src';
54
import {
5+
getOctokit,
66
disableNetConnect,
77
testEnv,
88
getContext,
@@ -11,9 +11,10 @@ import {
1111
stdoutCalledWith,
1212
} from '@technote-space/github-action-test-helper';
1313
import {Logger} from '@technote-space/github-action-log-helper';
14-
import {GitCreateCommitResponseData} from '@octokit/types';
14+
import {components} from '@octokit/openapi-types';
1515
import {ApiHelper} from '../src';
1616

17+
type GitCreateCommitResponseData = components['schemas']['git-commit'];
1718
const rootDir = path.resolve(__dirname, 'fixtures');
1819
const context = getContext({
1920
ref: 'refs/heads/test',
@@ -33,7 +34,7 @@ const context = getContext({
3334
},
3435
},
3536
});
36-
const octokit = Utils.getOctokit('test-token');
37+
const octokit = getOctokit();
3738
const logger = new Logger();
3839

3940
const createCommitResponse: GitCreateCommitResponseData = {

__tests__/utils2.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import {testEnv} from '@technote-space/github-action-test-helper';
33
import {Utils} from '../src';
44

5-
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, isCommandDebug, isOutputDebug} = Utils;
6-
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, mask} = Utils;
5+
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, isCommandDebug, isOutputDebug, mask} = Utils;
6+
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, ensureNotNull} = Utils;
77

88
jest.useFakeTimers();
99

@@ -319,3 +319,14 @@ describe('isOutputDebug', () => {
319319
expect(isOutputDebug()).toBe(false);
320320
});
321321
});
322+
323+
describe('ensureNotNull', () => {
324+
it('should return value', () => {
325+
expect(ensureNotNull('test')).toBe('test');
326+
});
327+
328+
it('should return empty string', () => {
329+
expect(ensureNotNull(null)).toBe('');
330+
expect(ensureNotNull(undefined)).toBe('');
331+
});
332+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@actions/core": "^1.2.6",
3838
"@actions/github": "^4.0.0",
39+
"@octokit/openapi-types": "^2.0.0",
3940
"@octokit/plugin-rest-endpoint-methods": "^4.4.1",
4041
"@technote-space/github-action-log-helper": "^0.1.9",
4142
"shell-escape": "^0.2.0",

src/api-helper.ts

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@ import path from 'path';
33
import {Context} from '@actions/github/lib/context';
44
import {PaginateInterface} from '@octokit/plugin-paginate-rest';
55
import {RestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types';
6-
import {
7-
OctokitResponse,
8-
GitGetCommitResponseData,
9-
PullsGetResponseData,
10-
GitCreateTreeResponseData,
11-
GitCreateCommitResponseData,
12-
GitGetRefResponseData,
13-
PullsListResponseData,
14-
PullsCreateResponseData,
15-
PullsUpdateResponseData,
16-
GitListMatchingRefsResponseData,
17-
} from '@octokit/types';
6+
import {OctokitResponse} from '@octokit/types';
7+
import {components} from '@octokit/openapi-types';
188
import {exportVariable} from '@actions/core';
199
import {Logger} from '@technote-space/github-action-log-helper';
20-
import {getRefForUpdate, isPrRef, getBranch, trimRef, versionCompare, generateNewPatchVersion, generateNewMajorVersion, generateNewMinorVersion} from './utils';
10+
import {getRefForUpdate, isPrRef, getBranch, trimRef, versionCompare, generateNewPatchVersion, generateNewMajorVersion, generateNewMinorVersion, ensureNotNull} from './utils';
2111
import {getSender} from './context-helper';
2212
import {Octokit} from './types';
2313

14+
type GitGetCommitResponseData = components['schemas']['git-commit'];
15+
type PullsGetResponseData = components['schemas']['pull-request'];
16+
type GitCreateTreeResponseData = components['schemas']['git-tree'];
17+
type GitCreateCommitResponseData = components['schemas']['git-commit'];
18+
type GitGetRefResponseData = components['schemas']['git-ref'];
19+
type PullsListResponseData = components['schemas']['pull-request-simple'];
20+
type PullsCreateResponseData = components['schemas']['pull-request'];
21+
type PullsUpdateResponseData = components['schemas']['pull-request'];
22+
2423
type PullsUpdateParams = {
2524
body?: string;
2625
draft?: boolean;
@@ -163,18 +162,18 @@ export default class ApiHelper {
163162
/**
164163
* @param {string} rootDir root dir
165164
* @param {object} files files
166-
* @return {Promise<{ path: string, sha: string }[]>} blobs
165+
* @return {Promise<Array<{ path: string, sha: string }>>} blobs
167166
*/
168-
public filesToBlobs = async(rootDir: string, files: Array<string>): Promise<{ path: string; sha: string }[]> => await Promise.all(Object.values(files).map(file => this.createBlob(rootDir, file)));
167+
public filesToBlobs = async(rootDir: string, files: Array<string>): Promise<Array<{ path: string; sha: string }>> => await Promise.all(files.map(file => this.createBlob(rootDir, file)));
169168

170169
/**
171-
* @param {{ path: string, sha: string }[]} blobs blobs
170+
* @param {Array<{ path: string, sha: string }>} blobs blobs
172171
* @return {Promise<GitCreateTreeResponseData>} tree
173172
*/
174-
public createTree = async(blobs: { path: string; sha: string }[]): Promise<GitCreateTreeResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createTree({
173+
public createTree = async(blobs: Array<{ path: string; sha: string }>): Promise<GitCreateTreeResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createTree({
175174
...this.context.repo,
176175
'base_tree': (await this.getCommit()).tree.sha,
177-
tree: Object.values(blobs).map(blob => ({
176+
tree: blobs.map(blob => ({
178177
path: blob.path,
179178
type: 'blob',
180179
mode: '100644',
@@ -220,7 +219,7 @@ export default class ApiHelper {
220219
await (this.octokit as RestEndpointMethods).git.updateRef({
221220
...this.context.repo,
222221
ref: refName,
223-
sha: commit.sha,
222+
sha: ensureNotNull(commit.sha),
224223
force,
225224
});
226225

@@ -245,7 +244,7 @@ export default class ApiHelper {
245244
await (this.octokit as RestEndpointMethods).git.createRef({
246245
...this.context.repo,
247246
ref: refName,
248-
sha: commit.sha,
247+
sha: ensureNotNull(commit.sha),
249248
});
250249
};
251250

@@ -264,7 +263,7 @@ export default class ApiHelper {
264263
* @param {string} branchName branch name
265264
* @return {Promise<PullsListResponseData | null>} pull request
266265
*/
267-
public findPullRequest = async(branchName: string): Promise<PullsListResponseData[number] | null> => {
266+
public findPullRequest = async(branchName: string): Promise<PullsListResponseData | null> => {
268267
const response = await (this.octokit as RestEndpointMethods).pulls.list({
269268
...this.context.repo,
270269
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
@@ -278,9 +277,9 @@ export default class ApiHelper {
278277

279278
/**
280279
* @param {PullsListParams} params params
281-
* @return {AsyncIterable<PullsListResponseData>} pull request list
280+
* @return {AsyncIterable<Array<PullsListResponseData>>} pull request list
282281
*/
283-
public pullsList = (params: PullsListParams): Promise<PullsListResponseData> => (this.octokit.paginate as PaginateInterface)(
282+
public pullsList = (params: PullsListParams): Promise<Array<PullsListResponseData>> => (this.octokit.paginate as PaginateInterface)(
284283
(this.octokit as RestEndpointMethods).pulls.list,
285284
Object.assign({
286285
sort: 'created',
@@ -403,10 +402,10 @@ export default class ApiHelper {
403402
private isProtectedBranchError = (error: Error): boolean => /required status checks?.* (is|are) expected/i.test(error.message);
404403

405404
/**
406-
* @param {string[]} files files
405+
* @param {Array<string>} files files
407406
* @return {boolean} diff?
408407
*/
409-
private checkDiff = (files: string[]): boolean => {
408+
private checkDiff = (files: Array<string>): boolean => {
410409
if (!files.length) {
411410
this.callLogger(logger => logger.info('There is no diff.'));
412411
return false;
@@ -418,10 +417,10 @@ export default class ApiHelper {
418417
/**
419418
* @param {string} rootDir root dir
420419
* @param {string} commitMessage commit message
421-
* @param {string[]} files files
420+
* @param {Array<string>} files files
422421
* @return {Promise<GitCreateCommitResponseData>} commit
423422
*/
424-
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<GitCreateCommitResponseData> => {
423+
private prepareCommit = async(rootDir: string, commitMessage: string, files: Array<string>): Promise<GitCreateCommitResponseData> => {
425424
this.callLogger(logger => logger.startProcess('Creating blobs...'));
426425
const blobs = await this.filesToBlobs(rootDir, files);
427426

@@ -435,10 +434,10 @@ export default class ApiHelper {
435434
/**
436435
* @param {string} rootDir root dir
437436
* @param {string} commitMessage commit message
438-
* @param {string[]} files files
437+
* @param {Array<string>} files files
439438
* @return {Promise<boolean>} result
440439
*/
441-
public commit = async(rootDir: string, commitMessage: string, files: string[]): Promise<boolean> => {
440+
public commit = async(rootDir: string, commitMessage: string, files: Array<string>): Promise<boolean> => {
442441
if (!this.checkDiff(files)) {
443442
return false;
444443
}
@@ -459,12 +458,12 @@ export default class ApiHelper {
459458
/**
460459
* @param {string} rootDir root dir
461460
* @param {string} commitMessage commit message
462-
* @param {string[]} files files
461+
* @param {Array<string>} files files
463462
* @param {string} createBranchName branch name
464463
* @param {PullsCreateParams} detail detail
465464
* @return {Promise<boolean|PullsInfo>} result
466465
*/
467-
public createPR = async(rootDir: string, commitMessage: string, files: string[], createBranchName: string, detail: PullsCreateParams): Promise<boolean | PullsInfo> => {
466+
public createPR = async(rootDir: string, commitMessage: string, files: Array<string>, createBranchName: string, detail: PullsCreateParams): Promise<boolean | PullsInfo> => {
468467
if (!this.checkDiff(files)) {
469468
return false;
470469
}
@@ -530,8 +529,8 @@ export default class ApiHelper {
530529

531530
return {
532531
login: user.login,
533-
email: user.email,
534-
name: user.name,
532+
email: ensureNotNull(user.email),
533+
name: ensureNotNull(user.name),
535534
id: user.id,
536535
};
537536
};
@@ -552,7 +551,7 @@ export default class ApiHelper {
552551
...this.context.repo,
553552
ref: 'tags/',
554553
},
555-
)).map((item): string => trimRef((item as GitListMatchingRefsResponseData[number]).ref));
554+
)).map((item): string => trimRef(item.ref));
556555

557556
/**
558557
* @return {Promise<string>} tag

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,5 @@ export const replaceVariables = async(string: string, variables: { key: string;
256256
export const isCommandDebug = (): boolean => getInput('UTILS_COMMAND_DEBUG') === 'true';
257257

258258
export const isOutputDebug = (): boolean => getInput('UTILS_OUTPUT_DEBUG') === 'true';
259+
260+
export const ensureNotNull = (value: string | null | undefined): string => value ?? '';

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
/* Basic Options */
44
// "incremental": true, /* Enable incremental compilation */
5-
"target": "es6",
5+
"target": "ES2019",
66
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
77
"module": "commonjs",
88
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */

yarn.lock

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@
717717
dependencies:
718718
"@octokit/types" "^6.0.1"
719719

720-
"@octokit/plugin-rest-endpoint-methods@^4.0.0", "@octokit/plugin-rest-endpoint-methods@^4.2.1", "@octokit/plugin-rest-endpoint-methods@^4.4.1":
720+
"@octokit/plugin-rest-endpoint-methods@^4.0.0", "@octokit/plugin-rest-endpoint-methods@^4.4.1":
721721
version "4.4.1"
722722
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-4.4.1.tgz#105cf93255432155de078c9efc33bd4e14d1cd63"
723723
integrity sha512-+v5PcvrUcDeFXf8hv1gnNvNLdm4C0+2EiuWt9EatjjUmfriM1pTMM+r4j1lLHxeBQ9bVDmbywb11e3KjuavieA==
@@ -770,22 +770,22 @@
770770
dependencies:
771771
"@sinonjs/commons" "^1.7.0"
772772

773-
"@technote-space/github-action-log-helper@^0.1.8":
774-
version "0.1.8"
775-
resolved "https://registry.yarnpkg.com/@technote-space/github-action-log-helper/-/github-action-log-helper-0.1.8.tgz#73b8e1d90a3e81f60d122f0988d0ffc8cae04590"
776-
integrity sha512-xGXMF18RWPebt92CzgE0F/dKojLJAj1GCoun+BI6OfIWYA9itu+FgGQXHeykniX24v2PvfFtuoyNfll44tKxKQ==
773+
"@technote-space/github-action-log-helper@^0.1.9":
774+
version "0.1.9"
775+
resolved "https://registry.yarnpkg.com/@technote-space/github-action-log-helper/-/github-action-log-helper-0.1.9.tgz#dffa42319cb6aee8365bd1240edbe91b65e184dc"
776+
integrity sha512-OqxsuOTYctvfZTZETf3CXPwX1/n4BPOyynlufEplTTu73kpEbl9WQDKMDEYyCQPXuSi8x9QyIk6jW/3VTGNsYw==
777777
dependencies:
778778
"@actions/core" "^1.2.6"
779779
sprintf-js "^1.1.2"
780780

781-
"@technote-space/github-action-test-helper@^0.6.4":
782-
version "0.6.4"
783-
resolved "https://registry.yarnpkg.com/@technote-space/github-action-test-helper/-/github-action-test-helper-0.6.4.tgz#dd8a97666254ba02782320d6a12dfce47d6f332e"
784-
integrity sha512-WLzgkL6OjWWiU/mYRmXSj3gFQihu9emdbTWTHo7ymCGBAf+nkrWH0kn+hYeu4AQH+HkKVGSPC2aSqh/NsAJ2VA==
781+
"@technote-space/github-action-test-helper@^0.6.5":
782+
version "0.6.5"
783+
resolved "https://registry.yarnpkg.com/@technote-space/github-action-test-helper/-/github-action-test-helper-0.6.5.tgz#cb5a938e09508edce1d5e939d42dfb16276ad895"
784+
integrity sha512-yy28r9Ny0yWjKaKZcqxkoGvWPZbKrJ95tWXoG13GSLqDunCBIZqR+hjEshcaG4GL3fCyglM8aHbhTmQWlwLiMA==
785785
dependencies:
786786
"@actions/core" "^1.2.6"
787787
"@actions/github" "^4.0.0"
788-
"@octokit/plugin-rest-endpoint-methods" "^4.2.1"
788+
"@octokit/plugin-rest-endpoint-methods" "^4.4.1"
789789
js-yaml "^3.14.0"
790790

791791
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7":
@@ -1560,9 +1560,9 @@ copy-descriptor@^0.1.0:
15601560
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=
15611561

15621562
core-js@^3.6.1:
1563-
version "3.8.0"
1564-
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.8.0.tgz#0fc2d4941cadf80538b030648bb64d230b4da0ce"
1565-
integrity sha512-W2VYNB0nwQQE7tKS7HzXd7r2y/y2SVJl4ga6oH/dnaLFzM0o2lB2P3zCkWj5Wc/zyMYjtgd5Hmhk0ObkQFZOIA==
1563+
version "3.8.1"
1564+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.8.1.tgz#f51523668ac8a294d1285c3b9db44025fda66d47"
1565+
integrity sha512-9Id2xHY1W7m8hCl8NkhQn5CufmF/WuR30BTRewvCXc1aZd3kMECwNZ69ndLbekKfakw9Rf2Xyc+QR6E7Gg+obg==
15661566

15671567
[email protected], core-util-is@~1.0.0:
15681568
version "1.0.2"

0 commit comments

Comments
 (0)