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

Support [F-Droid] custom repos #10792

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 31 additions & 6 deletions services/f-droid/f-droid.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const schema = Joi.object({
}).required()

const queryParamSchema = Joi.object({
serverFqdn: Joi.string().hostname(),
endpoint: Joi.string(),
Comment on lines +19 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we merge these two into one input? just endpoint, there is no need to split here.

Also i think Joi.string() is too loose for part of a url and could intrudoce invalid input that passes this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep it separate, as they are different components in the URL.
If we would combine them, how would you call it to not confuse the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the endpoint format. What about "relativeUri" from validators.js?
It seems other services use the same validator for paths.

Copy link
Member

@jNullj jNullj Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other services we use something like gitea_url or gitlab_url
i propose using something similar.
notice that in some tests the base url can even include get params.
also you can use the optionalUrl for the schema.

we could keep the project consistency and use fdroid_url here

i recommend you to take a look at one of the services already using hosted instances like gitea

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing the full URL does not feel right.
Especially with #2637 in mind, at least the protocol should be hard-coded to https.

The way Matrix (https://shields.io/badges/matrix) badges do it seems much better and "cleaner" for the user.

Copy link
Member

@jNullj jNullj Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for highlighting #2637. It seems like a valuable addition that could be implemented as a project-wide feature. While you can explore this idea in the current PR, I believe it would be better to separate these two subjects and ensure each PR focuses on a single topic for clarity and maintainability.

As a temporary solution, you could consider introducing a new validator, optionalUrlHttps, in validators.js. This would address the immediate need without over complicating this PR.

Regarding the Matrix badges, it uses only the FQDN for the host/endpoint. The mode parameter, however, serves a distinct purpose—it separates result types and facilitates different goals beyond accessing the endpoint. The key takeaway here is that mode provides functionality tied to achieving separate objectives.

Could you provide an example where your proposed endpoint achieves a distinct goal separate from simply accessing the same information? For comparison, consider how the Matrix badge's separation of FQDN and mode addresses two clearly different goals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A URL per RFC1738 also contains the scheme so we have to find a different name.

I see what you mean, so let's consider using only one parameter instead.

include_prereleases: Joi.equal(''),
}).required()

Expand All @@ -26,13 +28,25 @@ export default class FDroid extends BaseJsonService {
'/f-droid/v/{appId}': {
get: {
summary: 'F-Droid Version',
description:
'[F-Droid](https://f-droid.org/) is a catalogue of Open Source Android apps',
description: `
[F-Droid](https://f-droid.org/) is a catalogue of Open Source Android apps.

This badge by default uses <code>f-droid.org</code>, but also supports custom repos.
`,
parameters: [
pathParam({
name: 'appId',
example: 'org.dystopia.email',
}),
queryParam({
name: 'serverFqdn',
example: 'apt.izzysoft.de',
}),
queryParam({
name: 'endpoint',
example: 'fdroid',
description: `If the API is not located at root path, specify the additional path to the API.`,
}),
queryParam({
name: 'include_prereleases',
schema: { type: 'boolean' },
Expand All @@ -45,8 +59,12 @@ export default class FDroid extends BaseJsonService {

static defaultBadgeData = { label: 'f-droid' }

async fetch({ appId }) {
const url = `https://f-droid.org/api/v1/packages/${appId}`
async fetch({ serverFqdn, endpoint, appId }) {
endpoint = endpoint.replace('^/|/$', '')
if (endpoint !== '') {
endpoint = `/${endpoint}`
}
const url = `https://${serverFqdn}${endpoint}/api/v1/packages/${appId}`
return this._requestJson({
schema,
url,
Expand All @@ -71,8 +89,15 @@ export default class FDroid extends BaseJsonService {
return { version }
}

async handle({ appId }, { include_prereleases: includePre }) {
const json = await this.fetch({ appId })
async handle(
{ appId },
{
serverFqdn = 'f-droid.org',
endpoint = '',
include_prereleases: includePre,
},
) {
const json = await this.fetch({ serverFqdn, endpoint, appId })
const suggested = includePre === undefined
const { version } = this.transform({ json, suggested })
return renderVersionBadge({ version })
Expand Down
82 changes: 75 additions & 7 deletions services/f-droid/f-droid.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,33 @@ const testJson = `
const base = 'https://f-droid.org/api/v1'
const path = `/packages/${testPkg}`

t.create('Package is found')
t.create('f-droid.org: Package is found')
.get(`/v/${testPkg}.json`)
.intercept(nock => nock(base).get(path).reply(200, testJson))
.expectBadge({ label: 'f-droid', message: 'v0.2.7' })

t.create('Package is found (pre-release)')
t.create('f-droid.org: Package is found (pre-release)')
.get(`/v/${testPkg}.json?include_prereleases`)
.intercept(nock => nock(base).get(path).reply(200, testJson))
.expectBadge({ label: 'f-droid', message: 'v0.2.11' })

t.create('Package is not found with 403')
t.create('f-droid.org: Package is not found with 403')
.get(`/v/${testPkg}.json`)
.intercept(nock => nock(base).get(path).reply(403, 'some 403 text'))
.expectBadge({ label: 'f-droid', message: 'app not found' })

t.create('Package is not found with 404')
t.create('f-droid.org: Package is not found with 404')
.get('/v/io.shiels.does.not.exist.json')
.intercept(nock =>
nock(base)
.get('/packages/io.shiels.does.not.exist')
.reply(404, 'some 404 text'),
)
jNullj marked this conversation as resolved.
Show resolved Hide resolved
.expectBadge({ label: 'f-droid', message: 'app not found' })

t.create('Package is not found with no packages available (empty array)"')
t.create(
'f-droid.org: Package is not found with no packages available (empty array)"',
)
.get(`/v/${testPkg}.json`)
.intercept(nock =>
nock(base)
Expand All @@ -59,17 +66,78 @@ t.create('Package is not found with no packages available (empty array)"')
)
.expectBadge({ label: 'f-droid', message: 'no packages found' })

t.create('Package is not found with no packages available (missing array)"')
t.create(
'f-droid.org: Package is not found with no packages available (missing array)"',
)
.get(`/v/${testPkg}.json`)
.intercept(nock =>
nock(base).get(path).reply(200, `{"packageName":"${testPkg}"}`),
)
.expectBadge({ label: 'f-droid', message: 'no packages found' })

/* If this test fails, either the API has changed or the app was deleted. */
t.create('The real api did not change')
t.create('f-droid.org: The real api did not change')
.get('/v/org.thosp.yourlocalweather.json')
.expectBadge({
label: 'f-droid',
message: isVPlusDottedVersionAtLeastOne,
})

const base2 = 'https://apt.izzysoft.de/fdroid/api/v1'
const path2 = `/packages/${testPkg}`

t.create('custom repo: Package is found')
.get(`/v/${testPkg}.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid`)
.intercept(nock => nock(base2).get(path2).reply(200, testJson))
.expectBadge({ label: 'f-droid', message: 'v0.2.7' })

t.create('custom repo: Package is found (pre-release)')
.get(
`/v/${testPkg}.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid&include_prereleases`,
)
.intercept(nock => nock(base2).get(path2).reply(200, testJson))
.expectBadge({ label: 'f-droid', message: 'v0.2.11' })

t.create('custom repo: Package is not found with 403')
.get(`/v/${testPkg}.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid`)
.intercept(nock => nock(base2).get(path2).reply(403, 'some 403 text'))
.expectBadge({ label: 'f-droid', message: 'app not found' })

t.create('custom repo: Package is not found with 404')
.get(
'/v/io.shiels.does.not.exist.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid',
)
.intercept(nock =>
nock(base2)
.get('/packages/io.shiels.does.not.exist')
.reply(404, 'some 404 text'),
)
.expectBadge({ label: 'f-droid', message: 'app not found' })

t.create(
'custom repo: Package is not found with no packages available (empty array)"',
)
.get(`/v/${testPkg}.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid`)
.intercept(nock =>
nock(base2)
.get(path2)
.reply(200, `{"packageName":"${testPkg}","packages":[]}`),
)
.expectBadge({ label: 'f-droid', message: 'no packages found' })

t.create(
'custom repo: Package is not found with no packages available (missing array)"',
)
.get(`/v/${testPkg}.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid`)
.intercept(nock =>
nock(base2).get(path2).reply(200, `{"packageName":"${testPkg}"}`),
)
.expectBadge({ label: 'f-droid', message: 'no packages found' })

/* If this test fails, either the API has changed or the app was deleted. */
t.create('custom repo: The real api did not change')
.get('/v/com.looker.droidify.json?serverFqdn=apt.izzysoft.de&endpoint=fdroid')
.expectBadge({
label: 'f-droid',
message: isVPlusDottedVersionAtLeastOne,
})
Loading