BigW Consortium Gitlab

project_snippets.rb 4.56 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: { id: %r{[^/]+} } do
11 12 13 14 15 16 17
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
          not_found!
        end
18 19 20 21 22

        def snippets_for_current_user
          finder_params = { filter: :by_project, project: user_project }
          SnippetsFinder.new.execute(current_user, finder_params)
        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 54 55
        requires :visibility, type: String,
                              values: Gitlab::VisibilityLevel.string_values,
                              desc: 'The visibility of the snippet'
56
      end
57
      post ":id/snippets" do
58
        authorize! :create_project_snippet, user_project
59
        snippet_params = declared_params.merge(request: request, api: true)
60
        snippet_params[:content] = snippet_params.delete(:code)
61

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

64 65
        render_spam_error! if snippet.spam?

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

73 74 75 76 77 78 79 80
      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'
81 82 83
        optional :visibility, type: String,
                              values: Gitlab::VisibilityLevel.string_values,
                              desc: 'The visibility of the snippet'
84 85
        at_least_one_of :title, :file_name, :code, :visibility_level
      end
86
      put ":id/snippets/:snippet_id" do
87 88 89 90 91
        snippet = snippets_for_current_user.find_by(id: params.delete(:snippet_id))
        not_found!('Snippet') unless snippet

        authorize! :update_project_snippet, snippet

92
        snippet_params = declared_params(include_missing: false)
93 94
          .merge(request: request, api: true)

95
        snippet_params[:content] = snippet_params.delete(:code) if snippet_params[:code].present?
96

97 98
        UpdateSnippetService.new(user_project, current_user, snippet,
                                 snippet_params).execute
99

100 101 102
        render_spam_error! if snippet.spam?

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

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

        authorize! :admin_project_snippet, snippet
        snippet.destroy
119 120
      end

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

        env['api.format'] = :txt
130
        content_type 'text/plain'
131
        present snippet.content
132 133 134 135
      end
    end
  end
end