Skip to content

Commit

Permalink
refactor: Rename validateUrl to isHttpUrl (#28484)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 17, 2024
1 parent e7d9c05 commit d6d1e57
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/modules/datasource/metadata.ts
Expand Up @@ -5,7 +5,7 @@ import { detectPlatform } from '../../util/common';
import { parseGitUrl } from '../../util/git/url';
import * as hostRules from '../../util/host-rules';
import { regEx } from '../../util/regex';
import { parseUrl, trimTrailingSlash, validateUrl } from '../../util/url';
import { isHttpUrl, parseUrl, trimTrailingSlash } from '../../util/url';
import { manualChangelogUrls, manualSourceUrls } from './metadata-manual';
import type { ReleaseResult } from './types';

Expand Down Expand Up @@ -195,7 +195,7 @@ export function addMetaData(
];
for (const urlKey of urlKeys) {
const urlVal = dep[urlKey];
if (is.string(urlVal) && validateUrl(urlVal.trim())) {
if (is.string(urlVal) && isHttpUrl(urlVal.trim())) {
dep[urlKey] = urlVal.trim() as never;
} else {
delete dep[urlKey];
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/datasource/npm/npmrc.ts
Expand Up @@ -8,7 +8,7 @@ import type { HostRule } from '../../../types';
import * as hostRules from '../../../util/host-rules';
import { regEx } from '../../../util/regex';
import { fromBase64 } from '../../../util/string';
import { ensureTrailingSlash, validateUrl } from '../../../util/url';
import { ensureTrailingSlash, isHttpUrl } from '../../../util/url';
import { defaultRegistryUrls } from './common';
import type { NpmrcRules } from './types';

Expand Down Expand Up @@ -89,7 +89,7 @@ export function convertNpmrcToRules(npmrc: Record<string, any>): NpmrcRules {
const { registry } = npmrc;
// packageRules order matters, so look for a default registry first
if (is.nonEmptyString(registry)) {
if (validateUrl(registry)) {
if (isHttpUrl(registry)) {
// Default registry
rules.packageRules?.push({
matchDatasources,
Expand All @@ -108,7 +108,7 @@ export function convertNpmrcToRules(npmrc: Record<string, any>): NpmrcRules {
const keyType = keyParts.pop();
if (keyType === 'registry' && keyParts.length && is.nonEmptyString(value)) {
const scope = keyParts.join(':');
if (validateUrl(value)) {
if (isHttpUrl(value)) {
rules.packageRules?.push({
matchDatasources,
matchPackagePrefixes: [scope + '/'],
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/terraform-module/index.ts
Expand Up @@ -2,7 +2,7 @@ import { logger } from '../../../logger';
import { cache } from '../../../util/cache/package/decorator';
import { regEx } from '../../../util/regex';
import { coerceString } from '../../../util/string';
import { validateUrl } from '../../../util/url';
import { isHttpUrl } from '../../../util/url';
import * as hashicorpVersioning from '../../versioning/hashicorp';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import { TerraformDatasource } from './base';
Expand Down Expand Up @@ -162,7 +162,7 @@ export class TerraformModuleDatasource extends TerraformDatasource {
};

// Add the source URL if given
if (validateUrl(res.modules[0].source)) {
if (isHttpUrl(res.modules[0].source)) {
dep.sourceUrl = res.modules[0].source;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/terraform-module/utils.ts
@@ -1,4 +1,4 @@
import { joinUrlParts, validateUrl } from '../../../util/url';
import { isHttpUrl, joinUrlParts } from '../../../util/url';
import type {
ServiceDiscoveryEndpointType,
ServiceDiscoveryResult,
Expand All @@ -12,7 +12,7 @@ export function createSDBackendURL(
): string {
const sdEndpoint = sdResult[sdType] ?? '';
const fullPath = joinUrlParts(sdEndpoint, subPath);
if (validateUrl(fullPath)) {
if (isHttpUrl(fullPath)) {
return fullPath;
}
return joinUrlParts(registryURL, fullPath);
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/npm/post-update/rules.ts
Expand Up @@ -2,7 +2,7 @@ import is from '@sindresorhus/is';
import * as hostRules from '../../../../util/host-rules';
import { regEx } from '../../../../util/regex';
import { toBase64 } from '../../../../util/string';
import { validateUrl } from '../../../../util/url';
import { isHttpUrl } from '../../../../util/url';

export interface HostRulesResult {
additionalNpmrcContent: string[];
Expand All @@ -21,7 +21,7 @@ export function processHostRules(): HostRulesResult {
if (hostRule.resolvedHost) {
let uri = hostRule.matchHost;
uri =
is.string(uri) && validateUrl(uri)
is.string(uri) && isHttpUrl(uri)
? uri.replace(regEx(/^https?:/), '')
: // TODO: types (#22198)
`//${uri}/`;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/git/auth.ts
Expand Up @@ -4,7 +4,7 @@ import type { HostRule } from '../../types';
import { detectPlatform } from '../common';
import { find, getAll } from '../host-rules';
import { regEx } from '../regex';
import { createURLFromHostOrURL, validateUrl } from '../url';
import { createURLFromHostOrURL, isHttpUrl } from '../url';
import type { AuthenticationRule } from './types';
import { parseGitUrl } from './url';

Expand Down Expand Up @@ -203,7 +203,7 @@ function addAuthFromHostRule(
): NodeJS.ProcessEnv {
let environmentVariables = env;
const httpUrl = createURLFromHostOrURL(hostRule.matchHost!)?.toString();
if (validateUrl(httpUrl)) {
if (isHttpUrl(httpUrl)) {
logger.trace(`Adding Git authentication for ${httpUrl} using token auth.`);
environmentVariables = getGitAuthenticatedEnvironmentVariables(
httpUrl!,
Expand Down
4 changes: 2 additions & 2 deletions lib/util/host-rules.ts
Expand Up @@ -6,7 +6,7 @@ import type { CombinedHostRule, HostRule } from '../types';
import { clone } from './clone';
import * as sanitize from './sanitize';
import { toBase64 } from './string';
import { parseUrl, validateUrl } from './url';
import { isHttpUrl, parseUrl } from './url';

let hostRules: HostRule[] = [];

Expand Down Expand Up @@ -108,7 +108,7 @@ function isOnlyMatchHost(
}

function matchesHost(url: string, matchHost: string): boolean {
if (validateUrl(url) && validateUrl(matchHost)) {
if (isHttpUrl(url) && isHttpUrl(matchHost)) {
return url.startsWith(matchHost);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/http/gerrit.ts
@@ -1,6 +1,6 @@
import { parseJson } from '../common';
import { regEx } from '../regex';
import { validateUrl } from '../url';
import { isHttpUrl } from '../url';
import type { HttpOptions, HttpResponse, InternalHttpOptions } from './types';
import { Http } from './index';

Expand All @@ -24,7 +24,7 @@ export class GerritHttp extends Http {
path: string,
options?: InternalHttpOptions,
): Promise<HttpResponse<T>> {
const url = validateUrl(path) ? path : baseUrl + path;
const url = isHttpUrl(path) ? path : baseUrl + path;
const opts: InternalHttpOptions = {
parseJson: (text: string) =>
parseJson(text.replace(GerritHttp.magicPrefix, ''), path),
Expand Down
19 changes: 9 additions & 10 deletions lib/util/url.spec.ts
Expand Up @@ -3,14 +3,14 @@ import {
ensurePathPrefix,
ensureTrailingSlash,
getQueryString,
isHttpUrl,
joinUrlParts,
parseLinkHeader,
parseUrl,
replaceUrlPath,
resolveBaseUrl,
trimSlashes,
trimTrailingSlash,
validateUrl,
} from './url';

describe('util/url', () => {
Expand Down Expand Up @@ -97,15 +97,14 @@ describe('util/url', () => {
expect(getQueryString({ a: 1, b: [1, 2] })).toBe('a=1&b=1&b=2');
});

it('validates URLs', () => {
expect(validateUrl(undefined)).toBeFalse();
expect(validateUrl('')).toBeFalse();
expect(validateUrl(null)).toBeFalse();
expect(validateUrl('foo')).toBeFalse();
expect(validateUrl('ssh://github.com')).toBeFalse();
expect(validateUrl('http://github.com')).toBeTrue();
expect(validateUrl('https://github.com')).toBeTrue();
expect(validateUrl('https://github.com', false)).toBeTrue();
it('validates http-based URLs', () => {
expect(isHttpUrl(undefined)).toBeFalse();
expect(isHttpUrl('')).toBeFalse();
expect(isHttpUrl(null)).toBeFalse();
expect(isHttpUrl('foo')).toBeFalse();
expect(isHttpUrl('ssh://github.com')).toBeFalse();
expect(isHttpUrl('http://github.com')).toBeTrue();
expect(isHttpUrl('https://github.com')).toBeTrue();
});

it('parses URL', () => {
Expand Down
7 changes: 2 additions & 5 deletions lib/util/url.ts
Expand Up @@ -85,16 +85,13 @@ export function getQueryString(params: Record<string, any>): string {
return usp.toString();
}

export function validateUrl(
url: string | null | undefined,
httpOnly = true,
): boolean {
export function isHttpUrl(url: unknown): boolean {
if (!is.nonEmptyString(url)) {
return false;
}
try {
const { protocol } = new URL(url);
return httpOnly ? !!protocol.startsWith('http') : !!protocol;
return protocol === 'https:' || protocol === 'http:';
} catch (err) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/workers/repository/update/pr/changelog/release-notes.ts
Expand Up @@ -9,7 +9,7 @@ import { detectPlatform } from '../../../../../util/common';
import { linkify } from '../../../../../util/markdown';
import { newlineRegex, regEx } from '../../../../../util/regex';
import { coerceString } from '../../../../../util/string';
import { validateUrl } from '../../../../../util/url';
import { isHttpUrl } from '../../../../../util/url';
import type { BranchUpgradeConfig } from '../../../../types';
import * as bitbucket from './bitbucket';
import * as gitea from './gitea';
Expand Down Expand Up @@ -346,12 +346,12 @@ export async function getReleaseNotesMd(
.filter(Boolean);
let body = section.replace(regEx(/.*?\n(-{3,}\n)?/), '').trim();
for (const word of title) {
if (word.includes(version) && !validateUrl(word)) {
if (word.includes(version) && !isHttpUrl(word)) {
logger.trace({ body }, 'Found release notes for v' + version);
// TODO: fix url
const notesSourceUrl = `${baseUrl}${repository}/blob/HEAD/${changelogFile}`;
const mdHeadingLink = title
.filter((word) => !validateUrl(word))
.filter((word) => !isHttpUrl(word))
.join('-')
.replace(regEx(/[^A-Za-z0-9-]/g), '');
const url = `${notesSourceUrl}#${mdHeadingLink}`;
Expand Down

0 comments on commit d6d1e57

Please sign in to comment.