export async function listBranchesFunc()

in operations/codeup/branches.ts [146:202]


export async function listBranchesFunc(
    organizationId: string,
    repositoryId: string,
    page?: number,
    perPage?: number,
    sort?: string, // Possible values: name_asc, name_desc, updated_asc, updated_desc
    search?: string
): Promise<z.infer<typeof CodeupBranchSchema>[]> {
  console.error("listBranchesFunc page:" + page + " perPage:" + perPage + " sort:" + sort + " search:" + search);
  // Automatically handle unencoded slashes in repositoryId
  if (repositoryId.includes("/")) {
    // Found unencoded slash, automatically URL encode it
    const parts = repositoryId.split("/", 2);
    if (parts.length === 2) {
      const encodedRepoName = encodeURIComponent(parts[1]);
      // Remove + signs from encoding (spaces are encoded as +, but we need %20)
      const formattedEncodedName = encodedRepoName.replace(/\+/g, "%20");
      repositoryId = `${parts[0]}%2F${formattedEncodedName}`;
    }
  }

  const baseUrl = `/oapi/v1/codeup/organizations/${organizationId}/repositories/${repositoryId}/branches`;

  // Build query parameters - use lowercase parameter names as expected by the API
  const queryParams: Record<string, string | number | undefined> = {};
  if (page !== undefined && page !== null) {
    queryParams.page = page;
  }
  if (perPage !== undefined && perPage !== null) {
    queryParams.perPage = perPage;
  }
  if (sort !== undefined && sort !== null) {
    queryParams.sort = sort;
  }
  if (search !== undefined && search !== null) {
    queryParams.search = search;
  }

  // Use buildUrl function to construct URL with query parameters
  const url = buildUrl(baseUrl, queryParams);

  const response = await yunxiaoRequest(url, {
    method: "GET",
  });

  // Ensure the response is an array
  if (!Array.isArray(response)) {
    return [];
  }

  // Map each branch object and handle null values
  return response.map(branchData => {
    // Filter out null values that would cause parsing errors
    // This is a defensive approach until we update all schemas properly
    return CodeupBranchSchema.parse(branchData);
  });
}