From a00579ad6e66cbf54bae0627074531d8f461dde4 Mon Sep 17 00:00:00 2001 From: Nigel George Date: Wed, 31 Jan 2024 21:54:18 +0900 Subject: [PATCH] fix(gitea): update based on feedback --- services/gitea/gitea-forks.service.js | 2 +- services/gitea/gitea-forks.tester.js | 5 +- services/gitea/gitea-helper.js | 23 ++++++- services/gitea/gitea-issues.service.js | 63 ++++++------------- services/gitea/gitea-issues.tester.js | 16 ++--- services/gitea/gitea-pull-requests.service.js | 62 ++++++------------ services/gitea/gitea-pull-requests.tester.js | 20 +++--- services/gitea/gitea-stars.service.js | 2 +- 8 files changed, 75 insertions(+), 118 deletions(-) diff --git a/services/gitea/gitea-forks.service.js b/services/gitea/gitea-forks.service.js index 92afee43eb619..ac830c85383dc 100644 --- a/services/gitea/gitea-forks.service.js +++ b/services/gitea/gitea-forks.service.js @@ -61,7 +61,7 @@ export default class GiteaForks extends GiteaBase { return super.fetch({ schema, url: `${baseUrl}/api/v1/repos/${user}/${repo}`, - httpErrors: httpErrorsFor('user or repo not found'), + httpErrors: httpErrorsFor(), }) } diff --git a/services/gitea/gitea-forks.tester.js b/services/gitea/gitea-forks.tester.js index a36a0b9f35116..faf0684723f56 100644 --- a/services/gitea/gitea-forks.tester.js +++ b/services/gitea/gitea-forks.tester.js @@ -1,5 +1,4 @@ -import Joi from 'joi' -import { isMetric } from '../test-validators.js' +import { isMetric, isMetricAllowNegative } from '../test-validators.js' import { createServiceTester } from '../tester.js' export const t = await createServiceTester() @@ -17,7 +16,7 @@ t.create('Forks (self-managed)') .get('/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org') .expectBadge({ label: 'forks', - message: Joi.number().integer(), + message: isMetricAllowNegative, color: 'blue', link: [ 'https://codeberg.org/CanisHelix/shields-badge-test', diff --git a/services/gitea/gitea-helper.js b/services/gitea/gitea-helper.js index 684f41d6bf44f..ab27fc2882f91 100644 --- a/services/gitea/gitea-helper.js +++ b/services/gitea/gitea-helper.js @@ -1,3 +1,5 @@ +import { metric } from '../text-formatters.js' + const description = ` By default this badge looks for repositories on [gitea.com](https://gitea.com). To specify another instance like [codeberg](https://codeberg.org/), [forgejo](https://forgejo.org/) or a self-hosted instance, use the \`gitea_url\` query param. @@ -10,4 +12,23 @@ function httpErrorsFor() { } } -export { description, httpErrorsFor } +function renderIssue({ variant, raw, labels, defaultBadgeData, count }) { + const state = variant + const isMultiLabel = labels && labels.includes(',') + const labelText = labels ? `${isMultiLabel ? `${labels}` : labels} ` : '' + + let labelPrefix = '' + let messageSuffix = '' + if (raw) { + labelPrefix = `${state} ` + } else { + messageSuffix = state + } + return { + label: `${labelPrefix}${labelText}${defaultBadgeData.label}`, + message: `${metric(count)}${messageSuffix ? ' ' : ''}${messageSuffix}`, + color: count > 0 ? 'yellow' : 'brightgreen', + } +} + +export { description, httpErrorsFor, renderIssue } diff --git a/services/gitea/gitea-issues.service.js b/services/gitea/gitea-issues.service.js index a1191b90e5a6e..38d037384d5eb 100644 --- a/services/gitea/gitea-issues.service.js +++ b/services/gitea/gitea-issues.service.js @@ -1,8 +1,7 @@ import Joi from 'joi' import { pathParam, queryParam } from '../index.js' import { optionalUrl, nonNegativeInteger } from '../validators.js' -import { metric } from '../text-formatters.js' -import { description, httpErrorsFor } from './gitea-helper.js' +import { description, httpErrorsFor, renderIssue } from './gitea-helper.js' import GiteaBase from './gitea-base.js' const schema = Joi.object({ 'x-total-count': nonNegativeInteger }).required() @@ -58,64 +57,40 @@ export default class GiteaIssues extends GiteaBase { static defaultBadgeData = { label: 'issues', color: 'informational' } - static render({ variant, raw, labels, issueCount }) { - const state = variant - const isMultiLabel = labels && labels.includes(',') - const labelText = labels ? `${isMultiLabel ? `${labels}` : labels} ` : '' - - let labelPrefix = '' - let messageSuffix = '' - if (raw) { - labelPrefix = `${state} ` - } else { - messageSuffix = state - } - return { - label: `${labelPrefix}${labelText}issues`, - message: `${metric(issueCount)}${ - messageSuffix ? ' ' : '' - }${messageSuffix}`, - color: issueCount > 0 ? 'yellow' : 'brightgreen', - } - } - async handle( { variant, user, repo }, { gitea_url: baseUrl = 'https://gitea.com', labels }, ) { + const options = { + searchParams: { + page: '1', + limit: '1', + type: 'issues', + state: variant.replace('-raw', ''), + }, + } + if (labels) { + options.searchParams.labels = labels + } + const { res } = await this._request( this.authHelper.withBearerAuthHeader({ url: `${baseUrl}/api/v1/repos/${user}/${repo}/issues`, - options: labels - ? { - searchParams: { - page: '1', - limit: '1', - type: 'issues', - state: variant.replace('-raw', ''), - labels, - }, - } - : { - searchParams: { - page: '1', - limit: '1', - type: 'issues', - state: variant.replace('-raw', ''), - }, - }, + options, httpErrors: httpErrorsFor(), }), ) const data = this.constructor._validate(res.headers, schema) // The total number of issues is in the `x-total-count` field in the headers. + // Pull requests are an issue of type pulls // https://gitea.com/api/swagger#/issue - const issueCount = data['x-total-count'] - return this.constructor.render({ + const count = data['x-total-count'] + return renderIssue({ variant: variant.split('-')[0], raw: variant.endsWith('-raw'), labels, - issueCount, + defaultBadgeData: this.constructor.defaultBadgeData, + count, }) } } diff --git a/services/gitea/gitea-issues.tester.js b/services/gitea/gitea-issues.tester.js index 324fc39ba8d7f..cfbf71133548e 100644 --- a/services/gitea/gitea-issues.tester.js +++ b/services/gitea/gitea-issues.tester.js @@ -1,9 +1,9 @@ -import Joi from 'joi' import { createServiceTester } from '../tester.js' import { isMetric, isMetricOpenIssues, isMetricClosedIssues, + isMetricWithPattern, } from '../test-validators.js' export const t = await createServiceTester() @@ -102,7 +102,7 @@ t.create('Closed issues by label is > zero') message: isMetricClosedIssues, }) -t.create('Closed issues by multi-word label is > zero') +t.create('Closed issues by multi-word label is > zero') .get( '/closed/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org&labels=bug,good%20first%20issue', ) @@ -127,9 +127,7 @@ t.create('All issues') .get('/all/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org') .expectBadge({ label: 'issues', - message: Joi.string().regex( - /^([0-9]+[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) all$/, - ), + message: isMetricWithPattern(/ all/), }) t.create('All issues raw') @@ -147,9 +145,7 @@ t.create('All issues by label is > zero') ) .expectBadge({ label: 'question issues', - message: Joi.string().regex( - /^([0-9]+[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) all$/, - ), + message: isMetricWithPattern(/ all/), }) t.create('All issues by multi-word label is > zero') @@ -158,9 +154,7 @@ t.create('All issues by multi-word label is > zero') ) .expectBadge({ label: 'question,enhancement issues', - message: Joi.string().regex( - /^([0-9]+[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) all$/, - ), + message: isMetricWithPattern(/ all/), }) t.create('All issues by label (raw)') diff --git a/services/gitea/gitea-pull-requests.service.js b/services/gitea/gitea-pull-requests.service.js index 30d521d0aad57..e2c213c249093 100644 --- a/services/gitea/gitea-pull-requests.service.js +++ b/services/gitea/gitea-pull-requests.service.js @@ -1,8 +1,7 @@ import Joi from 'joi' import { pathParam, queryParam } from '../index.js' import { optionalUrl, nonNegativeInteger } from '../validators.js' -import { metric } from '../text-formatters.js' -import { description, httpErrorsFor } from './gitea-helper.js' +import { description, httpErrorsFor, renderIssue } from './gitea-helper.js' import GiteaBase from './gitea-base.js' const schema = Joi.object({ 'x-total-count': nonNegativeInteger }).required() @@ -58,52 +57,26 @@ export default class GiteaPullRequests extends GiteaBase { static defaultBadgeData = { label: 'pull requests', color: 'informational' } - static render({ variant, raw, labels, pullsCount }) { - const state = variant - const isMultiLabel = labels && labels.includes(',') - const labelText = labels ? `${isMultiLabel ? `${labels}` : labels} ` : '' - - let labelPrefix = '' - let messageSuffix = '' - if (raw) { - labelPrefix = `${state} ` - } else { - messageSuffix = state - } - return { - label: `${labelPrefix}${labelText}pull requests`, - message: `${metric(pullsCount)}${ - messageSuffix ? ' ' : '' - }${messageSuffix}`, - color: pullsCount > 0 ? 'yellow' : 'brightgreen', - } - } - async handle( { variant, user, repo }, { gitea_url: baseUrl = 'https://gitea.com', labels }, ) { + const options = { + searchParams: { + page: '1', + limit: '1', + type: 'pulls', + state: variant.replace('-raw', ''), + }, + } + if (labels) { + options.searchParams.labels = labels + } + const { res } = await this._request( this.authHelper.withBearerAuthHeader({ url: `${baseUrl}/api/v1/repos/${user}/${repo}/issues`, - options: labels - ? { - searchParams: { - page: '1', - limit: '1', - type: 'pulls', - state: variant.replace('-raw', ''), - labels, - }, - } - : { - searchParams: { - page: '1', - limit: '1', - type: 'pulls', - state: variant.replace('-raw', ''), - }, - }, + options, httpErrors: httpErrorsFor(), }), ) @@ -111,12 +84,13 @@ export default class GiteaPullRequests extends GiteaBase { // The total number of issues is in the `x-total-count` field in the headers. // Pull requests are an issue of type pulls // https://gitea.com/api/swagger#/issue - const pullsCount = data['x-total-count'] - return this.constructor.render({ + const count = data['x-total-count'] + return renderIssue({ variant: variant.split('-')[0], raw: variant.endsWith('-raw'), labels, - pullsCount, + defaultBadgeData: this.constructor.defaultBadgeData, + count, }) } } diff --git a/services/gitea/gitea-pull-requests.tester.js b/services/gitea/gitea-pull-requests.tester.js index 145657be8016a..a2849fbc7e9aa 100644 --- a/services/gitea/gitea-pull-requests.tester.js +++ b/services/gitea/gitea-pull-requests.tester.js @@ -1,9 +1,9 @@ -import Joi from 'joi' import { createServiceTester } from '../tester.js' import { isMetric, isMetricOpenIssues, isMetricClosedIssues, + isMetricWithPattern, } from '../test-validators.js' export const t = await createServiceTester() @@ -102,7 +102,7 @@ t.create('Closed pulls by label is > zero') message: isMetricClosedIssues, }) -t.create('Closed pulls by multi-word label is > zero') +t.create('Closed pulls by multi-word label is > zero') .get( '/closed/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org&labels=bug,good%20first%20issue', ) @@ -127,9 +127,7 @@ t.create('All pulls') .get('/all/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org') .expectBadge({ label: 'pull requests', - message: Joi.string().regex( - /^([0-9]+[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) all$/, - ), + message: isMetricWithPattern(/ all/), }) t.create('All pulls raw') @@ -147,20 +145,16 @@ t.create('All pulls by label is > zero') ) .expectBadge({ label: 'upstream pull requests', - message: Joi.string().regex( - /^([0-9]+[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) all$/, - ), + message: isMetricWithPattern(/ all/), }) t.create('All pulls by multi-word label is > zero') .get( - '/all/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org&labels=question,enhancement', + '/all/CanisHelix/shields-badge-test.json?gitea_url=https://codeberg.org&labels=upstream,enhancement', ) .expectBadge({ - label: 'question,enhancement pull requests', - message: Joi.string().regex( - /^([0-9]+[kMGTPEZY]?|[1-9]\.[1-9][kMGTPEZY]) all$/, - ), + label: 'upstream,enhancement pull requests', + message: isMetricWithPattern(/ all/), }) t.create('All pulls by label (raw)') diff --git a/services/gitea/gitea-stars.service.js b/services/gitea/gitea-stars.service.js index 41f28589d5c73..43732b4068e59 100644 --- a/services/gitea/gitea-stars.service.js +++ b/services/gitea/gitea-stars.service.js @@ -61,7 +61,7 @@ export default class GiteaStars extends GiteaBase { return super.fetch({ schema, url: `${baseUrl}/api/v1/repos/${user}/${repo}`, - httpErrors: httpErrorsFor('user or repo not found'), + httpErrors: httpErrorsFor(), }) }