Skip to content

Commit 4e9c6e0

Browse files
Merge pull request #223 from technote-space/release/v1.0.0
Release/v1.0.0
2 parents f000975 + e029b63 commit 4e9c6e0

File tree

11 files changed

+162
-92
lines changed

11 files changed

+162
-92
lines changed

__tests__/api-helper2.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,28 @@ describe('ApiHelper with params', () => {
212212
});
213213
});
214214
});
215+
216+
describe('ApiHelper without logger', () => {
217+
disableNetConnect(nock);
218+
testEnv();
219+
220+
const helper = new ApiHelper(octokit, context, undefined, {branch: 'test-branch', sender: 'test-sender', refForUpdate: 'test-ref', suppressBPError: true});
221+
222+
describe('updateRef', () => {
223+
it('should not output warning', async() => {
224+
const mockStdout = spyOnStdout();
225+
nock('https://api.github.com')
226+
.patch('/repos/hello/world/git/refs/' + encodeURIComponent('test-ref'), body => {
227+
expect(body).toHaveProperty('sha');
228+
return body;
229+
})
230+
.reply(403, {
231+
'message': 'Required status check "Test" is expected.',
232+
});
233+
234+
await helper.updateRef(createCommitResponse, 'test-ref', false);
235+
236+
stdoutCalledWith(mockStdout, []);
237+
});
238+
});
239+
});

__tests__/context-helper.test.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
import { testEnv, getContext } from '@technote-space/github-action-test-helper';
88
import { Logger, ContextHelper } from '../src';
99

10-
const {isRelease, isPush, isPr, isIssue, isCron, isCustomEvent, getGitUrl, getRepository, getTagName, getSender, showActionInfo} = ContextHelper;
10+
const {isRelease, isPush, isPr, isIssue, isCron, isCustomEvent, isCreateTag, getGitUrl, getRepository, getTagName, getSender, showActionInfo} = ContextHelper;
1111

