1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00

use downloads renderer in next batch of classes, run [hexpm homebrewdownloads jenkinsplugininstalls jetbrainsdownloads jsdelivr modrinth]] (#7210)

* refactor: use downloads renderer in next batch of classes

* chore: run prettier
This commit is contained in:
Caleb Cartwright 2021-10-31 10:55:25 -05:00 committed by GitHub
parent ee544d13b6
commit 89410389e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 115 deletions

View File

@ -18,6 +18,8 @@ import { metric } from './text-formatters.js'
* value instead of the color being based on the count of downloads
* @param {string} [attrs.messageSuffixOverride] If provided then the badge message will
* will have this value added to the download count, separated with a space
* @param {string} [attrs.versionedLabelPrefix] If provided then the badge label will use
* this value as the prefix for versioned badges, e.g. `foobar@v1.23`. Defaults to 'downloads'
* @returns {object} Badge
*/
function renderDownloadsBadge({
@ -27,6 +29,7 @@ function renderDownloadsBadge({
labelOverride,
colorOverride,
messageSuffixOverride,
versionedLabelPrefix = 'downloads',
}) {
let messageSuffix = ''
if (messageSuffixOverride) {
@ -39,7 +42,7 @@ function renderDownloadsBadge({
if (labelOverride) {
label = labelOverride
} else if (version) {
label = `downloads@${version}`
label = `${versionedLabelPrefix}@${version}`
}
return {

View File

@ -20,6 +20,15 @@ describe('downloads', function () {
color,
message,
})
given({
downloads,
versionedLabelPrefix: 'installs',
version: 'v1.0.0',
}).expect({
label: 'installs@v1.0.0',
color,
message,
})
given({
downloads,
messageSuffixOverride: '[foo.tar.gz]',

View File

@ -1,6 +1,7 @@
import Joi from 'joi'
import { metric, addv, maybePluralize } from '../text-formatters.js'
import { downloadCount, version as versionColor } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { addv, maybePluralize } from '../text-formatters.js'
import { version as versionColor } from '../color-formatters.js'
import { BaseJsonService } from '../index.js'
const hexSchema = Joi.object({
@ -92,24 +93,23 @@ class HexPmVersion extends BaseHexPmService {
}
}
function DownloadsForInterval(interval) {
const { base, messageSuffix, name } = {
function DownloadsForInterval(downloadInterval) {
const { base, interval, name } = {
day: {
base: 'hexpm/dd',
messageSuffix: '/day',
interval: 'day',
name: 'HexPmDownloadsDay',
},
week: {
base: 'hexpm/dw',
messageSuffix: '/week',
interval: 'week',
name: 'HexPmDownloadsWeek',
},
all: {
base: 'hexpm/dt',
messageSuffix: '',
name: 'HexPmDownloadsTotal',
},
}[interval]
}[downloadInterval]
return class HexPmDownloads extends BaseHexPmService {
static name = name
@ -125,22 +125,16 @@ function DownloadsForInterval(interval) {
{
title: 'Hex.pm',
namedParams: { packageName: 'plug' },
staticPreview: this.render({ downloads: 85000 }),
staticPreview: renderDownloadsBadge({ downloads: 85000 }),
},
]
static defaultBadgeData = { label: 'downloads' }
static render({ downloads }) {
return {
message: `${metric(downloads)}${messageSuffix}`,
color: downloadCount(downloads),
}
}
async handle({ packageName }) {
const json = await this.fetch({ packageName })
return this.constructor.render({ downloads: json.downloads[interval] })
const downloads = json.downloads[downloadInterval]
return renderDownloadsBadge({ downloads, interval })
}
}
}

View File

@ -1,6 +1,5 @@
import Joi from 'joi'
import { downloadCount } from '../color-formatters.js'
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { BaseJsonService } from '../index.js'
import { nonNegativeInteger } from '../validators.js'
@ -19,15 +18,15 @@ function getSchema({ formula }) {
const periodMap = {
dm: {
api_field: '30d',
suffix: '/month',
interval: 'month',
},
dq: {
api_field: '90d',
suffix: '/quarter',
interval: 'quarter',
},
dy: {
api_field: '365d',
suffix: '/year',
interval: 'year',
},
}
@ -43,19 +42,12 @@ export default class HomebrewDownloads extends BaseJsonService {
{
title: 'homebrew downloads',
namedParams: { interval: 'dm', formula: 'cake' },
staticPreview: this.render({ interval: 'dm', downloads: 93 }),
staticPreview: renderDownloadsBadge({ interval: 'month', downloads: 93 }),
},
]
static defaultBadgeData = { label: 'downloads' }
static render({ interval, downloads }) {
return {
message: `${metric(downloads)}${periodMap[interval].suffix}`,
color: downloadCount(downloads),
}
}
async fetch({ formula }) {
const schema = getSchema({ formula })
return this._requestJson({
@ -66,10 +58,12 @@ export default class HomebrewDownloads extends BaseJsonService {
}
async handle({ interval, formula }) {
const data = await this.fetch({ formula })
return this.constructor.render({
interval,
downloads: data.analytics.install[periodMap[interval].api_field][formula],
const {
analytics: { install },
} = await this.fetch({ formula })
return renderDownloadsBadge({
downloads: install[periodMap[interval].api_field][formula],
interval: periodMap[interval].interval,
})
}
}

View File

@ -1,6 +1,5 @@
import Joi from 'joi'
import { downloadCount as downloadCountColor } from '../color-formatters.js'
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, NotFound } from '../index.js'
@ -23,22 +22,6 @@ const schemaInstallationsPerVersion = Joi.object()
.required()
export default class JenkinsPluginInstalls extends BaseJsonService {
static _getSchema(version) {
if (version) {
return schemaInstallationsPerVersion
} else {
return schemaInstallations
}
}
static _getLabel(version) {
if (version) {
return `installs@${version}`
} else {
return 'installs'
}
}
static category = 'downloads'
static route = {
@ -53,10 +36,7 @@ export default class JenkinsPluginInstalls extends BaseJsonService {
namedParams: {
plugin: 'view-job-filters',
},
staticPreview: this.render({
label: this._getLabel(),
installs: 10247,
}),
staticPreview: this.render({ installs: 10247 }),
},
{
title: 'Jenkins Plugin installs (version)',
@ -65,52 +45,48 @@ export default class JenkinsPluginInstalls extends BaseJsonService {
plugin: 'view-job-filters',
version: '1.26',
},
staticPreview: this.render({
label: this._getLabel('1.26'),
installs: 955,
}),
staticPreview: this.render({ installs: 955 }),
},
]
static defaultBadgeData = { label: 'installs' }
static render({ label, installs }) {
return {
label,
message: metric(installs),
color: downloadCountColor(installs),
}
static render({ installs: downloads, version }) {
return renderDownloadsBadge({
downloads,
versionedLabelPrefix: 'installs',
version,
})
}
async fetch({ plugin, version }) {
const url = `https://stats.jenkins.io/plugin-installation-trend/${plugin}.stats.json`
const schema = this.constructor._getSchema(version)
return this._requestJson({
url,
schema,
url: `https://stats.jenkins.io/plugin-installation-trend/${plugin}.stats.json`,
schema: version ? schemaInstallationsPerVersion : schemaInstallations,
errorMessages: {
404: 'plugin not found',
},
})
}
async handle({ plugin, version }) {
const label = this.constructor._getLabel(version)
const json = await this.fetch({ plugin, version })
let installs
if (version) {
installs = json.installationsPerVersion[version]
if (!installs) {
throw new NotFound({
prettyMessage: 'version not found',
})
}
} else {
static transform({ json, version }) {
if (!version) {
const latestDate = Object.keys(json.installations).sort().slice(-1)[0]
installs = json.installations[latestDate]
return { installs: json.installations[latestDate] }
}
return this.constructor.render({ label, installs })
const installs = json.installationsPerVersion[version]
if (!installs) {
throw new NotFound({
prettyMessage: 'version not found',
})
}
return { installs }
}
async handle({ plugin, version }) {
const json = await this.fetch({ plugin, version })
const { installs } = this.constructor.transform({ json, version })
return this.constructor.render({ installs, version })
}
}

View File

@ -1,6 +1,5 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { downloadCount as downloadCountColor } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import JetbrainsBase from './jetbrains-base.js'
@ -36,17 +35,10 @@ export default class JetbrainsDownloads extends JetbrainsBase {
namedParams: {
pluginId: '1347',
},
staticPreview: this.render({ downloads: 10200000 }),
staticPreview: renderDownloadsBadge({ downloads: 10200000 }),
},
]
static render({ downloads }) {
return {
message: `${metric(downloads)}`,
color: downloadCountColor(downloads),
}
}
async handle({ pluginId }) {
let downloads
if (this.constructor._isLegacyPluginId(pluginId)) {
@ -69,6 +61,6 @@ export default class JetbrainsDownloads extends JetbrainsBase {
downloads = jetbrainsPluginData.downloads
}
return this.constructor.render({ downloads })
return renderDownloadsBadge({ downloads })
}
}

View File

@ -1,6 +1,5 @@
import Joi from 'joi'
import { downloadCount } from '../color-formatters.js'
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { BaseJsonService } from '../index.js'
const schema = Joi.object({
@ -21,11 +20,8 @@ class BaseJsDelivrService extends BaseJsonService {
label: 'jsdelivr',
}
static render({ period, hits }) {
return {
message: `${metric(hits)}/${periodMap[period]}`,
color: downloadCount(hits),
}
static render({ period, hits: downloads }) {
return renderDownloadsBadge({ downloads, interval: periodMap[period] })
}
}

View File

@ -1,7 +1,6 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
import { metric } from '../text-formatters.js'
import { downloadCount as downloadCountColor } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
const schema = Joi.object({
@ -20,19 +19,12 @@ export default class Modrinth extends BaseJsonService {
{
title: 'Modrinth',
namedParams: { modId: 'AANobbMI' },
staticPreview: this.render({ downloads: 120000 }),
staticPreview: renderDownloadsBadge({ downloads: 120000 }),
},
]
static defaultBadgeData = { label: 'downloads' }
static render({ downloads }) {
return {
message: metric(downloads),
color: downloadCountColor(downloads),
}
}
async fetch({ modId }) {
return this._requestJson({
schema,
@ -42,6 +34,6 @@ export default class Modrinth extends BaseJsonService {
async handle({ modId }) {
const { downloads } = await this.fetch({ modId })
return this.constructor.render({ downloads })
return renderDownloadsBadge({ downloads })
}
}