Module: Cyclid::API::Organizations::Stages

Defined in:
app/cyclid/controllers/organizations/stages.rb

Overview

API endpoints for Organization Stages

Defined Under Namespace

Modules: Helpers

Organizations collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/cyclid/controllers/organizations/stages.rb', line 81

def self.registered(app)
  include Errors::HTTPErrors

  # Get all of the stages.
  app.get do
    authorized_for!(params[:name], Operations::READ)

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    # Convert each Stage to a hash & sanitize it
    stages = org.stages.all.map do |stage|
      stage_hash = sanitize_stage(stage.serializable_hash)

      # Santize each step in this stage
      steps = stage.steps.map do |step|
        sanitize_step(step.serializable_hash)
      end
      stage_hash['steps'] = steps

      stage_hash
    end

    return stages.to_json
  end

  # Create a new stage.
  app.post do
    authorized_for!(params[:name], Operations::ADMIN)

    payload = parse_request_body
    Cyclid.logger.debug payload

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    halt_with_json_response(400, INVALID_JSON, 'stage does not define any steps') \
      unless payload.key? 'steps'

    # Ensure that a stage with the same name & version doesn't already exist
    version = payload['version'] || '0.0.1'
    halt_with_json_response(409, \
                            DUPLICATE, \
                            'A stage with that name and version already exists') \
    if org.stages.exists?(name: payload['name'], version: version)

    begin
      stage = Stage.new

      stage.name = payload['name']
      stage.version = payload['version'] if payload.key? 'version'
      stage.organization = org

      # Create the steps & store their serialized form
      stage.steps << create_steps(payload['steps'])

      stage.save!
    rescue ActiveRecord::ActiveRecordError, \
           ActiveRecord::UnknownAttributeError => ex

      Cyclid.logger.debug ex.message
      halt_with_json_response(400, INVALID_JSON, ex.message)
    end
  end

  # Returns every defined version for the given stage.
  app.get '/:stage' do
    authorized_for!(params[:name], Operations::READ)

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    # There may be multiple versions of the same stage, so we need to
    # find every instance of the given stage, convert each Stage to a
    # hash & sanitize it
    stages = org.stages.where(name: params[:stage]).map do |stage|
      stage_hash = sanitize_stage(stage.serializable_hash)

      # Santize each step in this stage
      steps = stage.steps.map do |step|
        sanitize_step(step.serializable_hash)
      end
      stage_hash['steps'] = steps

      stage_hash
    end

    halt_with_json_response(404, INVALID_STAGE, 'stage does not exist') \
      if stages.empty?

    return stages.to_json
  end

  # Get the details of the specified stage within the organization.
  # Returns the specified version of the given stage.
  app.get '/:stage/:version' do
    authorized_for!(params[:name], Operations::READ)

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    stage = org.stages.find_by(name: params[:stage], version: params[:version])
    halt_with_json_response(404, INVALID_STAGE, 'stage does not exist') \
      if stage.nil?

    # Sanitize the stage
    stage_hash = sanitize_stage(stage.serializable_hash)

    # Santize each step in this stage
    steps = stage.steps.map do |step|
      sanitize_step(step.serializable_hash)
    end
    stage_hash['steps'] = steps

    return stage_hash.to_json
  end

  app.helpers do
    include Helpers
  end
end

Instance Method Details

#GET(/organizations/: organization/stages) ⇒ Object

Get all of the stages.

Parameters:

  • organization (String)

    Name of the organization.

Returns:

  • All the stages within the organization.

  • (404)

    The organization does not exist



# File 'app/cyclid/controllers/organizations/stages.rb', line 27

#GET(/organizations/: organization/stages/:stage) ⇒ Object

Get the details of the specified stage within the organization. Returns every defined version for the given stage.

Parameters:

  • organization (String)

    Name of the organization.

  • stage (String)

    Name of the stage.

Returns:

  • The requested stage.

  • (404)

    The organization or stage does not exist



# File 'app/cyclid/controllers/organizations/stages.rb', line 56

#GET(/organizations/: organization/stages/:stage/:version) ⇒ Object

Get the details of the specified stage within the organization. Returns the specified version of the given stage.

Parameters:

  • organization (String)

    Name of the organization.

  • stage (String)

    Name of the stage.

  • version (String)

    Version of the stage.

Returns:

  • The requested stage.

  • (404)

    The organization, stage or version of the stage does not exist



# File 'app/cyclid/controllers/organizations/stages.rb', line 66

#POST(/organizations/: organization/stages) ⇒ 400, ...

Create a new stage.

Examples:

Create a simple stage for ‘example’ organization

POST /organizations/example/stages <= {"name": "example",
                                       "steps": [
                                         {
                                           "action": "command",
                                           "cmd": "bundle install"
                                         }
                                       ]}

Parameters:

  • organization (String)

    Name of the organization.

  • body (JSON)

    New stage

Options Hash (body):

  • name (String)

    Name of the new stage

  • version (String) — default: 0.0.1

    Version of the new stage

  • steps (Array<Object>)

    List of steps for the stage

Returns:

  • (400)

    The definition for the stage is invalid.

  • (404)

    The organization does not exist.

  • (409)

    A stage with the same name & version already exists.



# File 'app/cyclid/controllers/organizations/stages.rb', line 35