export async function createChangeRequestFunc()

in operations/codeup/changeRequests.ts [194:287]


export async function createChangeRequestFunc(
  organizationId: string,
  repositoryId: string,
  title: string,
  sourceBranch: string,
  targetBranch: string,
  description?: string,
  sourceProjectId?: number,
  targetProjectId?: number,
  reviewerUserIds?: string[],
  workItemIds?: string[],
  createFrom: string = "WEB" // Possible values: WEB, COMMAND_LINE
): Promise<z.infer<typeof ChangeRequestSchema>> {
  const encodedRepoId = handleRepositoryIdEncoding(repositoryId);
  
  // 检查和获取sourceProjectId和targetProjectId
  let sourceIdString: string | undefined;
  let targetIdString: string | undefined;
  
  if (sourceProjectId !== undefined) {
    sourceIdString = floatToIntString(sourceProjectId);
  }
  
  if (targetProjectId !== undefined) {
    targetIdString = floatToIntString(targetProjectId);
  }
  
  // 如果repositoryId是纯数字,且sourceProjectId或targetProjectId未提供,直接使用repositoryId的值
  if (!isNaN(Number(repositoryId))) {
    // 是数字ID,可以直接使用
    if (sourceIdString === undefined) {
      sourceIdString = repositoryId;
    }
    if (targetIdString === undefined) {
      targetIdString = repositoryId;
    }
  } else if (repositoryId.includes("%2F") || repositoryId.includes("/")) {
    // 如果是组织ID与仓库名称的组合,调用API获取数字ID
    if (sourceIdString === undefined || targetIdString === undefined) {
      try {
        const numericId = await getRepositoryNumericId(organizationId, encodedRepoId);
        
        if (sourceIdString === undefined) {
          sourceIdString = numericId;
        }
        if (targetIdString === undefined) {
          targetIdString = numericId;
        }
      } catch (error) {
        throw new Error(`When using 'organizationId%2Frepo-name' format, you must first get the numeric ID of the repository and use it for sourceProjectId and targetProjectId parameters. Please use get_repository tool to get the numeric ID of '${repositoryId}' and then use that ID as the value for sourceProjectId and targetProjectId.`);
      }
    }
  }
  
  // 确保sourceProjectId和targetProjectId已设置
  if (sourceIdString === undefined) {
    throw new Error("Could not get sourceProjectId, please provide this parameter manually");
  }
  if (targetIdString === undefined) {
    throw new Error("Could not get targetProjectId, please provide this parameter manually");
  }
  
  const url = `/oapi/v1/codeup/organizations/${organizationId}/repositories/${encodedRepoId}/changeRequests`;
  
  // 准备payload
  const payload: Record<string, any> = {
    title: title,
    sourceBranch: sourceBranch,
    targetBranch: targetBranch,
    sourceProjectId: sourceIdString,
    targetProjectId: targetIdString,
    createFrom: createFrom,
  };
  
  // 添加可选参数
  if (description !== undefined) {
    payload.description = description;
  }
  
  if (reviewerUserIds !== undefined) {
    payload.reviewerUserIds = reviewerUserIds;
  }
  
  if (workItemIds !== undefined) {
    payload.workItemIds = workItemIds;
  }
  
  const response = await yunxiaoRequest(url, {
    method: "POST",
    body: payload,
  });
  
  return ChangeRequestSchema.parse(response);
}