1212
describe('isRelease', () => {
1313
it('should return true', () => {
@@ -93,6 +93,32 @@ describe('isCustomEvent', () => {
9393
});
9494
});
9595

96+
describe('isCreateTag', () => {
97+
it('should return true', () => {
98+
expect(isCreateTag(getContext({
99+
eventName: 'create',
100+
payload: {
101+
'ref_type': 'tag',
102+
},
103+
}))).toBe(true);
104+
});
105+
106+
it('should return false 1', () => {
107+
expect(isCreateTag(getContext({
108+
eventName: 'create',
109+
payload: {
110+
'ref_type': 'branch',
111+
},
112+
}))).toBe(false);
113+
});
114+
115+
it('should return false 2', () => {
116+
expect(isCreateTag(getContext({
117+
eventName: 'release',
118+
}))).toBe(false);
119+
});
120+
});
121+
96122
describe('getGitUrl', () => {
97123
testEnv();
98124

@@ -221,7 +247,7 @@ describe('showActionInfo', () => {
221247
stdoutCalledWith(mockStdout, [
222248
'',
223249
'==================================================',
224-
'Version: v1.2.3',
250+
'Version: v1.2.3(undefined)',
225251
'Event: push',
226252
'Action: rerequested',
227253
'sha: test-sha',
@@ -262,7 +288,7 @@ describe('showActionInfo', () => {
262288
stdoutCalledWith(mockStdout, [
263289
'',
264290
'==================================================',
265-
'Version: hello/[email protected]',
291+
'Version: hello/[email protected](162553222be3497f057501e028b47afc64944d84)',
266292
'Event: push',
267293
'Action: rerequested',
268294
'sha: test-sha',

__tests__/fixtures/build2.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
{
2-
"owner": "hello",
3-
"repo": "world",
4-
"sha": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2",
5-
"ref": "release/v1.2.3",
6-
"tagName": "v1.2.3",
7-
"branch": "gh-actions",
8-
"tags": [
9-
"v1.2.3",
10-
"v1",
11-
"v1.2"
12-
],
13-
"updated_at": "2020-01-01T01:23:45.000Z"
14-
}
1+
{
2+
"owner": "hello",
3+
"repo": "world",
4+
"sha": "162553222be3497f057501e028b47afc64944d84",
5+
"ref": "release/v1.2.3",
6+
"tagName": "v1.2.3",
7+
"branch": "gh-actions",
8+
"tags": [
9+
"v1.2.3",
10+
"v1",
11+
"v1.2"
12+
],
13+
"updated_at": "2020-01-01T01:23:45.000Z"
14+
}

__tests__/fixtures/test/build.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
{
2-
"owner": "hello",
3-
"repo": "world",
4-
"sha": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2",
5-
"ref": "release/v1.2.3",
6-
"tagName": "v1.2.3",
7-
"branch": "gh-actions",
8-
"tags": [
9-
"v1.2.3",
10-
"v1",
11-
"v1.2"
12-
],
13-
"updated_at": "2020-01-01T01:23:45.000Z"
14-
}
1+
{
2+
"owner": "hello",
3+
"repo": "world",
4+
"sha": "162553222be3497f057501e028b47afc64944d84",
5+
"ref": "release/v1.2.3",
6+
"tagName": "v1.2.3",
7+
"branch": "gh-actions",
8+
"tags": [
9+
"v1.2.3",
10+
"v1",
11+
"v1.2"
12+
],
13+
"updated_at": "2020-01-01T01:23:45.000Z"
14+
}

__tests__/git-helper.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,20 +603,20 @@ describe('GitHelper', () => {
603603
it('should run push', async() => {
604604
const mockExec = spyOnExec();
605605

606-
await helper.push(workDir, 'test-branch', true, context());
606+
await helper.push(workDir, 'test-branch', context(), {withTag: true, args: ['--prune', '--verbose']});
607607

608608
execCalledWith(mockExec, [
609-
'git push --tags \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
609+
'git push --tags --prune --verbose \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
610610
]);
611611
});
612612

613613
it('should run push without tags', async() => {
614614
const mockExec = spyOnExec();
615615

616-
await helper.push(workDir, 'test-branch', false, context());
616+
await helper.push(workDir, 'test-branch', context());
617617

618618
execCalledWith(mockExec, [
619-
'git push \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
619+
'git push \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
620620
]);
621621
});
622622
});
@@ -628,7 +628,7 @@ describe('GitHelper', () => {
628628
await helper.forcePush(workDir, 'test-branch', context());
629629

630630
execCalledWith(mockExec, [
631-
'git push --force \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
631+
'git push --force \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
632632
]);
633633
});
634634
});

__tests__/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ describe('getBuildInfo', () => {
275275
expect(getBuildInfo(path.resolve(__dirname, 'fixtures', 'build2.json'))).toEqual({
276276
'owner': 'hello',
277277
'repo': 'world',
278-
'sha': 'ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2',
278+
'sha': '162553222be3497f057501e028b47afc64944d84',
279279
'ref': 'release/v1.2.3',
280280
'tagName': 'v1.2.3',
281281
'branch': 'gh-actions',

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "0.8.7",
3+
"version": "1.0.0",
44
"description": "Helper to filter GitHub Action.",
55
"author": {
66
"name": "Technote",
@@ -32,7 +32,7 @@
3232
"sprintf-js": "^1.1.2"
3333
},
3434
"devDependencies": {
35-
"@technote-space/github-action-test-helper": "^0.2.0",
35+
"@technote-space/github-action-test-helper": "^0.2.1",
3636
"@types/jest": "^25.1.2",
3737
"@types/node": "^13.7.0",
3838
"@typescript-eslint/eslint-plugin": "^2.19.0",

src/api-helper.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class ApiHelper {
5959
constructor(
6060
private readonly octokit: Octokit,
6161
private readonly context: Context,
62-
private readonly logger: Logger,
62+
private readonly logger?: Logger,
6363
options?: { branch?: string; sender?: string; refForUpdate?: string; suppressBPError?: boolean },
6464
) {
6565
this.branch = options?.branch;
@@ -68,6 +68,15 @@ export default class ApiHelper {
6868
this.suppressBPError = options?.suppressBPError;
6969
}
7070

71+
/**
72+
* @param {function} caller caller
73+
*/
74+
private callLogger = (caller: (logger: Logger) => void): void => {
75+
if (this.logger) {
76+
caller(this.logger);
77+
}
78+
};
79+
7180
/**
7281
* @return {string|boolean} sender
7382
*/
@@ -204,7 +213,7 @@ export default class ApiHelper {
204213
return true;
205214
} catch (error) {
206215
if (this.suppressBPError === true && this.isProtectedBranchError(error)) {
207-
this.logger.warn('Branch is protected.');
216+
this.callLogger(logger => logger.warn('Branch is protected.'));
208217
} else {
209218
throw error;
210219
}
@@ -319,6 +328,18 @@ export default class ApiHelper {
319328
return {branchName, headName, refName};
320329
};
321330

331+
/**
332+
* @param {string} createBranchName branch name
333+
* @param {PullsCreateParams} detail detail
334+
* @return {Promise<PullsInfo>} info
335+
*/
336+
private createPulls = async(createBranchName: string, detail: PullsCreateParams): Promise<PullsInfo> => {
337+
this.callLogger(async logger => logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
338+
const created = await this.pullsCreate(createBranchName, detail);
339+
this.callLogger(logger => logger.endProcess());
340+
return Object.assign({isPrCreated: true}, created.data);
341+
};
342+
322343
/**
323344
* @param {string} createBranchName branch name
324345
* @param {PullsCreateParams} detail detail
@@ -327,16 +348,13 @@ export default class ApiHelper {
327348
public pullsCreateOrUpdate = async(createBranchName: string, detail: PullsCreateParams): Promise<PullsInfo> => {
328349
const pullRequest = await this.findPullRequest(createBranchName);
329350
if (pullRequest) {
330-
this.logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
351+
this.callLogger(async logger => logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
331352
const updated = await this.pullsUpdate(pullRequest.number, detail);
332-
this.logger.endProcess();
353+
this.callLogger(logger => logger.endProcess());
333354
return Object.assign({isPrCreated: false}, updated.data);
334-
} else {
335-
this.logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
336-
const created = await this.pullsCreate(createBranchName, detail);
337-
this.logger.endProcess();
338-
return Object.assign({isPrCreated: true}, created.data);
339355
}
356+
357+
return this.createPulls(createBranchName, detail);
340358
};
341359

342360
/**
@@ -347,16 +365,13 @@ export default class ApiHelper {
347365
public pullsCreateOrComment = async(createBranchName: string, detail: PullsCreateParams): Promise<PullsInfo> => {
348366
const pullRequest = await this.findPullRequest(createBranchName);
349367
if (pullRequest) {
350-
this.logger.startProcess('Creating comment to PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
368+
this.callLogger(async logger => logger.startProcess('Creating comment to PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
351369
await this.createCommentToPr(createBranchName, detail.body);
352-
this.logger.endProcess();
370+
this.callLogger(logger => logger.endProcess());
353371
return Object.assign({isPrCreated: false}, pullRequest);
354-
} else {
355-
this.logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
356-
const created = await this.pullsCreate(createBranchName, detail);
357-
this.logger.endProcess();
358-
return Object.assign({isPrCreated: true}, created.data);
359372
}
373+
374+
return this.createPulls(createBranchName, detail);
360375
};
361376

362377
/**
@@ -396,7 +411,7 @@ export default class ApiHelper {
396411
*/
397412
private checkDiff = (files: string[]): boolean => {
398413
if (!files.length) {
399-
this.logger.info('There is no diff.');
414+
this.callLogger(logger => logger.info('There is no diff.'));
400415
return false;
401416
}
402417

@@ -410,13 +425,13 @@ export default class ApiHelper {
410425
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
411426
*/
412427
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => {
413-
this.logger.startProcess('Creating blobs...');
428+
this.callLogger(logger => logger.startProcess('Creating blobs...'));
414429
const blobs = await this.filesToBlobs(rootDir, files);
415430

416-
this.logger.startProcess('Creating tree...');
431+
this.callLogger(logger => logger.startProcess('Creating tree...'));
417432
const tree = await this.createTree(blobs);
418433

419-
this.logger.startProcess('Creating commit... [%s]', tree.data.sha);
434+
this.callLogger(logger => logger.startProcess('Creating commit... [%s]', tree.data.sha));
420435
return this.createCommit(commitMessage, tree);
421436
};
422437

@@ -434,13 +449,13 @@ export default class ApiHelper {
434449
const commit = await this.prepareCommit(rootDir, commitMessage, files);
435450
const ref = await this.getRefForUpdate(true);
436451

437-
this.logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha);
452+
this.callLogger(logger => logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha));
438453
if (await this.updateRef(commit, ref, false)) {
439454
process.env.GITHUB_SHA = commit.data.sha;
440455
exportVariable('GITHUB_SHA', commit.data.sha);
441456
}
442457

443-
this.logger.endProcess();
458+
this.callLogger(logger => logger.endProcess());
444459
return true;
445460
};
446461

@@ -461,10 +476,10 @@ export default class ApiHelper {
461476
const commit = await this.prepareCommit(rootDir, commitMessage, files);
462477
const ref = await this.getRef(headName);
463478
if (null === ref) {
464-
this.logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha);
479+
this.callLogger(logger => logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha));
465480
await this.createRef(commit, refName);
466481
} else {
467-
this.logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha);
482+
this.callLogger(logger => logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha));
468483
await this.updateRef(commit, headName, true);
469484
}
470485

@@ -479,7 +494,7 @@ export default class ApiHelper {
479494
const {branchName, headName, refName} = this.getBranchInfo(createBranchName);
480495
const pullRequest = await this.findPullRequest(branchName);
481496
if (pullRequest) {
482-
this.logger.startProcess('Closing PullRequest... [%s]', branchName);
497+
this.callLogger(logger => logger.startProcess('Closing PullRequest... [%s]', branchName));
483498
if (message) {
484499
await this.createCommentToPr(branchName, message);
485500
}
@@ -489,18 +504,18 @@ export default class ApiHelper {
489504
base: undefined,
490505
});
491506
} else {
492-
this.logger.info('There is no PullRequest named [%s]', branchName);
507+
this.callLogger(logger => logger.info('There is no PullRequest named [%s]', branchName));
493508

494509
const ref = await this.getRef(headName);
495510
if (!ref) {
496-
this.logger.info('There is no reference named [%s]', refName);
511+
this.callLogger(logger => logger.info('There is no reference named [%s]', refName));
497512
return;
498513
}
499514
}
500515

501-
this.logger.startProcess('Deleting reference... [%s]', refName);
516+
this.callLogger(logger => logger.startProcess('Deleting reference... [%s]', refName));
502517
await this.deleteRef(headName);
503-
this.logger.endProcess();
518+
this.callLogger(logger => logger.endProcess());
504519
};
505520

506521
/**

0 commit comments

Comments
 (0)