Skip to content

Commit 552b269

Browse files
authored
Create PR shows error if there has been a previous PR on that branch (#7270)
Fixes #7018
1 parent f488363 commit 552b269

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/github/createPRViewProvider.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,13 @@ export class CreatePullRequestViewProvider extends BaseCreatePullRequestViewProv
618618
}
619619

620620
private async existingPRMessage(): Promise<string | undefined> {
621-
const existingPR = await PullRequestGitHelper.getMatchingPullRequestMetadataForBranch(this._folderRepositoryManager.repository, this.model.compareBranch);
622-
return existingPR ? vscode.l10n.t('A pull request already exists for this branch.') : '';
621+
const [existingPR, hasUpstream] = await Promise.all([PullRequestGitHelper.getMatchingPullRequestMetadataForBranch(this._folderRepositoryManager.repository, this.model.compareBranch), this.model.getCompareHasUpstream()]);
622+
if (!existingPR || !hasUpstream) {
623+
return undefined;
624+
}
625+
626+
const [pr, compareBranch] = await Promise.all([await this._folderRepositoryManager.resolvePullRequest(existingPR.owner, existingPR.repositoryName, existingPR.prNumber), this._folderRepositoryManager.repository.getBranch(this.model.compareBranch)]);
627+
return (pr?.head?.sha === compareBranch.commit) ? vscode.l10n.t('A pull request already exists for this branch.') : undefined;
623628
}
624629

625630
public async setDefaultCompareBranch(compareBranch: Branch | undefined) {

src/github/githubRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ export class GitHubRepository extends Disposable {
11141114
return buff;
11151115
}
11161116

1117-
async hasBranch(branchName: string): Promise<boolean> {
1117+
async hasBranch(branchName: string): Promise<string | undefined> {
11181118
Logger.appendLine(`Fetch branch ${branchName} - enter`, this.id);
11191119
const { query, remote, schema } = await this.ensure();
11201120

@@ -1127,7 +1127,7 @@ export class GitHubRepository extends Disposable {
11271127
}
11281128
});
11291129
Logger.appendLine(`Fetch branch ${branchName} - done: ${data.repository?.ref !== null}`, this.id);
1130-
return data.repository?.ref !== null;
1130+
return data.repository?.ref?.target.oid;
11311131
}
11321132

11331133
async listBranches(owner: string, repositoryName: string): Promise<string[]> {

src/github/graphql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ export interface GetBranchResponse {
576576
target: {
577577
oid: string;
578578
}
579-
}
579+
} | null;
580580
} | null;
581581
}
582582

src/view/createPullRequestDataModel.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ export class CreatePullRequestDataModel extends Disposable {
155155
private async updateHasUpstream(branch: string): Promise<boolean> {
156156
const compareBranch = await this.folderRepositoryManager.repository.getBranch(branch);
157157
this._compareHasUpstream = !!compareBranch.upstream;
158+
// Check that the upstream head matches the local head
159+
if (this._compareHasUpstream) {
160+
const upstream = await this.gitHubRepository?.hasBranch(branch);
161+
this._compareHasUpstream = upstream === compareBranch.commit;
162+
}
158163
return this._compareHasUpstream;
159164
}
160165

0 commit comments

Comments
 (0)