Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(presets): add support to presets ending with .json5 or .json #15377

Merged
merged 15 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/usage/config-presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
rarkins marked this conversation as resolved.
Show resolved Hide resolved
| 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` |
Expand All @@ -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` |
Expand All @@ -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` |
Expand All @@ -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.saobby.my.eu.orgpany.com/abc/foo` | `default.json` | Default branch |
| Local with preset path | `local>abc/foo:xyz` | `xyz` | `https://github.saobby.my.eu.orgpany.com/abc/foo` | `xyz.json` | Default branch |
| Local with preset path (JSON5) | `local>abc/foo:xyz.json5` | `xyz` | `https://github.saobby.my.eu.orgpany.com/abc/foo` | `xyz.json5` | Default branch |
| Local default with a tag | `local>abc/foo#1.5.4` | `default` | `https://github.saobby.my.eu.orgpany.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.saobby.my.eu.orgpany.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.saobby.my.eu.orgpany.com/abc/foo` | `path/xyz.json` | `1.5.4` |
Expand Down
18 changes: 18 additions & 0 deletions lib/config/presets/bitbucket-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions lib/config/presets/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
28 changes: 28 additions & 0 deletions lib/config/presets/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions lib/config/presets/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
48 changes: 48 additions & 0 deletions lib/config/presets/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion lib/config/presets/util.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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
);
Expand Down