Replace slash stripping regexp with trimEnd

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2021-10-06 16:42:03 +02:00
parent 69b8b6efd4
commit a31afc5b62
10 changed files with 35 additions and 16 deletions
+9
View File
@@ -0,0 +1,9 @@
---
'@backstage/integration': patch
'@backstage/backend-common': patch
'@backstage/integration': patch
'@backstage/plugin-catalog-backend-module-ldap': patch
'@backstage/plugin-catalog-backend-module-msgraph': patch
---
Replace slash stripping regexp with trimEnd to remove CodeQL warning
@@ -25,6 +25,7 @@ import {
} from '@backstage/integration';
import fetch from 'cross-fetch';
import parseGitUrl from 'git-url-parse';
import { trimEnd } from 'lodash';
import { Minimatch } from 'minimatch';
import { Readable } from 'stream';
import {
@@ -149,7 +150,7 @@ export class BitbucketUrlReader implements UrlReader {
// a future improvement, we could be smart and try to deduce that non-glob
// prefixes (like for filepaths such as some-prefix/**/a.yaml) can be used
// to get just that part of the repo.
const treeUrl = url.replace(filepath, '').replace(/\/+$/, '');
const treeUrl = trimEnd(url.replace(filepath, ''), '/');
const tree = await this.readTree(treeUrl, {
etag: options?.etag,
@@ -37,6 +37,7 @@ import {
ReadUrlResponse,
ReadUrlOptions,
} from './types';
import { trimEnd } from 'lodash';
/** @public */
export class GitlabUrlReader implements UrlReader {
@@ -186,7 +187,7 @@ export class GitlabUrlReader implements UrlReader {
// a future improvement, we could be smart and try to deduce that non-glob
// prefixes (like for filepaths such as some-prefix/**/a.yaml) can be used
// to get just that part of the repo.
const treeUrl = url.replace(filepath, '').replace(/\/+$/, '');
const treeUrl = trimEnd(url.replace(filepath, ''), '/');
const tree = await this.readTree(treeUrl, {
etag: options?.etag,
+2 -1
View File
@@ -35,7 +35,8 @@
"git-url-parse": "^11.6.0",
"@octokit/rest": "^18.5.3",
"@octokit/auth-app": "^3.4.0",
"luxon": "^2.0.2"
"luxon": "^2.0.2",
"lodash": "^4.17.21"
},
"devDependencies": {
"@backstage/cli": "^0.7.14",
+1 -1
View File
@@ -83,7 +83,7 @@ export function readBitbucketIntegrationConfig(
}
if (apiBaseUrl) {
apiBaseUrl = apiBaseUrl.replace(/\/+$/, '');
apiBaseUrl = trimEnd(apiBaseUrl, '/');
} else if (host === BITBUCKET_HOST) {
apiBaseUrl = BITBUCKET_API_BASE_URL;
}
+3 -2
View File
@@ -15,6 +15,7 @@
*/
import { Config } from '@backstage/config';
import { trimEnd } from 'lodash';
import { isValidHost } from '../helpers';
const GITHUB_HOST = 'github.com';
@@ -133,13 +134,13 @@ export function readGitHubIntegrationConfig(
}
if (apiBaseUrl) {
apiBaseUrl = apiBaseUrl.replace(/\/+$/, '');
apiBaseUrl = trimEnd(apiBaseUrl, '/');
} else if (host === GITHUB_HOST) {
apiBaseUrl = GITHUB_API_BASE_URL;
}
if (rawBaseUrl) {
rawBaseUrl = rawBaseUrl.replace(/\/+$/, '');
rawBaseUrl = trimEnd(rawBaseUrl, '/');
} else if (host === GITHUB_HOST) {
rawBaseUrl = GITHUB_RAW_BASE_URL;
}
+3 -2
View File
@@ -15,6 +15,7 @@
*/
import { Config } from '@backstage/config';
import { trimEnd } from 'lodash';
import { isValidHost, isValidUrl } from '../helpers';
const GITLAB_HOST = 'gitlab.com';
@@ -67,13 +68,13 @@ export function readGitLabIntegrationConfig(
let baseUrl = config.getOptionalString('baseUrl');
if (apiBaseUrl) {
apiBaseUrl = apiBaseUrl.replace(/\/+$/, '');
apiBaseUrl = trimEnd(apiBaseUrl, '/');
} else if (host === GITLAB_HOST) {
apiBaseUrl = GITLAB_API_BASE_URL;
}
if (baseUrl) {
baseUrl = baseUrl.replace(/\/+$/, '');
baseUrl = trimEnd(baseUrl, '/');
} else {
baseUrl = `https://${host}`;
}
+5 -3
View File
@@ -15,6 +15,7 @@
*/
import parseGitUrl from 'git-url-parse';
import { trimEnd } from 'lodash';
import { ScmIntegration, ScmIntegrationsGroup } from './types';
/** Checks whether the given argument is a valid URL hostname */
@@ -83,9 +84,10 @@ export function defaultScmResolveUrl(options: {
// If it is an absolute path, move relative to the repo root
const { filepath } = parseGitUrl(base);
updated = new URL(base);
const repoRootPath = updated.pathname
.substring(0, updated.pathname.length - filepath.length)
.replace(/\/+$/, '');
const repoRootPath = trimEnd(
updated.pathname.substring(0, updated.pathname.length - filepath.length),
'/',
);
updated.pathname = `${repoRootPath}${url}`;
} else {
// For relative URLs, just let the default URL constructor handle the
@@ -18,6 +18,7 @@ import { Config, JsonValue } from '@backstage/config';
import { SearchOptions } from 'ldapjs';
import mergeWith from 'lodash/mergeWith';
import { RecursivePartial } from '@backstage/plugin-catalog-backend';
import { trimEnd } from 'lodash';
/**
* The configuration parameters for a single LDAP provider.
@@ -286,7 +287,7 @@ export function readLdapConfig(config: Config): LdapProviderConfig[] {
const providerConfigs = config.getOptionalConfigArray('providers') ?? [];
return providerConfigs.map(c => {
const newConfig = {
target: c.getString('target').replace(/\/+$/, ''),
target: trimEnd(c.getString('target'), '/'),
bind: readBindConfig(c.getOptionalConfig('bind')),
users: readUserConfig(c.getConfig('users')),
groups: readGroupConfig(c.getConfig('groups')),
@@ -15,6 +15,7 @@
*/
import { Config } from '@backstage/config';
import { trimEnd } from 'lodash';
/**
* The configuration parameters for a single Microsoft Graph provider.
@@ -72,10 +73,11 @@ export function readMicrosoftGraphConfig(
const providerConfigs = config.getOptionalConfigArray('providers') ?? [];
for (const providerConfig of providerConfigs) {
const target = providerConfig.getString('target').replace(/\/+$/, '');
const authority =
providerConfig.getOptionalString('authority')?.replace(/\/+$/, '') ||
'https://login.microsoftonline.com';
const target = trimEnd(providerConfig.getString('target'), '/');
const authority = providerConfig.getOptionalString('authority')
? trimEnd(providerConfig.getOptionalString('authority'), '/')
: 'https://login.microsoftonline.com';
const tenantId = providerConfig.getString('tenantId');
const clientId = providerConfig.getString('clientId');
const clientSecret = providerConfig.getString('clientSecret');