BigW Consortium Gitlab

project_snippets.rb 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
module API
  # Projects API
  class ProjectSnippets < Grape::API
    before { authenticate! }

    resource :projects do
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
          not_found!
        end
14 15 16 17 18

        def snippets_for_current_user
          finder_params = { filter: :by_project, project: user_project }
          SnippetsFinder.new.execute(current_user, finder_params)
        end
19 20 21 22 23 24 25 26 27
      end

      # Get a project snippets
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   GET /projects/:id/snippets
      get ":id/snippets" do
28
        present paginate(snippets_for_current_user), with: Entities::ProjectSnippet
29 30 31 32 33 34 35 36 37 38
      end

      # Get a project snippet
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id
      get ":id/snippets/:snippet_id" do
39
        @snippet = snippets_for_current_user.find(params[:snippet_id])
40 41 42 43 44 45 46 47 48 49
        present @snippet, with: Entities::ProjectSnippet
      end

      # Create a new project snippet
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   title (required) - The title of a snippet
      #   file_name (required) - The name of a snippet file
      #   code (required) - The content of a snippet
50
      #   visibility_level (required) - The snippet's visibility
51 52 53
      # Example Request:
      #   POST /projects/:id/snippets
      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 65
        else
          present @snippet, with: Entities::ProjectSnippet
66 67 68 69 70 71 72 73 74 75 76
        end
      end

      # Update an existing project snippet
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   snippet_id (required) - The ID of a project snippet
      #   title (optional) - The title of a snippet
      #   file_name (optional) - The name of a snippet file
      #   code (optional) - The content of a snippet
77
      #   visibility_level (optional) - The snippet's visibility
78 79 80
      # Example Request:
      #   PUT /projects/:id/snippets/:snippet_id
      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 86
        attrs[:content] = params[:code] if params[:code].present?

87 88 89
        UpdateSnippetService.new(user_project, current_user, @snippet,
                                 attrs).execute
        if @snippet.errors.any?
90
          render_validation_error!(@snippet)
91 92
        else
          present @snippet, with: Entities::ProjectSnippet
93 94 95 96 97 98 99 100 101 102 103 104
        end
      end

      # Delete a project snippet
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
        begin
105
          @snippet = snippets_for_current_user.find(params[:snippet_id])
106
          authorize! :update_project_snippet, @snippet
107 108
          @snippet.destroy
        rescue
109
          not_found!('Snippet')
110 111 112 113 114 115 116 117 118 119 120
        end
      end

      # Get a raw project snippet
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id/raw
      get ":id/snippets/:snippet_id/raw" do
121
        @snippet = snippets_for_current_user.find(params[:snippet_id])
122 123

        env['api.format'] = :txt
124 125 126 127 128 129
        content_type 'text/plain'
        present @snippet.content
      end
    end
  end
end