diff --git a/docs/usage/config-presets.md b/docs/usage/config-presets.md index 28fb2109b51ea5..5f6cadfb20b798 100644 --- a/docs/usage/config-presets.md +++ b/docs/usage/config-presets.md @@ -55,6 +55,7 @@ You can set a Git tag (like a SemVer) to use a specific release of your shared c | ------------------------------------------- | -------------------------------- | --------- | ---------------------------- | --------------- | -------------- | | GitHub default | `github>abc/foo` | `default` | `https://github.com/abc/foo` | `default.json` | Default branch | | GitHub with preset name | `github>abc/foo:xyz` | `xyz` | `https://github.com/abc/foo` | `xyz.json` | Default branch | +| GitHub with preset name (JSON5) | `github>abc/foo:xyz.json5` | `xyz` | `https://github.com/abc/foo` | `xyz.json5` | Default branch | | GitHub default with a tag | `github>abc/foo#1.5.4` | `default` | `https://github.com/abc/foo` | `default.json` | `1.5.4` | | GitHub with preset name with a tag | `github>abc/foo:xyz#1.5.4` | `xyz` | `https://github.com/abc/foo` | `xyz.json` | `1.5.4` | | GitHub with preset name and path with a tag | `github>abc/foo//path/xyz#1.5.4` | `xyz` | `https://github.com/abc/foo` | `path/xyz.json` | `1.5.4` | @@ -66,6 +67,7 @@ You can set a Git tag (like a SemVer) to use a specific release of your shared c | ------------------------------------------- | -------------------------------- | --------- | ---------------------------- | --------------- | -------------- | | GitLab default | `gitlab>abc/foo` | `default` | `https://gitlab.com/abc/foo` | `default.json` | Default branch | | GitLab with preset name | `gitlab>abc/foo:xyz` | `xyz` | `https://gitlab.com/abc/foo` | `xyz.json` | Default branch | +| GitLab with preset name (JSON5) | `gitlab>abc/foo:xyz.json5` | `xyz` | `https://gitlab.com/abc/foo` | `xyz.json5` | Default branch | | GitLab default with a tag | `gitlab>abc/foo#1.5.4` | `default` | `https://gitlab.com/abc/foo` | `default.json` | `1.5.4` | | GitLab with preset name with a tag | `gitlab>abc/foo:xyz#1.5.4` | `xyz` | `https://gitlab.com/abc/foo` | `xyz.json` | `1.5.4` | | GitLab with preset name and path with a tag | `gitlab>abc/foo//path/xyz#1.5.4` | `xyz` | `https://gitlab.com/abc/foo` | `path/xyz.json` | `1.5.4` | @@ -77,6 +79,7 @@ You can set a Git tag (like a SemVer) to use a specific release of your shared c | ------------------------------------------ | ------------------------------- | --------- | --------------------------- | --------------- | -------------- | | Gitea default | `gitea>abc/foo` | `default` | `https://gitea.com/abc/foo` | `default.json` | Default branch | | Gitea with preset name | `gitea>abc/foo:xyz` | `xyz` | `https://gitea.com/abc/foo` | `xyz.json` | Default branch | +| Gitea with preset name (JSON5) | `gitea>abc/foo:xyz.json5` | `xyz` | `https://gitea.com/abc/foo` | `xyz.json5` | Default branch | | Gitea default with a tag | `gitea>abc/foo#1.5.4` | `default` | `https://gitea.com/abc/foo` | `default.json` | `1.5.4` | | Gitea with preset name with a tag | `gitea>abc/foo:xyz#1.5.4` | `xyz` | `https://gitea.com/abc/foo` | `xyz.json` | `1.5.4` | | Gitea with preset name and path with a tag | `gitea>abc/foo//path/xyz#1.5.4` | `xyz` | `https://gitea.com/abc/foo` | `path/xyz.json` | `1.5.4` | @@ -88,6 +91,7 @@ You can set a Git tag (like a SemVer) to use a specific release of your shared c | ------------------------------------------ | ------------------------------- | --------- | ------------------------------------ | --------------- | -------------- | | Local default | `local>abc/foo` | `default` | `https://github.company.com/abc/foo` | `default.json` | Default branch | | Local with preset path | `local>abc/foo:xyz` | `xyz` | `https://github.company.com/abc/foo` | `xyz.json` | Default branch | +| Local with preset path (JSON5) | `local>abc/foo:xyz.json5` | `xyz` | `https://github.company.com/abc/foo` | `xyz.json5` | Default branch | | Local default with a tag | `local>abc/foo#1.5.4` | `default` | `https://github.company.com/abc/foo` | `default.json` | `1.5.4` | | Local with preset name with a tag | `local>abc/foo:xyz#1.5.4` | `xyz` | `https://github.company.com/abc/foo` | `xyz.json` | `1.5.4` | | Local with preset name and path with a tag | `local>abc/foo//path/xyz#1.5.4` | `xyz` | `https://github.company.com/abc/foo` | `path/xyz.json` | `1.5.4` | diff --git a/lib/config/presets/bitbucket-server/index.spec.ts b/lib/config/presets/bitbucket-server/index.spec.ts index 000e1a23703f29..a0d30d6b596469 100644 --- a/lib/config/presets/bitbucket-server/index.spec.ts +++ b/lib/config/presets/bitbucket-server/index.spec.ts @@ -35,6 +35,24 @@ describe('config/presets/bitbucket-server/index', () => { expect(res).toEqual({ from: 'api' }); }); + it('returns JSON5', async () => { + httpMock + .scope(bitbucketApiHost) + .get(`${basePath}/some-filename.json5`) + .query({ limit: 20000 }) + .reply(200, { + isLastPage: true, + lines: [{ text: '{from:"api"' }, { text: '}' }], + }); + + const res = await bitbucketServer.fetchJSONFile( + 'some/repo', + 'some-filename.json5', + bitbucketApiHost + ); + expect(res).toEqual({ from: 'api' }); + }); + it('handles branches/tags', async () => { httpMock .scope(bitbucketApiHost) diff --git a/lib/config/presets/gitea/index.spec.ts b/lib/config/presets/gitea/index.spec.ts index dd38faa217fe34..98a7d0c609f930 100644 --- a/lib/config/presets/gitea/index.spec.ts +++ b/lib/config/presets/gitea/index.spec.ts @@ -36,6 +36,23 @@ describe('config/presets/gitea/index', () => { ); expect(res).toEqual({ from: 'api' }); }); + + it('returns JSON5', async () => { + httpMock + .scope(giteaApiHost) + .get(`${basePath}/some-filename.json5`) + .reply(200, { + content: toBase64('{from:"api"}'), + }); + + const res = await gitea.fetchJSONFile( + 'some/repo', + 'some-filename.json5', + giteaApiHost, + null + ); + expect(res).toEqual({ from: 'api' }); + }); }); describe('getPreset()', () => { diff --git a/lib/config/presets/github/index.spec.ts b/lib/config/presets/github/index.spec.ts index 7df7244d062921..93ef768c1971f9 100644 --- a/lib/config/presets/github/index.spec.ts +++ b/lib/config/presets/github/index.spec.ts @@ -98,6 +98,34 @@ describe('config/presets/github/index', () => { expect(content).toEqual({ foo: 'bar' }); }); + it('should query preset within the file when .json extension provided', async () => { + httpMock + .scope(githubApiHost) + .get(`${basePath}/somefile.json`) + .reply(200, { + content: toBase64('{"foo":"bar"}'), + }); + const content = await github.getPreset({ + repo: 'some/repo', + presetName: 'somefile.json', + }); + expect(content).toEqual({ foo: 'bar' }); + }); + + it('should query preset within the file when .json5 extension provided', async () => { + httpMock + .scope(githubApiHost) + .get(`${basePath}/somefile.json5`) + .reply(200, { + content: toBase64('{foo:"bar"}'), + }); + const content = await github.getPreset({ + repo: 'some/repo', + presetName: 'somefile.json5', + }); + expect(content).toEqual({ foo: 'bar' }); + }); + it('should query subpreset', async () => { httpMock .scope(githubApiHost) diff --git a/lib/config/presets/gitlab/index.spec.ts b/lib/config/presets/gitlab/index.spec.ts index 6ab2e65dede2bc..0e69c42a554446 100644 --- a/lib/config/presets/gitlab/index.spec.ts +++ b/lib/config/presets/gitlab/index.spec.ts @@ -93,6 +93,54 @@ describe('config/presets/gitlab/index', () => { }); expect(content).toEqual({ foo: 'bar' }); }); + + it('should query custom paths with .json extension', async () => { + httpMock + .scope(gitlabApiHost) + .get(`${basePath}/branches`) + .reply(200, [ + { + name: 'devel', + }, + { + name: 'master', + default: true, + }, + ]) + .get(`${basePath}/files/path%2Fcustom.json/raw?ref=master`) + .reply(200, { foo: 'bar' }, {}); + + const content = await gitlab.getPreset({ + repo: 'some/repo', + presetPath: 'path', + presetName: 'custom.json', + }); + expect(content).toEqual({ foo: 'bar' }); + }); + + it('should query custom paths with .json5 extension', async () => { + httpMock + .scope(gitlabApiHost) + .get(`${basePath}/branches`) + .reply(200, [ + { + name: 'devel', + }, + { + name: 'master', + default: true, + }, + ]) + .get(`${basePath}/files/path%2Fcustom.json5/raw?ref=master`) + .reply(200, { foo: 'bar' }, {}); + + const content = await gitlab.getPreset({ + repo: 'some/repo', + presetPath: 'path', + presetName: 'custom.json5', + }); + expect(content).toEqual({ foo: 'bar' }); + }); }); describe('getPresetFromEndpoint()', () => { diff --git a/lib/config/presets/index.spec.ts b/lib/config/presets/index.spec.ts index 6fa528c27b99e5..842f2e02af0c0d 100644 --- a/lib/config/presets/index.spec.ts +++ b/lib/config/presets/index.spec.ts @@ -391,6 +391,54 @@ describe('config/presets/index', () => { }); }); + it('parses github file with preset name with .json extension', () => { + expect(presets.parsePreset('github>some/repo:somefile.json')).toEqual({ + repo: 'some/repo', + params: undefined, + presetName: 'somefile.json', + presetPath: undefined, + presetSource: 'github', + tag: undefined, + }); + }); + + it('parses github file with preset name with .json5 extension', () => { + expect(presets.parsePreset('github>some/repo:somefile.json5')).toEqual({ + repo: 'some/repo', + params: undefined, + presetName: 'somefile.json5', + presetPath: undefined, + presetSource: 'github', + tag: undefined, + }); + }); + + it('parses github subfiles with preset name with .json extension', () => { + expect( + presets.parsePreset('github>some/repo:somefile.json/somepreset') + ).toEqual({ + repo: 'some/repo', + params: undefined, + presetName: 'somefile.json/somepreset', + presetPath: undefined, + presetSource: 'github', + tag: undefined, + }); + }); + + it('parses github subfiles with preset name with .json5 extension', () => { + expect( + presets.parsePreset('github>some/repo:somefile.json5/somepreset') + ).toEqual({ + repo: 'some/repo', + params: undefined, + presetName: 'somefile.json5/somepreset', + presetPath: undefined, + presetSource: 'github', + tag: undefined, + }); + }); + it('parses github subfiles with preset and sub-preset name', () => { expect( presets.parsePreset( diff --git a/lib/config/presets/util.ts b/lib/config/presets/util.ts index 3ada40a587ea92..270da8757e5ff7 100644 --- a/lib/config/presets/util.ts +++ b/lib/config/presets/util.ts @@ -1,5 +1,6 @@ import JSON5 from 'json5'; import { logger } from '../../logger'; +import { regEx } from '../../util/regex'; import { ensureTrailingSlash } from '../../util/url'; import type { FetchPresetConfig, Preset } from './types'; @@ -51,7 +52,9 @@ export async function fetchPreset({ } else { jsonContent = await fetch( repo, - buildFilePath(`${fileName}.json`), + buildFilePath( + regEx(/\.json5?$/).test(fileName) ? fileName : `${fileName}.json` + ), endpoint, tag );