Skip to content

Commit

Permalink
refactor: git url functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Apr 16, 2021
1 parent 39cd2c6 commit 03223c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
12 changes: 2 additions & 10 deletions lib/manager/git-submodules/extract.ts
Expand Up @@ -3,8 +3,7 @@ import Git, { SimpleGit } from 'simple-git';
import upath from 'upath';
import * as datasourceGitRefs from '../../datasource/git-refs';
import { logger } from '../../logger';
import { getHttpUrl } from '../../util/git';
import * as hostRules from '../../util/host-rules';
import { getHttpUrl, getRemoteUrlWithToken } from '../../util/git/url';
import type { ManagerConfig, PackageFile } from '../types';

type GitModule = {
Expand Down Expand Up @@ -113,14 +112,7 @@ export default async function extractPackageFile(
// hostRules only understands HTTP URLs
// Find HTTP URL, then apply token
let httpSubModuleUrl = getHttpUrl(subModuleUrl);
const hostRule = hostRules.find({ url: httpSubModuleUrl });
if (hostRule?.token) {
logger.debug(
{ httpSubModuleUrl },
'Found hostRules token for submodule'
);
httpSubModuleUrl = getHttpUrl(subModuleUrl, hostRule.token);
}
httpSubModuleUrl = getRemoteUrlWithToken(httpSubModuleUrl);
const currentValue = await getBranch(
gitModulesPath,
name,
Expand Down
7 changes: 0 additions & 7 deletions lib/util/git/index.ts
@@ -1,6 +1,5 @@
import URL from 'url';
import fs from 'fs-extra';
import GitUrlParse from 'git-url-parse';
import Git, {
DiffResult as DiffResult_,
ResetMode,
Expand Down Expand Up @@ -762,9 +761,3 @@ export function getUrl({
pathname: repository + '.git',
});
}

export function getHttpUrl(url: string, token?: string): string {
const parsedUrl = GitUrlParse(url);
parsedUrl.token = token;
return parsedUrl.toString('https');
}
21 changes: 21 additions & 0 deletions lib/util/git/url.ts
@@ -0,0 +1,21 @@
import GitUrlParse from 'git-url-parse';
import { logger } from '../../logger';
import * as hostRules from '../host-rules';

export function getHttpUrl(url: string, token?: string): string {
const parsedUrl = GitUrlParse(url);
parsedUrl.token = token;
return parsedUrl.toString('https');
}

export function getRemoteUrlWithToken(url: string): string {
let remote = url;

const hostRule = hostRules.find({ url });
if (hostRule?.token) {
logger.debug(`Found hostRules token for url ${url}`);
remote = getHttpUrl(url, hostRule.token);
}

return remote;
}

0 comments on commit 03223c6

Please sign in to comment.