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

deprecate [apm] service #8773

Merged
merged 2 commits into from
Jan 7, 2023
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
140 changes: 28 additions & 112 deletions services/apm/apm.service.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,33 @@
import Joi from 'joi'
import { renderLicenseBadge } from '../licenses.js'
import { renderVersionBadge } from '../version.js'
import { renderDownloadsBadge } from '../downloads.js'
import { nonNegativeInteger } from '../validators.js'
import { BaseJsonService, InvalidResponse } from '../index.js'

const keywords = ['atom']

const schema = Joi.object({
downloads: nonNegativeInteger,
releases: Joi.object({
latest: Joi.string().required(),
}),
metadata: Joi.object({
license: Joi.string().required(),
}),
import { deprecatedService } from '../index.js'

const APMDownloads = deprecatedService({
category: 'downloads',
route: {
base: 'apm/dm',
pattern: ':various*',
},
label: 'downloads',
dateAdded: new Date('2023-01-04'),
})

class BaseAPMService extends BaseJsonService {
static defaultBadgeData = { label: 'apm' }

async fetch({ packageName }) {
return this._requestJson({
schema,
url: `https://atom.io/api/packages/${packageName}`,
errorMessages: { 404: 'package not found' },
})
}
}

class APMDownloads extends BaseAPMService {
static category = 'downloads'
static route = { base: 'apm/dm', pattern: ':packageName' }

static examples = [
{
title: 'APM',
namedParams: { packageName: 'vim-mode' },
staticPreview: this.render({ downloads: '60043' }),
keywords,
},
]

static defaultBadgeData = { label: 'downloads' }

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

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

class APMVersion extends BaseAPMService {
static category = 'version'
static route = { base: 'apm/v', pattern: ':packageName' }

static examples = [
{
title: 'APM',
namedParams: { packageName: 'vim-mode' },
staticPreview: this.render({ version: '0.6.0' }),
keywords,
},
]

static render({ version }) {
return renderVersionBadge({ version })
}

async handle({ packageName }) {
const json = await this.fetch({ packageName })

const version = json.releases.latest
if (!version)
throw new InvalidResponse({
underlyingError: new Error('version is invalid'),
})
return this.constructor.render({ version })
}
}

class APMLicense extends BaseAPMService {
static category = 'license'
static route = { base: 'apm/l', pattern: ':packageName' }

static examples = [
{
title: 'APM',
namedParams: { packageName: 'vim-mode' },
staticPreview: this.render({ license: 'MIT' }),
keywords,
},
]

static defaultBadgeData = { label: 'license' }

static render({ license }) {
return renderLicenseBadge({ license })
}

async handle({ packageName }) {
const json = await this.fetch({ packageName })
const APMVersion = deprecatedService({
category: 'version',
route: {
base: 'apm/v',
pattern: ':various*',
},
label: 'apm',
dateAdded: new Date('2023-01-04'),
})

const license = json.metadata.license
if (!license)
throw new InvalidResponse({
underlyingError: new Error('licence is invalid'),
})
return this.constructor.render({ license })
}
}
const APMLicense = deprecatedService({
category: 'license',
route: {
base: 'apm/l',
pattern: ':various*',
},
label: 'license',
dateAdded: new Date('2023-01-04'),
})

export { APMDownloads, APMVersion, APMLicense }
46 changes: 4 additions & 42 deletions services/apm/apm.tester.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
import { ServiceTester } from '../tester.js'
import { invalidJSON } from '../response-fixtures.js'
import { isMetric, isVPlusTripleDottedVersion } from '../test-validators.js'

export const t = new ServiceTester({
id: 'apm',
title: 'Atom Package Manager',
pathPrefix: '/apm',
})

t.create('Downloads')
.get('/dm/vim-mode.json')
.expectBadge({ label: 'downloads', message: isMetric })
.expectBadge({ label: 'downloads', message: 'no longer available' })

t.create('Version')
.get('/v/vim-mode.json')
.expectBadge({ label: 'apm', message: isVPlusTripleDottedVersion })
.expectBadge({ label: 'apm', message: 'no longer available' })

t.create('License')
.get('/l/vim-mode.json')
.expectBadge({ label: 'license', message: 'MIT' })

t.create('Downloads | Package not found')
.get('/dm/notapackage.json')
.expectBadge({ label: 'downloads', message: 'package not found' })

t.create('Version | Package not found')
.get('/v/notapackage.json')
.expectBadge({ label: 'apm', message: 'package not found' })

t.create('License | Package not found')
.get('/l/notapackage.json')
.expectBadge({ label: 'license', message: 'package not found' })

t.create('Invalid version')
.get('/dm/vim-mode.json')
.intercept(nock =>
nock('https://atom.io')
.get('/api/packages/vim-mode')
.reply(200, '{"releases":{}}')
)
.expectBadge({ label: 'downloads', message: 'invalid response data' })

t.create('Invalid License')
.get('/l/vim-mode.json')
.intercept(nock =>
nock('https://atom.io')
.get('/api/packages/vim-mode')
.reply(200, '{"metadata":{}}')
)
.expectBadge({ label: 'license', message: 'invalid response data' })

t.create('Unexpected response')
.get('/dm/vim-mode.json')
.intercept(nock =>
nock('https://atom.io').get('/api/packages/vim-mode').reply(invalidJSON)
)
.expectBadge({ label: 'downloads', message: 'unparseable json response' })
.expectBadge({ label: 'license', message: 'no longer available' })