Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add [GitlabStars] service #8209

Merged
merged 5 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`],
sunny0826 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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 })
}
}
20 changes: 20 additions & 0 deletions services/gitlab/gitlab-stars.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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'],
})

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