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

Add a render helper for downloads badges, run [amo ansible apm chromewebstore clojars conda crates docker dub eclipse gem githubdownloads] #7163

Merged
merged 4 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 2 additions & 6 deletions services/amo/amo-downloads.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { metric } from '../text-formatters.js'
import { downloadCount } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { redirector } from '../index.js'
import { BaseAmoService, keywords } from './amo-base.js'

Expand Down Expand Up @@ -28,10 +27,7 @@ class AmoWeeklyDownloads extends BaseAmoService {
static defaultBadgeData = { label: 'downloads' }

static render({ downloads }) {
return {
message: `${metric(downloads)}/week`,
color: downloadCount(downloads),
}
return renderDownloadsBadge({ downloads, interval: 'week' })
}

async handle({ addonId }) {
Expand Down
9 changes: 3 additions & 6 deletions services/amo/amo-users.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { BaseAmoService, keywords } from './amo-base.js'

export default class AmoUsers extends BaseAmoService {
Expand All @@ -16,11 +16,8 @@ export default class AmoUsers extends BaseAmoService {

static defaultBadgeData = { label: 'users' }

static render({ users }) {
return {
message: metric(users),
color: 'blue',
}
static render({ users: downloads }) {
return renderDownloadsBadge({ downloads, colorOverride: 'blue' })
}

async handle({ addonId }) {
Expand Down
14 changes: 3 additions & 11 deletions services/ansible/ansible-role.service.js
Original file line number Diff line number Diff line change
@@ -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 { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'

Expand Down Expand Up @@ -32,22 +31,15 @@ class AnsibleGalaxyRoleDownloads extends AnsibleGalaxyRole {
{
title: 'Ansible Role',
namedParams: { roleId: '3078' },
staticPreview: this.render({ downloads: 76 }),
staticPreview: renderDownloadsBadge({ downloads: 76 }),
},
]

static defaultBadgeData = { label: 'role downloads' }

static render({ downloads }) {
return {
message: metric(downloads),
color: downloadCount(downloads),
}
}

async handle({ roleId }) {
const json = await this.fetch({ roleId })
return this.constructor.render({ downloads: json.download_count })
return renderDownloadsBadge({ downloads: json.download_count })
}
}

Expand Down
4 changes: 2 additions & 2 deletions services/apm/apm.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Joi from 'joi'
import { renderLicenseBadge } from '../licenses.js'
import { renderVersionBadge } from '../version.js'
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, InvalidResponse } from '../index.js'

Expand Down Expand Up @@ -45,7 +45,7 @@ class APMDownloads extends BaseAPMService {
static defaultBadgeData = { label: 'downloads' }

static render({ downloads }) {
return { message: metric(downloads), color: 'green' }
return renderDownloadsBadge({ downloads, colorOverride: 'green' })
}

async handle({ packageName }) {
Expand Down
14 changes: 3 additions & 11 deletions services/chrome-web-store/chrome-web-store-users.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { metric } from '../text-formatters.js'
import { downloadCount } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { redirector, NotFound } from '../index.js'
import BaseChromeWebStoreService from './chrome-web-store-base.js'

Expand All @@ -11,26 +10,19 @@ class ChromeWebStoreUsers extends BaseChromeWebStoreService {
{
title: 'Chrome Web Store',
namedParams: { storeId: 'ogffaloegjglncjfehdfplabnoondfjo' },
staticPreview: this.render({ downloads: 573 }),
staticPreview: renderDownloadsBadge({ downloads: 573 }),
},
]

static defaultBadgeData = { label: 'users' }

static render({ downloads }) {
return {
message: `${metric(downloads)}`,
color: downloadCount(downloads),
}
}

async handle({ storeId }) {
const chromeWebStore = await this.fetch({ storeId })
const downloads = chromeWebStore.users()
if (downloads == null) {
throw new NotFound({ prettyMessage: 'not found' })
}
return this.constructor.render({ downloads })
return renderDownloadsBadge({ downloads })
}
}

Expand Down
15 changes: 3 additions & 12 deletions services/clojars/clojars-downloads.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { metric } from '../text-formatters.js'
import { downloadCount as downloadsColor } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { BaseClojarsService } from './clojars-base.js'

export default class ClojarsDownloads extends BaseClojarsService {
Expand All @@ -9,22 +8,14 @@ export default class ClojarsDownloads extends BaseClojarsService {
static examples = [
{
namedParams: { clojar: 'prismic' },
staticPreview: this.render({ downloads: 117 }),
staticPreview: renderDownloadsBadge({ downloads: 117 }),
},
]

static defaultBadgeData = { label: 'downloads' }

static render({ downloads }) {
return {
label: 'downloads',
message: metric(downloads),
color: downloadsColor(downloads),
}
}

async handle({ clojar }) {
const json = await this.fetch({ clojar })
return this.constructor.render({ downloads: json.downloads })
return renderDownloadsBadge({ downloads: json.downloads })
}
}
10 changes: 3 additions & 7 deletions services/conda/conda-downloads.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { metric } from '../text-formatters.js'
import { downloadCount } from '../color-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import BaseCondaService from './conda-base.js'

export default class CondaDownloads extends BaseCondaService {
Expand All @@ -16,11 +15,8 @@ export default class CondaDownloads extends BaseCondaService {
]

static render({ variant, downloads }) {
return {
label: variant === 'dn' ? 'downloads' : 'conda|downloads',
message: metric(downloads),
color: downloadCount(downloads),
}
const labelOverride = variant === 'dn' ? 'downloads' : 'conda|downloads'
return renderDownloadsBadge({ downloads, labelOverride })
}

async handle({ variant, channel, pkg }) {
Expand Down
26 changes: 9 additions & 17 deletions services/crates/crates-downloads.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { downloadCount as downloadCountColor } from '../color-formatters.js'
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { InvalidParameter, NotFound } from '../index.js'
import { BaseCratesService, keywords } from './crates-base.js'

Expand Down Expand Up @@ -54,23 +53,16 @@ export default class CratesDownloads extends BaseCratesService {
},
]

static _getLabel(version, variant) {
switch (variant) {
case 'dv':
return version ? `downloads@${version}` : 'downloads@latest'
case 'dr':
return 'recent downloads'
default:
return version ? `downloads@${version}` : 'downloads'
}
}

static render({ variant, downloads, version }) {
return {
label: this._getLabel(version, variant),
message: metric(downloads),
color: downloadCountColor(downloads),
let labelOverride
if (variant === 'dr') {
labelOverride = 'recent downloads'
} else if (variant === 'dv' && !version) {
version = 'latest'
} else if (!version) {
labelOverride = 'downloads'
}
return renderDownloadsBadge({ downloads, labelOverride, version })
}

transform({ variant, json }) {
Expand Down
9 changes: 3 additions & 6 deletions services/docker/docker-pulls.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { metric } from '../text-formatters.js'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService } from '../index.js'
import {
Expand Down Expand Up @@ -28,11 +28,8 @@ export default class DockerPulls extends BaseJsonService {

static defaultBadgeData = { label: 'docker pulls' }

static render({ count }) {
return {
message: metric(count),
color: dockerBlue,
}
static render({ count: downloads }) {
return renderDownloadsBadge({ downloads, colorOverride: dockerBlue })
}

async fetch({ user, repo }) {
Expand Down
52 changes: 52 additions & 0 deletions services/downloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { downloadCount } from './color-formatters.js'
import { metric } from './text-formatters.js'

/**
* Handles rendering concerns of badges that display
* download counts, with override/customization support
*
* @param {object} attrs Refer to individual attrs
* @param {number} attrs.downloads Number of downloads
* @param {string} [attrs.interval] Period or interval the downloads occurred
* (e.g. day, week, month). If provided then this Will be reflected
* in the badge message unless overridden by other message-related parameters
* @param {string} [attrs.version] Version or tag that was downloaded
* which will be reflected in the badge label (unless the label is overridden)
* @param {string} [attrs.labelOverride] If provided then the badge label is set to this
* value overriding any other label-related parameters
* @param {string} [attrs.colorOverride] If provided then the badge color is set to this
* 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
* @returns {object} Badge
*/
function renderDownloadsBadge({
downloads,
interval,
version,
labelOverride,
colorOverride,
messageSuffixOverride,
}) {
Comment on lines +23 to +30
Copy link
Member

Choose a reason for hiding this comment

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

I think this helper is a good example of a function that would benefit from a docstring comment explaining what each of these 6 params does, which ones are required/optional, etc as it is a bit more complex than something like renderLicenseBadge or renderVersionBadge.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point, and there's not actually any input validation in here currently. Do you think it'd be worthwhile to add some? E.g. ensure downloads is provided and numerical (or at least numerically coercible)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah its a good question that I don't have a completely solid answer to.

Just looking over renderVersionBadge, renderLicenseBadge, renderBuildStatusBadge they're a bit of a mixed bag.
I reckon its mostly fine to delegate responsibility for passing sensible values to renderDownloadsBadge to the service layer. My instinct is that at this layer of the application we probably don't want to be throwing a lot of exceptions that we need to remember to catch in the service layer but we probably do have some formatting helpers that do throw under some circumstances. Maybe we should be aiming to guard against outputting stuff like message: '12k/undefined' though.

Dunno how much help that is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Documented in 3eda04b. I couldn't quite convince myself to add any input validation and exception throwing so have opted to leave that possibility for a future day should the need arise

let messageSuffix = ''
if (messageSuffixOverride) {
messageSuffix = ` ${messageSuffixOverride}`
} else if (interval) {
messageSuffix = `/${interval}`
}

let label
if (labelOverride) {
label = labelOverride
} else if (version) {
label = `downloads@${version}`
}

return {
label,
color: colorOverride || downloadCount(downloads),
message: `${metric(downloads)}${messageSuffix}`,
}
}

export { renderDownloadsBadge }
43 changes: 43 additions & 0 deletions services/downloads.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { test, given } from 'sazerac'
import { renderDownloadsBadge } from './downloads.js'
import { downloadCount } from './color-formatters.js'
import { metric } from './text-formatters.js'

const downloads = 2345
const message = metric(downloads)
const color = downloadCount(downloads)

describe('downloads', function () {
test(renderDownloadsBadge, () => {
given({ downloads }).expect({ label: undefined, color, message })
given({ downloads, labelOverride: 'recent downloads' }).expect({
label: 'recent downloads',
color,
message,
})
given({ downloads, version: 'v1.0.0' }).expect({
label: '[email protected]',
color,
message,
})
given({
downloads,
messageSuffixOverride: '[foo.tar.gz]',
interval: 'week',
}).expect({
label: undefined,
color,
message: `${message} [foo.tar.gz]`,
})
given({ downloads, interval: 'year' }).expect({
label: undefined,
color,
message: `${message}/year`,
})
given({ downloads, colorOverride: 'pink' }).expect({
label: undefined,
color: 'pink',
message,
})
})
})
Loading