Skip to content

Commit e083b86

Browse files
Merge pull request #341 from technote-space/release/next-v4.4.1
release: v4.4.2
2 parents bf4fa34 + 948e515 commit e083b86

File tree

8 files changed

+310
-256
lines changed

8 files changed

+310
-256
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: 26 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, objectGet} = Utils;
6+
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, mask, ensureNotNull} = Utils;
77

88
jest.useFakeTimers();
99

@@ -319,3 +319,27 @@ 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+
});
333+
334+
describe('objectGet', () => {
335+
it('should return object value', () => {
336+
expect(objectGet({test1: {test2: 123}}, 'test1')).toEqual({test2: 123});
337+
expect(objectGet({test1: {test2: 123}}, 'test1.test2')).toBe(123);
338+
});
339+
340+
it('should return undefined', () => {
341+
expect(objectGet(undefined, 'test1.test2')).toBeUndefined();
342+
expect(objectGet({test1: {test2: 123}}, '')).toBeUndefined();
343+
expect(objectGet({test1: {test2: 123}}, 'test1.test3')).toBeUndefined();
344+
});
345+
});

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "4.4.1",
3+
"version": "4.4.2",
44
"description": "Helper for GitHub Action.",
55
"keywords": [
66
"github",
@@ -36,24 +36,25 @@
3636
"dependencies": {
3737
"@actions/core": "^1.2.6",
3838
"@actions/github": "^4.0.0",
39-
"@octokit/plugin-rest-endpoint-methods": "^4.2.1",
40-
"@technote-space/github-action-log-helper": "^0.1.8",
39+
"@octokit/openapi-types": "^2.0.0",
40+
"@octokit/plugin-rest-endpoint-methods": "^4.4.1",
41+
"@technote-space/github-action-log-helper": "^0.1.9",
4142
"shell-escape": "^0.2.0",
4243
"sprintf-js": "^1.1.2"
4344
},
4445
"devDependencies": {
4546
"@commitlint/cli": "^11.0.0",
4647
"@commitlint/config-conventional": "^11.0.0",
47-
"@technote-space/github-action-test-helper": "^0.6.4",
48-
"@types/jest": "^26.0.15",
49-
"@types/node": "^14.14.9",
50-
"@typescript-eslint/eslint-plugin": "^4.8.2",
51-
"@typescript-eslint/parser": "^4.8.2",
52-
"eslint": "^7.14.0",
53-
"husky": "^4.3.0",
48+
"@technote-space/github-action-test-helper": "^0.6.5",
49+
"@types/jest": "^26.0.16",
50+
"@types/node": "^14.14.10",
51+
"@typescript-eslint/eslint-plugin": "^4.9.0",
52+
"@typescript-eslint/parser": "^4.9.0",
53+
"eslint": "^7.15.0",
54+
"husky": "^4.3.4",
5455
"jest": "^26.6.3",
5556
"jest-circus": "^26.6.3",
56-
"lint-staged": "^10.5.2",
57+
"lint-staged": "^10.5.3",
5758
"nock": "^13.0.5",
5859
"ts-jest": "^26.4.4",
5960
"typescript": "^4.1.2"

src/api-helper.ts

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@ 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 {
11+
getRefForUpdate,
12+
isPrRef,
13+
getBranch,
14+
trimRef,
15+
versionCompare,
16+
generateNewPatchVersion,
17+
generateNewMajorVersion,
18+
generateNewMinorVersion,
19+
ensureNotNull,
20+
objectGet,
21+
} from './utils';
2122
import {getSender} from './context-helper';
2223
import {Octokit} from './types';
2324

25+
type GitGetCommitResponseData = components['schemas']['git-commit'];
26+
type PullsGetResponseData = components['schemas']['pull-request'];
27+
type GitCreateTreeResponseData = components['schemas']['git-tree'];
28+
type GitCreateCommitResponseData = components['schemas']['git-commit'];
29+
type GitGetRefResponseData = components['schemas']['git-ref'];
30+
type PullsListResponseData = components['schemas']['pull-request-simple'];
31+
type PullsCreateResponseData = components['schemas']['pull-request'];
32+
type PullsUpdateResponseData = components['schemas']['pull-request'];
33+
2434
type PullsUpdateParams = {
2535
body?: string;
2636
draft?: boolean;
@@ -163,18 +173,18 @@ export default class ApiHelper {
163173
/**
164174
* @param {string} rootDir root dir
165175
* @param {object} files files
166-
* @return {Promise<{ path: string, sha: string }[]>} blobs
176+
* @return {Promise<Array<{ path: string, sha: string }>>} blobs
167177
*/
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)));
178+
public filesToBlobs = async(rootDir: string, files: Array<string>): Promise<Array<{ path: string; sha: string }>> => await Promise.all(files.map(file => this.createBlob(rootDir, file)));
169179

170180
/**
171-
* @param {{ path: string, sha: string }[]} blobs blobs
181+
* @param {Array<{ path: string, sha: string }>} blobs blobs
172182
* @return {Promise<GitCreateTreeResponseData>} tree
173183
*/
174-
public createTree = async(blobs: { path: string; sha: string }[]): Promise<GitCreateTreeResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createTree({
184+
public createTree = async(blobs: Array<{ path: string; sha: string }>): Promise<GitCreateTreeResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createTree({
175185
...this.context.repo,
176-
'base_tree': (await this.getCommit()).tree.sha,
177-
tree: Object.values(blobs).map(blob => ({
186+
'base_tree': ensureNotNull(objectGet((await this.getCommit()), 'tree.sha')),
187+
tree: blobs.map(blob => ({
178188
path: blob.path,
179189
type: 'blob',
180190
mode: '100644',
@@ -220,7 +230,7 @@ export default class ApiHelper {
220230
await (this.octokit as RestEndpointMethods).git.updateRef({
221231
...this.context.repo,
222232
ref: refName,
223-
sha: commit.sha,
233+
sha: ensureNotNull(commit.sha),
224234
force,
225235
});
226236

@@ -245,7 +255,7 @@ export default class ApiHelper {
245255
await (this.octokit as RestEndpointMethods).git.createRef({
246256
...this.context.repo,
247257
ref: refName,
248-
sha: commit.sha,
258+
sha: ensureNotNull(commit.sha),
249259
});
250260
};
251261

@@ -264,7 +274,7 @@ export default class ApiHelper {
264274
* @param {string} branchName branch name
265275
* @return {Promise<PullsListResponseData | null>} pull request
266276
*/
267-
public findPullRequest = async(branchName: string): Promise<PullsListResponseData[number] | null> => {
277+
public findPullRequest = async(branchName: string): Promise<PullsListResponseData | null> => {
268278
const response = await (this.octokit as RestEndpointMethods).pulls.list({
269279
...this.context.repo,
270280
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
@@ -278,9 +288,9 @@ export default class ApiHelper {
278288

279289
/**
280290
* @param {PullsListParams} params params
281-
* @return {AsyncIterable<PullsListResponseData>} pull request list
291+
* @return {AsyncIterable<Array<PullsListResponseData>>} pull request list
282292
*/
283-
public pullsList = (params: PullsListParams): Promise<PullsListResponseData> => (this.octokit.paginate as PaginateInterface)(
293+
public pullsList = (params: PullsListParams): Promise<Array<PullsListResponseData>> => (this.octokit.paginate as PaginateInterface)(
284294
(this.octokit as RestEndpointMethods).pulls.list,
285295
Object.assign({
286296
sort: 'created',
@@ -403,10 +413,10 @@ export default class ApiHelper {
403413
private isProtectedBranchError = (error: Error): boolean => /required status checks?.* (is|are) expected/i.test(error.message);
404414

405415
/**
406-
* @param {string[]} files files
416+
* @param {Array<string>} files files
407417
* @return {boolean} diff?
408418
*/
409-
private checkDiff = (files: string[]): boolean => {
419+
private checkDiff = (files: Array<string>): boolean => {
410420
if (!files.length) {
411421
this.callLogger(logger => logger.info('There is no diff.'));
412422
return false;
@@ -418,10 +428,10 @@ export default class ApiHelper {
418428
/**
419429
* @param {string} rootDir root dir
420430
* @param {string} commitMessage commit message
421-
* @param {string[]} files files
431+
* @param {Array<string>} files files
422432
* @return {Promise<GitCreateCommitResponseData>} commit
423433
*/
424-
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<GitCreateCommitResponseData> => {
434+
private prepareCommit = async(rootDir: string, commitMessage: string, files: Array<string>): Promise<GitCreateCommitResponseData> => {
425435
this.callLogger(logger => logger.startProcess('Creating blobs...'));
426436
const blobs = await this.filesToBlobs(rootDir, files);
427437

@@ -435,10 +445,10 @@ export default class ApiHelper {
435445
/**
436446
* @param {string} rootDir root dir
437447
* @param {string} commitMessage commit message
438-
* @param {string[]} files files
448+
* @param {Array<string>} files files
439449
* @return {Promise<boolean>} result
440450
*/
441-
public commit = async(rootDir: string, commitMessage: string, files: string[]): Promise<boolean> => {
451+
public commit = async(rootDir: string, commitMessage: string, files: Array<string>): Promise<boolean> => {
442452
if (!this.checkDiff(files)) {
443453
return false;
444454
}
@@ -459,12 +469,12 @@ export default class ApiHelper {
459469
/**
460470
* @param {string} rootDir root dir
461471
* @param {string} commitMessage commit message
462-
* @param {string[]} files files
472+
* @param {Array<string>} files files
463473
* @param {string} createBranchName branch name
464474
* @param {PullsCreateParams} detail detail
465475
* @return {Promise<boolean|PullsInfo>} result
466476
*/
467-
public createPR = async(rootDir: string, commitMessage: string, files: string[], createBranchName: string, detail: PullsCreateParams): Promise<boolean | PullsInfo> => {
477+
public createPR = async(rootDir: string, commitMessage: string, files: Array<string>, createBranchName: string, detail: PullsCreateParams): Promise<boolean | PullsInfo> => {
468478
if (!this.checkDiff(files)) {
469479
return false;
470480
}
@@ -530,8 +540,8 @@ export default class ApiHelper {
530540

531541
return {
532542
login: user.login,
533-
email: user.email,
534-
name: user.name,
543+
email: ensureNotNull(user.email),
544+
name: ensureNotNull(user.name),
535545
id: user.id,
536546
};
537547
};
@@ -552,7 +562,7 @@ export default class ApiHelper {
552562
...this.context.repo,
553563
ref: 'tags/',
554564
},
555-
)).map((item): string => trimRef((item as GitListMatchingRefsResponseData[number]).ref));
565+
)).map((item): string => trimRef(item.ref));
556566

557567
/**
558568
* @return {Promise<string>} tag

src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,22 @@ 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+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
261+
export const objectGet = <T>(value: { [key: string]: any } | undefined, key: string): T | undefined => {
262+
const keys = key.split('.');
263+
264+
if (!keys.length || !value || !(keys[0] in value)) {
265+
return undefined;
266+
}
267+
268+
// eslint-disable-next-line no-magic-numbers
269+
if (keys.length > 1) {
270+
// eslint-disable-next-line no-magic-numbers
271+
return objectGet(value[keys[0]], keys.slice(1).join('.'));
272+
}
273+
274+
return value[keys[0]];
275+
};
276+
277+
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'. */

0 commit comments

Comments
 (0)