files.rb 4.0 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
module API
  class Files < Grape::API
3 4 5 6
    helpers do
      def commit_params(attrs)
        {
          file_path: attrs[:file_path],
L
Lin Jen-Shin 已提交
7
          start_branch: attrs[:branch_name],
8 9 10
          target_branch: attrs[:branch_name],
          commit_message: attrs[:commit_message],
          file_content: attrs[:content],
11 12 13
          file_content_encoding: attrs[:encoding],
          author_email: attrs[:author_email],
          author_name: attrs[:author_name]
14 15 16 17 18 19
        }
      end

      def commit_response(attrs)
        {
          file_path: attrs[:file_path],
20
          branch_name: attrs[:branch_name]
21 22
        }
      end
R
Robert Schilling 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36

      params :simple_file_params do
        requires :file_path, type: String, desc: 'The path to new file. Ex. lib/class.rb'
        requires :branch_name, type: String, desc: 'The name of branch'
        requires :commit_message, type: String, desc: 'Commit Message'
        optional :author_email, type: String, desc: 'The email of the author'
        optional :author_name, type: String, desc: 'The name of the author'
      end

      params :extended_file_params do
        use :simple_file_params
        requires :content, type: String, desc: 'File content'
        optional :encoding, type: String, values: %w[base64], desc: 'File encoding'
      end
37 38
    end

R
Robert Schilling 已提交
39 40 41
    params do
      requires :id, type: String, desc: 'The project ID'
    end
D
Dmitriy Zaporozhets 已提交
42
    resource :projects do
R
Robert Schilling 已提交
43 44 45 46 47
      desc 'Get a file from repository'
      params do
        requires :file_path, type: String, desc: 'The path to the file. Ex. lib/class.rb'
        requires :ref, type: String, desc: 'The name of branch, tag, or commit'
      end
D
Dmitriy Zaporozhets 已提交
48
      get ":id/repository/files" do
N
Nihad Abbasov 已提交
49 50
        authorize! :download_code, user_project

R
Robert Schilling 已提交
51 52
        commit = user_project.commit(params[:ref])
        not_found!('Commit') unless commit
D
Dmitriy Zaporozhets 已提交
53

J
Jacob Vosmaer 已提交
54
        repo = user_project.repository
R
Robert Schilling 已提交
55 56
        blob = repo.blob_at(commit.sha, params[:file_path])
        not_found!('File') unless blob
D
Dmitriy Zaporozhets 已提交
57

R
Robert Schilling 已提交
58 59
        blob.load_all_data!(repo)
        status(200)
D
Dmitriy Zaporozhets 已提交
60

R
Robert Schilling 已提交
61 62 63 64 65 66 67 68 69
        {
          file_name: blob.name,
          file_path: blob.path,
          size: blob.size,
          encoding: "base64",
          content: Base64.strict_encode64(blob.data),
          ref: params[:ref],
          blob_id: blob.id,
          commit_id: commit.id,
H
Hiroyuki Sato 已提交
70
          last_commit_id: repo.last_commit_id_for_path(commit.sha, params[:file_path])
R
Robert Schilling 已提交
71
        }
D
Dmitriy Zaporozhets 已提交
72 73
      end

R
Robert Schilling 已提交
74 75 76 77
      desc 'Create new file in repository'
      params do
        use :extended_file_params
      end
D
Dmitriy Zaporozhets 已提交
78
      post ":id/repository/files" do
N
Nihad Abbasov 已提交
79 80
        authorize! :push_code, user_project

R
Robert Schilling 已提交
81 82
        file_params = declared_params(include_missing: false)
        result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
D
Dmitriy Zaporozhets 已提交
83 84 85

        if result[:status] == :success
          status(201)
R
Robert Schilling 已提交
86
          commit_response(file_params)
D
Dmitriy Zaporozhets 已提交
87
        else
88
          render_api_error!(result[:message], 400)
D
Dmitriy Zaporozhets 已提交
89 90
        end
      end
D
Dmitriy Zaporozhets 已提交
91

R
Robert Schilling 已提交
92 93 94 95
      desc 'Update existing file in repository'
      params do
        use :extended_file_params
      end
D
Dmitriy Zaporozhets 已提交
96
      put ":id/repository/files" do
N
Nihad Abbasov 已提交
97 98
        authorize! :push_code, user_project

R
Robert Schilling 已提交
99 100
        file_params = declared_params(include_missing: false)
        result = ::Files::UpdateService.new(user_project, current_user, commit_params(file_params)).execute
D
Dmitriy Zaporozhets 已提交
101 102 103

        if result[:status] == :success
          status(200)
R
Robert Schilling 已提交
104
          commit_response(file_params)
D
Dmitriy Zaporozhets 已提交
105
        else
106 107
          http_status = result[:http_status] || 400
          render_api_error!(result[:message], http_status)
D
Dmitriy Zaporozhets 已提交
108 109
        end
      end
110

R
Robert Schilling 已提交
111 112 113 114
      desc 'Delete an existing file in repository'
      params do
        use :simple_file_params
      end
115
      delete ":id/repository/files" do
N
Nihad Abbasov 已提交
116 117
        authorize! :push_code, user_project

R
Robert Schilling 已提交
118
        file_params = declared_params(include_missing: false)
119
        result = ::Files::DestroyService.new(user_project, current_user, commit_params(file_params)).execute
120 121 122

        if result[:status] == :success
          status(200)
R
Robert Schilling 已提交
123
          commit_response(file_params)
124
        else
125
          render_api_error!(result[:message], 400)
126 127
        end
      end
D
Dmitriy Zaporozhets 已提交
128 129 130
    end
  end
end