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:
@@ -188,6 +188,7 @@ class GithubApiProvider {
|
||||
'User-Agent': userAgent,
|
||||
Accept: 'application/vnd.github.v3+json',
|
||||
Authorization: `token ${tokenString}`,
|
||||
...options.headers,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
74
services/github/github-discussions-total.service.js
Normal file
74
services/github/github-discussions-total.service.js
Normal 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` })
|
||||
}
|
||||
}
|
||||
15
services/github/github-discussions-total.tester.js
Normal file
15
services/github/github-discussions-total.tester.js
Normal 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 })
|
||||
Reference in New Issue
Block a user