(input: {
id?: number;
gitlabId?: string;
owner: string;
repo: string;
})
| 202 | }; |
| 203 | |
| 204 | export const getGitlabBranches = async (input: { |
| 205 | id?: number; |
| 206 | gitlabId?: string; |
| 207 | owner: string; |
| 208 | repo: string; |
| 209 | }) => { |
| 210 | if (!input.gitlabId || !input.id || input.id === 0) { |
| 211 | return []; |
| 212 | } |
| 213 | |
| 214 | const gitlabProvider = await findGitlabById(input.gitlabId); |
| 215 | |
| 216 | const allBranches = []; |
| 217 | let page = 1; |
| 218 | const perPage = 100; // GitLab's max per page is 100 |
| 219 | const baseUrl = ( |
| 220 | gitlabProvider.gitlabInternalUrl || gitlabProvider.gitlabUrl |
| 221 | ).replace(/\/+$/, ""); |
| 222 | |
| 223 | while (true) { |
| 224 | const branchesResponse = await fetch( |
| 225 | `${baseUrl}/api/v4/projects/${input.id}/repository/branches?page=${page}&per_page=${perPage}`, |
| 226 | { |
| 227 | headers: { |
| 228 | Authorization: `Bearer ${gitlabProvider.accessToken}`, |
| 229 | }, |
| 230 | }, |
| 231 | ); |
| 232 | |
| 233 | if (!branchesResponse.ok) { |
| 234 | throw new Error( |
| 235 | `Failed to fetch branches: ${branchesResponse.statusText}`, |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | const branches = await branchesResponse.json(); |
| 240 | |
| 241 | if (branches.length === 0) { |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | allBranches.push(...branches); |
| 246 | page++; |
| 247 | |
| 248 | // Check if we've reached the total using headers (optional optimization) |
| 249 | const total = branchesResponse.headers.get("x-total"); |
| 250 | if (total && allBranches.length >= Number.parseInt(total)) { |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return allBranches as { |
| 256 | id: string; |
| 257 | name: string; |
| 258 | commit: { |
| 259 | id: string; |
| 260 | }; |
| 261 | }[]; |
no test coverage detected