From d080e9f93c40f1a3918c2ab01bbc9b052aa3fe5d Mon Sep 17 00:00:00 2001 From: guoxudong Date: Wed, 20 Jul 2022 16:43:57 +0800 Subject: [PATCH 1/4] feat: add gitlab stars service --- services/gitlab/gitlab-stars.service.js | 74 +++++++++++++++++++++++++ services/gitlab/gitlab-stars.tester.js | 20 +++++++ 2 files changed, 94 insertions(+) create mode 100644 services/gitlab/gitlab-stars.service.js create mode 100644 services/gitlab/gitlab-stars.tester.js diff --git a/services/gitlab/gitlab-stars.service.js b/services/gitlab/gitlab-stars.service.js new file mode 100644 index 0000000000000..792cf76c85c5e --- /dev/null +++ b/services/gitlab/gitlab-stars.service.js @@ -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 = ` +

+ 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/. +

+` + +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}`], + } + } + + 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 }) + } +} diff --git a/services/gitlab/gitlab-stars.tester.js b/services/gitlab/gitlab-stars.tester.js new file mode 100644 index 0000000000000..8051feb93f237 --- /dev/null +++ b/services/gitlab/gitlab-stars.tester.js @@ -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', + }) From 9125b8ceb6dec672bdbc22b1e8261bceb371646a Mon Sep 17 00:00:00 2001 From: guoxudong Date: Fri, 22 Jul 2022 08:18:15 +0800 Subject: [PATCH 2/4] add test case --- services/gitlab/gitlab-stars.tester.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/gitlab/gitlab-stars.tester.js b/services/gitlab/gitlab-stars.tester.js index 8051feb93f237..ff63b8ee88e4b 100644 --- a/services/gitlab/gitlab-stars.tester.js +++ b/services/gitlab/gitlab-stars.tester.js @@ -12,6 +12,15 @@ t.create('Stars') link: ['https://gitlab.com/gitlab-org/gitlab'], }) +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-org/gitlab'], + }) + t.create('Stars (project not found)') .get('/user1/gitlab-does-not-have-this-repo.json') .expectBadge({ From c9b9e42abea328cad8ddc2e5db2f48f29c9c147e Mon Sep 17 00:00:00 2001 From: guoxudong Date: Fri, 22 Jul 2022 08:26:12 +0800 Subject: [PATCH 3/4] fix --- services/gitlab/gitlab-stars.tester.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gitlab/gitlab-stars.tester.js b/services/gitlab/gitlab-stars.tester.js index ff63b8ee88e4b..84a66432361a1 100644 --- a/services/gitlab/gitlab-stars.tester.js +++ b/services/gitlab/gitlab-stars.tester.js @@ -18,7 +18,7 @@ t.create('Stars (self-managed)') label: 'stars', message: isMetric, color: 'blue', - link: ['https://jihulab.com/gitlab-org/gitlab'], + link: ['https://jihulab.com/gitlab-cn/gitlab'], }) t.create('Stars (project not found)') From 17197c5e4af7a449699a6c670af88e39ca9524aa Mon Sep 17 00:00:00 2001 From: guoxudong Date: Mon, 8 Aug 2022 10:19:16 +0800 Subject: [PATCH 4/4] fix link --- services/gitlab/gitlab-stars.service.js | 2 +- services/gitlab/gitlab-stars.tester.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/services/gitlab/gitlab-stars.service.js b/services/gitlab/gitlab-stars.service.js index 792cf76c85c5e..a440c814f34a1 100644 --- a/services/gitlab/gitlab-stars.service.js +++ b/services/gitlab/gitlab-stars.service.js @@ -49,7 +49,7 @@ export default class GitlabStars extends GitLabBase { return { message: metric(starCount), color: 'blue', - link: [`${baseUrl}/${project}`], + link: [`${baseUrl}/${project}`, `${baseUrl}/${project}/-/starrers`], } } diff --git a/services/gitlab/gitlab-stars.tester.js b/services/gitlab/gitlab-stars.tester.js index 84a66432361a1..11fc18aa6f49c 100644 --- a/services/gitlab/gitlab-stars.tester.js +++ b/services/gitlab/gitlab-stars.tester.js @@ -9,7 +9,10 @@ t.create('Stars') label: 'stars', message: isMetric, color: 'blue', - link: ['https://gitlab.com/gitlab-org/gitlab'], + link: [ + 'https://gitlab.com/gitlab-org/gitlab', + 'https://gitlab.com/gitlab-org/gitlab/-/starrers', + ], }) t.create('Stars (self-managed)') @@ -18,7 +21,10 @@ t.create('Stars (self-managed)') label: 'stars', message: isMetric, color: 'blue', - link: ['https://jihulab.com/gitlab-cn/gitlab'], + link: [ + 'https://jihulab.com/gitlab-cn/gitlab', + 'https://jihulab.com/gitlab-cn/gitlab/-/starrers', + ], }) t.create('Stars (project not found)')