Skip to content

Commit

Permalink
feat: add optional draft flag to openPullRequest() (#207)
Browse files Browse the repository at this point in the history
First step towards Owl Bot opening pull requests.  Owl bot invokes this library to open pull requests.
  • Loading branch information
SurferJeffAtGoogle committed Apr 22, 2021
1 parent b2f440c commit ff9516c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/github-handler/pull-request-handler.ts
Expand Up @@ -27,6 +27,7 @@ const DEFAULT_PRIMARY = 'master';
* @param {Description} description The pull request title and detailed description
* @param {boolean} maintainersCanModify Whether or not maintainers can modify the pull request. Default is true
* @param {string} upstreamPrimary The upstream repository's primary branch. Default is master.
* @param draft Open a DRAFT pull request. Defaults to false.
* @returns {Promise<void>}
*/
async function openPullRequest(
Expand All @@ -35,7 +36,8 @@ async function openPullRequest(
origin: BranchDomain,
description: Description,
maintainersCanModify = true,
upstreamPrimary: string = DEFAULT_PRIMARY
upstreamPrimary: string = DEFAULT_PRIMARY,
draft = false
): Promise<number> {
const head = `${origin.owner}:${origin.branch}`;
const existingPullRequest = (
Expand All @@ -60,6 +62,7 @@ async function openPullRequest(
base: upstreamPrimary,
body: description.body,
maintainer_can_modify: maintainersCanModify,
draft: draft,
})
).data;
logger.info(
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -179,7 +179,8 @@ async function createPullRequest(
originBranch,
description,
gitHubConfigs.maintainersCanModify,
gitHubConfigs.primary
gitHubConfigs.primary,
options.draft
);
logger.info(`Successfully opened pull request: ${prNumber}.`);

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Expand Up @@ -97,6 +97,8 @@ export interface CreatePullRequestUserOptions {
labels?: string[];
// Number of times to retry if the request fails. Defaults to 5.
retry?: number;
// Create a DRAFT pull request.
draft?: boolean;
}

/**
Expand Down
20 changes: 18 additions & 2 deletions test/pr.ts
Expand Up @@ -86,7 +86,10 @@ describe('Opening a pull request', async () => {
octokit,
upstream,
origin,
description
description,
undefined,
undefined,
true
);
sandbox.assert.calledOnceWithExactly(stub, {
owner: upstream.owner,
Expand All @@ -96,6 +99,7 @@ describe('Opening a pull request', async () => {
base: 'master',
body: description.body,
maintainer_can_modify: true,
draft: true,
});
assert.strictEqual(number, 1347);
});
Expand Down Expand Up @@ -136,7 +140,15 @@ describe('Opening a pull request', async () => {
branch: 'new-topic-1',
};
// tests
await openPullRequest(octokit, upstream, similarOrigin1, description);
await openPullRequest(
octokit,
upstream,
similarOrigin1,
description,
undefined,
undefined,
false
);
sandbox.assert.calledOnceWithExactly(stub, {
owner: upstream.owner,
repo: similarOrigin1.repo,
Expand All @@ -145,6 +157,7 @@ describe('Opening a pull request', async () => {
base: 'master',
body: description.body,
maintainer_can_modify: true,
draft: false,
});
});

Expand All @@ -170,6 +183,7 @@ describe('Opening a pull request', async () => {
base: 'master',
body: description.body,
maintainer_can_modify: true,
draft: false,
});
});

Expand All @@ -196,6 +210,7 @@ describe('Opening a pull request', async () => {
base: 'master',
body: description.body,
maintainer_can_modify: true,
draft: false,
});
});

Expand All @@ -221,6 +236,7 @@ describe('Opening a pull request', async () => {
base: 'master',
body: description.body,
maintainer_can_modify: true,
draft: false,
});
});
});
Expand Down

0 comments on commit ff9516c

Please sign in to comment.