project_snippets.rb 5.25 KB
Newer Older
1 2
module API
  class ProjectSnippets < Grape::API
3 4
    include PaginationParams

5 6
    before { authenticate! }

7 8 9
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
10
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
11 12 13 14 15
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
16

17 18
          not_found!
        end
19 20

        def snippets_for_current_user
21
          SnippetsFinder.new(current_user, project: user_project).execute
22
        end
23 24
      end

25 26 27
      desc 'Get all project snippets' do
        success Entities::ProjectSnippet
      end
28 29 30
      params do
        use :pagination
      end
31
      get ":id/snippets" do
32
        present paginate(snippets_for_current_user), with: Entities::ProjectSnippet
33 34
      end

35 36 37 38 39 40
      desc 'Get a single project snippet' do
        success Entities::ProjectSnippet
      end
      params do
        requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
      end
41
      get ":id/snippets/:snippet_id" do
42 43 44 45 46 47 48 49 50 51 52
        snippet = snippets_for_current_user.find(params[:snippet_id])
        present snippet, with: Entities::ProjectSnippet
      end

      desc 'Create a new project snippet' do
        success Entities::ProjectSnippet
      end
      params do
        requires :title, type: String, desc: 'The title of the snippet'
        requires :file_name, type: String, desc: 'The file name of the snippet'
        requires :code, type: String, desc: 'The content of the snippet'
53
        optional :description, type: String, desc: 'The description of a snippet'
54 55 56
        requires :visibility, type: String,
                              values: Gitlab::VisibilityLevel.string_values,
                              desc: 'The visibility of the snippet'
57
      end
58
      post ":id/snippets" do
59
        authorize! :create_project_snippet, user_project
60
        snippet_params = declared_params.merge(request: request, api: true)
61
        snippet_params[:content] = snippet_params.delete(:code)
62

63
        snippet = CreateSnippetService.new(user_project, current_user, snippet_params).execute
64

65 66
        render_spam_error! if snippet.spam?

67 68
        if snippet.persisted?
          present snippet, with: Entities::ProjectSnippet
69
        else
70
          render_validation_error!(snippet)
71 72 73
        end
      end

74 75 76 77 78 79 80 81
      desc 'Update an existing project snippet' do
        success Entities::ProjectSnippet
      end
      params do
        requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
        optional :title, type: String, desc: 'The title of the snippet'
        optional :file_name, type: String, desc: 'The file name of the snippet'
        optional :code, type: String, desc: 'The content of the snippet'
82
        optional :description, type: String, desc: 'The description of a snippet'
83 84 85
        optional :visibility, type: String,
                              values: Gitlab::VisibilityLevel.string_values,
                              desc: 'The visibility of the snippet'
86 87
        at_least_one_of :title, :file_name, :code, :visibility_level
      end
88
      put ":id/snippets/:snippet_id" do
89 90 91 92 93
        snippet = snippets_for_current_user.find_by(id: params.delete(:snippet_id))
        not_found!('Snippet') unless snippet

        authorize! :update_project_snippet, snippet

94
        snippet_params = declared_params(include_missing: false)
95 96
          .merge(request: request, api: true)

97
        snippet_params[:content] = snippet_params.delete(:code) if snippet_params[:code].present?
98

99 100
        UpdateSnippetService.new(user_project, current_user, snippet,
                                 snippet_params).execute
101

102 103 104
        render_spam_error! if snippet.spam?

        if snippet.valid?
105
          present snippet, with: Entities::ProjectSnippet
106
        else
107
          render_validation_error!(snippet)
108 109 110
        end
      end

111 112 113 114
      desc 'Delete a project snippet'
      params do
        requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
      end
115
      delete ":id/snippets/:snippet_id" do
116 117 118 119
        snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
        not_found!('Snippet') unless snippet

        authorize! :admin_project_snippet, snippet
120

121
        destroy_conditionally!(snippet)
122 123
      end

124 125 126 127
      desc 'Get a raw project snippet'
      params do
        requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
      end
128
      get ":id/snippets/:snippet_id/raw" do
129 130
        snippet = snippets_for_current_user.find_by(id: params[:snippet_id])
        not_found!('Snippet') unless snippet
131 132

        env['api.format'] = :txt
133
        content_type 'text/plain'
134
        present snippet.content
135
      end
136 137 138 139 140 141 142 143 144 145

      desc 'Get the user agent details for a project snippet' do
        success Entities::UserAgentDetail
      end
      params do
        requires :snippet_id, type: Integer, desc: 'The ID of a project snippet'
      end
      get ":id/snippets/:snippet_id/user_agent_detail" do
        authenticated_as_admin!

146
        snippet = Snippet.find_by!(id: params[:snippet_id], project_id: params[:id])
147 148 149 150 151

        return not_found!('UserAgentDetail') unless snippet.user_agent_detail

        present snippet.user_agent_detail, with: Entities::UserAgentDetail
      end
152 153 154
    end
  end
end