projects.rb 6.4 KB
Newer Older
N
Nihad Abbasov 已提交
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
N
Nihad Abbasov 已提交
12
        @projects = paginate current_user.projects
13
        present @projects, with: Entities::Project
N
Nihad Abbasov 已提交
14 15 16 17 18
      end

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

26 27 28 29
      # Create new project
      #
      # Parameters:
      #   name (required) - name for new project
A
Alex Denisov 已提交
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

N
Nihad Abbasov 已提交
47 48 49
      # Get a project repository branches
      #
      # Parameters:
N
Nihad Abbasov 已提交
50
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
51 52 53
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
54
        present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
N
Nihad Abbasov 已提交
55 56
      end

57 58 59 60
      # Get a single branch
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
61
      #   branch (required) - The name of the branch
62
      # Example Request:
63 64 65
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
66
        present @branch, with: Entities::RepoObject
67 68
      end

N
Nihad Abbasov 已提交
69 70 71
      # Get a project repository tags
      #
      # Parameters:
N
Nihad Abbasov 已提交
72
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
73 74 75
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
76
        present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject
N
Nihad Abbasov 已提交
77
      end
N
Nihad Abbasov 已提交
78 79 80 81

      # Get a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
82
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
83 84 85 86
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   GET /projects/:id/snippets/:snippet_id
      get ":id/snippets/:snippet_id" do
N
Nihad Abbasov 已提交
87
        @snippet = user_project.snippets.find(params[:snippet_id])
88
        present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
89 90 91 92 93
      end

      # Create a new project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
94
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
95 96 97 98 99 100 101
      #   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
N
Nihad Abbasov 已提交
102
        @snippet = user_project.snippets.new(
103 104 105 106
          title: params[:title],
          file_name: params[:file_name],
          expires_at: params[:lifetime],
          content: params[:code]
N
Nihad Abbasov 已提交
107 108 109 110
        )
        @snippet.author = current_user

        if @snippet.save
111
          present @snippet, with: Entities::ProjectSnippet
N
Nihad Abbasov 已提交
112 113 114 115 116
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

117 118 119
      # Update an existing project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
120
      #   id (required) - The ID or code name of a project
121 122 123 124 125 126 127 128
      #   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
N
Nihad Abbasov 已提交
129
        @snippet = user_project.snippets.find(params[:snippet_id])
130
        parameters = {
131 132 133 134
          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)
135 136 137
        }

        if @snippet.update_attributes(parameters)
138
          present @snippet, with: Entities::ProjectSnippet
139 140 141 142 143
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

N
Nihad Abbasov 已提交
144 145 146
      # Delete a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
147
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
148 149 150 151
      #   snippet_id (required) - The ID of a project snippet
      # Example Request:
      #   DELETE /projects/:id/snippets/:snippet_id
      delete ":id/snippets/:snippet_id" do
N
Nihad Abbasov 已提交
152
        @snippet = user_project.snippets.find(params[:snippet_id])
N
Nihad Abbasov 已提交
153 154
        @snippet.destroy
      end
155 156 157 158

      # Get a raw project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
159
      #   id (required) - The ID or code name of a project
160 161 162 163
      #   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
N
Nihad Abbasov 已提交
164
        @snippet = user_project.snippets.find(params[:snippet_id])
165
        content_type 'text/plain'
166 167
        present @snippet.content
      end
168 169 170 171 172

      # Get a raw file contents
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
173
      #   sha (required) - The commit or branch name
174 175 176 177 178 179 180 181
      #   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
182

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

186 187 188 189 190 191 192 193 194 195
        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

N
Nihad Abbasov 已提交
196 197 198
    end
  end
end