Skip to content

Commit 25f9c1c

Browse files
authored
refactor(gitlab): move code (renovatebot#37483)
1 parent a5f3956 commit 25f9c1c

File tree

3 files changed

+85
-75
lines changed

3 files changed

+85
-75
lines changed

lib/modules/platform/gitlab/index.ts

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import URL from 'node:url';
21
import { setTimeout } from 'timers/promises';
32
import is from '@sindresorhus/is';
43
import pMap from 'p-map';
54
import semver from 'semver';
65
import {
7-
CONFIG_GIT_URL_UNAVAILABLE,
86
REPOSITORY_ACCESS_FORBIDDEN,
97
REPOSITORY_ARCHIVED,
108
REPOSITORY_CHANGED,
@@ -20,7 +18,6 @@ import { coerceArray } from '../../../util/array';
2018
import { noLeadingAtSymbol, parseJson } from '../../../util/common';
2119
import { getEnv } from '../../../util/env';
2220
import * as git from '../../../util/git';
23-
import * as hostRules from '../../../util/host-rules';
2421
import { memCacheProvider } from '../../../util/http/cache/memory-http-cache-provider';
2522
import type { GitlabHttpOptions } from '../../../util/http/gitlab';
2623
import { setBaseUrl } from '../../../util/http/gitlab';
@@ -29,11 +26,7 @@ import { parseInteger } from '../../../util/number';
2926
import * as p from '../../../util/promises';
3027
import { regEx } from '../../../util/regex';
3128
import { sanitize } from '../../../util/sanitize';
32-
import {
33-
ensureTrailingSlash,
34-
getQueryString,
35-
parseUrl,
36-
} from '../../../util/url';
29+
import { ensureTrailingSlash, getQueryString } from '../../../util/url';
3730
import type {
3831
AutodiscoverConfig,
3932
BranchStatusConfig,
@@ -42,7 +35,6 @@ import type {
4235
EnsureCommentRemovalConfig,
4336
EnsureIssueConfig,
4437
FindPRConfig,
45-
GitUrlOption,
4638
Issue,
4739
MergePRConfig,
4840
PlatformParams,
@@ -74,7 +66,13 @@ import type {
7466
MergeMethod,
7567
RepoResponse,
7668
} from './types';
77-
import { DRAFT_PREFIX, DRAFT_PREFIX_DEPRECATED, prInfo } from './utils';
69+
import {
70+
DRAFT_PREFIX,
71+
DRAFT_PREFIX_DEPRECATED,
72+
defaults,
73+
getRepoUrl,
74+
prInfo,
75+
} from './utils';
7876
export { extractRulesFromCodeOwnersLines } from './code-owners';
7977

8078
let config: {
@@ -99,12 +97,6 @@ export function resetPlatform(): void {
9997
setBaseUrl(defaults.endpoint);
10098
}
10199

102-
const defaults = {
103-
hostType: 'gitlab',
104-
endpoint: 'https://gitlab.com/api/v4/',
105-
version: '0.0.0',
106-
};
107-
108100
export const id = 'gitlab';
109101

110102
let draftPrefix = DRAFT_PREFIX;
@@ -264,62 +256,6 @@ export async function getJsonFile(
264256
return parseJson(raw, fileName);
265257
}
266258

267-
function getRepoUrl(
268-
repository: string,
269-
gitUrl: GitUrlOption | undefined,
270-
res: HttpResponse<RepoResponse>,
271-
): string {
272-
if (gitUrl === 'ssh') {
273-
if (!res.body.ssh_url_to_repo) {
274-
throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
275-
}
276-
logger.debug(`Using ssh URL: ${res.body.ssh_url_to_repo}`);
277-
return res.body.ssh_url_to_repo;
278-
}
279-
280-
const opts = hostRules.find({
281-
hostType: defaults.hostType,
282-
url: defaults.endpoint,
283-
});
284-
const env = getEnv();
285-
286-
if (
287-
gitUrl === 'endpoint' ||
288-
is.nonEmptyString(env.GITLAB_IGNORE_REPO_URL) ||
289-
res.body.http_url_to_repo === null
290-
) {
291-
if (res.body.http_url_to_repo === null) {
292-
logger.debug('no http_url_to_repo found. Falling back to old behavior.');
293-
}
294-
if (env.GITLAB_IGNORE_REPO_URL) {
295-
logger.warn(
296-
'GITLAB_IGNORE_REPO_URL environment variable is deprecated. Please use "gitUrl" option.',
297-
);
298-
}
299-
300-
// TODO: null check (#22198)
301-
const { protocol, host, pathname } = parseUrl(defaults.endpoint)!;
302-
const newPathname = pathname.slice(0, pathname.indexOf('/api'));
303-
const url = URL.format({
304-
protocol:
305-
/* v8 ignore next: should never happen */
306-
protocol.slice(0, -1) || 'https',
307-
// TODO: types (#22198)
308-
auth: `oauth2:${opts.token!}`,
309-
host,
310-
pathname: `${newPathname}/${repository}.git`,
311-
});
312-
logger.debug(`Using URL based on configured endpoint, url:${url}`);
313-
return url;
314-
}
315-
316-
logger.debug(`Using http URL: ${res.body.http_url_to_repo}`);
317-
const repoUrl = URL.parse(`${res.body.http_url_to_repo}`);
318-
// TODO: types (#22198)
319-
repoUrl.auth = `oauth2:${opts.token!}`;
320-
return URL.format(repoUrl);
321-
}
322-
323259
// Initialize GitLab by getting base branch
324260
export async function initRepo({
325261
repository,

lib/modules/platform/gitlab/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export interface RepoResponse {
6262
mirror: boolean;
6363
default_branch: string;
6464
empty_repo: boolean;
65-
ssh_url_to_repo: string;
66-
http_url_to_repo: string;
65+
ssh_url_to_repo: string | null;
66+
http_url_to_repo: string | null;
6767
forked_from_project: boolean;
6868
repository_access_level: 'disabled' | 'private' | 'enabled';
6969
merge_requests_access_level: 'disabled' | 'private' | 'enabled';

lib/modules/platform/gitlab/utils.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
import url from 'node:url';
12
import is from '@sindresorhus/is';
3+
import { CONFIG_GIT_URL_UNAVAILABLE } from '../../../constants/error-messages';
4+
import { logger } from '../../../logger';
5+
import { getEnv } from '../../../util/env';
6+
import * as hostRules from '../../../util/host-rules';
7+
import type { HttpResponse } from '../../../util/http/types';
8+
import { parseUrl } from '../../../util/url';
29
import { getPrBodyStruct } from '../pr-body';
3-
import type { GitLabMergeRequest, GitlabPr } from './types';
10+
import type { GitUrlOption } from '../types';
11+
import type { GitLabMergeRequest, GitlabPr, RepoResponse } from './types';
412

513
export const DRAFT_PREFIX = 'Draft: ';
614
export const DRAFT_PREFIX_DEPRECATED = 'WIP: ';
715

16+
export const defaults = {
17+
hostType: 'gitlab',
18+
endpoint: 'https://gitlab.com/api/v4/',
19+
version: '0.0.0',
20+
};
21+
822
export function prInfo(mr: GitLabMergeRequest): GitlabPr {
923
const pr: GitlabPr = {
1024
sourceBranch: mr.source_branch,
@@ -40,3 +54,63 @@ export function prInfo(mr: GitLabMergeRequest): GitlabPr {
4054

4155
return pr;
4256
}
57+
58+
export function getRepoUrl(
59+
repository: string,
60+
gitUrl: GitUrlOption | undefined,
61+
res: HttpResponse<RepoResponse>,
62+
): string {
63+
if (gitUrl === 'ssh') {
64+
if (!res.body.ssh_url_to_repo) {
65+
throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
66+
}
67+
logger.debug(`Using ssh URL: ${res.body.ssh_url_to_repo}`);
68+
return res.body.ssh_url_to_repo;
69+
}
70+
71+
const opts = hostRules.find({
72+
hostType: defaults.hostType,
73+
url: defaults.endpoint,
74+
});
75+
const env = getEnv();
76+
77+
if (
78+
gitUrl === 'endpoint' ||
79+
is.nonEmptyString(env.GITLAB_IGNORE_REPO_URL) ||
80+
res.body.http_url_to_repo === null
81+
) {
82+
if (res.body.http_url_to_repo === null) {
83+
logger.debug('no http_url_to_repo found. Falling back to old behavior.');
84+
}
85+
if (env.GITLAB_IGNORE_REPO_URL) {
86+
logger.warn(
87+
'GITLAB_IGNORE_REPO_URL environment variable is deprecated. Please use "gitUrl" option.',
88+
);
89+
}
90+
91+
// TODO: null check (#22198)
92+
const { protocol, host, pathname } = parseUrl(defaults.endpoint)!;
93+
const newPathname = pathname.slice(0, pathname.indexOf('/api'));
94+
const uri = url.format({
95+
protocol:
96+
/* v8 ignore next: should never happen */
97+
protocol.slice(0, -1) || 'https',
98+
// TODO: types (#22198)
99+
auth: `oauth2:${opts.token!}`,
100+
host,
101+
pathname: `${newPathname}/${repository}.git`,
102+
});
103+
logger.debug(`Using URL based on configured endpoint, url:${uri}`);
104+
return uri;
105+
}
106+
107+
logger.debug(`Using http URL: ${res.body.http_url_to_repo}`);
108+
const repoUrl = parseUrl(res.body.http_url_to_repo);
109+
// should never happen, but bad tests are causing that
110+
if (!repoUrl) {
111+
return '';
112+
}
113+
repoUrl.username = 'oauth2';
114+
repoUrl.password = opts.token!;
115+
return repoUrl.toString();
116+
}

0 commit comments

Comments
 (0)