From b6442c11407c3f29405c639a6b0e4c81e8357c32 Mon Sep 17 00:00:00 2001 From: bcoe Date: Fri, 4 Sep 2020 19:08:49 -0700 Subject: [PATCH 1/3] feat: make forking functionality optional --- README.md | 5 ++-- src/index.ts | 3 ++- src/types/index.ts | 2 ++ test/main-make-pr.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 20856f52..1dd32ccb 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ npm i code-suggester ### Example -``` +```js const suggester = require("code-suggester"); async function main() { @@ -89,8 +89,7 @@ The `createPullRequest()` method creates a GitHub Pull request with the files gi | primary | `string` | The primary upstream branch to open a PR against. Default is `'master'`. | | message | `string` | The commit message for the changes. Default is `'code suggestions'`. We recommend following [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).| | force | `boolean` | Whether or not to force push the reference even if the ancestor commits differs. Default is `false`. | - - +| fork | `boolean` | Whether or not code suggestion should be made from a fork `true` (forking does not work when using `secrets.GITHUB_TOKEN` in an action). | #### `logger` *[Logger](https://www.npmjs.com/package/@types/pino)*
diff --git a/src/index.ts b/src/index.ts index 09529fdb..b230b1f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -72,7 +72,8 @@ async function createPullRequest( owner: gitHubConfigs.upstreamOwner, repo: gitHubConfigs.upstreamRepo, }; - const origin: RepoDomain = await handler.fork(octokit, upstream); + const origin: RepoDomain = + options.fork === false ? upstream : await handler.fork(octokit, upstream); const originBranch: BranchDomain = { ...origin, branch: gitHubConfigs.branch, diff --git a/src/types/index.ts b/src/types/index.ts index aa401dcf..aa7ebc74 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -87,6 +87,8 @@ export interface CreatePullRequestUserOptions { branch?: string; // Whether or not to force branch reference updates. Default is false. (optional) force?: boolean; + // Should a fork be used when creating pull request + fork?: boolean; // Primary upstream branch to open PRs against. Default is 'master' (optional) primary?: string; // Whether or not maintainers can modify the PR. Default is true. (optional) diff --git a/test/main-make-pr.ts b/test/main-make-pr.ts index 4535dfd7..8bbcfab0 100644 --- a/test/main-make-pr.ts +++ b/test/main-make-pr.ts @@ -123,6 +123,65 @@ describe('Make PR main function', () => { await stubMakePr.createPullRequest(octokit, changes, options); }); + it('does not create fork when fork is false', async () => { + const stubHelperHandlers = { + fork: (octokit: Octokit, upstream: {owner: string; repo: string}) => { + // throw Error('should not call fork'); + }, + branch: ( + octokit: Octokit, + origin: {owner: string; repo: string}, + upstream: {owner: string; repo: string}, + testBranch: string, + testprimary: string + ) => { + expect(upstream.owner).equals(origin.owner); + expect(upstream.repo).equals(origin.repo); + return oldHeadSha; + }, + commitAndPush: ( + octokit: Octokit, + testOldHeadSha: string, + testChanges: Changes, + originBranch: {owner: string; repo: string; branch: string}, + testMessage: string + ) => { + expect(testOldHeadSha).equals(oldHeadSha); + expect(originBranch.owner).equals(upstreamOwner); + expect(originBranch.repo).equals(upstreamRepo); + expect(originBranch.branch).equals(branch); + expect(testChanges).deep.equals(changes); + expect(testMessage).equals(message); + }, + openPullRequest: ( + octokit: Octokit, + upstream: {owner: string; repo: string}, + originBranch: {owner: string; repo: string; branch: string}, + testDescription: {title: string; body: string}, + testMaintainersCanModify: boolean, + testPrimary: string + ) => { + expect(originBranch.owner).equals(upstreamOwner); + expect(originBranch.repo).equals(upstreamRepo); + expect(originBranch.branch).equals(branch); + expect(upstream.owner).equals(upstreamOwner); + expect(upstream.repo).equals(upstreamRepo); + expect(testDescription.body).equals(description); + expect(testDescription.title).equals(title); + expect(testMaintainersCanModify).equals(maintainersCanModify); + expect(testPrimary).equals(primary); + }, + }; + const stubMakePr = proxyquire.noCallThru()('../src/', { + './github-handler': stubHelperHandlers, + }); + await stubMakePr.createPullRequest( + octokit, + changes, + Object.assign({fork: false}, options) + ); + }); + it('Passes up the error message with a throw when create fork helper function fails', async () => { // setup From 31dea107a641ea4230dd7ea89cbf84ace8cfefba Mon Sep 17 00:00:00 2001 From: bcoe Date: Fri, 4 Sep 2020 19:25:09 -0700 Subject: [PATCH 2/3] docs: fix up config description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1dd32ccb..8d9e6731 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ The `createPullRequest()` method creates a GitHub Pull request with the files gi | primary | `string` | The primary upstream branch to open a PR against. Default is `'master'`. | | message | `string` | The commit message for the changes. Default is `'code suggestions'`. We recommend following [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/).| | force | `boolean` | Whether or not to force push the reference even if the ancestor commits differs. Default is `false`. | -| fork | `boolean` | Whether or not code suggestion should be made from a fork `true` (forking does not work when using `secrets.GITHUB_TOKEN` in an action). | +| fork | `boolean` | Whether or not code suggestion should be made from a fork, defaults to `true` (_Note: forking does not work when using `secrets.GITHUB_TOKEN` in an action_). | #### `logger` *[Logger](https://www.npmjs.com/package/@types/pino)*
From 15eec189168005088fda319ddbf5b8f497bb5b9d Mon Sep 17 00:00:00 2001 From: bcoe Date: Fri, 4 Sep 2020 19:25:50 -0700 Subject: [PATCH 3/3] test: throw if fork called --- test/main-make-pr.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main-make-pr.ts b/test/main-make-pr.ts index 8bbcfab0..d76ef50e 100644 --- a/test/main-make-pr.ts +++ b/test/main-make-pr.ts @@ -126,7 +126,7 @@ describe('Make PR main function', () => { it('does not create fork when fork is false', async () => { const stubHelperHandlers = { fork: (octokit: Octokit, upstream: {owner: string; repo: string}) => { - // throw Error('should not call fork'); + throw Error('should not call fork'); }, branch: ( octokit: Octokit,