diff --git a/services/color-formatters.js b/services/color-formatters.js index 0976343be8c9f2db99ddb5b7436303ed441fe8af..f43b8a3f932db92c13d892a9a0fa14bb6d31d5d9 100644 --- a/services/color-formatters.js +++ b/services/color-formatters.js @@ -5,8 +5,8 @@ * @module */ -import dayjs from 'dayjs' import pep440 from '@renovatebot/pep440' +import dayjs from 'dayjs' /** * Determines the color used for a badge based on version. @@ -175,24 +175,26 @@ function colorScale(steps, colors, reversed) { /** * Determines the color used for a badge according to the age. * Age is calculated as days elapsed till current date. - * The color varies from bright green to red as the age increases. + * The color varies from bright green to red as the age increases + * or the other way around if `reverse` is given `true`. * * @param {string} date Date string + * @param {boolean} reversed Reverse the color scale a.k.a. the older, the better * @returns {string} Badge color */ -function age(date) { - const colorByAge = colorScale([7, 30, 180, 365, 730], undefined, true) +function age(date, reversed = false) { + const colorByAge = colorScale([7, 30, 180, 365, 730], undefined, !reversed) const daysElapsed = dayjs().diff(dayjs(date), 'days') return colorByAge(daysElapsed) } export { - version, - pep440VersionColor, - downloadCount, + age, + colorScale, coveragePercentage, + downloadCount, floorCount, letterScore, - colorScale, - age, + pep440VersionColor, + version, } diff --git a/services/color-formatters.spec.js b/services/color-formatters.spec.js index c661acc86447fe962bb2597792162817514d749d..39348a4288a79bc1a7426c69f3da5f5e79f75344 100644 --- a/services/color-formatters.spec.js +++ b/services/color-formatters.spec.js @@ -1,12 +1,12 @@ -import { test, given, forCases } from 'sazerac' import { expect } from 'chai' +import { forCases, given, test } from 'sazerac' import { - coveragePercentage, + age, colorScale, + coveragePercentage, letterScore, - age, - version, pep440VersionColor, + version, } from './color-formatters.js' describe('Color formatters', function () { @@ -75,6 +75,22 @@ describe('Color formatters', function () { given(monthsAgo(15)) .describe('when given a Date 15 months ago') .expect('orange') + // --- reversed --- // + given(Date.now(), true) + .describe('when given the current timestamp and reversed') + .expect('red') + given(new Date(), true) + .describe('when given the current Date and reversed') + .expect('red') + given(new Date(2001, 1, 1), true) + .describe('when given a Date many years ago and reversed') + .expect('brightgreen') + given(monthsAgo(2), true) + .describe('when given a Date two months ago and reversed') + .expect('yellow') + given(monthsAgo(15), true) + .describe('when given a Date 15 months ago and reversed') + .expect('green') }) test(version, () => { diff --git a/services/github/github-created-at.service.js b/services/github/github-created-at.service.js new file mode 100644 index 0000000000000000000000000000000000000000..c6f3f6c0b9abb18a60fb7ae68d5bc9e347f1679b --- /dev/null +++ b/services/github/github-created-at.service.js @@ -0,0 +1,54 @@ +import dayjs from 'dayjs' +import Joi from 'joi' +import { age } from '../color-formatters.js' +import { pathParams } from '../index.js' +import { formatDate } from '../text-formatters.js' +import { GithubAuthV3Service } from './github-auth-service.js' +import { documentation, httpErrorsFor } from './github-helpers.js' + +const schema = Joi.object({ + created_at: Joi.date().required(), +}).required() + +export default class GithubCreatedAt extends GithubAuthV3Service { + static category = 'activity' + static route = { base: 'github/created-at', pattern: ':user/:repo' } + static openApi = { + '/github/created-at/{user}/{repo}': { + get: { + summary: 'Github Created At', + description: documentation, + parameters: pathParams( + { + name: 'user', + example: 'mashape', + }, + { + name: 'repo', + example: 'apistatus', + }, + ), + }, + }, + } + + static defaultBadgeData = { label: 'created at' } + + static render({ createdAt }) { + const date = dayjs(createdAt) + return { + message: formatDate(date), + color: age(date, true), + } + } + + async handle({ user, repo }) { + const { created_at: createdAt } = await this._requestJson({ + schema, + url: `/repos/${user}/${repo}`, + httpErrors: httpErrorsFor('repo not found'), + }) + + return this.constructor.render({ createdAt }) + } +} diff --git a/services/github/github-created-at.tester.js b/services/github/github-created-at.tester.js new file mode 100644 index 0000000000000000000000000000000000000000..0f473468202fcc4e57ad593ea60dfe4d41f3dba5 --- /dev/null +++ b/services/github/github-created-at.tester.js @@ -0,0 +1,14 @@ +import { isFormattedDate } from '../test-validators.js' +import { createServiceTester } from '../tester.js' + +export const t = await createServiceTester() + +t.create('created at').get('/erayerdin/firereact.json').expectBadge({ + label: 'created at', + message: isFormattedDate, +}) + +t.create('created at').get('/erayerdin/not-a-valid-repo.json').expectBadge({ + label: 'created at', + message: 'repo not found', +})