BigW Consortium Gitlab

variables.rb 2.68 KB
Newer Older
1 2 3 4
module API
  # Projects variables API
  class Variables < Grape::API
    before { authenticate! }
5
    before { authorize! :admin_build, user_project }
6

7 8 9 10
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end

11
    resource :projects do
12 13 14 15 16 17 18
      desc 'Get project variables' do
        success Entities::Variable
      end
      params do
        optional :page, type: Integer, desc: 'The page number for pagination'
        optional :per_page, type: Integer, desc: 'The value of items per page to show'
      end
19 20 21 22 23
      get ':id/variables' do
        variables = user_project.variables
        present paginate(variables), with: Entities::Variable
      end

24 25 26 27 28 29
      desc 'Get a specific variable from a project' do
        success Entities::Variable
      end
      params do
        requires :key, type: String, desc: 'The key of the variable'
      end
30 31
      get ':id/variables/:key' do
        key = params[:key]
32
        variable = user_project.variables.find_by(key: key.to_s)
33

34
        return not_found!('Variable') unless variable
35

36
        present variable, with: Entities::Variable
37
      end
38

39 40 41 42 43 44 45
      desc 'Create a new variable in a project' do
        success Entities::Variable
      end
      params do
        requires :key, type: String, desc: 'The key of the variable'
        requires :value, type: String, desc: 'The value of the variable'
      end
46
      post ':id/variables' do
47
        variable = user_project.variables.create(declared(params, include_parent_namespaces: false).to_h)
48

49 50 51 52 53
        if variable.valid?
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
54 55
      end

56 57 58 59 60 61 62
      desc 'Update an existing variable from a project' do
        success Entities::Variable
      end
      params do
        optional :key, type: String, desc: 'The key of the variable'
        optional :value, type: String, desc: 'The value of the variable'
      end
63
      put ':id/variables/:key' do
64
        variable = user_project.variables.find_by(key: params[:key])
65

66 67
        return not_found!('Variable') unless variable

68
        if variable.update(value: params[:value])
69 70 71 72
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
73
      end
74

75 76 77 78 79 80
      desc 'Delete an existing variable from a project' do
        success Entities::Variable
      end
      params do
        requires :key, type: String, desc: 'The key of the variable'
      end
81
      delete ':id/variables/:key' do
82
        variable = user_project.variables.find_by(key: params[:key])
83 84

        return not_found!('Variable') unless variable
85

86
        present variable.destroy, with: Entities::Variable
87
      end
88 89 90
    end
  end
end