projects.rb 6.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
module Gitlab
  # Projects API
  class Projects < Grape::API
    before { authenticate! }

    resource :projects do
      # Get a projects list for authenticated user
      #
      # Example Request:
      #   GET /projects
      get do
Nihad Abbasov's avatar
Nihad Abbasov committed
12
        @projects = paginate current_user.projects
13
        present @projects, with: Entities::Project
14 15 16 17 18
      end

      # Get a single project
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
19
      #   id (required) - The ID or code name of a project
20 21 22
      # Example Request:
      #   GET /projects/:id
      get ":id" do
23
        present user_project, with: Entities::Project
24 25
      end

26 27 28 29
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
Alex Denisov's avatar
Alex Denisov committed
30 31
      #   code (optional) - code for new project, uses project name if not set
      #   path (optional) - path for new project, uses project name if not set
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      # Example Request
      #   POST /projects
      post do
        project = {}
        project[:name] = params[:name]
        project[:code] = params[:code] || project[:name]
        project[:path] = params[:path] || project[:name]
        @project = Project.create_by_user(project, current_user)
        if @project.saved?
          present @project, with: Entities::Project
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

47 48 49 50 51 52 53 54 55
      # Add users to project with specified access level
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
      #   user_ids (required) - The ID list of users to add
      #   project_access (required) - Project access level
      # Example Request:
      #   PUT /projects/:id/add_users
      put ":id/add_users" do
miks's avatar
miks committed
56
        user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access])
57 58
      end

59 60 61
      # Get a project repository branches
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
62
      #   id (required) - The ID or code name of a project
63 64 65
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
66
        present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
67 68
      end

69 70 71 72
      # Get a single branch
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
73
      #   branch (required) - The name of the branch
74
      # Example Request:
75 76 77
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
78
        present @branch, with: Entities::RepoObject
79 80
      end

81 82 83
      # Get a project repository tags
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
84
      #   id (required) - The ID or code name of a project
85 86 87
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
88
        present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
89
      end
Nihad Abbasov's avatar
Nihad Abbasov committed
90 91 92 93

      # Get a project snippet
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
94
      #   id (required) - The ID or code name of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
95 96 97 98
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id
      get ":id/snippets/:snippet_id" do
Nihad Abbasov's avatar
Nihad Abbasov committed
99
        @snippet = user_project.snippets.find(params[:snippet_id])
100
        present @snippet, with: Entities::ProjectSnippet
Nihad Abbasov's avatar
Nihad Abbasov committed
101 102 103 104 105
      end

      # Create a new project snippet
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
106
      #   id (required) - The ID or code name of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
107 108 109 110 111 112 113
      #   title (required) - The title of a snippet
      #   file_name (required) - The name of a snippet file
      #   lifetime (optional) - The expiration date of a snippet
      #   code (required) - The content of a snippet
      # Example Request:
      #   POST /projects/:id/snippets
      post ":id/snippets" do
Nihad Abbasov's avatar
Nihad Abbasov committed
114
        @snippet = user_project.snippets.new(
115 116 117 118
          title: params[:title],
          file_name: params[:file_name],
          expires_at: params[:lifetime],
          content: params[:code]
Nihad Abbasov's avatar
Nihad Abbasov committed
119 120 121 122
        )
        @snippet.author = current_user

        if @snippet.save
123
          present @snippet, with: Entities::ProjectSnippet
Nihad Abbasov's avatar
Nihad Abbasov committed
124 125 126 127 128
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

129 130 131
      # Update an existing project snippet
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
132
      #   id (required) - The ID or code name of a project
133 134 135 136 137 138 139 140
      #   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
      #   lifetime (optional) - The expiration date of a snippet
      #   code (optional) - The content of a snippet
      # Example Request:
      #   PUT /projects/:id/snippets/:snippet_id
      put ":id/snippets/:snippet_id" do
Nihad Abbasov's avatar
Nihad Abbasov committed
141
        @snippet = user_project.snippets.find(params[:snippet_id])
142
        parameters = {
143 144 145 146
          title: (params[:title] || @snippet.title),
          file_name: (params[:file_name] || @snippet.file_name),
          expires_at: (params[:lifetime] || @snippet.expires_at),
          content: (params[:code] || @snippet.content)
147 148 149
        }

        if @snippet.update_attributes(parameters)
150
          present @snippet, with: Entities::ProjectSnippet
151 152 153 154 155
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

Nihad Abbasov's avatar
Nihad Abbasov committed
156 157 158
      # Delete a project snippet
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
159
      #   id (required) - The ID or code name of a project
Nihad Abbasov's avatar
Nihad Abbasov committed
160 161 162 163
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
Nihad Abbasov's avatar
Nihad Abbasov committed
164
        @snippet = user_project.snippets.find(params[:snippet_id])
Nihad Abbasov's avatar
Nihad Abbasov committed
165 166
        @snippet.destroy
      end
167 168 169 170

      # Get a raw project snippet
      #
      # Parameters:
Nihad Abbasov's avatar
Nihad Abbasov committed
171
      #   id (required) - The ID or code name of a project
172 173 174 175
      #   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
Nihad Abbasov's avatar
Nihad Abbasov committed
176
        @snippet = user_project.snippets.find(params[:snippet_id])
177
        content_type 'text/plain'
178 179
        present @snippet.content
      end
180 181 182 183 184

      # Get a raw file contents
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
185
      #   sha (required) - The commit or branch name
186 187 188 189 190 191 192 193
      #   filepath (required) - The path to the file to display
      # Example Request:
      #   GET /projects/:id/repository/commits/:sha/blob
      get ":id/repository/commits/:sha/blob" do
        ref = params[:sha]

        commit = user_project.commit ref
        error!('404 Commit Not Found', 404) unless commit
194

195 196
        tree = Tree.new commit.tree, user_project, ref, params[:filepath]
        error!('404 File Not Found', 404) unless tree.try(:tree)
197

198 199 200 201 202 203 204 205 206 207
        if tree.text?
          encoding = Gitlab::Encode.detect_encoding(tree.data)
          content_type encoding ? "text/plain; charset=#{encoding}" : "text/plain"
        else
          content_type tree.mime_type
        end

        present tree.data
      end

208 209 210
    end
  end
end