2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
class Projects::TemplatesController < Projects::ApplicationController
|
2019-05-23 00:33:11 -04:00
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :authorize_can_read_issuable!
|
|
|
|
before_action :get_template_class
|
2016-06-24 15:43:46 -04:00
|
|
|
|
2021-07-30 08:10:12 -04:00
|
|
|
feature_category :source_code_management
|
2021-11-12 13:12:20 -05:00
|
|
|
urgency :low, [:names]
|
2020-10-08 14:08:32 -04:00
|
|
|
|
2020-11-10 04:08:45 -05:00
|
|
|
def index
|
|
|
|
templates = @template_type.template_subsets(project)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: templates.to_json }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def show
|
|
|
|
template = @template_type.find(params[:key], project)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: template.to_json }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
def names
|
|
|
|
respond_to do |format|
|
2021-05-28 02:10:48 -04:00
|
|
|
format.json { render json: TemplateFinder.all_template_names(project, params[:template_type].to_s.pluralize) }
|
2019-09-18 10:02:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
private
|
|
|
|
|
2019-05-23 00:33:11 -04:00
|
|
|
# User must have:
|
|
|
|
# - `read_merge_request` to see merge request templates, or
|
|
|
|
# - `read_issue` to see issue templates
|
|
|
|
#
|
|
|
|
# Note params[:template_type] has a route constraint to limit it to
|
|
|
|
# `merge_request` or `issue`
|
|
|
|
def authorize_can_read_issuable!
|
|
|
|
action = [:read_, params[:template_type]].join
|
|
|
|
|
|
|
|
authorize_action!(action)
|
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def get_template_class
|
|
|
|
template_types = { issue: Gitlab::Template::IssueTemplate, merge_request: Gitlab::Template::MergeRequestTemplate }.with_indifferent_access
|
|
|
|
@template_type = template_types[params[:template_type]]
|
|
|
|
end
|
|
|
|
end
|