Skip to content

Commit

Permalink
fix(manager/git-submodule): use appropriate hostType for auth (#17400)
Browse files Browse the repository at this point in the history
Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
3 people committed Apr 21, 2023
1 parent 7b3e01f commit 455de72
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
7 changes: 7 additions & 0 deletions lib/modules/manager/git-submodules/__fixtures__/.gitmodules.5
Expand Up @@ -7,3 +7,10 @@
[submodule "renovate-config"]
path = deps/renovate-config
url = git@github.com:renovatebot/renovate-config.git
[submodule "some-other"]
path = some-other
url = https://domain.test/some/other.git

[submodule "some-gitlab"]
path = some-gitlab
url = https://gitlab.com/some/repo.git
64 changes: 58 additions & 6 deletions lib/modules/manager/git-submodules/extract.spec.ts
Expand Up @@ -18,11 +18,16 @@ const Git = jest.requireActual('simple-git') as SimpleGitFactory;

describe('modules/manager/git-submodules/extract', () => {
// flaky ci tests
jest.setTimeout(10 * 1000);
//jest.setTimeout(10 * 1000);

beforeAll(() => {
simpleGit.mockImplementation((basePath: string) => {
const git = Git(basePath);
const lsRemote: Record<string, string> = {
'https://abc@domain.test/some/other.git': '',
'https://gitlab-ci-token:xyz@gitlab.com/some/repo.git':
'ref: refs/heads/dev HEAD\n',
};
return {
subModule(): Response<string> {
return Promise.resolve(
Expand All @@ -40,7 +45,13 @@ describe('modules/manager/git-submodules/extract', () => {
}
return git.raw(options);
},
listRemote(): Response<string> {
listRemote(options: TaskOptions): Response<string> {
if (
is.array(options, is.string) &&
lsRemote[options[1]] !== undefined
) {
return Promise.resolve(lsRemote[options[1]]) as Response<string>;
}
return Promise.resolve(
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD'
) as Response<string>;
Expand All @@ -54,6 +65,16 @@ describe('modules/manager/git-submodules/extract', () => {
it('extracts submodules', async () => {
GlobalConfig.set({ localDir: `${__dirname}/__fixtures__` });
hostRules.add({ matchHost: 'github.com', token: '123test' });
hostRules.add({
matchHost: 'domain.test',
token: 'abc',
hostType: 'git-refs',
});
hostRules.add({
matchHost: 'gitlab.com',
token: 'xyz',
hostType: 'gitlab',
});
let res: PackageFileContent | null;
expect(await extractPackageFile('', '.gitmodules.1', {})).toBeNull();
res = await extractPackageFile('', '.gitmodules.2', {});
Expand All @@ -64,10 +85,41 @@ describe('modules/manager/git-submodules/extract', () => {
res = await extractPackageFile('', '.gitmodules.4', {});
expect(res?.deps).toHaveLength(1);
res = await extractPackageFile('', '.gitmodules.5', {});
expect(res?.deps).toHaveLength(3);
expect(res?.deps[2].packageName).toBe(
'https://github.com/renovatebot/renovate-config.git'
);
expect(res).toEqual({
datasource: 'git-refs',
deps: [
{
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
currentValue: 'main',
depName: 'deps/renovate',
packageName: 'https://github.com/renovatebot/renovate.git',
},
{
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
currentValue: 'main',
depName: 'deps/renovate-pro',
packageName: 'https://github.com/renovatebot/pro.git',
},
{
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
currentValue: 'main',
depName: 'deps/renovate-config',
packageName: 'https://github.com/renovatebot/renovate-config.git',
},
{
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
currentValue: 'master',
depName: 'some-other',
packageName: 'https://domain.test/some/other.git',
},
{
currentDigest: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
currentValue: 'dev',
depName: 'some-gitlab',
packageName: 'https://gitlab.com/some/repo.git',
},
],
});
});
});
});
4 changes: 3 additions & 1 deletion lib/modules/manager/git-submodules/extract.ts
Expand Up @@ -3,6 +3,7 @@ import Git, { SimpleGit } from 'simple-git';
import upath from 'upath';
import { GlobalConfig } from '../../../config/global';
import { logger } from '../../../logger';
import { detectPlatform } from '../../../util/common';
import { simpleGitConfig } from '../../../util/git/config';
import { getHttpUrl, getRemoteUrlWithToken } from '../../../util/git/url';
import { regEx } from '../../../util/regex';
Expand Down Expand Up @@ -115,7 +116,8 @@ export default async function extractPackageFile(
// hostRules only understands HTTP URLs
// Find HTTP URL, then apply token
let httpSubModuleUrl = getHttpUrl(subModuleUrl);
httpSubModuleUrl = getRemoteUrlWithToken(httpSubModuleUrl);
const hostType = detectPlatform(httpSubModuleUrl) ?? GitRefsDatasource.id;
httpSubModuleUrl = getRemoteUrlWithToken(httpSubModuleUrl, hostType);
const currentValue = await getBranch(
gitModulesPath,
name,
Expand Down

0 comments on commit 455de72

Please sign in to comment.