async createPullRequest()

in src/github/folderRepositoryManager.ts [1195:1279]


	async createPullRequest(params: OctokitCommon.PullsCreateParams): Promise<PullRequestModel | undefined> {
		const repo = this._githubRepositories.find(
			r => r.remote.owner === params.owner && r.remote.repositoryName === params.repo,
		);
		if (!repo) {
			throw new Error(`No matching repository ${params.repo} found for ${params.owner}`);
		}

		try {
			const pullRequestModel = await repo.createPullRequest(params);

			const branchNameSeparatorIndex = params.head.indexOf(':');
			const branchName = params.head.slice(branchNameSeparatorIndex + 1);
			await PullRequestGitHelper.associateBranchWithPullRequest(this._repository, pullRequestModel, branchName);

			/* __GDPR__
				"pr.create.success" : {
					"isDraft" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
				}
			*/
			this.telemetry.sendTelemetryEvent('pr.create.success', { isDraft: (params.draft || '').toString() });
			return pullRequestModel;
		} catch (e) {
			if (e.message.indexOf('No commits between ') > -1) {
				// There are unpushed commits
				if (this._repository.state.HEAD?.ahead) {
					// Offer to push changes
					const shouldPush = await vscode.window.showInformationMessage(
						`There are no commits between '${params.base}' and '${params.head}'.\n\nDo you want to push your local commits and create the pull request?`,
						{ modal: true },
						'Push commits',
					);
					if (shouldPush === 'Push commits') {
						await this._repository.push();
						return this.createPullRequest(params);
					} else {
						return;
					}
				}

				// There are uncommitted changes
				if (this._repository.state.workingTreeChanges.length || this._repository.state.indexChanges.length) {
					const shouldCommit = await vscode.window.showInformationMessage(
						`There are no commits between '${params.base}' and '${params.head}'.\n\nDo you want to commit your changes and create the pull request?`,
						{ modal: true },
						'Commit changes',
					);
					if (shouldCommit === 'Commit changes') {
						await vscode.commands.executeCommand('git.commit');
						await this._repository.push();
						return this.createPullRequest(params);
					} else {
						return;
					}
				}
			}

			if (!this._repository.state.HEAD?.upstream) {
				const shouldPushUpstream = await vscode.window.showInformationMessage(
					`There is no upstream branch for '${params.base}'.\n\nDo you want to publish it and create the pull request?`,
					{ modal: true },
					'Publish branch',
				);
				if (shouldPushUpstream === 'Publish branch') {
					await this._repository.push(repo.remote.remoteName, params.base, true);
					return this.createPullRequest(params);
				} else {
					return;
				}
			}

			Logger.appendLine(`GitHubRepository> Creating pull requests failed: ${e}`);

			/* __GDPR__
				"pr.create.failure" : {
					"isDraft" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
				}
			*/
			this.telemetry.sendTelemetryErrorEvent('pr.create.failure', {
				isDraft: (params.draft || '').toString(),
			});

			throw new Error(formatError(e));
		}
	}