projects.rb 5.7 KB
Newer Older
N
Nihad Abbasov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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
        @projects = current_user.projects
        present @projects, :with => Entities::Project
      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
N
Nihad Abbasov 已提交
23
        present user_project, :with => Entities::Project
N
Nihad Abbasov 已提交
24 25 26 27 28
      end

      # Get a project repository branches
      #
      # Parameters:
N
Nihad Abbasov 已提交
29
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
30 31 32
      # Example Request:
      #   GET /projects/:id/repository/branches
      get ":id/repository/branches" do
N
Nihad Abbasov 已提交
33
        present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject
N
Nihad Abbasov 已提交
34 35
      end

36 37 38 39
      # Get a single branch
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
40
      #   branch (required) - The name of the branch
41
      # Example Request:
42 43 44
      #   GET /projects/:id/repository/branches/:branch
      get ":id/repository/branches/:branch" do
        @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
45 46 47
        present @branch, :with => Entities::RepoObject
      end

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

      # Get a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
61
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
62 63 64 65
      #   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 已提交
66
        @snippet = user_project.snippets.find(params[:snippet_id])
N
Nihad Abbasov 已提交
67 68 69 70 71 72
        present @snippet, :with => Entities::ProjectSnippet
      end

      # Create a new project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
73
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
74 75 76 77 78 79 80
      #   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 已提交
81
        @snippet = user_project.snippets.new(
N
Nihad Abbasov 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
          :title      => params[:title],
          :file_name  => params[:file_name],
          :expires_at => params[:lifetime],
          :content    => params[:code]
        )
        @snippet.author = current_user

        if @snippet.save
          present @snippet, :with => Entities::ProjectSnippet
        else
          error!({'message' => '404 Not found'}, 404)
        end
      end

96 97 98
      # Update an existing project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
99
      #   id (required) - The ID or code name of a project
100 101 102 103 104 105 106 107
      #   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 已提交
108
        @snippet = user_project.snippets.find(params[:snippet_id])
109 110 111 112 113 114 115 116 117 118 119 120 121 122
        parameters = {
          :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)
        }

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

N
Nihad Abbasov 已提交
123 124 125
      # Delete a project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
126
      #   id (required) - The ID or code name of a project
N
Nihad Abbasov 已提交
127 128 129 130
      #   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 已提交
131
        @snippet = user_project.snippets.find(params[:snippet_id])
N
Nihad Abbasov 已提交
132 133
        @snippet.destroy
      end
134 135 136 137

      # Get a raw project snippet
      #
      # Parameters:
N
Nihad Abbasov 已提交
138
      #   id (required) - The ID or code name of a project
139 140 141 142
      #   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 已提交
143
        @snippet = user_project.snippets.find(params[:snippet_id])
144 145
        present @snippet.content
      end
146 147 148 149 150

      # Get a raw file contents
      #
      # Parameters:
      #   id (required) - The ID or code name of a project
151
      #   sha (required) - The commit or branch name
152 153 154 155 156 157 158 159
      #   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
160

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

164 165 166 167 168 169 170 171 172 173
        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 已提交
174 175 176
    end
  end
end