Files
shields/services/osslifecycle/osslifecycle.service.js
Joe Izzard 9717fc202f [OSSLifecycle OSSLifecycleRedirect] Add file_url param to pull from non-github sources (#10489)
* add file_url param

Add file_url param to allow pulling from non-github sources

* add test for file_url param

Add test using file_url variant of OSS Lifecycle badge using Netflix OSSTracker repo

* remove old pattern from main service

Remove all references and code for the old /{user}/{repo} and /{user}/{repo}/{branch} functionality.
This will be replaced by a redirect service.

Also updated all tests to use the new file_url method

* add osslifecycle redirector

add a redirector for the original osslifecycle pattern

* tweaks to docs and handle function

Tweak the Description to remove reference to GitHub as now works with any repository,
and updated handle function to remove reference to unused variables.
2024-09-05 16:06:15 +00:00

96 lines
2.7 KiB
JavaScript

import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { BaseService, InvalidResponse, queryParam } from '../index.js'
const description = `
OSS Lifecycle is an initiative started by Netflix to classify open-source projects into lifecycles
and clearly identify which projects are active and which ones are retired. To enable this badge,
simply create an OSSMETADATA tagging file at the root of your repository containing a
single line similar to the following: \`osslifecycle=active\`. Other suggested values are
\`osslifecycle=maintenance\` and \`osslifecycle=archived\`. A working example
can be viewed on the [OSS Tracker repository](https://github.com/Netflix/osstracker).
`
const queryParamSchema = Joi.object({
file_url: optionalUrl.required(),
}).required()
export default class OssTracker extends BaseService {
static category = 'other'
static route = {
base: '',
pattern: 'osslifecycle',
queryParamSchema,
}
static openApi = {
'/osslifecycle': {
get: {
summary: 'OSS Lifecycle',
description,
parameters: [
queryParam({
name: 'file_url',
example:
'https://raw.githubusercontent.com/Netflix/aws-autoscaling/master/OSSMETADATA',
required: true,
description: 'URL for the `OSSMETADATA` file',
}),
],
},
},
}
static defaultBadgeData = { label: 'oss lifecycle' }
/**
* Return color for active, maintenance and archived statuses, which were the three
* example keywords used in Netflix's open-source meetup.
* See https://slideshare.net/aspyker/netflix-open-source-meetup-season-4-episode-1
* Other keywords are possible, but will appear in grey.
*
* @param {object} attrs Refer to individual attrs
* @param {string} attrs.status Specifies the current maintenance status
* @returns {string} color
*/
static getColor({ status }) {
if (status === 'active') {
return 'brightgreen'
} else if (status === 'maintenance') {
return 'yellow'
} else if (status === 'archived') {
return 'red'
}
return 'lightgrey'
}
static render({ status }) {
const color = this.getColor({ status })
return {
message: status,
color,
}
}
async fetch({ fileUrl }) {
return this._request({
url: fileUrl,
})
}
async handle(pathParams, { file_url: fileUrl = '' }) {
const { buffer } = await this.fetch({
fileUrl,
})
try {
const status = buffer.match(/osslifecycle=([a-z]+)/im)[1]
return this.constructor.render({ status })
} catch (e) {
throw new InvalidResponse({
prettyMessage: 'metadata in unexpected format',
})
}
}
}