From c14f0782a3f348202f976e2fa8c481d2adb8003b Mon Sep 17 00:00:00 2001 From: Joshua O'Brien Date: Mon, 19 Jul 2021 22:52:35 +1000 Subject: [PATCH 1/3] Include PR template in PR description if there is just one commit --- src/github/createPRViewProvider.ts | 52 ++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/github/createPRViewProvider.ts b/src/github/createPRViewProvider.ts index ad24a61d7d..4b7695ab77 100644 --- a/src/github/createPRViewProvider.ts +++ b/src/github/createPRViewProvider.ts @@ -119,20 +119,15 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); let useBranchName = false; - if (this.compareBranch?.upstream) { - const headRepo = this._folderRepositoryManager.findRepo(byRemoteName(this.compareBranch.upstream.remote)); - if (headRepo) { - const headBranch = `${headRepo.remote.owner}:${this.compareBranch.name ?? ''}`; - try { - const commits = await origin.compareCommits(`${this._pullRequestDefaults.owner}:${this._pullRequestDefaults.base}`, headBranch); - if (commits.total_commits > 1) { - const defaultBranch = await origin.getDefaultBranch(); - useBranchName = defaultBranch !== this.compareBranch.name; - } - } catch (e) { - // Ignore and fall back to commit message - } + + try { + const totalCommits = await this.getTotalCommits(); + if (totalCommits > 1) { + const defaultBranch = await origin.getDefaultBranch(); + useBranchName = defaultBranch !== this.compareBranch?.name; } + } catch (e) { + // Ignore and fall back to commit message } if (useBranchName) { @@ -160,10 +155,41 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs return undefined; } + private async getTotalCommits() { + const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); + + if (this.compareBranch?.upstream) { + const headRepo = this._folderRepositoryManager.findRepo(byRemoteName(this.compareBranch.upstream.remote)); + + if (headRepo) { + const headBranch = `${headRepo.remote.owner}:${this.compareBranch.name ?? ''}`; + const baseBranch = `${this._pullRequestDefaults.owner}:${this._pullRequestDefaults.base}`; + const { total_commits } = await origin.compareCommits(baseBranch, headBranch); + + return total_commits; + } + } + + return 0; + } + private async getDescription(): Promise { // Try to match github's default, first look for template, then use commit body if available. const pullRequestTemplate = await this.getPullRequestTemplate(); if (pullRequestTemplate) { + try { + const totalCommits = await this.getTotalCommits(); + + // If there's just a single commit, we include it as well as the PR template + if (totalCommits === 1 && this.compareBranch?.name) { + const message = titleAndBodyFrom(await this._folderRepositoryManager.getTipCommitMessage(this.compareBranch.name)).body; + + return `${message}\n\n${pullRequestTemplate}`; + } + } catch (e) { + // Ignore and fall back to the template + } + return pullRequestTemplate; } From fba7de3351cf2a4b22be1f0aa5bdfa5314373ca0 Mon Sep 17 00:00:00 2001 From: Joshua O'Brien Date: Mon, 19 Jul 2021 23:03:42 +1000 Subject: [PATCH 2/3] Move function --- src/github/createPRViewProvider.ts | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/github/createPRViewProvider.ts b/src/github/createPRViewProvider.ts index 4b7695ab77..eb05548075 100644 --- a/src/github/createPRViewProvider.ts +++ b/src/github/createPRViewProvider.ts @@ -112,6 +112,24 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs super.show(); } + private async getTotalCommits() { + const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); + + if (this.compareBranch?.upstream) { + const headRepo = this._folderRepositoryManager.findRepo(byRemoteName(this.compareBranch.upstream.remote)); + + if (headRepo) { + const headBranch = `${headRepo.remote.owner}:${this.compareBranch.name ?? ''}`; + const baseBranch = `${this._pullRequestDefaults.owner}:${this._pullRequestDefaults.base}`; + const { total_commits } = await origin.compareCommits(baseBranch, headBranch); + + return total_commits; + } + } + + return 0; + } + private async getTitle(): Promise { // Use same default as GitHub, if there is only one commit, use the commit, otherwise use the branch name, as long as it is not the default branch. // By default, the base branch we use for comparison is the base branch of origin. Compare this to the @@ -155,24 +173,6 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs return undefined; } - private async getTotalCommits() { - const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); - - if (this.compareBranch?.upstream) { - const headRepo = this._folderRepositoryManager.findRepo(byRemoteName(this.compareBranch.upstream.remote)); - - if (headRepo) { - const headBranch = `${headRepo.remote.owner}:${this.compareBranch.name ?? ''}`; - const baseBranch = `${this._pullRequestDefaults.owner}:${this._pullRequestDefaults.base}`; - const { total_commits } = await origin.compareCommits(baseBranch, headBranch); - - return total_commits; - } - } - - return 0; - } - private async getDescription(): Promise { // Try to match github's default, first look for template, then use commit body if available. const pullRequestTemplate = await this.getPullRequestTemplate(); From 8231a5c34e126a7583c70487d496652a1844267f Mon Sep 17 00:00:00 2001 From: Alex Ross Date: Wed, 11 Aug 2021 16:46:45 +0200 Subject: [PATCH 3/3] Use git API for getting number of changes --- src/github/createPRViewProvider.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/github/createPRViewProvider.ts b/src/github/createPRViewProvider.ts index eb05548075..7a344a5979 100644 --- a/src/github/createPRViewProvider.ts +++ b/src/github/createPRViewProvider.ts @@ -112,7 +112,7 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs super.show(); } - private async getTotalCommits() { + private async getTotalCommits(): Promise { const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); if (this.compareBranch?.upstream) { @@ -125,6 +125,13 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs return total_commits; } + } else if (this.compareBranch?.commit) { + // We can use the git API instead of the GitHub API + const baseBranch = await this._folderRepositoryManager.repository.getBranch(this._pullRequestDefaults.base); + if (baseBranch.commit) { + const changes = await this._folderRepositoryManager.repository.diffBetween(baseBranch.commit, this.compareBranch.commit); + return changes.length; + } } return 0;