From 60125707adfd7593dba6216489c312b5cd5330e6 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 21 Jul 2022 21:14:29 -0600 Subject: [PATCH] [greasyfork] Add Greasy Fork rating badges (#8087) * Add greasy fork rating badges * refactor: combine rating classes * refactor: remove Base from class name * Change to a single badge with all values * Add unit tests for GreasyForkRatingCount.render * Average totals in color algorithm * chore(deps): bump moment from 2.29.3 to 2.29.4 (#8170) Bumps [moment](https://github.com/moment/moment) from 2.29.3 to 2.29.4. - [Release notes](https://github.com/moment/moment/releases) - [Changelog](https://github.com/moment/moment/blob/develop/CHANGELOG.md) - [Commits](https://github.com/moment/moment/compare/2.29.3...2.29.4) --- updated-dependencies: - dependency-name: moment dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * chore(deps-dev): bump @typescript-eslint/eslint-plugin (#8183) Bumps [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) from 5.30.0 to 5.30.5. - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v5.30.5/packages/eslint-plugin) --- updated-dependencies: - dependency-name: "@typescript-eslint/eslint-plugin" dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * chore(deps): bump @sentry/node from 7.4.1 to 7.5.1 (#8174) Bumps [@sentry/node](https://github.com/getsentry/sentry-javascript) from 7.4.1 to 7.5.1. - [Release notes](https://github.com/getsentry/sentry-javascript/releases) - [Changelog](https://github.com/getsentry/sentry-javascript/blob/master/CHANGELOG.md) - [Commits](https://github.com/getsentry/sentry-javascript/compare/7.4.1...7.5.1) --- updated-dependencies: - dependency-name: "@sentry/node" dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * chore(deps): bump simple-icons from 7.3.0 to 7.4.0 (#8181) Bumps [simple-icons](https://github.com/simple-icons/simple-icons) from 7.3.0 to 7.4.0. - [Release notes](https://github.com/simple-icons/simple-icons/releases) - [Commits](https://github.com/simple-icons/simple-icons/compare/7.3.0...7.4.0) --- updated-dependencies: - dependency-name: simple-icons dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * chore(deps-dev): bump nodemon from 2.0.18 to 2.0.19 (#8179) Bumps [nodemon](https://github.com/remy/nodemon) from 2.0.18 to 2.0.19. - [Release notes](https://github.com/remy/nodemon/releases) - [Commits](https://github.com/remy/nodemon/compare/v2.0.18...v2.0.19) --- updated-dependencies: - dependency-name: nodemon dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * Add [ROS] version service (#8169) * Add [ROS] version service * review feedback * add spaces * add spaces round pipe in [conda] badge (#8189) Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * refactor(deps): Replace moment with dayjs (#8192) * chore(deps): bump @sentry/node from 7.5.1 to 7.6.0 (#8197) Bumps [@sentry/node](https://github.com/getsentry/sentry-javascript) from 7.5.1 to 7.6.0. - [Release notes](https://github.com/getsentry/sentry-javascript/releases) - [Changelog](https://github.com/getsentry/sentry-javascript/blob/master/CHANGELOG.md) - [Commits](https://github.com/getsentry/sentry-javascript/compare/7.5.1...7.6.0) --- updated-dependencies: - dependency-name: "@sentry/node" dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Fix missing `dayjs` -> `moment` (#8204) Co-authored-by: Caleb Cartwright * chore(deps): bump ioredis from 5.1.0 to 5.2.0 (#8201) Bumps [ioredis](https://github.com/luin/ioredis) from 5.1.0 to 5.2.0. - [Release notes](https://github.com/luin/ioredis/releases) - [Changelog](https://github.com/luin/ioredis/blob/main/CHANGELOG.md) - [Commits](https://github.com/luin/ioredis/compare/v5.1.0...v5.2.0) --- updated-dependencies: - dependency-name: ioredis dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> * chore(deps): bump fast-xml-parser from 4.0.8 to 4.0.9 (#8203) Bumps [fast-xml-parser](https://github.com/NaturalIntelligence/fast-xml-parser) from 4.0.8 to 4.0.9. - [Release notes](https://github.com/NaturalIntelligence/fast-xml-parser/releases) - [Changelog](https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/CHANGELOG.md) - [Commits](https://github.com/NaturalIntelligence/fast-xml-parser/compare/v4.0.8...v4.0.9) --- updated-dependencies: - dependency-name: fast-xml-parser dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * revert rebase * Add test for all good ratings Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> Co-authored-by: Jacob Bandes-Storch Co-authored-by: chris48s Co-authored-by: chase Co-authored-by: Caleb Cartwright --- .../greasyfork/greasyfork-rating.service.js | 40 +++++++++++++++++++ services/greasyfork/greasyfork-rating.spec.js | 31 ++++++++++++++ .../greasyfork/greasyfork-rating.tester.js | 14 +++++++ 3 files changed, 85 insertions(+) create mode 100644 services/greasyfork/greasyfork-rating.service.js create mode 100644 services/greasyfork/greasyfork-rating.spec.js create mode 100644 services/greasyfork/greasyfork-rating.tester.js diff --git a/services/greasyfork/greasyfork-rating.service.js b/services/greasyfork/greasyfork-rating.service.js new file mode 100644 index 0000000000..830d48645d --- /dev/null +++ b/services/greasyfork/greasyfork-rating.service.js @@ -0,0 +1,40 @@ +import { floorCount as floorCountColor } from '../color-formatters.js' +import { metric } from '../text-formatters.js' +import BaseGreasyForkService from './greasyfork-base.js' + +export default class GreasyForkRatingCount extends BaseGreasyForkService { + static category = 'rating' + static route = { base: 'greasyfork', pattern: 'rating-count/:scriptId' } + + static examples = [ + { + title: 'Greasy Fork', + namedParams: { scriptId: '407466' }, + staticPreview: this.render({ good: 17, ok: 2, bad: 3 }), + }, + ] + + static defaultBadgeData = { label: 'rating' } + + static render({ good, ok, bad }) { + let color = 'lightgrey' + const total = good + bad + ok + if (total > 0) { + const score = (good * 3 + ok * 2 + bad * 1) / total - 1 + color = floorCountColor(score, 1, 1.5, 2) + } + return { + message: `${metric(good)} good, ${metric(ok)} ok, ${metric(bad)} bad`, + color, + } + } + + async handle({ scriptId }) { + const data = await this.fetch({ scriptId }) + return this.constructor.render({ + good: data.good_ratings, + ok: data.ok_ratings, + bad: data.bad_ratings, + }) + } +} diff --git a/services/greasyfork/greasyfork-rating.spec.js b/services/greasyfork/greasyfork-rating.spec.js new file mode 100644 index 0000000000..459cb72982 --- /dev/null +++ b/services/greasyfork/greasyfork-rating.spec.js @@ -0,0 +1,31 @@ +import { test, given } from 'sazerac' +import GreasyForkRatingCount from './greasyfork-rating.service.js' + +describe('GreasyForkRatingCount', function () { + test(GreasyForkRatingCount.render, () => { + given({ good: 0, ok: 0, bad: 30 }).expect({ + message: '0 good, 0 ok, 30 bad', + color: 'red', + }) + given({ good: 10, ok: 20, bad: 30 }).expect({ + message: '10 good, 20 ok, 30 bad', + color: 'yellow', + }) + given({ good: 10, ok: 20, bad: 10 }).expect({ + message: '10 good, 20 ok, 10 bad', + color: 'yellowgreen', + }) + given({ good: 20, ok: 10, bad: 0 }).expect({ + message: '20 good, 10 ok, 0 bad', + color: 'green', + }) + given({ good: 30, ok: 0, bad: 0 }).expect({ + message: '30 good, 0 ok, 0 bad', + color: 'brightgreen', + }) + given({ good: 0, ok: 0, bad: 0 }).expect({ + message: '0 good, 0 ok, 0 bad', + color: 'lightgrey', + }) + }) +}) diff --git a/services/greasyfork/greasyfork-rating.tester.js b/services/greasyfork/greasyfork-rating.tester.js new file mode 100644 index 0000000000..1fc2cdedca --- /dev/null +++ b/services/greasyfork/greasyfork-rating.tester.js @@ -0,0 +1,14 @@ +import Joi from 'joi' +import { createServiceTester } from '../tester.js' +export const t = await createServiceTester() + +t.create('Rating Count') + .get('/rating-count/407466.json') + .expectBadge({ + label: 'rating', + message: Joi.string().regex(/^\d+ good, \d+ ok, \d+ bad$/), + }) + +t.create('Rating Count (not found)') + .get('/rating-count/000000.json') + .expectBadge({ label: 'rating', message: 'not found' })