debian-mirror-gitlab/lib/api/badges.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
4.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-27 19:54:05 +05:30
module API
2021-01-03 14:25:43 +05:30
class Badges < ::API::Base
2018-03-27 19:54:05 +05:30
include PaginationParams
before { authenticate_non_get! }
helpers ::API::Helpers::BadgesHelpers
2021-10-27 15:23:28 +05:30
feature_category :projects
2021-01-29 00:20:46 +05:30
2018-03-27 19:54:05 +05:30
helpers do
def find_source_if_admin(source_type)
source = find_source(source_type, params[:id])
authorize_admin_source!(source_type, source)
source
end
end
%w[group project].each do |source_type|
params do
requires :id, type: String, desc: "The ID of a #{source_type}"
end
2019-02-15 15:39:39 +05:30
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2018-03-27 19:54:05 +05:30
desc "Gets a list of #{source_type} badges viewable by the authenticated user." do
detail 'This feature was introduced in GitLab 10.6.'
success Entities::Badge
end
params do
use :pagination
2022-11-25 23:54:43 +05:30
optional :name, type: String, desc: 'Name for the badge'
2018-03-27 19:54:05 +05:30
end
2022-07-23 23:45:48 +05:30
get ":id/badges", urgency: :low do
2018-03-27 19:54:05 +05:30
source = find_source(source_type, params[:id])
2020-01-01 13:55:28 +05:30
badges = source.badges
name = params[:name]
badges = badges.with_name(name) if name
present_badges(source, paginate(badges))
2018-03-27 19:54:05 +05:30
end
desc "Preview a badge from a #{source_type}." do
detail 'This feature was introduced in GitLab 10.6.'
success Entities::BasicBadgeDetails
end
params do
requires :link_url, type: String, desc: 'URL of the badge link'
requires :image_url, type: String, desc: 'URL of the badge image'
end
get ":id/badges/render" do
authenticate!
source = find_source_if_admin(source_type)
badge = ::Badges::BuildService.new(declared_params(include_missing: false))
.execute(source)
if badge.valid?
present_badges(source, badge, with: Entities::BasicBadgeDetails)
else
render_validation_error!(badge)
end
end
desc "Gets a badge of a #{source_type}." do
detail 'This feature was introduced in GitLab 10.6.'
success Entities::Badge
end
params do
requires :badge_id, type: Integer, desc: 'The badge ID'
end
2022-07-16 23:28:13 +05:30
# TODO: Set PUT /projects/:id/badges/:badge_id to low urgency and GET to default urgency
# after different urgencies are supported for different HTTP verbs.
# See https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/1670
get ":id/badges/:badge_id", urgency: :low do
2018-03-27 19:54:05 +05:30
source = find_source(source_type, params[:id])
badge = find_badge(source)
present_badges(source, badge)
end
desc "Adds a badge to a #{source_type}." do
detail 'This feature was introduced in GitLab 10.6.'
success Entities::Badge
end
params do
requires :link_url, type: String, desc: 'URL of the badge link'
requires :image_url, type: String, desc: 'URL of the badge image'
2020-01-01 13:55:28 +05:30
optional :name, type: String, desc: 'Name for the badge'
2018-03-27 19:54:05 +05:30
end
2022-07-23 23:45:48 +05:30
post ":id/badges" do
2018-03-27 19:54:05 +05:30
source = find_source_if_admin(source_type)
badge = ::Badges::CreateService.new(declared_params(include_missing: false)).execute(source)
if badge.persisted?
present_badges(source, badge)
else
render_validation_error!(badge)
end
end
desc "Updates a badge of a #{source_type}." do
detail 'This feature was introduced in GitLab 10.6.'
success Entities::Badge
end
params do
optional :link_url, type: String, desc: 'URL of the badge link'
optional :image_url, type: String, desc: 'URL of the badge image'
2020-01-01 13:55:28 +05:30
optional :name, type: String, desc: 'Name for the badge'
2018-03-27 19:54:05 +05:30
end
put ":id/badges/:badge_id" do
source = find_source_if_admin(source_type)
2020-09-03 11:15:55 +05:30
badge = find_badge(source)
2018-03-27 19:54:05 +05:30
badge = ::Badges::UpdateService.new(declared_params(include_missing: false))
2020-09-03 11:15:55 +05:30
.execute(badge)
2018-03-27 19:54:05 +05:30
if badge.valid?
present_badges(source, badge)
else
render_validation_error!(badge)
end
end
desc 'Removes a badge from a project or group.' do
detail 'This feature was introduced in GitLab 10.6.'
end
params do
requires :badge_id, type: Integer, desc: 'The badge ID'
end
delete ":id/badges/:badge_id" do
source = find_source_if_admin(source_type)
badge = find_badge(source)
destroy_conditionally!(badge)
end
end
end
end
end