Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make forking functionality optional #101

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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