PageRenderTime 47ms CodeModel.GetById 35ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/api/project_snippets.rb

https://gitlab.com/vicvega/gitlab-ce
Ruby | 129 lines | 67 code | 13 blank | 49 comment | 5 complexity | 6d10e1e269a3b1a9f7c3e77421be3b26 MD5 | raw file
  1module API
  2  # Projects API
  3  class ProjectSnippets < Grape::API
  4    before { authenticate! }
  5
  6    resource :projects do
  7      helpers do
  8        def handle_project_member_errors(errors)
  9          if errors[:project_access].any?
 10            error!(errors[:project_access], 422)
 11          end
 12          not_found!
 13        end
 14
 15        def snippets_for_current_user
 16          finder_params = { filter: :by_project, project: user_project }
 17          SnippetsFinder.new.execute(current_user, finder_params)
 18        end
 19      end
 20
 21      # Get a project snippets
 22      #
 23      # Parameters:
 24      #   id (required) - The ID of a project
 25      # Example Request:
 26      #   GET /projects/:id/snippets
 27      get ":id/snippets" do
 28        present paginate(snippets_for_current_user), with: Entities::ProjectSnippet
 29      end
 30
 31      # Get a project snippet
 32      #
 33      # Parameters:
 34      #   id (required) - The ID of a project
 35      #   snippet_id (required) - The ID of a project snippet
 36      # Example Request:
 37      #   GET /projects/:id/snippets/:snippet_id
 38      get ":id/snippets/:snippet_id" do
 39        @snippet = snippets_for_current_user.find(params[:snippet_id])
 40        present @snippet, with: Entities::ProjectSnippet
 41      end
 42
 43      # Create a new project snippet
 44      #
 45      # Parameters:
 46      #   id (required) - The ID of a project
 47      #   title (required) - The title of a snippet
 48      #   file_name (required) - The name of a snippet file
 49      #   code (required) - The content of a snippet
 50      #   visibility_level (required) - The snippet's visibility
 51      # Example Request:
 52      #   POST /projects/:id/snippets
 53      post ":id/snippets" do
 54        authorize! :create_project_snippet, user_project
 55        required_attributes! [:title, :file_name, :code, :visibility_level]
 56
 57        attrs = attributes_for_keys [:title, :file_name, :visibility_level]
 58        attrs[:content] = params[:code] if params[:code].present?
 59        @snippet = CreateSnippetService.new(user_project, current_user,
 60                                            attrs).execute
 61
 62        if @snippet.errors.any?
 63          render_validation_error!(@snippet)
 64        else
 65          present @snippet, with: Entities::ProjectSnippet
 66        end
 67      end
 68
 69      # Update an existing project snippet
 70      #
 71      # Parameters:
 72      #   id (required) - The ID of a project
 73      #   snippet_id (required) - The ID of a project snippet
 74      #   title (optional) - The title of a snippet
 75      #   file_name (optional) - The name of a snippet file
 76      #   code (optional) - The content of a snippet
 77      #   visibility_level (optional) - The snippet's visibility
 78      # Example Request:
 79      #   PUT /projects/:id/snippets/:snippet_id
 80      put ":id/snippets/:snippet_id" do
 81        @snippet = snippets_for_current_user.find(params[:snippet_id])
 82        authorize! :update_project_snippet, @snippet
 83
 84        attrs = attributes_for_keys [:title, :file_name, :visibility_level]
 85        attrs[:content] = params[:code] if params[:code].present?
 86
 87        UpdateSnippetService.new(user_project, current_user, @snippet,
 88                                 attrs).execute
 89        if @snippet.errors.any?
 90          render_validation_error!(@snippet)
 91        else
 92          present @snippet, with: Entities::ProjectSnippet
 93        end
 94      end
 95
 96      # Delete a project snippet
 97      #
 98      # Parameters:
 99      #   id (required) - The ID of a project
100      #   snippet_id (required) - The ID of a project snippet
101      # Example Request:
102      #   DELETE /projects/:id/snippets/:snippet_id
103      delete ":id/snippets/:snippet_id" do
104        begin
105          @snippet = snippets_for_current_user.find(params[:snippet_id])
106          authorize! :update_project_snippet, @snippet
107          @snippet.destroy
108        rescue
109          not_found!('Snippet')
110        end
111      end
112
113      # Get a raw project snippet
114      #
115      # Parameters:
116      #   id (required) - The ID of a project
117      #   snippet_id (required) - The ID of a project snippet
118      # Example Request:
119      #   GET /projects/:id/snippets/:snippet_id/raw
120      get ":id/snippets/:snippet_id/raw" do
121        @snippet = snippets_for_current_user.find(params[:snippet_id])
122
123        env['api.format'] = :txt
124        content_type 'text/plain'
125        present @snippet.content
126      end
127    end
128  end
129end