Skip to content

Commit

Permalink
Merge branch 'master' into jacob/ros-clarify-naming
Browse files Browse the repository at this point in the history
  • Loading branch information
repo-ranger[bot] authored Aug 9, 2022
2 parents 6d9d545 + af47032 commit ce36eab
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
4 changes: 2 additions & 2 deletions frontend/lib/generate-image-markup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ test(reStructuredText, () => {
'.. image:: https://img.shields.io/badge'
)
given('https://img.shields.io/badge', undefined, 'Example').expect(
'.. image:: https://img.shields.io/badge :alt: Example'
'.. image:: https://img.shields.io/badge\n :alt: Example'
)
given(
'https://img.shields.io/badge',
'https://example.com/example',
'Example'
).expect(
'.. image:: https://img.shields.io/badge :alt: Example :target: https://example.com/example'
'.. image:: https://img.shields.io/badge\n :alt: Example\n :target: https://example.com/example'
)
})

Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/generate-image-markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export function reStructuredText(
): string {
let result = `.. image:: ${badgeUrl}`
if (title) {
result += ` :alt: ${title}`
result += `\n :alt: ${title}`
}
if (link) {
result += ` :target: ${link}`
result += `\n :target: ${link}`
}
return result
}
Expand Down
74 changes: 74 additions & 0 deletions services/gitlab/gitlab-stars.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Joi from 'joi'
import { optionalUrl, nonNegativeInteger } from '../validators.js'
import { metric } from '../text-formatters.js'
import GitLabBase from './gitlab-base.js'

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

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

const documentation = `
<p>
You may use your GitLab Project Id (e.g. 278964) or your Project Path (e.g. gitlab-org/gitlab ).
Note that only internet-accessible GitLab instances are supported, for example https://jihulab.com, https://gitlab.gnome.org, or https://gitlab.com/.
</p>
`

export default class GitlabStars extends GitLabBase {
static category = 'social'

static route = {
base: 'gitlab/stars',
pattern: ':project+',
queryParamSchema,
}

static examples = [
{
title: 'GitLab stars',
namedParams: {
project: 'gitlab-org/gitlab',
},
queryParams: { gitlab_url: 'https://gitlab.com' },
staticPreview: {
label: 'stars',
message: '3.9k',
style: 'social',
},
documentation,
},
]

static defaultBadgeData = { label: 'stars', namedLogo: 'gitlab' }

static render({ baseUrl, project, starCount }) {
return {
message: metric(starCount),
color: 'blue',
link: [`${baseUrl}/${project}`, `${baseUrl}/${project}/-/starrers`],
}
}

async fetch({ project, baseUrl }) {
// https://docs.gitlab.com/ee/api/projects.html#get-single-project
return super.fetch({
schema,
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(project)}`,
errorMessages: {
404: 'project not found',
},
})
}

async handle({ project }, { gitlab_url: baseUrl = 'https://gitlab.com' }) {
const { star_count: starCount } = await this.fetch({
project,
baseUrl,
})
return this.constructor.render({ baseUrl, project, starCount })
}
}
35 changes: 35 additions & 0 deletions services/gitlab/gitlab-stars.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('Stars')
.get('/gitlab-org/gitlab.json')
.expectBadge({
label: 'stars',
message: isMetric,
color: 'blue',
link: [
'https://gitlab.com/gitlab-org/gitlab',
'https://gitlab.com/gitlab-org/gitlab/-/starrers',
],
})

t.create('Stars (self-managed)')
.get('/gitlab-cn/gitlab.json?gitlab_url=https://jihulab.com')
.expectBadge({
label: 'stars',
message: isMetric,
color: 'blue',
link: [
'https://jihulab.com/gitlab-cn/gitlab',
'https://jihulab.com/gitlab-cn/gitlab/-/starrers',
],
})

t.create('Stars (project not found)')
.get('/user1/gitlab-does-not-have-this-repo.json')
.expectBadge({
label: 'stars',
message: 'project not found',
})

0 comments on commit ce36eab

Please sign in to comment.