Skip to content

Commit

Permalink
feat: return the PR number created (#95)
Browse files Browse the repository at this point in the history
* feat: return the PR created

* chore: address code review

* docs: fix jsdoc header

* deps: pin to fixed version of jsdoc-region-tag

* Update src/index.ts

Co-authored-by: Tyler Bui-Palsulich <26876514+tbpg@users.noreply.github.com>

* chore: fix log message

Co-authored-by: Tyler Bui-Palsulich <26876514+tbpg@users.noreply.github.com>
  • Loading branch information
bcoe and tbpg committed Sep 2, 2020
1 parent 9579bae commit 43bf6de
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -63,7 +63,7 @@
"gts": "^2.0.0",
"jsdoc": "^3.6.4",
"jsdoc-fresh": "^1.0.1",
"jsdoc-region-tag": "^1.0.2",
"jsdoc-region-tag": "^1.0.6",
"linkinator": "^2.0.0",
"mocha": "^8.0.0",
"nock": "^13.0.2",
Expand Down
20 changes: 10 additions & 10 deletions src/github-handler/pull-request-handler.ts
Expand Up @@ -36,21 +36,20 @@ async function openPullRequest(
description: Description,
maintainersCanModify = true,
upstreamPrimary: string = DEFAULT_PRIMARY
): Promise<void> {
): Promise<number> {
const head = `${origin.owner}:${origin.branch}`;
const existingPullRequest =
(
await octokit.pulls.list({
owner: upstream.owner,
repo: origin.repo,
head,
})
).data.findIndex(pr => pr.head.label === head) >= 0;
const existingPullRequest = (
await octokit.pulls.list({
owner: upstream.owner,
repo: origin.repo,
head,
})
).data.find(pr => pr.head.label === head);
if (existingPullRequest) {
logger.info(
`Found existing pull request for reference ${origin.owner}:${origin.branch}. Skipping creating a new pull request.`
);
return;
return existingPullRequest.number;
}
const pullResponseData = (
await octokit.pulls.create({
Expand All @@ -66,6 +65,7 @@ async function openPullRequest(
logger.info(
`Successfully opened pull request available at url: ${pullResponseData.url}.`
);
return pullResponseData.number;
}

export {openPullRequest};
11 changes: 6 additions & 5 deletions src/index.ts
Expand Up @@ -48,22 +48,22 @@ import * as retry from 'async-retry';
* @param {Changes | null | undefined} changes A set of changes. The changes may be empty
* @param {CreatePullRequestUserOptions} options The configuration for interacting with GitHub provided by the user.
* @param {Logger} logger The logger instance (optional).
* @returns {Promise<void>} a void promise
* @returns {Promise<number>} a void promise
*/
async function createPullRequest(
octokit: Octokit,
changes: Changes | null | undefined,
options: CreatePullRequestUserOptions,
loggerOption?: Logger
): Promise<void> {
): Promise<number> {
setupLogger(loggerOption);
// if null undefined, or the empty map then no changes have been provided.
// Do not execute GitHub workflow
if (changes === null || changes === undefined || changes.size === 0) {
logger.info(
'Empty change set provided. No changes need to be made. Cancelling workflow.'
);
return;
return 0;
}
const gitHubConfigs = addPullRequestDefaults(options);
logger.info('Starting GitHub PR workflow...');
Expand Down Expand Up @@ -109,15 +109,16 @@ async function createPullRequest(
body: gitHubConfigs.description,
title: gitHubConfigs.title,
};
await handler.openPullRequest(
const prNumber = await handler.openPullRequest(
octokit,
upstream,
originBranch,
description,
gitHubConfigs.maintainersCanModify,
gitHubConfigs.primary
);
logger.info('Finished PR workflow');
logger.info(`Successfully opened pull request: ${prNumber}.`);
return prNumber;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion test/pr.ts
Expand Up @@ -60,7 +60,12 @@ describe('Opening a pull request', async () => {
.stub(octokit.pulls, 'create')
.resolves(createPrResponse);
// tests
await openPullRequest(octokit, upstream, origin, description);
const number = await openPullRequest(
octokit,
upstream,
origin,
description
);
sandbox.assert.calledOnceWithExactly(stub, {
owner: upstream.owner,
repo: origin.repo,
Expand All @@ -70,6 +75,7 @@ describe('Opening a pull request', async () => {
body: description.body,
maintainer_can_modify: true,
});
expect(number).to.equal(1347);
});

describe('When there are similar refs with pull requests open, the current new and unique ref still opens a pr', async () => {
Expand Down

0 comments on commit 43bf6de

Please sign in to comment.