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 [HexPM] packages with no stable release #7685

Merged
merged 3 commits into from
Mar 5, 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
13 changes: 8 additions & 5 deletions services/hexpm/hexpm.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Joi from 'joi'
import { renderDownloadsBadge } from '../downloads.js'
import { addv, maybePluralize } from '../text-formatters.js'
import { version as versionColor } from '../color-formatters.js'
import { maybePluralize } from '../text-formatters.js'
import { renderVersionBadge } from '../version.js'
import { BaseJsonService } from '../index.js'

const hexSchema = Joi.object({
Expand All @@ -14,7 +14,8 @@ const hexSchema = Joi.object({
meta: Joi.object({
licenses: Joi.array().required(),
}).required(),
latest_stable_version: Joi.string().required(),
latest_stable_version: Joi.string(),
latest_version: Joi.string().required(),
}).required()

class BaseHexPmService extends BaseJsonService {
Expand Down Expand Up @@ -84,12 +85,14 @@ class HexPmVersion extends BaseHexPmService {
]

static render({ version }) {
return { message: addv(version), color: versionColor(version) }
return renderVersionBadge({ version })
}

async handle({ packageName }) {
const json = await this.fetch({ packageName })
return this.constructor.render({ version: json.latest_stable_version })
return this.constructor.render({
version: json.latest_stable_version || json.latest_version,
})
}
}

Expand Down
34 changes: 28 additions & 6 deletions services/hexpm/hexpm.tester.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Joi from 'joi'
import { ServiceTester } from '../tester.js'
import { isMetric, isMetricOverTimePeriod } from '../test-validators.js'

const isHexpmVersion = Joi.string().regex(/^v\d+.\d+.?\d?$/)
import {
isMetric,
isMetricOverTimePeriod,
isVPlusDottedVersionNClausesWithOptionalSuffix,
} from '../test-validators.js'

export const t = new ServiceTester({ id: 'hexpm', title: 'Hex.pm' })

Expand All @@ -22,6 +24,7 @@ t.create('downloads (zero for period)')
.reply(200, {
downloads: { all: 100 }, // there is no 'day' key here
latest_stable_version: '1.0',
latest_version: '1.0',
meta: { licenses: ['MIT'] },
})
)
Expand All @@ -35,9 +38,26 @@ t.create('downloads (not found)')
.get('/dt/this-package-does-not-exist.json')
.expectBadge({ label: 'downloads', message: 'not found' })

t.create('version')
.get('/v/cowboy.json')
.expectBadge({ label: 'hex', message: isHexpmVersion })
t.create('version').get('/v/cowboy.json').expectBadge({
label: 'hex',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

t.create('version (no stable version)')
.get('/v/prima_opentelemetry_ex.json')
.intercept(nock =>
nock('https://hex.pm/')
.get('/api/packages/prima_opentelemetry_ex')
.reply(200, {
downloads: { all: 100 },
latest_version: '1.0.0-rc.3',
meta: { licenses: ['MIT'] },
})
)
.expectBadge({
label: 'hex',
message: isVPlusDottedVersionNClausesWithOptionalSuffix,
})

t.create('version (not found)')
.get('/v/this-package-does-not-exist.json')
Expand All @@ -57,6 +77,7 @@ t.create('license (multiple licenses)')
.reply(200, {
downloads: { all: 100 },
latest_stable_version: '1.0',
latest_version: '1.0',
meta: { licenses: ['GPLv2', 'MIT'] },
})
)
Expand All @@ -74,6 +95,7 @@ t.create('license (no license)')
.reply(200, {
downloads: { all: 100 },
latest_stable_version: '1.0',
latest_version: '1.0',
meta: { licenses: [] },
})
)
Expand Down