import ky from 'ky' const rand = (arr: T[]): T => arr[Math.floor(Math.random() * arr.length)] // request gitlab api v4 (graphql) export function queryGitlab(query) { const token = pickGitlabToken() const headers = { authorization: token ? `Bearer ${token}` : undefined, } const json = { query } const endpoint = process.env.GITLAB_API_GRAPHQL || 'https://gitlab.com/api/graphql' return ky.post(endpoint, { json, headers }).json() } export function restGitlab(path: string, fullResponse = false) { const token = pickGitlabToken() const headers = { authorization: token ? `Bearer ${token}` : undefined, } const prefixUrl = process.env.GITLAB_API || 'https://gitlab.com/api/v4' return fullResponse ? ky.get(path, { prefixUrl, headers }) : ky.get(path, { prefixUrl, headers }).json() } function pickGitlabToken() { const { GITLAB_TOKENS } = process.env if (!GITLAB_TOKENS) { return null } const tokens = GITLAB_TOKENS.split(',').map(segment => segment.trim()) return rand(tokens) }