Skip to content

Commit

Permalink
Merge branch 'master' into test-summary
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow committed Dec 16, 2018
2 parents 00b556f + df22adf commit 0d4a6bd
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 306 deletions.
97 changes: 97 additions & 0 deletions doc/deprecating-badges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Deprecating Badges
When a service that Shields integrates with shuts down, those badges will no longer work and need to be deprecated within Shields.

Deprecating a badge involves 3 steps:
1. Adding an entry for the service to the deprecated service list in `deprecated-services.js`
2. Updating the service code to use the `DeprecatedService` class
3. Updating the service tests to reflect the new behavior of the deprecated service

## Update Deprecated Service List
All deprecated services are enumerated in the `deprecated-services.js` [file](https://github.com/badges/shields/blob/master/lib/deprecated-services.js) which can be found in the `lib` directory (`./lib/deprecated-services.js`).

Add a key for the service with the corresponding date for deprecation, for example: `nsp: new Date('2018-12-13')`, to the `deprecatedServices` object.


## Update Service Implementation
Locate the source file(s) for the service, which can be found in `*.service.js` files located within the directory for the service (`./services/:service-name/`) such as `./services/imagelayers/imagelayers.service.js`.

Replace the existing service class implementation with the `DeprecatedService` class from `./services/deprecated-service.js` using the respective `category`, `url`, and `label` values for that service. For example:

```js
'use strict'

const deprecatedService = require('../deprecated-service')

// image layers integration - deprecated as of November 2018.
module.exports = deprecatedService({
category: 'size',
url: {
base: 'imagelayers',
format: '(?:.+)',
},
label: 'imagelayers',
})

```

## Update Service Tests
Locate the test file(s) for the service, which can be found in `*.tester.js` files located in the service directory (`./services/:service-name/`), such as `./services/imagelayers/imagelayers.tester.js`.

With `DeprecatedService` classes we cannot use the utility functions from `create-service-tester.js` so you will need to create the `ServiceTester` class directly. For example:

```js
const ServiceTester = require('../service-tester')

const t = (module.exports = new ServiceTester({
id: 'imagelayers',
title: 'ImageLayers',
}))
```

Next you will need to replace/refactor the existing tests to validate the new deprecated badge behavior for this service. Deprecated badges always return a message of `no longer available` (such as `imagelayers | no longer available`) so the tests need to be updated to reflect that message value. For example:

```js
t.create('no longer available (previously image size)')
.get('/image-size/_/ubuntu/latest.json')
.expectJSON({
name: 'imagelayers',
value: 'no longer available',
})
```

Make sure to have a live (non-mocked) test for each badge the service provides that validates the each badge returns the `no longer available` message.

Here is an example of what the final result would look like for a test file:

```js
'use strict'

const ServiceTester = require('../service-tester')
const t = (module.exports = new ServiceTester({
id: 'imagelayers',
title: 'ImageLayers',
}))

t.create('no longer available (previously image size)')
.get('/image-size/_/ubuntu/latest.json')
.expectJSON({
name: 'imagelayers',
value: 'no longer available',
})

t.create('no longer available (previously number of layers)')
.get('/layers/_/ubuntu/latest.json')
.expectJSON({
name: 'imagelayers',
value: 'no longer available',
})

```

## Additional Information
Some other information that may be useful:

* [Contributing Docs](../CONTRIBUTING.md)
* [Badge Tutorial](./TUTORIAL.md)
* [Service Tests Tutorial](./service-tests.md)
* Previous Pull Requests that deprecated badges like [#2352](https://github.com/badges/shields/pull/2352) and [#2410](https://github.com/badges/shields/pull/2410)
12 changes: 6 additions & 6 deletions lib/badge-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ describe('Badge data helpers', function() {

test(makeLogo, () => {
forCases([
given('gratipay', { logo: 'image/svg+xml;base64,PHN2ZyB4bWxu' }),
given('gratipay', { logo: 'data:image/svg+xml;base64,PHN2ZyB4bWxu' }),
given('gratipay', { logo: 'data:image/svg xml;base64,PHN2ZyB4bWxu' }),
given('gratipay', { logo: 'data:image/svg+xml;base64,PHN2ZyB\n4bWxu' }),
given('npm', { logo: 'image/svg+xml;base64,PHN2ZyB4bWxu' }),
given('npm', { logo: 'data:image/svg+xml;base64,PHN2ZyB4bWxu' }),
given('npm', { logo: 'data:image/svg xml;base64,PHN2ZyB4bWxu' }),
given('npm', { logo: 'data:image/svg+xml;base64,PHN2ZyB\n4bWxu' }),
]).expect('data:image/svg+xml;base64,PHN2ZyB4bWxu')
forCases([given('gratipay', { logo: '' }), given(undefined, {})]).expect(
forCases([given('npm', { logo: '' }), given(undefined, {})]).expect(
undefined
)
given('gratipay', {}).assert(
given('npm', {}).assert(
'should not be empty',
v => expect(v).not.to.be.empty
)
Expand Down
3 changes: 2 additions & 1 deletion lib/deprecated-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const deprecatedServices = {
gittip: new Date('2017-12-29'),
gratipay: new Date('2017-12-29'),
dockbit: new Date('2017-12-31'),
gemnasium: new Date('2018-05-15'),
snap: new Date('2018-01-23'),
'snap-ci': new Date('2018-01-23'),
Expand All @@ -14,7 +15,7 @@ const deprecatedServices = {
issuestats: new Date('2018-09-01'),
libscore: new Date('2018-09-22'),
imagelayers: new Date('2018-11-18'),
dockbit: new Date('2017-12-31'),
nsp: new Date('2018-12-13'),
}

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion logo/bithound.svg

This file was deleted.

3 changes: 0 additions & 3 deletions logo/gratipay.svg

This file was deleted.

116 changes: 44 additions & 72 deletions services/cran/cran.service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
'use strict'

const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLabel: getLabel,
} = require('../../lib/badge-data')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')
const Joi = require('joi')
const BaseJsonService = require('../base-json')
const { renderVersionBadge } = require('../../lib/version')

class CranLicense extends LegacyService {
const schema = Joi.object({
License: Joi.string().required(),
Version: Joi.string().required(),
}).required()

class BaseCranService extends BaseJsonService {
async fetch({ packageName }) {
const url = `http://crandb.r-pkg.org/${packageName}`
return this._requestJson({ schema, url })
}

static get defaultBadgeData() {
return { label: 'cran' }
}
}

class CranLicense extends BaseCranService {
static get category() {
return 'license'
}
Expand All @@ -25,97 +37,57 @@ class CranLicense extends LegacyService {
{
title: 'CRAN/METACRAN',
namedParams: { packageName: 'devtools' },
staticPreview: {
label: 'license',
message: 'GPL (>= 2)',
color: 'blue',
},
staticPreview: this.render({ license: 'GPL (>= 2)' }),
keywords: ['R'],
},
]
}

static registerLegacyRouteHandler() {}
static render({ license }) {
return {
label: 'license',
message: license,
color: 'blue',
}
}

async handle({ packageName }) {
const data = await this.fetch({ packageName })
return this.constructor.render({ license: data['License'] })
}
}

class CranVersion extends LegacyService {
class CranVersion extends BaseCranService {
static get category() {
return 'version'
}

static get route() {
return {
base: 'cran/v',
pattern: ':packageName',
}
}

static get examples() {
return [
{
title: 'CRAN/METACRAN',
previewUrl: 'devtools',
namedParams: { packageName: 'devtools' },
staticPreview: this.render({ version: '2.0.1' }),
keywords: ['R'],
},
]
}

static registerLegacyRouteHandler() {}
}

class Cran extends LegacyService {
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/cran\/([vl])\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((queryParams, match, sendBadge, request) => {
const info = match[1] // either `v` or `l`
const pkg = match[2] // eg, devtools
const format = match[3]
const url = `http://crandb.r-pkg.org/${pkg}`
const badgeData = getBadgeData('cran', queryParams)
request(url, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
if (res.statusCode === 404) {
badgeData.text[1] = 'not found'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
static render({ version }) {
return renderVersionBadge({ version })
}

if (info === 'v') {
const version = data.Version
badgeData.text[1] = versionText(version)
badgeData.colorscheme = versionColor(version)
sendBadge(format, badgeData)
} else if (info === 'l') {
badgeData.text[0] = getLabel('license', queryParams)
const license = data.License
if (license) {
badgeData.text[1] = license
badgeData.colorscheme = 'blue'
} else {
badgeData.text[1] = 'unknown'
}
sendBadge(format, badgeData)
} else {
throw Error('Unreachable due to regex')
}
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
async handle({ packageName }) {
const data = await this.fetch({ packageName })
return this.constructor.render({ version: data['Version'] })
}
}

module.exports = {
CranLicense,
CranVersion,
Cran,
}
module.exports = { CranLicense, CranVersion }
38 changes: 7 additions & 31 deletions services/cran/cran.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const t = (module.exports = new ServiceTester({
title: 'CRAN/METACRAN',
}))

t.create('version')
t.create('version (valid)')
.get('/v/devtools.json')
.expectJSONTypes(
Joi.object().keys({
Expand All @@ -18,38 +18,14 @@ t.create('version')
})
)

t.create('specified license')
t.create('version (not found)')
.get('/v/some-bogus-package.json')
.expectJSON({ name: 'cran', value: 'not found' })

t.create('license (valid)')
.get('/l/devtools.json')
.expectJSON({ name: 'license', value: 'GPL (>= 2)' })

t.create('unknown package')
t.create('license (not found)')
.get('/l/some-bogus-package.json')
.expectJSON({ name: 'cran', value: 'not found' })

t.create('unknown info')
.get('/z/devtools.json')
.expectStatus(404)
.expectJSON({ name: '404', value: 'badge not found' })

t.create('malformed response')
.get('/v/foobar.json')
.intercept(nock =>
nock('http://crandb.r-pkg.org')
.get('/foobar')
.reply(200)
) // JSON without Version.
.expectJSON({ name: 'cran', value: 'invalid' })

t.create('connection error')
.get('/v/foobar.json')
.networkOff()
.expectJSON({ name: 'cran', value: 'inaccessible' })

t.create('unspecified license')
.get('/l/foobar.json')
.intercept(nock =>
nock('http://crandb.r-pkg.org')
.get('/foobar')
.reply(200, {})
) // JSON without License.
.expectJSON({ name: 'license', value: 'unknown' })
Loading

0 comments on commit 0d4a6bd

Please sign in to comment.