BigW Consortium Gitlab

variables.rb 2.83 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 11 12 13 14 15 16 17 18 19 20

    resource :projects do
      # Get project variables
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   page (optional) - The page number for pagination
      #   per_page (optional) - The value of items per page to show
      # Example Request:
      #   GET /projects/:id/variables
      get ':id/variables' do
        variables = user_project.variables
        present paginate(variables), with: Entities::Variable
      end

Tomasz Maczukin committed
21
      # Get specific variable of a project
22 23 24
      #
      # Parameters:
      #   id (required) - The ID of a project
25
      #   key (required) - The `key` of variable
26
      # Example Request:
27 28 29
      #   GET /projects/:id/variables/:key
      get ':id/variables/:key' do
        key = params[:key]
30
        variable = user_project.variables.find_by(key: key.to_s)
31

32
        return not_found!('Variable') unless variable
33

34
        present variable, with: Entities::Variable
35
      end
36

37 38 39 40
      # Create a new variable in project
      #
      # Parameters:
      #   id (required) - The ID of a project
41 42
      #   key (required) - The key of variable
      #   value (required) - The value of variable
43 44 45 46 47 48 49
      # Example Request:
      #   POST /projects/:id/variables
      post ':id/variables' do
        required_attributes! [:key, :value]

        variable = user_project.variables.create(key: params[:key], value: params[:value])

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

57 58 59 60
      # Update existing variable of a project
      #
      # Parameters:
      #   id (required) - The ID of a project
61 62
      #   key (optional) - The `key` of variable
      #   value (optional) - New value for `value` field of variable
63
      # Example Request:
64 65
      #   PUT /projects/:id/variables/:key
      put ':id/variables/:key' do
66
        variable = user_project.variables.find_by(key: params[:key].to_s)
67

68 69
        return not_found!('Variable') unless variable

70 71 72 73 74 75
        attrs = attributes_for_keys [:value]
        if variable.update(attrs)
          present variable, with: Entities::Variable
        else
          render_validation_error!(variable)
        end
76
      end
77 78 79 80 81

      # Delete existing variable of a project
      #
      # Parameters:
      #   id (required) - The ID of a project
82
      #   key (required) - The ID of a variable
Tomasz Maczukin committed
83
      # Example Request:
84 85
      #   DELETE /projects/:id/variables/:key
      delete ':id/variables/:key' do
86
        variable = user_project.variables.find_by(key: params[:key].to_s)
87 88

        return not_found!('Variable') unless variable
89
        variable.destroy
90 91

        present variable, with: Entities::Variable
92
      end
93 94 95
    end
  end
end