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 [gitlablicense] services #8024

Merged
merged 6 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
89 changes: 89 additions & 0 deletions services/gitlab/gitlab-license.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { renderLicenseBadge } from '../licenses.js'
import GitLabBase from './gitlab-base.js'

const schema = Joi.object({
license: Joi.object({
name: Joi.string().required(),
}).allow(null),
}).required()

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

const documentation = `
<p>
You may use your GitLab Project Id (e.g. 13083) or your Project Path (e.g. gitlab-org/gitlab-foss )
</p>
`
const commonProps = {
namedParams: {
project: 'gitlab-org/gitlab-foss',
},
documentation,
}

export default class GitlabLicense extends GitLabBase {
static category = 'license'

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

static examples = [
{
title: 'GitLab',
...commonProps,
staticPreview: {
label: 'license',
message: 'MIT License',
color: 'green',
},
},
{
title: 'GitLab (custom server)',
...commonProps,
queryParams: { gitlab_url: 'https://jihulab.com' },
staticPreview: {
label: 'license',
message: 'MIT License',
color: 'green',
},
},
sunny0826 marked this conversation as resolved.
Show resolved Hide resolved
]

static defaultBadgeData = { label: 'license' }

static render({ license }) {
if (license) {
return renderLicenseBadge({ license })
} else {
return { message: 'not specified' }
}
Comment on lines +62 to +66
Copy link
Member

Choose a reason for hiding this comment

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

The renderLicenseBadge helper actually handles cases when there's no license provided:

if (licenses.length === 0) {
return { message: 'missing', color: 'red' }
}

and the benefit of utilizing that is both simplicity in code and also consistency across our badges.

Did you structure this with different behavior deliberately or was it just simply a case of not knowing the helper handled that, and if it's the former, would you mind explaining your thought process?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refer to the Github licence for this part.

static render({ license }) {
if (license === 'NOASSERTION') {
return { message: 'not identifiable by github' }
} else if (license) {
return renderLicenseBadge({ license })
} else {
return { message: 'not specified' }
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, thanks for sharing. I'd be curious to know what other maintainers think, if there's more backstory on why GitHub is handled this way. Given the goals of the project I'd be hesitant to introduce this another case of inconsistent behavior in the no license scenario, unless there's good reason

}

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)}`,
options: { searchParams: { license: '1' } },
errorMessages: {
404: 'repo not found',
},
})
}

async handle({ project }, { gitlab_url: baseUrl = 'https://gitlab.com' }) {
const { license: licenseObject } = await this.fetch({
project,
baseUrl,
})
const license = licenseObject ? licenseObject.name : undefined
return this.constructor.render({ license })
}
}
57 changes: 57 additions & 0 deletions services/gitlab/gitlab-license.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { licenseToColor } from '../licenses.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

const publicDomainLicenseColor = licenseToColor('MIT License')
const unknownLicenseColor = licenseToColor()

t.create('License')
.get('/guoxudong.io/shields-test/licenced-test.json')
.expectBadge({
label: 'license',
message: 'MIT License',
color: `${publicDomainLicenseColor}`,
})

t.create('License for repo without a license')
.get('/guoxudong.io/shields-test/no-license-test.json')
.expectBadge({
label: 'license',
message: 'not specified',
color: 'lightgrey',
})

t.create('Other license').get('/gitlab-org/gitlab-foss.json').expectBadge({
label: 'license',
message: 'Other',
color: unknownLicenseColor,
})

t.create('License for unknown repo')
.get('/user1/gitlab-does-not-have-this-repo.json')
.expectBadge({
label: 'license',
message: 'repo not found',
color: 'red',
})

t.create('Mocking License')
.get('/group/project.json')
.intercept(nock =>
nock('https://gitlab.com')
.get('/api/v4/projects/group%2Fproject?license=1')
.reply(200, {
license: {
key: 'apache-2.0',
name: 'Apache License 2.0',
nickname: '',
html_url: 'http://choosealicense.com/licenses/apache-2.0/',
source_url: '',
},
})
)
.expectBadge({
label: 'license',
message: 'Apache License 2.0',
color: unknownLicenseColor,
})
Comment on lines +38 to +57
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we necessarily have to remove this test (in part because I realize it bears similarity to the corresponding github tests), but did want to note that it's not necessary for purposes of service/integration tests.

The license renderer is already extensively tested so we're typically content not trying to retest that logic in service tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I wrote this test more to show the data structure of licence, so that the developer can understand what fields are in the structure directly through this test case, without having to actually call it.

Copy link
Member

Choose a reason for hiding this comment

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

Understood, and again don't feel like you need to change anything, but my point is that we don't really do that especially not with our service tests. The schema we define in the service class that is used for validating response objects from the upstream APIs is how we typically "document" the structure and relevant fields, along with trying to include links to the upstream documentation.

Compared to other types of tests our service tests are slow and expensive and we typically use them to provide validation against the upstream services we integrate with/to catch when vendors/platforms make breaking changes to their APIs. They can be used to test/do other things, it's just that we typically try to refrain from doing so because of the associated drawbacks.

This is just an fyi/food for thought in case you decide you'd like to work on other badges in the future, GitLab or otherwise