BigW Consortium Gitlab

variables.rb 2.91 KB
Newer Older
1 2
module API
  class Variables < Grape::API
3 4
    include PaginationParams

5
    before { authenticate! }
6
    before { authorize! :admin_build, user_project }
7

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

12
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS  do
13 14 15 16
      desc 'Get project variables' do
        success Entities::Variable
      end
      params do
17
        use :pagination
18
      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)
33

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

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

39 40 41 42 43 44
      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'
45
        optional :protected, type: String, desc: 'Whether the variable is protected'
46
      end
47
      post ':id/variables' do
Lin Jen-Shin committed
48 49 50
        variable_params = declared_params(include_missing: false)

        variable = user_project.variables.create(variable_params)
51

52 53 54 55 56
        if variable.valid?
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
57 58
      end

59 60 61 62 63 64
      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'
65
        optional :protected, type: String, desc: 'Whether the variable is protected'
66
      end
67
      put ':id/variables/:key' do
68
        variable = user_project.variables.find_by(key: params[:key])
69

70 71
        return not_found!('Variable') unless variable

Lin Jen-Shin committed
72 73 74
        variable_params = declared_params(include_missing: false).except(:key)

        if variable.update(variable_params)
75 76 77 78
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
79
      end
80

81 82 83 84 85 86
      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
87
      delete ':id/variables/:key' do
88
        variable = user_project.variables.find_by(key: params[:key])
Robert Schilling committed
89
        not_found!('Variable') unless variable
90

91
        # Variables don't have any timestamp. Therfore, destroy unconditionally.
Dmitriy Zaporozhets committed
92
        status 204
93
        variable.destroy
94
      end
95 96 97
    end
  end
end