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

Fixing incorrect JetBrains Plugin rating values for [JetBrainsRating] #7140

Merged
merged 3 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion services/jetbrains/jetbrains-rating.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Joi from 'joi'
import { starRating } from '../text-formatters.js'
import { colorScale } from '../color-formatters.js'
import { NotFound } from '../index.js'
import JetbrainsBase from './jetbrains-base.js'

const pluginRatingColor = colorScale([2, 3, 4])
Expand All @@ -22,6 +23,8 @@ const intelliJschema = Joi.object({
}).required()

const jetbrainsSchema = Joi.object({
votes: Joi.object().pattern(Joi.string(), Joi.number()).required(),
ChrisCarini marked this conversation as resolved.
Show resolved Hide resolved
meanVotes: Joi.number().min(0).required(),
meanRating: Joi.number().min(0).required(),
}).required()

Expand Down Expand Up @@ -89,7 +92,23 @@ export default class JetbrainsRating extends JetbrainsBase {
)}/rating`,
errorMessages: { 400: 'not found' },
})
rating = jetbrainsPluginData.meanRating

let voteSum = 0
let voteCount = 0
const votes = jetbrainsPluginData.votes
Object.entries(votes).forEach(([rating, votes]) => {
voteSum += parseInt(rating) * votes
voteCount += votes
})
const meanRating = jetbrainsPluginData.meanRating

if (voteCount === 0) {
throw new NotFound({ prettyMessage: 'No Plugin Ratings' })
}

// JetBrains Plugin Rating Formula from:
// https://plugins.jetbrains.com/docs/marketplace/plugins-rating.html
rating = (voteSum + 2 * meanRating) / (voteCount + 2)
}

return this.constructor.render({ rating, format })
Expand Down
33 changes: 30 additions & 3 deletions services/jetbrains/jetbrains-rating.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,29 @@ t.create('rating number (numeric id)')
.intercept(nock =>
nock('https://plugins.jetbrains.com')
.get('/api/plugins/11941/rating')
.reply(200, { meanRating: 4.4848 })
.reply(200, {
votes: {
4: 1,
5: 4,
},
meanVotes: 2,
meanRating: 4.15669,
})
)
.expectBadge({ label: 'rating', message: '4.5/5' })
.expectBadge({ label: 'rating', message: '4.6/5' })

t.create('rating number for "no vote" plugin (numeric id)')
.get('/rating/10998.json')
.intercept(nock =>
nock('https://plugins.jetbrains.com')
.get('/api/plugins/10998/rating')
.reply(200, {
votes: {},
meanVotes: 2,
meanRating: 4.15669,
})
)
.expectBadge({ label: 'rating', message: 'No Plugin Ratings' })

t.create('rating number (string id)')
.get('/rating/com.chriscarini.jetbrains.jetbrains-auto-power-saver.json')
Expand Down Expand Up @@ -91,7 +111,14 @@ t.create('rating stars (numeric id)')
.intercept(nock =>
nock('https://plugins.jetbrains.com')
.get('/api/plugins/11941/rating')
.reply(200, { meanRating: 4.4848 })
.reply(200, {
votes: {
4: 1,
5: 4,
},
meanVotes: 2,
meanRating: 4.15669,
})
)
.expectBadge({ label: 'rating', message: '★★★★½' })

Expand Down