1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
# frozen_string_literal: true
module API
class Topics < ::API::Base
include PaginationParams
feature_category :groups_and_projects
before do
set_current_organization
end
helpers do
def find_topic!(id)
topic = ::Projects::Topic.find(id)
find_organization!(topic.organization_id)
topic
end
end
desc 'Get topics' do
detail 'This feature was introduced in GitLab 14.5.'
success Entities::Projects::Topic
end
params do
optional :search, type: String,
desc: 'Return list of topics matching the search criteria',
documentation: { example: 'search' }
optional :without_projects, type: Boolean, desc: 'Return list of topics without assigned projects'
optional :organization_id, type: Integer, default: -> { ::Current.organization_id },
desc: 'The organization id for the topics'
use :pagination
end
get 'topics' do
organization = find_organization!(params[:organization_id])
topics = ::Projects::TopicsFinder.new(
params: declared_params(include_missing: false),
organization_id: organization.id
).execute
present paginate(topics), with: Entities::Projects::Topic
end
desc 'Get topic' do
detail 'This feature was introduced in GitLab 14.5.'
success Entities::Projects::Topic
end
params do
requires :id, type: Integer, desc: 'ID of project topic'
end
get 'topics/:id' do
topic = find_topic!(params[:id])
present topic, with: Entities::Projects::Topic
end
desc 'Create a topic' do
detail 'This feature was introduced in GitLab 14.5.'
success Entities::Projects::Topic
end
params do
requires :name, type: String, desc: 'Slug (name)'
requires :title, type: String, desc: 'Title'
optional :description, type: String, desc: 'Description'
optional :avatar, type: ::API::Validations::Types::WorkhorseFile, desc: 'Avatar image for topic',
documentation: { type: 'file' }
optional :organization_id, type: Integer, default: -> { ::Current.organization_id },
desc: 'The organization id for the topic'
end
post 'topics' do
authenticated_as_admin!
find_organization!(params[:organization_id]) if params[:organization_id].present?
topic = ::Projects::Topic.new(declared_params(include_missing: false))
if topic.save
present topic, with: Entities::Projects::Topic
else
render_validation_error!(topic)
end
end
desc 'Update a topic' do
detail 'This feature was introduced in GitLab 14.5.'
success Entities::Projects::Topic
end
params do
requires :id, type: Integer, desc: 'ID of project topic'
optional :name, type: String, desc: 'Slug (name)'
optional :title, type: String, desc: 'Title'
optional :description, type: String, desc: 'Description'
optional :avatar, type: ::API::Validations::Types::WorkhorseFile, desc: 'Avatar image for topic',
documentation: { type: 'file' }
end
put 'topics/:id' do
authenticated_as_admin!
topic = find_topic!(params[:id])
topic.remove_avatar! if params.key?(:avatar) && params[:avatar].nil?
if topic.update(declared_params(include_missing: false))
present topic, with: Entities::Projects::Topic
else
render_validation_error!(topic)
end
end
desc 'Delete a topic' do
detail 'This feature was introduced in GitLab 14.9.'
end
params do
requires :id, type: Integer, desc: 'ID of project topic'
end
delete 'topics/:id' do
authenticated_as_admin!
topic = find_topic!(params[:id])
destroy_conditionally!(topic)
end
desc 'Merge topics' do
detail 'This feature was introduced in GitLab 15.4.'
success Entities::Projects::Topic
end
params do
requires :source_topic_id, type: Integer, desc: 'ID of source project topic'
requires :target_topic_id, type: Integer, desc: 'ID of target project topic'
end
post 'topics/merge' do
authenticated_as_admin!
source_topic = find_topic!(params[:source_topic_id])
target_topic = find_topic!(params[:target_topic_id])
response = ::Topics::MergeService.new(source_topic, target_topic).execute
render_api_error!(response.message, :bad_request) if response.error?
present target_topic, with: Entities::Projects::Topic
end
end
end
|