1
0
mirror of https://github.com/badges/shields.git synced 2025-11-25 18:23:19 +03:00
Files
shields/services/gitlab/gitlab-last-commit.service.js
chris48s 14892e3943 Implement a pattern for dealing with upstream APIs which are slow on the first hit; affects [endpoint] (#9233)
* allow serviceData to override cacheSeconds with a longer value

* prevent [endpoint] json cacheSeconds property exceeding service default

* allow ShieldsRuntimeError to specify a cacheSeconds property

By default error responses use the cacheLength of
the service class throwing the error.

This allows error to tell the handling layer the maxAge
that should be set on the error badge response.

* add customExceptions param

This

1. allows us to specify custom properties to pass to the exception
   constructor if we throw any of the standard got errors
   e.g: `ETIMEDOUT`, `ECONNRESET`, etc
2. uses a custom `cacheSeconds` property (if set on the exception)
   to set the response maxAge

* customExceptions --> systemErrors

* errorMessages --> httpErrors
2023-06-13 21:08:43 +01:00

80 lines
2.0 KiB
JavaScript

import Joi from 'joi'
import { optionalUrl } from '../validators.js'
import { formatDate } from '../text-formatters.js'
import { age as ageColor } from '../color-formatters.js'
import { documentation, httpErrorsFor } from './gitlab-helper.js'
import GitLabBase from './gitlab-base.js'
const schema = Joi.array()
.items(
Joi.object({
committed_date: Joi.string().required(),
}).required()
)
.required()
.min(1)
const queryParamSchema = Joi.object({
ref: Joi.string(),
gitlab_url: optionalUrl,
}).required()
const refText = `
<p>
ref can be filled with the name of a branch, tag or revision range of the repository.
</p>
`
const defaultDocumentation = documentation + refText
export default class GitlabLastCommit extends GitLabBase {
static category = 'activity'
static route = {
base: 'gitlab/last-commit',
pattern: ':project+',
queryParamSchema,
}
static examples = [
{
title: 'GitLab last commit',
namedParams: {
project: 'gitlab-org/gitlab',
},
queryParams: { gitlab_url: 'https://gitlab.com' },
staticPreview: this.render({ commitDate: '2013-07-31T20:01:41Z' }),
documentation: defaultDocumentation,
},
]
static defaultBadgeData = { label: 'last commit' }
static render({ commitDate }) {
return {
message: formatDate(commitDate),
color: ageColor(Date.parse(commitDate)),
}
}
async fetch({ project, baseUrl, ref }) {
// https://docs.gitlab.com/ee/api/commits.html#list-repository-commits
return super.fetch({
url: `${baseUrl}/api/v4/projects/${encodeURIComponent(
project
)}/repository/commits`,
options: { searchParams: { ref_name: ref } },
schema,
httpErrors: httpErrorsFor('project not found'),
})
}
async handle(
{ project },
{ gitlab_url: baseUrl = 'https://gitlab.com', ref }
) {
const data = await this.fetch({ project, baseUrl, ref })
return this.constructor.render({ commitDate: data[0].committed_date })
}
}