Skip to content

Commit

Permalink
fix(gitea): update based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CanisHelix committed Jan 31, 2024
1 parent a4655d2 commit a00579a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 118 deletions.
2 changes: 1 addition & 1 deletion services/gitea/gitea-forks.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}

Expand Down
5 changes: 2 additions & 3 deletions services/gitea/gitea-forks.tester.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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',
Expand Down
23 changes: 22 additions & 1 deletion services/gitea/gitea-helper.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
63 changes: 19 additions & 44 deletions services/gitea/gitea-issues.service.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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,
})
}
}
16 changes: 5 additions & 11 deletions services/gitea/gitea-issues.tester.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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',
)
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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)')
Expand Down
62 changes: 18 additions & 44 deletions services/gitea/gitea-pull-requests.service.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -58,65 +57,40 @@ 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(),
}),
)
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 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,
})
}
}
20 changes: 7 additions & 13 deletions services/gitea/gitea-pull-requests.tester.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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',
)
Expand All @@ -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')
Expand All @@ -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)')
Expand Down
2 changes: 1 addition & 1 deletion services/gitea/gitea-stars.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}

Expand Down

0 comments on commit a00579a

Please sign in to comment.