Commit 91ce0467 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Make triggers to be user aware

parent 0a75de29
...@@ -95,6 +95,7 @@ class User < ActiveRecord::Base ...@@ -95,6 +95,7 @@ class User < ActiveRecord::Base
has_many :todos, dependent: :destroy has_many :todos, dependent: :destroy
has_many :notification_settings, dependent: :destroy has_many :notification_settings, dependent: :destroy
has_many :award_emoji, dependent: :destroy has_many :award_emoji, dependent: :destroy
has_many :triggers, dependent: :destroy, class_name: 'Ci::Trigger', foreign_key: :owner_id
has_many :assigned_issues, dependent: :nullify, foreign_key: :assignee_id, class_name: "Issue" has_many :assigned_issues, dependent: :nullify, foreign_key: :assignee_id, class_name: "Issue"
has_many :assigned_merge_requests, dependent: :nullify, foreign_key: :assignee_id, class_name: "MergeRequest" has_many :assigned_merge_requests, dependent: :nullify, foreign_key: :assignee_id, class_name: "MergeRequest"
......
...@@ -3,7 +3,7 @@ module Ci ...@@ -3,7 +3,7 @@ module Ci
def execute(project, trigger, ref, variables = nil) def execute(project, trigger, ref, variables = nil)
trigger_request = trigger.trigger_requests.create(variables: variables) trigger_request = trigger.trigger_requests.create(variables: variables)
pipeline = Ci::CreatePipelineService.new(project, nil, ref: ref). pipeline = Ci::CreatePipelineService.new(project, trigger.owner, ref: ref).
execute(ignore_skip_ci: true, trigger_request: trigger_request) execute(ignore_skip_ci: true, trigger_request: trigger_request)
if pipeline.persisted? if pipeline.persisted?
trigger_request trigger_request
......
...@@ -5,6 +5,6 @@ class AddOwnerIdToTriggers < ActiveRecord::Migration ...@@ -5,6 +5,6 @@ class AddOwnerIdToTriggers < ActiveRecord::Migration
def change def change
add_column :ci_triggers, :owner_id, :integer add_column :ci_triggers, :owner_id, :integer
add_foreign_key :ci_triggers, :users, column: :owner_id, on_delete: :nullify add_foreign_key :ci_triggers, :users, column: :owner_id, on_delete: :cascade
end end
end end
require 'spec_helper' require 'spec_helper'
describe Ci::Trigger, models: true do describe Ci::Trigger, models: true do
let(:project) { FactoryGirl.create :empty_project } let(:project) { create :empty_project }
describe 'associations' do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:owner) }
it { is_expected.to have_many(:trigger_requests) }
end
describe 'before_validation' do describe 'before_validation' do
it 'sets an random token if none provided' do it 'sets an random token if none provided' do
trigger = FactoryGirl.create :ci_trigger_without_token, project: project trigger = create(:ci_trigger_without_token, project: project)
expect(trigger.token).not_to be_nil expect(trigger.token).not_to be_nil
end end
it 'does not set an random token if one provided' do it 'does not set an random token if one provided' do
trigger = FactoryGirl.create :ci_trigger, project: project trigger = create(:ci_trigger, project: project)
expect(trigger.token).to eq('token') expect(trigger.token).to eq('token')
end end
end end
......
...@@ -32,6 +32,7 @@ describe User, models: true do ...@@ -32,6 +32,7 @@ describe User, models: true do
it { is_expected.to have_many(:spam_logs).dependent(:destroy) } it { is_expected.to have_many(:spam_logs).dependent(:destroy) }
it { is_expected.to have_many(:todos).dependent(:destroy) } it { is_expected.to have_many(:todos).dependent(:destroy) }
it { is_expected.to have_many(:award_emoji).dependent(:destroy) } it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
it { is_expected.to have_many(:triggers).dependent(:destroy) }
it { is_expected.to have_many(:builds).dependent(:nullify) } it { is_expected.to have_many(:builds).dependent(:nullify) }
it { is_expected.to have_many(:pipelines).dependent(:nullify) } it { is_expected.to have_many(:pipelines).dependent(:nullify) }
it { is_expected.to have_many(:chat_names).dependent(:destroy) } it { is_expected.to have_many(:chat_names).dependent(:destroy) }
......
...@@ -13,10 +13,24 @@ describe Ci::CreateTriggerRequestService, services: true do ...@@ -13,10 +13,24 @@ describe Ci::CreateTriggerRequestService, services: true do
context 'valid params' do context 'valid params' do
subject { service.execute(project, trigger, 'master') } subject { service.execute(project, trigger, 'master') }
context 'without owner' do
it { expect(subject).to be_kind_of(Ci::TriggerRequest) } it { expect(subject).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject.pipeline).to be_kind_of(Ci::Pipeline) }
it { expect(subject.builds.first).to be_kind_of(Ci::Build) } it { expect(subject.builds.first).to be_kind_of(Ci::Build) }
end end
context 'with owner' do
let(:owner) { create(:user) }
let(:trigger) { create(:ci_trigger, project: project, owner: owner) }
it { expect(subject).to be_kind_of(Ci::TriggerRequest) }
it { expect(subject.pipeline).to be_kind_of(Ci::Pipeline) }
it { expect(subject.pipeline.user).to eq(owner) }
it { expect(subject.builds.first).to be_kind_of(Ci::Build) }
it { expect(subject.builds.first.user).to eq(owner) }
end
end
context 'no commit for ref' do context 'no commit for ref' do
subject { service.execute(project, trigger, 'other-branch') } subject { service.execute(project, trigger, 'other-branch') }
......
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