MCPcopy
hub / github.com/conwnet/github1s / GitLabFetcher

Class GitLabFetcher

extensions/github1s/src/adapters/gitlab1s/fetcher.ts:45–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43const PREFER_SOURCEGRAPH_API = 'PREFER_SOURCEGRAPH_API';
44
45export class GitLabFetcher {
46 private static instance: GitLabFetcher | null = null;
47 private _emitter = new vscode.EventEmitter<boolean | null | undefined>();
48 public onDidChangePreferSourcegraphApi = this._emitter.event;
49 private _currentRepoPromise: Promise<any> | null = null;
50
51 public static getInstance(): GitLabFetcher {
52 if (GitLabFetcher.instance) {
53 return GitLabFetcher.instance;
54 }
55 return (GitLabFetcher.instance = new GitLabFetcher());
56 }
57
58 private constructor() {
59 this.initPreferSourcegraphApi();
60 GitLabTokenManager.getInstance().onDidChangeToken(() => this.initPreferSourcegraphApi());
61 }
62
63 private _request = reuseable(
64 (
65 command: string,
66 params: Record<string, string | number | boolean | undefined>,
67 ): Promise<{ status: number; data: any; headers: Headers }> => {
68 // eslint-disable-next-line prefer-const
69 let [method, path] = command.split(/\s+/).filter(Boolean);
70 Object.keys(params).forEach((el) => {
71 path = path.replace(`{${el}}`, `${encodeURIComponent(params[el] || '')}`);
72 });
73 const accessToken = GitLabTokenManager.getInstance().getToken();
74 const fetchOptions: { headers: Record<string, string> } =
75 accessToken?.length < 60
76 ? { headers: { 'PRIVATE-TOKEN': `${accessToken}` } }
77 : { headers: { Authorization: `Bearer ${accessToken}` } };
78 return fetch(GITLAB_API_PREFIX + path, {
79 ...fetchOptions,
80 method,
81 }).then(async (response: Response & { data: any }) => {
82 response.data = await response.json();
83 return response.ok ? response : Promise.reject({ response });
84 });
85 },
86 );
87
88 public request = (command: string, params: Record<string, string | number | boolean | undefined>) => {
89 return this._request(command, params).catch(async (error: { response: any }) => {
90 const errorStatus = error?.response?.status as number | undefined;
91 const repoNotFound = errorStatus === 404 && !(await this.resolveCurrentRepo());
92 if ((errorStatus && [401, 403].includes(errorStatus)) || repoNotFound) {
93 // maybe we have to acquire github access token to continue
94 const accessToken = GitLabTokenManager.getInstance().getToken();
95 const message = detectErrorMessage(error?.response, !!accessToken);
96 await GitLab1sAuthenticationView.getInstance().open(message, true);
97 return this._request(command, params);
98 }
99 throw error;
100 });
101 };
102

Callers

nothing calls this directly

Calls 7

resolveCurrentRepoMethod · 0.95
reuseableFunction · 0.90
replaceMethod · 0.80
getTokenMethod · 0.80
detectErrorMessageFunction · 0.70
openMethod · 0.65
getInstanceMethod · 0.45

Tested by

no test coverage detected