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

Add [GitLab] Top Language Badge #10750

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
80 changes: 80 additions & 0 deletions services/gitlab/gitlab-top-language.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { pathParam, queryParam } from '../index.js'
import GitLabBase from './gitlab-base.js'
import { description, httpErrorsFor } from './gitlab-helper.js'

const schema = Joi.object()
.pattern(Joi.string(), Joi.number().min(0).max(100).precision(2))
.required()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The object key and value should both be .required(), so

Suggested change
const schema = Joi.object()
.pattern(Joi.string(), Joi.number().min(0).max(100).precision(2))
.required()
const schema = Joi.object()
.pattern(
Joi.string().required(),
Joi.number().min(0).max(100).precision(2).required(),
)
.required()

here


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

export default class GitlabTopLanguage extends GitLabBase {
static category = 'analysis'

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

static openApi = {
'/gitlab/languages/{project}': {
get: {
summary: 'GitLab Top Language',
description,
parameters: [
pathParam({
name: 'project',
example: 'gitlab-org/gitlab',
}),
queryParam({
name: 'gitlab_url',
example: 'https://gitlab.com',
}),
],
},
},
}

static defaultBadgeData = { label: 'language' }

static render({ languageData }) {
if (Object.keys(languageData).length > 0) {
const topLanguage = Object.keys(languageData).reduce((a, b) =>
languageData[a] > languageData[b] ? a : b,
)
return {
label: topLanguage.toLowerCase(),
message: `${languageData[topLanguage].toFixed(1)}%`,
color: 'blue',
}
} else {
return {
label: 'no languages found',
message: 'NA',
color: 'blue',
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we

throw new InvalidResponse({ prettyMessage: 'no languages found' })

here and update the test.

}

async fetch({ project, baseUrl }) {
return super.fetch({
schema,
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(project)}/languages`,
httpErrors: httpErrorsFor('project not found'),
})
}

async handle({ project }, { gitlab_url: baseUrl = 'https://gitlab.com' }) {
const languageData = await this.fetch({
project,
baseUrl,
})

return this.constructor.render({ languageData })
}
}
25 changes: 25 additions & 0 deletions services/gitlab/gitlab-top-language.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Joi from 'joi'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('Valid Repository')
.get('/wireshark/wireshark.json')
.expectBadge({
label: 'c',
message: Joi.string().regex(/^([1-9]?[0-9]\.[0-9]|100\.0)%$/),
})

t.create('Valid Blank Repo')
.get('/KoruptTinker/gitlab-blank-repo.json')
.expectBadge({
label: 'no languages found',
message: 'NA',
})

t.create('Invalid Repository')
.get('/wireshark/invalidexample.json')
.expectBadge({
label: 'language',
message: 'project not found',
})
Loading