Skip to content

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

Merged
merged 3 commits into from
Aug 11, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions src/github/createPRViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't too sure how to handle the case where this.compareBranch == null. Advice appreciated!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 seems reasonable here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the upstream is undefined we can use the git API to get the number of changes. I have added that.

}

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) {
Expand Down Expand Up @@ -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;
}

Expand Down