Skip to content

Commit

Permalink
Add a render helper for downloads badges, run [amo ansible apm chrome…
Browse files Browse the repository at this point in the history
…webstore clojars conda crates docker dub eclipse gem githubdownloads] (#7163)

* refactor: add render helper for downloads badges

* refactor: use new helper in some download badge classes

* doc renderer function

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
calebcartwright and repo-ranger[bot] authored Oct 26, 2021
1 parent 6df390e commit e4e7b09
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 148 deletions.
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,
}) {
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

0 comments on commit e4e7b09

Please sign in to comment.