(config: GitlabConnectionConfig)
| 34 | } |
| 35 | |
| 36 | export const getGitLabReposFromConfig = async (config: GitlabConnectionConfig) => { |
| 37 | const hostname = config.url ? |
| 38 | new URL(config.url).hostname : |
| 39 | GITLAB_CLOUD_HOSTNAME; |
| 40 | |
| 41 | const token = config.token ? |
| 42 | await getTokenFromConfig(config.token) : |
| 43 | hostname === GITLAB_CLOUD_HOSTNAME ? |
| 44 | env.FALLBACK_GITLAB_CLOUD_TOKEN : |
| 45 | undefined; |
| 46 | |
| 47 | const api = await createGitLabFromPersonalAccessToken({ |
| 48 | token, |
| 49 | url: config.url, |
| 50 | }); |
| 51 | |
| 52 | let allRepos: ProjectSchema[] = []; |
| 53 | let allWarnings: string[] = []; |
| 54 | |
| 55 | if (config.all === true) { |
| 56 | if (hostname !== GITLAB_CLOUD_HOSTNAME) { |
| 57 | try { |
| 58 | logger.debug(`Fetching all projects visible in ${config.url}...`); |
| 59 | const { durationMs, data: _projects } = await measure(async () => { |
| 60 | const fetchFn = () => api.Projects.all({ |
| 61 | perPage: 100, |
| 62 | }); |
| 63 | return fetchWithRetry(fetchFn, `all projects in ${config.url}`, logger); |
| 64 | }); |
| 65 | logger.debug(`Found ${_projects.length} projects in ${durationMs}ms.`); |
| 66 | allRepos = allRepos.concat(_projects); |
| 67 | } catch (e) { |
| 68 | Sentry.captureException(e); |
| 69 | logger.error(`Failed to fetch all projects visible in ${config.url}.`, e); |
| 70 | throw e; |
| 71 | } |
| 72 | } else { |
| 73 | const warning = `Ignoring option all:true in config : host is ${GITLAB_CLOUD_HOSTNAME}`; |
| 74 | logger.warn(warning); |
| 75 | allWarnings = allWarnings.concat(warning); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (config.groups) { |
| 80 | const results = await Promise.allSettled(config.groups.map(async (group) => { |
| 81 | try { |
| 82 | logger.debug(`Fetching project info for group ${group}...`); |
| 83 | const { durationMs, data } = await measure(async () => { |
| 84 | const fetchFn = () => api.Groups.allProjects(group, { |
| 85 | perPage: 100, |
| 86 | includeSubgroups: true |
| 87 | }); |
| 88 | return fetchWithRetry(fetchFn, `group ${group}`, logger); |
| 89 | }); |
| 90 | logger.debug(`Found ${data.length} projects in group ${group} in ${durationMs}ms.`); |
| 91 | return { |
| 92 | type: 'valid' as const, |
| 93 | data |
no test coverage detected