Add GitHub discussions total badge [GithubTotalDiscussions] (#6472)

* use headers from options passed as argument in request method

* add github total discussions badge

* use nonNegativeInteger for total discussion count

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
Prafful Javare
2021-05-11 13:24:58 +05:30
committed by GitHub
parent 9dd2861c5a
commit a094c8a373
3 changed files with 90 additions and 0 deletions

View File

@@ -188,6 +188,7 @@ class GithubApiProvider {
'User-Agent': userAgent,
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${tokenString}`,
...options.headers,
},
},
}

View File

@@ -0,0 +1,74 @@
'use strict'
const { default: gql } = require('graphql-tag')
const Joi = require('joi')
const { nonNegativeInteger } = require('../validators')
const { GithubAuthV4Service } = require('./github-auth-service')
const { transformErrors } = require('./github-helpers')
const schema = Joi.object({
data: Joi.object({
repository: Joi.object({
discussions: Joi.object({
totalCount: nonNegativeInteger,
}).required(),
}).required(),
}).required(),
}).required()
module.exports = class GithubTotalDiscussions extends GithubAuthV4Service {
static category = 'other'
static route = {
base: 'github/discussions',
pattern: ':user/:repo',
}
static examples = [
{
title: 'GitHub Discussions',
namedParams: {
user: 'vercel',
repo: 'next.js',
},
staticPreview: this.render({
discussions: '6000 total',
}),
},
]
static defaultBadgeData = { label: 'discussions', color: 'blue' }
static render({ discussions }) {
return { message: discussions }
}
async fetch({ user, repo }) {
return this._requestGraphql({
query: gql`
query($user: String!, $repo: String!) {
repository(name: $repo, owner: $user) {
discussions {
totalCount
}
}
}
`,
variables: { user, repo },
schema,
options: { headers: { 'GraphQL-Features': 'discussions_api' } },
transformErrors,
})
}
async handle({ user, repo }) {
const json = await this.fetch({ user, repo })
const {
data: {
repository: {
discussions: { totalCount },
},
},
} = json
return this.constructor.render({ discussions: `${totalCount} total` })
}
}

View File

@@ -0,0 +1,15 @@
'use strict'
const { withRegex } = require('../test-validators')
const t = (module.exports = require('../tester').createServiceTester())
t.create('GitHub Total Discussions (repo not found)')
.get('/not-a-user/not-a-repo.json')
.expectBadge({ label: 'discussions', message: 'repo not found' })
// example: 6000 total
const numberSpaceTotal = withRegex(/^\d+ total$/)
t.create('GitHub Total Discussions (repo having discussions)')
.get('/vercel/next.js.json')
.expectBadge({ label: 'discussions', message: numberSpaceTotal })