private async create()

in src/github/createPRViewProvider.ts [299:369]


	private async create(message: IRequestMessage<CreatePullRequest>): Promise<void> {
		try {
			const compareOwner = message.args.compareOwner;
			const compareRepositoryName = message.args.compareRepo;
			const compareBranchName = message.args.compareBranch;
			const compareGithubRemoteName = `${compareOwner}/${compareRepositoryName}`;
			const compareBranch = await this._folderRepositoryManager.repository.getBranch(compareBranchName);
			let headRepo = compareBranch.upstream ? this._folderRepositoryManager.findRepo(byRemoteName(compareBranch.upstream.remote))
				: undefined;
			let existingCompareUpstream = headRepo?.remote;

			if (!existingCompareUpstream
				|| (existingCompareUpstream.owner !== compareOwner)
				|| (existingCompareUpstream.repositoryName !== compareRepositoryName)) {
				// We assume this happens only when the compare branch is based on the current branch.
				const shouldPushUpstream = await vscode.window.showInformationMessage(
					`There is no upstream branch for '${compareBranchName}'.\n\nDo you want to publish it and then create the pull request?`,
					{ modal: true },
					'Publish branch',
				);
				if (shouldPushUpstream === 'Publish branch') {
					let createdPushRemote: Remote | undefined;
					const pushRemote = this._folderRepositoryManager.repository.state.remotes.find(localRemote => {
						if (!localRemote.pushUrl) {
							return false;
						}
						const testRemote = new Remote(localRemote.name, localRemote.pushUrl, new Protocol(localRemote.pushUrl));
						if ((testRemote.owner === compareOwner) && (testRemote.repositoryName === compareRepositoryName)) {
							createdPushRemote = testRemote;
							return true;
						}
						return false;
					});

					if (pushRemote && createdPushRemote) {
						await this._folderRepositoryManager.repository.push(pushRemote.name, compareBranchName, true);
						existingCompareUpstream = createdPushRemote;
						headRepo = this._folderRepositoryManager.findRepo(byRemoteName(existingCompareUpstream.remoteName));
					} else {
						this._throwError(message, `The current repository does not have a push remote for ${compareGithubRemoteName}`);
					}
				}
			}
			if (!existingCompareUpstream) {
				this._throwError(message, 'No upstream for the compare branch.');
				return;
			}

			if (!headRepo) {
				throw new Error(`Unable to find GitHub repository matching '${existingCompareUpstream.remoteName}'. You can add '${existingCompareUpstream.remoteName}' to the setting "githubPullRequests.remotes" to ensure '${existingCompareUpstream.remoteName}' is found.`);
			}

			const head = `${headRepo.remote.owner}:${compareBranchName}`;
			const createdPR = await this._folderRepositoryManager.createPullRequest({ ...message.args, head });

			// Create was cancelled
			if (!createdPR) {
				this._throwError(message, 'There must be a difference in commits to create a pull request.');
			} else {
				await this._replyMessage(message, {});
				await PullRequestGitHelper.associateBranchWithPullRequest(
					this._folderRepositoryManager.repository,
					createdPR,
					compareBranchName,
				);
				this._onDone.fire(createdPR);
			}
		} catch (e) {
			this._throwError(message, e.message);
		}
	}