Files
shields/services/github/github-discussions.service.js
Adewale OLAMIDE Adesoba 8c77ded896 [GitHubDiscussions] Add the variant badge of github discussion (answered/unanswered) (#11951)
* added both the service and tester for both the answered and unanswered variant of  github discussion

* Remove GitHub discussions services and their corresponding tests for answered, total, and unanswered discussions and consolidate them as one service with different path

* Refactor GitHub Discussions service to support 'all' variant and update route patterns. Adjust tests to reflect new endpoint structure.

* Remove deprecated OpenAPI route for GitHub Discussions and retain only the 'all' variant endpoint.
2026-07-11 06:58:22 +00:00

101 lines
2.8 KiB
JavaScript

import gql from 'graphql-tag'
import Joi from 'joi'
import { pathParam } from '../index.js'
import { nonNegativeInteger } from '../validators.js'
import { GithubAuthV4Service } from './github-auth-service.js'
import { transformErrors } from './github-helpers.js'
const schema = Joi.object({
data: Joi.object({
repository: Joi.object({
discussions: Joi.object({
totalCount: nonNegativeInteger,
}).required(),
}).required(),
}).required(),
}).required()
const variantConfig = {
all: { color: 'blue', suffix: 'total', answered: null },
answered: { color: 'brightgreen', suffix: 'answered', answered: true },
unanswered: { color: 'orange', suffix: 'unanswered', answered: false },
}
export default class GithubDiscussions extends GithubAuthV4Service {
static category = 'other'
static route = {
base: 'github/discussions',
pattern: ':variant(all|answered|unanswered)?/:user/:repo',
}
static openApi = {
'/github/discussions/all/{user}/{repo}': {
get: {
summary: 'GitHub Discussions (all)',
parameters: [
pathParam({ name: 'user', example: 'vercel' }),
pathParam({ name: 'repo', example: 'next.js' }),
],
},
},
'/github/discussions/answered/{user}/{repo}': {
get: {
summary: 'GitHub Answered Discussions',
parameters: [
pathParam({ name: 'user', example: 'vercel' }),
pathParam({ name: 'repo', example: 'next.js' }),
],
},
},
'/github/discussions/unanswered/{user}/{repo}': {
get: {
summary: 'GitHub Unanswered Discussions',
parameters: [
pathParam({ name: 'user', example: 'vercel' }),
pathParam({ name: 'repo', example: 'next.js' }),
],
},
},
}
static defaultBadgeData = { label: 'discussions', color: 'blue' }
static render({ discussions, color }) {
return { message: discussions, color }
}
async fetch({ user, repo, answered }) {
return this._requestGraphql({
query: gql`
query ($user: String!, $repo: String!, $answered: Boolean) {
repository(name: $repo, owner: $user) {
discussions(answered: $answered) {
totalCount
}
}
}
`,
variables: { user, repo, answered },
schema,
options: { headers: { 'GraphQL-Features': 'discussions_api' } },
transformErrors,
})
}
async handle({ user, repo, variant }) {
const { answered, color, suffix } = variantConfig[variant || 'all']
const json = await this.fetch({ user, repo, answered })
const {
data: {
repository: {
discussions: { totalCount },
},
},
} = json
return this.constructor.render({
discussions: `${totalCount} ${suffix}`,
color,
})
}
}