mirror of
https://github.com/badges/shields.git
synced 2026-11-21 16:06:35 +03:00
* refactor(github): separate pull request count service Count all pull request badge variants through GitHub search so draft qualifiers can be composed consistently. Keep issue badges on the repository issues connection and document each preserved PR route explicitly. Co-authored-by: Philippe Martin <phmartin@redhat.com> * test(github): move pull request badge coverage Keep the existing live picture checks with the service that now owns the pull request routes. * test(github): cover pull request search variants Assert the exact unfiltered GitHub search qualifier and preserve open, closed, raw, and non-raw badge rendering. * test(github): cover draft pull request filters Verify excludeDrafts, onlyDrafts, label composition, and closed raw rendering against exact GitHub search qualifiers. * test(github): cover pull request request errors Reject incompatible draft filters before an outbound request and keep missing repositories distinct from valid zero-count results. * test(github): guard GraphQL response boundaries Cover malformed pull request search data and prove issue badges remain on the repository issues query after the service split. * fix(github): preserve unfiltered pull request counts * test(github): use live pull request fixtures --------- Co-authored-by: Philippe Martin <phmartin@redhat.com>
143 lines
3.6 KiB
JavaScript
143 lines
3.6 KiB
JavaScript
import gql from 'graphql-tag'
|
|
import Joi from 'joi'
|
|
import { pathParams } from '../index.js'
|
|
import { metric } from '../text-formatters.js'
|
|
import { nonNegativeInteger } from '../validators.js'
|
|
import { GithubAuthV4Service } from './github-auth-service.js'
|
|
import { documentation, transformErrors } from './github-helpers.js'
|
|
|
|
const issueCountSchema = Joi.object({
|
|
data: Joi.object({
|
|
repository: Joi.object({
|
|
issues: Joi.object({
|
|
totalCount: nonNegativeInteger,
|
|
}).required(),
|
|
}).required(),
|
|
}).required(),
|
|
}).required()
|
|
|
|
const isClosedVariant = {
|
|
'issues-closed': true,
|
|
'issues-closed-raw': true,
|
|
}
|
|
|
|
export default class GithubIssues extends GithubAuthV4Service {
|
|
static category = 'issue-tracking'
|
|
static route = {
|
|
base: 'github',
|
|
pattern:
|
|
':variant(issues|issues-raw|issues-closed|issues-closed-raw)/:user/:repo/:label*',
|
|
}
|
|
|
|
static openApi = {
|
|
'/github/{variant}/{user}/{repo}': {
|
|
get: {
|
|
summary: 'GitHub Issues',
|
|
description: documentation,
|
|
parameters: pathParams(
|
|
{
|
|
name: 'variant',
|
|
example: 'issues',
|
|
schema: { type: 'string', enum: this.getEnum('variant') },
|
|
},
|
|
{ name: 'user', example: 'badges' },
|
|
{ name: 'repo', example: 'shields' },
|
|
),
|
|
},
|
|
},
|
|
'/github/{variant}/{user}/{repo}/{label}': {
|
|
get: {
|
|
summary: 'GitHub Issues by label',
|
|
description: documentation,
|
|
parameters: pathParams(
|
|
{
|
|
name: 'variant',
|
|
example: 'issues',
|
|
schema: { type: 'string', enum: this.getEnum('variant') },
|
|
},
|
|
{ name: 'user', example: 'badges' },
|
|
{ name: 'repo', example: 'shields' },
|
|
{ name: 'label', example: 'service-badge' },
|
|
),
|
|
},
|
|
},
|
|
}
|
|
|
|
static defaultBadgeData = { label: 'issues', color: 'informational' }
|
|
|
|
static render({ isClosed, issueCount, raw, label }) {
|
|
const state = isClosed ? 'closed' : 'open'
|
|
|
|
let labelPrefix = ''
|
|
let messageSuffix = ''
|
|
if (raw) {
|
|
labelPrefix = `${state} `
|
|
} else {
|
|
messageSuffix = state
|
|
}
|
|
|
|
const isGhLabelMultiWord = label && label.includes(' ')
|
|
const labelText = label
|
|
? `${isGhLabelMultiWord ? `"${label}"` : label} `
|
|
: ''
|
|
return {
|
|
label: `${labelPrefix}${labelText}issues`,
|
|
message: `${metric(issueCount)}${
|
|
messageSuffix ? ' ' : ''
|
|
}${messageSuffix}`,
|
|
color: issueCount > 0 ? 'yellow' : 'brightgreen',
|
|
}
|
|
}
|
|
|
|
async fetch({ isClosed, user, repo, label }) {
|
|
const {
|
|
data: {
|
|
repository: {
|
|
issues: { totalCount },
|
|
},
|
|
},
|
|
} = await this._requestGraphql({
|
|
query: gql`
|
|
query (
|
|
$user: String!
|
|
$repo: String!
|
|
$states: [IssueState!]
|
|
$labels: [String!]
|
|
) {
|
|
repository(owner: $user, name: $repo) {
|
|
issues(states: $states, labels: $labels) {
|
|
totalCount
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
variables: {
|
|
user,
|
|
repo,
|
|
labels: label ? [label] : undefined,
|
|
states: isClosed ? ['CLOSED'] : ['OPEN'],
|
|
},
|
|
schema: issueCountSchema,
|
|
transformErrors,
|
|
})
|
|
return { issueCount: totalCount }
|
|
}
|
|
|
|
async handle({ variant, user, repo, label }) {
|
|
const raw = variant.endsWith('-raw')
|
|
const isClosed = isClosedVariant[variant]
|
|
const { issueCount } = await this.fetch({
|
|
isClosed,
|
|
user,
|
|
repo,
|
|
label,
|
|
})
|
|
return this.constructor.render({
|
|
isClosed,
|
|
issueCount,
|
|
raw,
|
|
label,
|
|
})
|
|
}
|
|
}
|