Commit 601f50c6 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Add endpoint that returns a list of deployments that happened within last 8.hours

add index created_at
parent 19a44034
class Projects::DeploymentsController < Projects::ApplicationController
before_action :authorize_read_deployment!
def index
deployments = environment.deployments.where('created_at > ?', 8.hours.ago)
.map { |d| d.slice(:id, :iid, :created_at, :sha, :ref, :tag) }
render json: { deployments: deployments }
end
private
def environment
@environment ||= project.environments.find(params[:environment_id])
end
end
...@@ -168,6 +168,8 @@ constraints(ProjectUrlConstrainer.new) do ...@@ -168,6 +168,8 @@ constraints(ProjectUrlConstrainer.new) do
collection do collection do
get :folder, path: 'folders/:id' get :folder, path: 'folders/:id'
end end
resources :deployments, only: [:index]
end end
resource :cycle_analytics, only: [:show] resource :cycle_analytics, only: [:show]
......
class AddCreatedAtIndexToDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def change
add_concurrent_index :deployments, :created_at
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170317203554) do ActiveRecord::Schema.define(version: 20170327091750) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
...@@ -346,6 +346,7 @@ ActiveRecord::Schema.define(version: 20170317203554) do ...@@ -346,6 +346,7 @@ ActiveRecord::Schema.define(version: 20170317203554) do
t.string "on_stop" t.string "on_stop"
end end
add_index "deployments", ["created_at"], name: "index_deployments_on_created_at", using: :btree
add_index "deployments", ["project_id", "environment_id", "iid"], name: "index_deployments_on_project_id_and_environment_id_and_iid", using: :btree add_index "deployments", ["project_id", "environment_id", "iid"], name: "index_deployments_on_project_id_and_environment_id_and_iid", using: :btree
add_index "deployments", ["project_id", "iid"], name: "index_deployments_on_project_id_and_iid", unique: true, using: :btree add_index "deployments", ["project_id", "iid"], name: "index_deployments_on_project_id_and_iid", unique: true, using: :btree
......
require 'spec_helper'
describe Projects::DeploymentsController do
include ApiHelpers
let(:user) { create(:user) }
let(:project) { create(:empty_project) }
let(:environment) { create(:environment, name: 'production', project: project) }
let(:deployment) { create(:deployment, project: project, environment: environment) }
before do
project.team << [user, :master]
sign_in(user)
end
describe 'GET #index' do
it 'returns list of deployments withing last 8 hours' do
create(:deployment, environment: environment, created_at: 9.hours.ago)
create(:deployment, environment: environment, created_at: 7.hours.ago)
create(:deployment, environment: environment)
get :index, environment_params
expect(response).to be_ok
expect(json_response['deployments'].count).to eq(2)
end
it 'returns a list with deployments information' do
deployment = create(:deployment, environment: environment)
get :index, environment_params
expect(response).to be_ok
deployments = json_response['deployments']
deployment_info = deployments.first.with_indifferent_access
created_at = deployment_info.delete(:created_at).to_time.utc
expect(deployments.count).to eq(1)
expect(deployment_info).to include(:id, :iid, :sha, :ref, :tag)
expect(deployment).to have_attributes(deployment_info)
expect(deployment.created_at).to be_within(1.second).of(created_at)
end
end
def environment_params(opts={})
opts.reverse_merge(namespace_id: project.namespace, project_id: project, environment_id: environment.id)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment