Skip to content

Commit 948e515

Browse files
fix: type error
1 parent bba4c33 commit 948e515

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

__tests__/utils2.test.ts

Lines changed: 15 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, mask} = Utils;
6-
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, ensureNotNull} = 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

@@ -330,3 +330,16 @@ describe('ensureNotNull', () => {
330330
expect(ensureNotNull(undefined)).toBe('');
331331
});
332332
});
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+
});

src/api-helper.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ import {OctokitResponse} from '@octokit/types';
77
import {components} from '@octokit/openapi-types';
88
import {exportVariable} from '@actions/core';
99
import {Logger} from '@technote-space/github-action-log-helper';
10-
import {getRefForUpdate, isPrRef, getBranch, trimRef, versionCompare, generateNewPatchVersion, generateNewMajorVersion, generateNewMinorVersion, ensureNotNull} 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';
1122
import {getSender} from './context-helper';
1223
import {Octokit} from './types';
1324

@@ -172,7 +183,7 @@ export default class ApiHelper {
172183
*/
173184
public createTree = async(blobs: Array<{ path: string; sha: string }>): Promise<GitCreateTreeResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createTree({
174185
...this.context.repo,
175-
'base_tree': (await this.getCommit()).tree.sha,
186+
'base_tree': ensureNotNull(objectGet((await this.getCommit()), 'tree.sha')),
176187
tree: blobs.map(blob => ({
177188
path: blob.path,
178189
type: 'blob',

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,21 @@ export const isCommandDebug = (): boolean => getInput('UTILS_COMMAND_DEBUG') ===
257257

258258
export const isOutputDebug = (): boolean => getInput('UTILS_OUTPUT_DEBUG') === 'true';
259259

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+
260277
export const ensureNotNull = (value: string | null | undefined): string => value ?? '';

0 commit comments

Comments
 (0)