All files / src/utils gitlab-base-url.ts

100% Statements 26/26
100% Branches 12/12
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46          261x 261x 261x 261x 261x           733x   261x 94x                 261x 1279x 135x 135x 131x   4x   1148x 1148x 1147x 1147x 1146x 1146x   1142x 1142x    
/**
 * Which GitLab instance a REST call goes to. Every tool builds its URLs from here, so a
 * call reaches the instance of its caller.
 */
 
import { logWarn } from '../logger';
import { isOAuthEnabled, getGitLabApiUrlFromContext } from '../oauth/index';
import { normalizeInstanceUrl } from './url';
import { currentCaller } from '../configuration/caller';
import { currentTokenAccount } from '../accounts/token-account';
 
/**
 * The instance the server's own token works with. The connection manager reports it; it
 * registers itself because it depends on the HTTP layer that depends on this module.
 */
let activeInstanceUrl: () => string | null = () => null;
 
export function setActiveInstanceSource(source: () => string | null): void {
  activeInstanceUrl = source;
}
 
/**
 * Base URL of the GitLab instance this call goes to (e.g. "https://gitlab.com"): the
 * OAuth token's instance, else the configured instance the chat switched to, else the
 * instance the running tool call started with, else the instance the server's own token
 * works with, else GITLAB_API_URL as configured at the time of the call.
 */
export function getGitLabBaseUrl(): string {
  if (isOAuthEnabled()) {
    const apiUrl = getGitLabApiUrlFromContext();
    if (apiUrl) {
      return normalizeInstanceUrl(apiUrl);
    }
    logWarn('OAuth mode: no API URL in context, falling back to global config');
  }
  const switched = currentTokenAccount()?.instanceUrl;
  if (switched) return switched;
  const pinned = currentCaller()?.instanceUrl;
  if (pinned) return pinned;
  const active = activeInstanceUrl();
  if (active) return active;
  // An unset or empty GITLAB_API_URL means gitlab.com, as in the startup configuration.
  const configured = process.env.GITLAB_API_URL;
  return configured ? normalizeInstanceUrl(configured) : 'https://gitlab.com';
}