mirror of
https://github.com/renovatebot/renovate.git
synced 2024-12-22 13:38:32 +00:00
c608ceeaac
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import is from '@sindresorhus/is';
|
|
import type { RetryObject } from 'got';
|
|
import { logger } from '../../logger';
|
|
import { ExternalHostError } from '../../types/errors/external-host-error';
|
|
import { parseLinkHeader, parseUrl } from '../url';
|
|
import type { HttpOptions, HttpResponse, InternalHttpOptions } from './types';
|
|
import { Http } from '.';
|
|
|
|
let baseUrl = 'https://gitlab.com/api/v4/';
|
|
export const setBaseUrl = (url: string): void => {
|
|
baseUrl = url;
|
|
};
|
|
|
|
export interface GitlabHttpOptions extends HttpOptions {
|
|
paginate?: boolean;
|
|
}
|
|
|
|
export class GitlabHttp extends Http<GitlabHttpOptions> {
|
|
constructor(type = 'gitlab', options?: GitlabHttpOptions) {
|
|
super(type, options);
|
|
}
|
|
|
|
protected override async request<T>(
|
|
url: string | URL,
|
|
options?: InternalHttpOptions & GitlabHttpOptions,
|
|
): Promise<HttpResponse<T>> {
|
|
const opts = {
|
|
baseUrl,
|
|
...options,
|
|
throwHttpErrors: true,
|
|
};
|
|
|
|
try {
|
|
const result = await super.request<T>(url, opts);
|
|
if (opts.paginate && is.array(result.body)) {
|
|
// Check if result is paginated
|
|
try {
|
|
const linkHeader = parseLinkHeader(result.headers.link);
|
|
const nextUrl = linkHeader?.next?.url
|
|
? parseUrl(linkHeader.next.url)
|
|
: null;
|
|
if (nextUrl) {
|
|
if (process.env.GITLAB_IGNORE_REPO_URL) {
|
|
const defaultEndpoint = new URL(baseUrl);
|
|
nextUrl.protocol = defaultEndpoint.protocol;
|
|
nextUrl.host = defaultEndpoint.host;
|
|
}
|
|
|
|
const nextResult = await this.request<T>(nextUrl, opts);
|
|
if (is.array(nextResult.body)) {
|
|
result.body.push(...nextResult.body);
|
|
}
|
|
}
|
|
} catch (err) /* istanbul ignore next */ {
|
|
logger.warn({ err }, 'Pagination error');
|
|
}
|
|
}
|
|
return result;
|
|
} catch (err) {
|
|
if (err.statusCode === 404) {
|
|
logger.trace({ err }, 'GitLab 404');
|
|
logger.debug({ url: err.url }, 'GitLab API 404');
|
|
throw err;
|
|
}
|
|
logger.debug({ err }, 'Gitlab API error');
|
|
if (
|
|
err.statusCode === 429 ||
|
|
(err.statusCode >= 500 && err.statusCode < 600)
|
|
) {
|
|
throw new ExternalHostError(err, 'gitlab');
|
|
}
|
|
const platformFailureCodes = [
|
|
'EAI_AGAIN',
|
|
'ECONNRESET',
|
|
'ETIMEDOUT',
|
|
'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
|
|
];
|
|
if (platformFailureCodes.includes(err.code)) {
|
|
throw new ExternalHostError(err, 'gitlab');
|
|
}
|
|
if (err.name === 'ParseError') {
|
|
throw new ExternalHostError(err, 'gitlab');
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
protected override calculateRetryDelay(retryObject: RetryObject): number {
|
|
const { error, attemptCount, retryOptions } = retryObject;
|
|
if (
|
|
attemptCount <= retryOptions.limit &&
|
|
error.options.method === 'POST' &&
|
|
error.response?.statusCode === 409 &&
|
|
error.response.rawBody.toString().includes('Resource lock')
|
|
) {
|
|
const noise = Math.random() * 100;
|
|
return 2 ** (attemptCount - 1) * 1000 + noise;
|
|
}
|
|
|
|
return super.calculateRetryDelay(retryObject);
|
|
}
|
|
}
|