@tiberriver256/mcp-server-azure-devops
Version:
Azure DevOps reference server for the Model Context Protocol (MCP)
99 lines • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPullRequest = createPullRequest;
const errors_1 = require("../../../shared/errors");
function normalizeTags(tags) {
if (!tags) {
return [];
}
const seen = new Set();
const normalized = [];
for (const rawTag of tags) {
const trimmed = rawTag.trim();
if (!trimmed) {
continue;
}
const key = trimmed.toLowerCase();
if (seen.has(key)) {
continue;
}
seen.add(key);
normalized.push(trimmed);
}
return normalized;
}
/**
* Create a pull request
*
* @param connection The Azure DevOps WebApi connection
* @param projectId The ID or name of the project
* @param repositoryId The ID or name of the repository
* @param options Options for creating the pull request
* @returns The created pull request
*/
async function createPullRequest(connection, projectId, repositoryId, options) {
try {
if (!options.title) {
throw new Error('Title is required');
}
if (!options.sourceRefName) {
throw new Error('Source branch is required');
}
if (!options.targetRefName) {
throw new Error('Target branch is required');
}
const gitApi = await connection.getGitApi();
const normalizedTags = normalizeTags(options.tags);
// Create the pull request object
const pullRequest = {
title: options.title,
description: options.description,
sourceRefName: options.sourceRefName,
targetRefName: options.targetRefName,
isDraft: options.isDraft || false,
workItemRefs: options.workItemRefs?.map((id) => ({
id: id.toString(),
})),
reviewers: options.reviewers?.map((reviewer) => ({
id: reviewer,
isRequired: true,
})),
};
if (options.additionalProperties) {
Object.assign(pullRequest, options.additionalProperties);
}
if (normalizedTags.length > 0) {
pullRequest.labels = normalizedTags.map((tag) => ({ name: tag }));
}
// Create the pull request
const createdPullRequest = await gitApi.createPullRequest(pullRequest, repositoryId, projectId);
if (!createdPullRequest) {
throw new Error('Failed to create pull request');
}
if (normalizedTags.length > 0) {
const pullRequestId = createdPullRequest.pullRequestId;
if (!pullRequestId) {
throw new Error('Pull request created without identifier for tagging');
}
const existing = new Set((createdPullRequest.labels ?? [])
.map((label) => label.name?.toLowerCase())
.filter((name) => Boolean(name)));
const tagsToCreate = normalizedTags.filter((tag) => !existing.has(tag.toLowerCase()));
if (tagsToCreate.length > 0) {
const createdLabels = await Promise.all(tagsToCreate.map((tag) => gitApi.createPullRequestLabel({ name: tag }, repositoryId, pullRequestId, projectId)));
createdPullRequest.labels = [
...(createdPullRequest.labels ?? []),
...createdLabels,
];
}
}
return createdPullRequest;
}
catch (error) {
if (error instanceof errors_1.AzureDevOpsError) {
throw error;
}
throw new Error(`Failed to create pull request: ${error instanceof Error ? error.message : String(error)}`);
}
}
//# sourceMappingURL=feature.js.map