Skip to content

Commit

Permalink
feat(github): update PR if there is a draft already
Browse files Browse the repository at this point in the history
Fixes #627
  • Loading branch information
tagoro9 committed Sep 22, 2020
1 parent ca6e923 commit 3f2eba9
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions src/git/Github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IssuesListLabelsForRepoResponseData,
OctokitResponse,
PullsCreateResponseData,
PullsListResponseData,
PullsRequestReviewersResponseData,
UsersGetByUsernameResponseData,
} from '@octokit/types';
Expand Down Expand Up @@ -92,8 +93,8 @@ export class Github implements Remote {
}: PullRequestData): Promise<PullRequest> {
this.debug(`Creating pull requests with args: %o`, { labels, reviewers, useDefaults });
this.messenger.emit('Creating pull request');
const prExists = await this.doesPullRequestExistForBranch(branchInfo.name);
if (prExists) {
const prInfo = await this.getPullRequestInfo(branchInfo.name);
if (prInfo !== undefined && (!prInfo.draft || isDraft)) {
throw new Error('A PR already exists for this branch');
}
const [ghLabels, ghReviewers] = await Promise.all([
Expand Down Expand Up @@ -139,7 +140,10 @@ export class Github implements Remote {
this.messenger,
);

const initialPrContent = this.getPullRequestContentFromTemplate(branchInfo, issues);
const initialPrContent =
prInfo !== undefined
? `${prInfo.title}\n${prInfo.body}`
: this.getPullRequestContentFromTemplate(branchInfo, issues);
this.messenger.inThread(true);
await this.pause();
const prContent = useDefaults
Expand All @@ -150,12 +154,19 @@ export class Github implements Remote {
prefix: 'fotingo-review',
});
this.messenger.inThread(false);

const githubPr = await this.submitPullRequest({
const prOptions = {
content: prContent,
isDraft,
pullRequestHead: branchInfo.name,
});
};

let githubPr;
if (prInfo !== undefined) {
githubPr = await this.updatePullRequest(prInfo, prOptions);
} else {
githubPr = await this.submitPullRequest(prOptions);
}

const pullRequest = {
issues,
number: githubPr.number,
Expand Down Expand Up @@ -283,6 +294,7 @@ export class Github implements Remote {
/**
* Submit a pull request for review
* @param content Content of the pull request
* @param isDraft Create a draft pull request
* @param pullRequestHead Name of the branch to use as head of the pull request
*/
private async submitPullRequest({
Expand All @@ -305,6 +317,31 @@ export class Github implements Remote {
.then((response) => response.data);
}

/**
* Update an existing pull request with the passed information
* @param pullRequest Current pull request
* @param options Update options
* @private
*/
private async updatePullRequest(
pullRequest: PullsListResponseData[number],
{ content, isDraft, pullRequestHead }: SubmitPullRequestOptions,
): Promise<PullsCreateResponseData> {
const baseBranch = await this.git.findBaseBranch(true);
return this.api.pulls
.update({
base: baseBranch,
body: compose<string, string[], string[], string>(join('\n'), tail, split('\n'))(content),
draft: isDraft,
head: pullRequestHead,
owner: this.config.owner,
pull_number: pullRequest.number,
repo: this.config.repo,
title: compose<string, string[], string>(head, split('\n'))(content),
})
.then((response) => response.data);
}

/**
* Add reviewers to a pull request
* @param reviewers Reviewers
Expand Down Expand Up @@ -365,10 +402,12 @@ export class Github implements Remote {
}

/**
* Check if a PR already exists for the specified branch
* Get the pull request information given a branch name
* @param branchName Branch name
*/
private doesPullRequestExistForBranch(branchName: string): Promise<boolean> {
private getPullRequestInfo(
branchName: string,
): Promise<PullsListResponseData[number] | undefined> {
// TODO check if this works with forks
return this.queueCall(
() =>
Expand All @@ -378,7 +417,7 @@ export class Github implements Remote {
owner: this.config.owner,
repo: this.config.repo,
})
.then((response) => response.data.length > 0),
.then((resonse: OctokitResponse<PullsListResponseData>) => resonse.data[0]),
'Checking if there is a PR for %s',
branchName,
);
Expand Down

0 comments on commit 3f2eba9

Please sign in to comment.