Skip to content

Commit

Permalink
feat: make forking functionality optional (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Sep 8, 2020
1 parent 47f57c4 commit 1a25661
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Expand Up @@ -29,7 +29,7 @@ npm i code-suggester

### Example

```
```js
const suggester = require("code-suggester");

async function main() {
Expand Down Expand Up @@ -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, 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)* <br>
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions test/main-make-pr.ts
Expand Up @@ -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

Expand Down

0 comments on commit 1a25661

Please sign in to comment.