-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GITEA] add forks, stars, issues and pr badges (#9923)
* 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
1 parent
8796460
commit 372052c
Showing
10 changed files
with
780 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
Oops, something went wrong.