-
Notifications
You must be signed in to change notification settings - Fork 609
Include PR template in PR description if there is just one commit #2846
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,27 +112,40 @@ 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't too sure how to handle the case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 seems reasonable here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the |
||
} | ||
|
||
private async getTitle(): Promise<string> { | ||
// 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 | ||
// compare branch if it has a GitHub remote. | ||
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) { | ||
|
@@ -164,6 +177,19 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs | |
// 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; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.