Skip to content

Commit

Permalink
[GITEA] add forks, stars, issues and pr badges (#9923)
Browse files Browse the repository at this point in the history
* feat(gitea): add forks, stars, issues and pr badges

* doc(comment): update comments

* fix(gitea): update based on feedback

* refactor(fetch): refactor fetch to be more generic

* refactor(fetch): remove unused function

* use isMetric

---------

Co-authored-by: chris48s <[email protected]>
  • Loading branch information
CanisHelix and chris48s authored Feb 11, 2024
1 parent 8796460 commit 372052c
Show file tree
Hide file tree
Showing 10 changed files with 780 additions and 1 deletion.
14 changes: 14 additions & 0 deletions services/gitea/gitea-common-fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
async function fetchIssue(
serviceInstance,
{ user, repo, baseUrl, options, httpErrors },
) {
return serviceInstance._request(
serviceInstance.authHelper.withBearerAuthHeader({
url: `${baseUrl}/api/v1/repos/${user}/${repo}/issues`,
options,
httpErrors,
}),
)
}

export { fetchIssue }
76 changes: 76 additions & 0 deletions services/gitea/gitea-forks.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Joi from 'joi'
import { pathParam, queryParam } from '../index.js'
import { optionalUrl, nonNegativeInteger } from '../validators.js'
import { metric } from '../text-formatters.js'
import GiteaBase from './gitea-base.js'
import { description, httpErrorsFor } from './gitea-helper.js'

const schema = Joi.object({
forks_count: nonNegativeInteger,
}).required()

const queryParamSchema = Joi.object({
gitea_url: optionalUrl,
}).required()

export default class GiteaForks extends GiteaBase {
static category = 'social'

static route = {
base: 'gitea/forks',
pattern: ':user/:repo',
queryParamSchema,
}

static openApi = {
'/gitea/forks/{user}/{repo}': {
get: {
summary: 'Gitea Forks',
description,
parameters: [
pathParam({
name: 'user',
example: 'gitea',
}),
pathParam({
name: 'repo',
example: 'tea',
}),
queryParam({
name: 'gitea_url',
example: 'https://gitea.com',
}),
],
},
},
}

static defaultBadgeData = { label: 'forks', namedLogo: 'gitea' }

static render({ baseUrl, user, repo, forkCount }) {
return {
message: metric(forkCount),
style: 'social',
color: 'blue',
link: [`${baseUrl}/${user}/${repo}`, `${baseUrl}/${user}/${repo}/forks`],
}
}

async fetch({ user, repo, baseUrl }) {
// https://gitea.com/api/swagger#/repository
return super.fetch({
schema,
url: `${baseUrl}/api/v1/repos/${user}/${repo}`,
httpErrors: httpErrorsFor(),
})
}

async handle({ user, repo }, { gitea_url: baseUrl = 'https://gitea.com' }) {
const { forks_count: forkCount } = await this.fetch({
user,
repo,
baseUrl,
})
return this.constructor.render({ baseUrl, user, repo, forkCount })
}
}
32 changes: 32 additions & 0 deletions services/gitea/gitea-forks.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('Forks')
.get('/gitea/tea.json')
.expectBadge({
label: 'forks',
message: isMetric,
color: 'blue',
link: ['https://gitea.com/gitea/tea', 'https://gitea.com/gitea/tea/forks'],
})

t.create('Forks (self-managed)')
.get('/Codeberg/forgejo.json?gitea_url=https://codeberg.org')
.expectBadge({
label: 'forks',
message: isMetric,
color: 'blue',
link: [
'https://codeberg.org/Codeberg/forgejo',
'https://codeberg.org/Codeberg/forgejo/forks',
],
})

t.create('Forks (project not found)')
.get('/CanisHelix/does-not-exist.json?gitea_url=https://codeberg.org')
.expectBadge({
label: 'forks',
message: 'user or repo not found',
})
24 changes: 23 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,24 @@ function httpErrorsFor() {
}
}

export { description, httpErrorsFor }
function renderIssue({ variant, labels, defaultBadgeData, count }) {
const state = variant.split('-')[0]
const raw = variant.endsWith('-raw')
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 }
96 changes: 96 additions & 0 deletions services/gitea/gitea-issues.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Joi from 'joi'
import { pathParam, queryParam } from '../index.js'
import { optionalUrl, nonNegativeInteger } from '../validators.js'
import { fetchIssue } from './gitea-common-fetch.js'
import { description, httpErrorsFor, renderIssue } from './gitea-helper.js'
import GiteaBase from './gitea-base.js'

const schema = Joi.object({ 'x-total-count': nonNegativeInteger }).required()

const queryParamSchema = Joi.object({
labels: Joi.string(),
gitea_url: optionalUrl,
}).required()

export default class GiteaIssues extends GiteaBase {
static category = 'issue-tracking'

static route = {
base: 'gitea/issues',
pattern:
':variant(all|all-raw|open|open-raw|closed|closed-raw)/:user/:repo+',
queryParamSchema,
}

static openApi = {
'/gitea/issues/{variant}/{user}/{repo}': {
get: {
summary: 'Gitea Issues',
description,
parameters: [
pathParam({
name: 'variant',
example: 'all',
schema: { type: 'string', enum: this.getEnum('variant') },
}),
pathParam({
name: 'user',
example: 'gitea',
}),
pathParam({
name: 'repo',
example: 'tea',
}),
queryParam({
name: 'gitea_url',
example: 'https://gitea.com',
}),
queryParam({
name: 'labels',
example: 'test,failure::new',
description:
'If you want to use multiple labels, you can use a comma (<code>,</code>) to separate them, e.g. <code>foo,bar</code>',
}),
],
},
},
}

static defaultBadgeData = { label: 'issues', color: 'informational' }
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 fetchIssue(this, {
user,
repo,
baseUrl,
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 count = data['x-total-count']
return renderIssue({
variant,
labels,
defaultBadgeData: this.constructor.defaultBadgeData,
count,
})
}
}
Loading

0 comments on commit 372052c

Please sign in to comment.