1
0
mirror of https://github.com/badges/shields.git synced 2025-04-18 19:44:04 +03:00
shields/services/gitlab/gitlab-last-commit.service.js
chris48s 5cdef88bcc
Add renderDateBadge helper; affects [aur BitbucketLastCommit chrome date eclipse factorio galaxytoolshed GiteaLastCommit GistLastCommit GithubCreatedAt GithubHacktoberfest GithubIssueDetail GithubLastCommit GithubReleaseDate GitlabLastCommit maven npm openvsx snapcraft SourceforgeLastCommit steam vaadin visualstudio wordpress] (#10682)
* add and consistently use parseDate and renderDateBadge helpers

also move

- age
- formatDate
- formatRelativeDate

to date.js

* fix bug in wordpress last update badge

* validate in formatDate() and age()

it is going to be unlikely we'll invoke either of these
directly now, but lets calidate here too

* remove unusued imports

* reverse colours for galaxy toolshed
2024-11-17 13:15:28 +00:00

92 lines
2.4 KiB
JavaScript

import Joi from 'joi'
import { renderDateBadge } from '../date.js'
import { NotFound, pathParam, queryParam } from '../index.js'
import { optionalUrl, relativeUri } from '../validators.js'
import GitLabBase from './gitlab-base.js'
import { description, httpErrorsFor } from './gitlab-helper.js'
const schema = Joi.array()
.items(
Joi.object({
committed_date: Joi.string().required(),
}),
)
.required()
const queryParamSchema = Joi.object({
ref: Joi.string(),
gitlab_url: optionalUrl,
path: relativeUri,
}).required()
const refText = `
ref can be filled with the name of a branch, tag or revision range of the repository.
`
const lastCommitDescription = description + refText
export default class GitlabLastCommit extends GitLabBase {
static category = 'activity'
static route = {
base: 'gitlab/last-commit',
pattern: ':project+',
queryParamSchema,
}
static openApi = {
'/gitlab/last-commit/{project}': {
get: {
summary: 'GitLab Last Commit',
description: lastCommitDescription,
parameters: [
pathParam({
name: 'project',
example: 'gitlab-org/gitlab',
}),
queryParam({
name: 'gitlab_url',
example: 'https://gitlab.com',
}),
queryParam({
name: 'ref',
example: 'master',
}),
queryParam({
name: 'path',
example: 'README.md',
schema: { type: 'string' },
description: 'File path to resolve the last commit for.',
}),
],
},
},
}
static defaultBadgeData = { label: 'last commit' }
async fetch({ project, baseUrl, ref, path }) {
// 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, path, per_page: 1 } },
schema,
httpErrors: httpErrorsFor('project not found'),
})
}
async handle(
{ project },
{ gitlab_url: baseUrl = 'https://gitlab.com', ref, path },
) {
const data = await this.fetch({ project, baseUrl, ref, path })
const [commit] = data
if (!commit) throw new NotFound({ prettyMessage: 'no commits found' })
return renderDateBadge(commit.committed_date)
}
}