community: better resolution of url

This commit is contained in:
Girish Ramakrishnan
2026-02-21 10:58:18 +01:00
parent 5014227028
commit eb5f8fcfa1
2 changed files with 151 additions and 63 deletions
+117 -32
View File
@@ -41,52 +41,137 @@ describe('Community', function () {
nock.cleanAll();
});
describe('_toRawUrl', function () {
it('rewrites github blob root and nested paths', async function () {
assert.equal(
community._toRawUrl(new URL('https://github.com/cloudron/test-app/blob/main/CloudronVersions.json')),
'https://raw.githubusercontent.com/cloudron/test-app/main/CloudronVersions.json'
describe('_getUrlCandidates', function () {
it('returns github blob URL and raw URL', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://github.com/cloudron/test-app/blob/main/CloudronVersions.json')),
[
'https://github.com/cloudron/test-app/blob/main/CloudronVersions.json',
'https://raw.githubusercontent.com/cloudron/test-app/main/CloudronVersions.json'
]
);
assert.equal(
community._toRawUrl(new URL('https://github.com/cloudron/test-app/blob/main/packaging/CloudronVersions.json')),
'https://raw.githubusercontent.com/cloudron/test-app/main/packaging/CloudronVersions.json'
assert.deepEqual(
community._getUrlCandidates(new URL('https://github.com/cloudron/test-app/blob/main/packaging/CloudronVersions.json')),
[
'https://github.com/cloudron/test-app/blob/main/packaging/CloudronVersions.json',
'https://raw.githubusercontent.com/cloudron/test-app/main/packaging/CloudronVersions.json'
]
);
});
it('rewrites gitlab blob root and nested paths', async function () {
assert.equal(
community._toRawUrl(new URL('https://gitlab.com/cloudron/test-app/-/blob/main/CloudronVersions.json')),
'https://gitlab.com/cloudron/test-app/-/raw/main/CloudronVersions.json'
it('returns gitlab blob URL and raw URL', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://gitlab.com/cloudron/test-app/-/blob/main/CloudronVersions.json')),
[
'https://gitlab.com/cloudron/test-app/-/blob/main/CloudronVersions.json',
'https://gitlab.com/cloudron/test-app/-/raw/main/CloudronVersions.json'
]
);
assert.equal(
community._toRawUrl(new URL('https://my.gitlab.company/group/subgroup/test-app/-/blob/main/path/CloudronVersions.json')),
'https://my.gitlab.company/group/subgroup/test-app/-/raw/main/path/CloudronVersions.json'
assert.deepEqual(
community._getUrlCandidates(new URL('https://my.gitlab.company/group/subgroup/test-app/-/blob/main/path/CloudronVersions.json')),
[
'https://my.gitlab.company/group/subgroup/test-app/-/blob/main/path/CloudronVersions.json',
'https://my.gitlab.company/group/subgroup/test-app/-/raw/main/path/CloudronVersions.json'
]
);
});
it('rewrites gitea/forgejo/gogs src paths', async function () {
assert.equal(
community._toRawUrl(new URL('https://gitea.example.com/user/repo/src/branch/main/CloudronVersions.json')),
'https://gitea.example.com/user/repo/raw/branch/main/CloudronVersions.json'
it('returns gitea/forgejo/gogs src URL and raw URL', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://gitea.example.com/user/repo/src/branch/main/CloudronVersions.json')),
[
'https://gitea.example.com/user/repo/src/branch/main/CloudronVersions.json',
'https://gitea.example.com/user/repo/raw/branch/main/CloudronVersions.json'
]
);
assert.equal(
community._toRawUrl(new URL('https://codeberg.org/forgejo/forgejo/src/branch/forgejo/packaging/CloudronVersions.json')),
'https://codeberg.org/forgejo/forgejo/raw/branch/forgejo/packaging/CloudronVersions.json'
assert.deepEqual(
community._getUrlCandidates(new URL('https://codeberg.org/forgejo/forgejo/src/branch/forgejo/packaging/CloudronVersions.json')),
[
'https://codeberg.org/forgejo/forgejo/src/branch/forgejo/packaging/CloudronVersions.json',
'https://codeberg.org/forgejo/forgejo/raw/branch/forgejo/packaging/CloudronVersions.json'
]
);
assert.equal(
community._toRawUrl(new URL('https://gogs.example.com/user/repo/src/tag/v1.0.0/CloudronVersions.json')),
'https://gogs.example.com/user/repo/raw/tag/v1.0.0/CloudronVersions.json'
assert.deepEqual(
community._getUrlCandidates(new URL('https://gogs.example.com/user/repo/src/tag/v1.0.0/CloudronVersions.json')),
[
'https://gogs.example.com/user/repo/src/tag/v1.0.0/CloudronVersions.json',
'https://gogs.example.com/user/repo/raw/tag/v1.0.0/CloudronVersions.json'
]
);
});
it('returns null for non-rewrite URLs', async function () {
assert.equal(
community._toRawUrl(new URL('https://raw.githubusercontent.com/cloudron/test-app/main/CloudronVersions.json')),
null
it('returns single candidate for already-raw github URL', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://raw.githubusercontent.com/cloudron/test-app/main/CloudronVersions.json')),
['https://raw.githubusercontent.com/cloudron/test-app/main/CloudronVersions.json']
);
assert.equal(
community._toRawUrl(new URL('https://github.com/cloudron/test-app')),
null
});
it('appends CloudronVersions.json when missing', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://example.com/cloudron/webmail/')),
['https://example.com/cloudron/webmail/CloudronVersions.json']
);
assert.deepEqual(
community._getUrlCandidates(new URL('https://github.com/cloudron/test-app/blob/main/packaging/')),
[
'https://github.com/cloudron/test-app/blob/main/packaging/CloudronVersions.json',
'https://raw.githubusercontent.com/cloudron/test-app/main/packaging/CloudronVersions.json'
]
);
assert.deepEqual(
community._getUrlCandidates(new URL('https://github.com/cloudron/test-app')),
['https://github.com/cloudron/test-app/CloudronVersions.json']
);
});
it('strips query params', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://gitlab.com/group/project/-/blob/main/CloudronVersions.json?ref_type=heads')),
[
'https://gitlab.com/group/project/-/blob/main/CloudronVersions.json',
'https://gitlab.com/group/project/-/raw/main/CloudronVersions.json'
]
);
});
it('returns single candidate for plain surfer URL', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://minimal-space.de/cloudron/webmail/CloudronVersions.json')),
['https://minimal-space.de/cloudron/webmail/CloudronVersions.json']
);
});
it('generates forge candidates for bare repo URL with git in subdomain', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://git.cloudron.io/platform/test-app')),
[
'https://git.cloudron.io/platform/test-app/CloudronVersions.json',
'https://git.cloudron.io/platform/test-app/-/raw/master/CloudronVersions.json',
'https://git.cloudron.io/platform/test-app/-/raw/main/CloudronVersions.json',
'https://git.cloudron.io/platform/test-app/raw/branch/master/CloudronVersions.json',
'https://git.cloudron.io/platform/test-app/raw/branch/main/CloudronVersions.json'
]
);
});
it('generates forge candidates for bare gitlab.com repo URL', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://gitlab.com/group/project')),
[
'https://gitlab.com/group/project/CloudronVersions.json',
'https://gitlab.com/group/project/-/raw/master/CloudronVersions.json',
'https://gitlab.com/group/project/-/raw/main/CloudronVersions.json',
'https://gitlab.com/group/project/raw/branch/master/CloudronVersions.json',
'https://gitlab.com/group/project/raw/branch/main/CloudronVersions.json'
]
);
});
it('does not generate forge candidates for non-git hostname', function () {
assert.deepEqual(
community._getUrlCandidates(new URL('https://example.com/some/path')),
['https://example.com/some/path/CloudronVersions.json']
);
});
});