From 2b8ebea84509062fc6bc1e6555098dd6dfbd1990 Mon Sep 17 00:00:00 2001 From: Technote Date: Sat, 8 Feb 2020 17:19:13 +0900 Subject: [PATCH 1/6] feat: update packages --- package.json | 2 +- yarn.lock | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0eb15096..d8fc2bb5 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "sprintf-js": "^1.1.2" }, "devDependencies": { - "@technote-space/github-action-test-helper": "^0.2.0", + "@technote-space/github-action-test-helper": "^0.2.1", "@types/jest": "^25.1.2", "@types/node": "^13.7.0", "@typescript-eslint/eslint-plugin": "^2.19.0", diff --git a/yarn.lock b/yarn.lock index 26049e52..31b5c9ea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -450,10 +450,10 @@ dependencies: type-detect "4.0.8" -"@technote-space/github-action-test-helper@^0.2.0": - version "0.2.0" - resolved "https://registry.yarnpkg.com/@technote-space/github-action-test-helper/-/github-action-test-helper-0.2.0.tgz#6a5dc8599fac75c19c3b7562a278427d44b17d7f" - integrity sha512-xxQEh4MiJJg/x4nSknEJVoLPYx2tdkCGaAGrhdg/SmHfWgipDdcP+HxC8FQLRzJHJdDfTOS7YV/+hRaDkStOTw== +"@technote-space/github-action-test-helper@^0.2.1": + version "0.2.1" + resolved "https://registry.yarnpkg.com/@technote-space/github-action-test-helper/-/github-action-test-helper-0.2.1.tgz#06869400e36b32a93eb73ab15cfd2a6354e9358e" + integrity sha512-BSaLwFazl9ZrUCZyoYDhL+3RoTXLWezi2BYHRHwvOypdvKaEABMn64IvqOFkl9jLs3Z5DIApOQcHyXqhNTrW/g== dependencies: "@actions/github" "^2.1.0" js-yaml "^3.13.1" @@ -3963,9 +3963,9 @@ v8-compile-cache@^2.0.3: integrity sha512-usZBT3PW+LOjM25wbqIlZwPeJV+3OSz3M1k1Ws8snlW39dZyYL9lOGC5FgPVHfk0jKmjiDV8Z0mIbVQPiwFs7g== v8-to-istanbul@^4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-4.0.1.tgz#d6a2a3823b8ff49bdf2167ff2a45d82dff81d02f" - integrity sha512-x0yZvZAkjJwdD3fPiJzYP37aod0ati4LlmD2RmpKjqewjKAov/u/ytZ8ViIZb07cN4cePKzl9ijiUi7C1LQ8hQ== + version "4.1.1" + resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-4.1.1.tgz#cc2a6737bb6ee2941ceed38027d7b93083dc9a3a" + integrity sha512-eDRcudafj/GAV2MSoDNmsV/deeIY9bJ94QuVOzZDEbBd4uX+5v/kODX+p/fqZ74/VYLK1BZv8BPJlNnCV8vDhw== dependencies: "@types/istanbul-lib-coverage" "^2.0.1" convert-source-map "^1.6.0" From 8b30046da567136e4e42b8b8a32cdf158c09753b Mon Sep 17 00:00:00 2001 From: Technote Date: Sat, 8 Feb 2020 17:49:17 +0900 Subject: [PATCH 2/6] feat: add create tag check --- __tests__/context-helper.test.ts | 28 +++++++++++++++++++++++++++- src/context-helper.ts | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/__tests__/context-helper.test.ts b/__tests__/context-helper.test.ts index 4bc311f5..2d62df40 100644 --- a/__tests__/context-helper.test.ts +++ b/__tests__/context-helper.test.ts @@ -7,7 +7,7 @@ import { import { testEnv, getContext } from '@technote-space/github-action-test-helper'; import { Logger, ContextHelper } from '../src'; -const {isRelease, isPush, isPr, isIssue, isCron, isCustomEvent, getGitUrl, getRepository, getTagName, getSender, showActionInfo} = ContextHelper; +const {isRelease, isPush, isPr, isIssue, isCron, isCustomEvent, isCreateTag, getGitUrl, getRepository, getTagName, getSender, showActionInfo} = ContextHelper; describe('isRelease', () => { it('should return true', () => { @@ -93,6 +93,32 @@ describe('isCustomEvent', () => { }); }); +describe('isCreateTag', () => { + it('should return true', () => { + expect(isCreateTag(getContext({ + eventName: 'create', + payload: { + 'ref_type': 'tag', + }, + }))).toBe(true); + }); + + it('should return false 1', () => { + expect(isCreateTag(getContext({ + eventName: 'create', + payload: { + 'ref_type': 'branch', + }, + }))).toBe(false); + }); + + it('should return false 2', () => { + expect(isCreateTag(getContext({ + eventName: 'release', + }))).toBe(false); + }); +}); + describe('getGitUrl', () => { testEnv(); diff --git a/src/context-helper.ts b/src/context-helper.ts index 8e92327f..d22f1676 100644 --- a/src/context-helper.ts +++ b/src/context-helper.ts @@ -15,6 +15,8 @@ export const isCron = (context: Context): boolean => 'schedule' === context.even export const isCustomEvent = (context: Context): boolean => 'repository_dispatch' === context.eventName; +export const isCreateTag = (context: Context): boolean => 'create' === context.eventName && 'tag' === context.payload.ref_type; + export const getTagName = (context: Context): string => isRelease(context) ? context.payload.release.tag_name : (/^refs\/tags\//.test(context.ref) ? context.ref.replace(/^refs\/tags\//, '') : ''); export const getSender = (context: Context): string | false => context.payload.sender && context.payload.sender.type === 'User' ? context.payload.sender.login : false; From 17d743d61af7d19d4d0915f47a55db321b37d6c7 Mon Sep 17 00:00:00 2001 From: Technote Date: Sat, 8 Feb 2020 17:49:44 +0900 Subject: [PATCH 3/6] feat: Update package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d8fc2bb5..abb385d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/github-action-helper", - "version": "0.8.7", + "version": "0.8.8", "description": "Helper to filter GitHub Action.", "author": { "name": "Technote", From 74d0467e346f9128b1127dcc86bab4e47eedd300 Mon Sep 17 00:00:00 2001 From: Technote Date: Sun, 9 Feb 2020 13:54:43 +0900 Subject: [PATCH 4/6] feat: use api helper without logger --- __tests__/api-helper2.test.ts | 25 ++++++++++++ src/api-helper.ts | 73 +++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 29 deletions(-) diff --git a/__tests__/api-helper2.test.ts b/__tests__/api-helper2.test.ts index e8e8fef9..452241c1 100644 --- a/__tests__/api-helper2.test.ts +++ b/__tests__/api-helper2.test.ts @@ -212,3 +212,28 @@ describe('ApiHelper with params', () => { }); }); }); + +describe('ApiHelper without logger', () => { + disableNetConnect(nock); + testEnv(); + + const helper = new ApiHelper(octokit, context, undefined, {branch: 'test-branch', sender: 'test-sender', refForUpdate: 'test-ref', suppressBPError: true}); + + describe('updateRef', () => { + it('should not output warning', async() => { + const mockStdout = spyOnStdout(); + nock('https://api.github.com') + .patch('/repos/hello/world/git/refs/' + encodeURIComponent('test-ref'), body => { + expect(body).toHaveProperty('sha'); + return body; + }) + .reply(403, { + 'message': 'Required status check "Test" is expected.', + }); + + await helper.updateRef(createCommitResponse, 'test-ref', false); + + stdoutCalledWith(mockStdout, []); + }); + }); +}); diff --git a/src/api-helper.ts b/src/api-helper.ts index e6cd77ab..7844f425 100644 --- a/src/api-helper.ts +++ b/src/api-helper.ts @@ -59,7 +59,7 @@ export default class ApiHelper { constructor( private readonly octokit: Octokit, private readonly context: Context, - private readonly logger: Logger, + private readonly logger?: Logger, options?: { branch?: string; sender?: string; refForUpdate?: string; suppressBPError?: boolean }, ) { this.branch = options?.branch; @@ -68,6 +68,15 @@ export default class ApiHelper { this.suppressBPError = options?.suppressBPError; } + /** + * @param {function} caller caller + */ + private callLogger = (caller: (logger: Logger) => void): void => { + if (this.logger) { + caller(this.logger); + } + }; + /** * @return {string|boolean} sender */ @@ -204,7 +213,7 @@ export default class ApiHelper { return true; } catch (error) { if (this.suppressBPError === true && this.isProtectedBranchError(error)) { - this.logger.warn('Branch is protected.'); + this.callLogger(logger => logger.warn('Branch is protected.')); } else { throw error; } @@ -319,6 +328,18 @@ export default class ApiHelper { return {branchName, headName, refName}; }; + /** + * @param {string} createBranchName branch name + * @param {PullsCreateParams} detail detail + * @return {Promise} info + */ + private createPulls = async(createBranchName: string, detail: PullsCreateParams): Promise => { + this.callLogger(async logger => logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false))); + const created = await this.pullsCreate(createBranchName, detail); + this.callLogger(logger => logger.endProcess()); + return Object.assign({isPrCreated: true}, created.data); + }; + /** * @param {string} createBranchName branch name * @param {PullsCreateParams} detail detail @@ -327,16 +348,13 @@ export default class ApiHelper { public pullsCreateOrUpdate = async(createBranchName: string, detail: PullsCreateParams): Promise => { const pullRequest = await this.findPullRequest(createBranchName); if (pullRequest) { - this.logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)); + this.callLogger(async logger => logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false))); const updated = await this.pullsUpdate(pullRequest.number, detail); - this.logger.endProcess(); + this.callLogger(logger => logger.endProcess()); return Object.assign({isPrCreated: false}, updated.data); - } else { - this.logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)); - const created = await this.pullsCreate(createBranchName, detail); - this.logger.endProcess(); - return Object.assign({isPrCreated: true}, created.data); } + + return this.createPulls(createBranchName, detail); }; /** @@ -347,16 +365,13 @@ export default class ApiHelper { public pullsCreateOrComment = async(createBranchName: string, detail: PullsCreateParams): Promise => { const pullRequest = await this.findPullRequest(createBranchName); if (pullRequest) { - this.logger.startProcess('Creating comment to PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)); + this.callLogger(async logger => logger.startProcess('Creating comment to PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false))); await this.createCommentToPr(createBranchName, detail.body); - this.logger.endProcess(); + this.callLogger(logger => logger.endProcess()); return Object.assign({isPrCreated: false}, pullRequest); - } else { - this.logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)); - const created = await this.pullsCreate(createBranchName, detail); - this.logger.endProcess(); - return Object.assign({isPrCreated: true}, created.data); } + + return this.createPulls(createBranchName, detail); }; /** @@ -396,7 +411,7 @@ export default class ApiHelper { */ private checkDiff = (files: string[]): boolean => { if (!files.length) { - this.logger.info('There is no diff.'); + this.callLogger(logger => logger.info('There is no diff.')); return false; } @@ -410,13 +425,13 @@ export default class ApiHelper { * @return {Promise>} commit */ private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise> => { - this.logger.startProcess('Creating blobs...'); + this.callLogger(logger => logger.startProcess('Creating blobs...')); const blobs = await this.filesToBlobs(rootDir, files); - this.logger.startProcess('Creating tree...'); + this.callLogger(logger => logger.startProcess('Creating tree...')); const tree = await this.createTree(blobs); - this.logger.startProcess('Creating commit... [%s]', tree.data.sha); + this.callLogger(logger => logger.startProcess('Creating commit... [%s]', tree.data.sha)); return this.createCommit(commitMessage, tree); }; @@ -434,13 +449,13 @@ export default class ApiHelper { const commit = await this.prepareCommit(rootDir, commitMessage, files); const ref = await this.getRefForUpdate(true); - this.logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha); + this.callLogger(logger => logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha)); if (await this.updateRef(commit, ref, false)) { process.env.GITHUB_SHA = commit.data.sha; exportVariable('GITHUB_SHA', commit.data.sha); } - this.logger.endProcess(); + this.callLogger(logger => logger.endProcess()); return true; }; @@ -461,10 +476,10 @@ export default class ApiHelper { const commit = await this.prepareCommit(rootDir, commitMessage, files); const ref = await this.getRef(headName); if (null === ref) { - this.logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha); + this.callLogger(logger => logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha)); await this.createRef(commit, refName); } else { - this.logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha); + this.callLogger(logger => logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha)); await this.updateRef(commit, headName, true); } @@ -479,7 +494,7 @@ export default class ApiHelper { const {branchName, headName, refName} = this.getBranchInfo(createBranchName); const pullRequest = await this.findPullRequest(branchName); if (pullRequest) { - this.logger.startProcess('Closing PullRequest... [%s]', branchName); + this.callLogger(logger => logger.startProcess('Closing PullRequest... [%s]', branchName)); if (message) { await this.createCommentToPr(branchName, message); } @@ -489,18 +504,18 @@ export default class ApiHelper { base: undefined, }); } else { - this.logger.info('There is no PullRequest named [%s]', branchName); + this.callLogger(logger => logger.info('There is no PullRequest named [%s]', branchName)); const ref = await this.getRef(headName); if (!ref) { - this.logger.info('There is no reference named [%s]', refName); + this.callLogger(logger => logger.info('There is no reference named [%s]', refName)); return; } } - this.logger.startProcess('Deleting reference... [%s]', refName); + this.callLogger(logger => logger.startProcess('Deleting reference... [%s]', refName)); await this.deleteRef(headName); - this.logger.endProcess(); + this.callLogger(logger => logger.endProcess()); }; /** From 390ae16190e97fb13d6ac52d0e3b0467b45804be Mon Sep 17 00:00:00 2001 From: Technote Date: Sun, 9 Feb 2020 14:01:00 +0900 Subject: [PATCH 5/6] feat: build sha (#219) --- __tests__/context-helper.test.ts | 4 ++-- __tests__/fixtures/build2.json | 28 ++++++++++++++-------------- __tests__/fixtures/test/build.json | 28 ++++++++++++++-------------- __tests__/utils.test.ts | 2 +- src/context-helper.ts | 4 ++-- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/__tests__/context-helper.test.ts b/__tests__/context-helper.test.ts index 2d62df40..28d54712 100644 --- a/__tests__/context-helper.test.ts +++ b/__tests__/context-helper.test.ts @@ -247,7 +247,7 @@ describe('showActionInfo', () => { stdoutCalledWith(mockStdout, [ '', '==================================================', - 'Version: v1.2.3', + 'Version: v1.2.3(undefined)', 'Event: push', 'Action: rerequested', 'sha: test-sha', @@ -288,7 +288,7 @@ describe('showActionInfo', () => { stdoutCalledWith(mockStdout, [ '', '==================================================', - 'Version: hello/world@v1.2.3', + 'Version: hello/world@v1.2.3(162553222be3497f057501e028b47afc64944d84)', 'Event: push', 'Action: rerequested', 'sha: test-sha', diff --git a/__tests__/fixtures/build2.json b/__tests__/fixtures/build2.json index 2ed0531f..e8971352 100644 --- a/__tests__/fixtures/build2.json +++ b/__tests__/fixtures/build2.json @@ -1,14 +1,14 @@ -{ - "owner": "hello", - "repo": "world", - "sha": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2", - "ref": "release/v1.2.3", - "tagName": "v1.2.3", - "branch": "gh-actions", - "tags": [ - "v1.2.3", - "v1", - "v1.2" - ], - "updated_at": "2020-01-01T01:23:45.000Z" -} \ No newline at end of file +{ + "owner": "hello", + "repo": "world", + "sha": "162553222be3497f057501e028b47afc64944d84", + "ref": "release/v1.2.3", + "tagName": "v1.2.3", + "branch": "gh-actions", + "tags": [ + "v1.2.3", + "v1", + "v1.2" + ], + "updated_at": "2020-01-01T01:23:45.000Z" +} diff --git a/__tests__/fixtures/test/build.json b/__tests__/fixtures/test/build.json index 2ed0531f..e8971352 100644 --- a/__tests__/fixtures/test/build.json +++ b/__tests__/fixtures/test/build.json @@ -1,14 +1,14 @@ -{ - "owner": "hello", - "repo": "world", - "sha": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2", - "ref": "release/v1.2.3", - "tagName": "v1.2.3", - "branch": "gh-actions", - "tags": [ - "v1.2.3", - "v1", - "v1.2" - ], - "updated_at": "2020-01-01T01:23:45.000Z" -} \ No newline at end of file +{ + "owner": "hello", + "repo": "world", + "sha": "162553222be3497f057501e028b47afc64944d84", + "ref": "release/v1.2.3", + "tagName": "v1.2.3", + "branch": "gh-actions", + "tags": [ + "v1.2.3", + "v1", + "v1.2" + ], + "updated_at": "2020-01-01T01:23:45.000Z" +} diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 82798ae5..46d64d3b 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -275,7 +275,7 @@ describe('getBuildInfo', () => { expect(getBuildInfo(path.resolve(__dirname, 'fixtures', 'build2.json'))).toEqual({ 'owner': 'hello', 'repo': 'world', - 'sha': 'ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2', + 'sha': '162553222be3497f057501e028b47afc64944d84', 'ref': 'release/v1.2.3', 'tagName': 'v1.2.3', 'branch': 'gh-actions', diff --git a/src/context-helper.ts b/src/context-helper.ts index d22f1676..98d0d50d 100644 --- a/src/context-helper.ts +++ b/src/context-helper.ts @@ -38,9 +38,9 @@ export const showActionInfo = (rootDir: string, logger: Logger, context: Context logger.log(separator); if (false !== info) { if ('owner' in info) { - logger.log('Version: %s/%s@%s', info.owner, info.repo, info.tagName); + logger.log('Version: %s/%s@%s(%s)', info.owner, info.repo, info.tagName, info.sha); } else { - logger.log('Version: %s', info.tagName); + logger.log('Version: %s(%s)', info.tagName, info.sha); } } logger.log('Event: %s', context.eventName); From 39e7d2ce92630b11c5f17becaa4ec0d84ff4957c Mon Sep 17 00:00:00 2001 From: technote-space Date: Sun, 9 Feb 2020 05:04:31 +0000 Subject: [PATCH 6/6] chore: update npm dependencies --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 31b5c9ea..ac124c74 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3963,9 +3963,9 @@ v8-compile-cache@^2.0.3: integrity sha512-usZBT3PW+LOjM25wbqIlZwPeJV+3OSz3M1k1Ws8snlW39dZyYL9lOGC5FgPVHfk0jKmjiDV8Z0mIbVQPiwFs7g== v8-to-istanbul@^4.0.1: - version "4.1.1" - resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-4.1.1.tgz#cc2a6737bb6ee2941ceed38027d7b93083dc9a3a" - integrity sha512-eDRcudafj/GAV2MSoDNmsV/deeIY9bJ94QuVOzZDEbBd4uX+5v/kODX+p/fqZ74/VYLK1BZv8BPJlNnCV8vDhw== + version "4.1.2" + resolved "https://registry.yarnpkg.com/v8-to-istanbul/-/v8-to-istanbul-4.1.2.tgz#387d173be5383dbec209d21af033dcb892e3ac82" + integrity sha512-G9R+Hpw0ITAmPSr47lSlc5A1uekSYzXxTMlFxso2xoffwo4jQnzbv1p9yXIinO8UMZKfAFewaCHwWvnH4Jb4Ug== dependencies: "@types/istanbul-lib-coverage" "^2.0.1" convert-source-map "^1.6.0"