variables.rb 3.22 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module API
  class Variables < Grape::API
5 6
    include PaginationParams

7
    before { authenticate! }
8
    before { authorize! :admin_build, user_project }
9

Z.J. van de Weg's avatar
Z.J. van de Weg committed
10 11 12 13
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end

14
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS  do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
15 16 17 18
      desc 'Get project variables' do
        success Entities::Variable
      end
      params do
19
        use :pagination
Z.J. van de Weg's avatar
Z.J. van de Weg committed
20
      end
21 22 23 24 25
      get ':id/variables' do
        variables = user_project.variables
        present paginate(variables), with: Entities::Variable
      end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
26 27 28 29 30 31
      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
32
      # rubocop: disable CodeReuse/ActiveRecord
33 34
      get ':id/variables/:key' do
        key = params[:key]
35
        variable = user_project.variables.find_by(key: key)
36

37
        break not_found!('Variable') unless variable
38

39
        present variable, with: Entities::Variable
40
      end
41
      # rubocop: enable CodeReuse/ActiveRecord
42

Z.J. van de Weg's avatar
Z.J. van de Weg committed
43 44 45 46 47 48
      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'
49
        optional :protected, type: String, desc: 'Whether the variable is protected'
Z.J. van de Weg's avatar
Z.J. van de Weg committed
50
      end
51
      post ':id/variables' do
Lin Jen-Shin's avatar
Lin Jen-Shin committed
52 53 54
        variable_params = declared_params(include_missing: false)

        variable = user_project.variables.create(variable_params)
55

56 57 58 59 60
        if variable.valid?
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
61 62
      end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
63 64 65 66 67 68
      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'
69
        optional :protected, type: String, desc: 'Whether the variable is protected'
Z.J. van de Weg's avatar
Z.J. van de Weg committed
70
      end
71
      # rubocop: disable CodeReuse/ActiveRecord
72
      put ':id/variables/:key' do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
73
        variable = user_project.variables.find_by(key: params[:key])
74

75
        break not_found!('Variable') unless variable
76

Lin Jen-Shin's avatar
Lin Jen-Shin committed
77 78 79
        variable_params = declared_params(include_missing: false).except(:key)

        if variable.update(variable_params)
80 81 82 83
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
84
      end
85
      # rubocop: enable CodeReuse/ActiveRecord
86

Z.J. van de Weg's avatar
Z.J. van de Weg committed
87 88 89 90 91 92
      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
93
      # rubocop: disable CodeReuse/ActiveRecord
94
      delete ':id/variables/:key' do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
95
        variable = user_project.variables.find_by(key: params[:key])
Robert Schilling's avatar
Robert Schilling committed
96
        not_found!('Variable') unless variable
97

98
        # Variables don't have any timestamp. Therfore, destroy unconditionally.
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
99
        status 204
100
        variable.destroy
101
      end
102
      # rubocop: enable CodeReuse/ActiveRecord
103 104 105
    end
  end
end