Commit e3d870d7 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Add user to Ci::Build to have pusher email address

parent 546a3c65
......@@ -32,9 +32,9 @@ module Ci
belongs_to :commit, class_name: 'Ci::Commit'
belongs_to :runner, class_name: 'Ci::Runner'
belongs_to :trigger_request, class_name: 'Ci::TriggerRequest'
belongs_to :user
serialize :options
serialize :push_data
validates :commit, presence: true
validates :status, presence: true
......@@ -196,8 +196,8 @@ module Ci
def project_recipients
recipients = project.email_recipients.split(' ')
if project.email_add_pusher? && push_data[:user_email].present?
recipients << push_data[:user_email]
if project.email_add_pusher? && user.present? && user.notification_email.present?
recipients << user.notification_email
end
recipients.uniq
......
......@@ -96,10 +96,10 @@ module Ci
builds_without_retry.group(:stage_idx).select(:stage).last
end
def create_builds(ref, tag, push_data, trigger_request = nil)
def create_builds(ref, tag, user, trigger_request = nil)
return if skip_ci? && trigger_request.blank?
return unless config_processor
CreateBuildsService.new.execute(self, config_processor, ref, tag, push_data, trigger_request)
CreateBuildsService.new.execute(self, config_processor, ref, tag, user, trigger_request)
end
def refs
......
......@@ -52,7 +52,7 @@ class GitlabCiService < CiService
ci_project = Ci::Project.find_by(gitlab_id: project.id)
if ci_project
Ci::CreateCommitService.new.execute(ci_project, data)
Ci::CreateCommitService.new.execute(ci_project, data, current_user)
end
end
......
......@@ -130,6 +130,7 @@ class User < ActiveRecord::Base
has_many :assigned_merge_requests, dependent: :destroy, foreign_key: :assignee_id, class_name: "MergeRequest"
has_many :oauth_applications, class_name: 'Doorkeeper::Application', as: :owner, dependent: :destroy
has_one :abuse_report, dependent: :destroy
has_many :ci_builds, dependent: :nullify, class_name: 'Ci::Build'
#
......
module Ci
class CreateBuildsService
def execute(commit, ref, tag, push_data, config_processor, trigger_request)
def execute(commit, ref, tag, user, config_processor, trigger_request)
config_processor.stages.any? do |stage|
builds_attrs = config_processor.builds_for_stage_and_ref(stage, ref, tag)
builds_attrs.map do |build_attrs|
......@@ -17,7 +17,7 @@ module Ci
trigger_request: trigger_request,
ref: ref,
tag: tag,
push_data: push_data,
user: user,
})
end
end
......
module Ci
class CreateCommitService
def execute(project, params)
def execute(project, params, user)
before_sha = params[:before]
sha = params[:checkout_sha] || params[:after]
origin_ref = params[:ref]
......@@ -17,21 +17,9 @@ module Ci
end
tag = origin_ref.start_with?('refs/tags/')
push_data = {
before: before_sha,
after: sha,
ref: ref,
user_name: params[:user_name],
user_email: params[:user_email],
repository: params[:repository],
commits: params[:commits],
total_commits_count: params[:total_commits_count],
ci_yaml_file: params[:ci_yaml_file]
}
commit = project.gl_project.ensure_ci_commit(sha)
commit.update_committed!
commit.create_builds(ref, tag, push_data)
commit.create_builds(ref, tag, user)
commit
end
......
class AddIndexForBuildName < ActiveRecord::Migration
class AddIndexForBuilds < ActiveRecord::Migration
def up
add_index :ci_builds, [:commit_id, :stage_idx, :created_at]
end
......
class AddShaAndRefToBuilds < ActiveRecord::Migration
class AddRefAndTagToBuilds < ActiveRecord::Migration
def change
add_column :ci_builds, :tag, :boolean
add_column :ci_builds, :ref, :string
add_column :ci_builds, :push_data, :text
end
end
class MigrateShaAndRefToBuild < ActiveRecord::Migration
class MigrateRefAndTagToBuild < ActiveRecord::Migration
def change
execute('UPDATE ci_builds SET ref=(SELECT ref FROM ci_commits WHERE ci_commits.id = ci_builds.commit_id) WHERE ref IS NULL')
execute('UPDATE ci_builds SET push_data=(SELECT push_data FROM ci_commits WHERE ci_commits.id = ci_builds.commit_id) WHERE push_data IS NULL')
execute('UPDATE ci_builds SET tag=(SELECT tag FROM ci_commits WHERE ci_commits.id = ci_builds.commit_id) WHERE tag IS NULL')
end
end
class AddUserIdToBuild < ActiveRecord::Migration
def change
add_column :ci_builds, :user_id, :integer
end
end
......@@ -51,7 +51,7 @@ module Ci
required_attributes! [:project_id, :data, :project_token]
project = Ci::Project.find(params[:project_id])
authenticate_project_token!(project)
commit = Ci::CreateCommitService.new.execute(project, params[:data])
commit = Ci::CreateCommitService.new.execute(project, params[:data], current_user)
if commit.persisted?
present commit, with: Entities::CommitWithBuilds
......
......@@ -27,9 +27,10 @@
FactoryGirl.define do
factory :ci_build, class: Ci::Build do
ref 'master'
tag false
started_at 'Di 29. Okt 09:51:28 CET 2013'
finished_at 'Di 29. Okt 09:53:28 CET 2013'
commands 'ls -a'
options do
{
image: "ruby:2.1",
......@@ -43,5 +44,9 @@ FactoryGirl.define do
started_at nil
finished_at nil
end
factory :ci_build_tag do
tag true
end
end
end
......@@ -18,59 +18,25 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :ci_commit, class: Ci::Commit do
ref 'master'
before_sha '76de212e80737a608d939f648d959671fb0a0142'
sha '97de212e80737a608d939f648d959671fb0a0142'
push_data do
{
ref: 'refs/heads/master',
before: '76de212e80737a608d939f648d959671fb0a0142',
after: '97de212e80737a608d939f648d959671fb0a0142',
user_name: 'Git User',
user_email: 'git@example.com',
repository: {
name: 'test-data',
url: 'ssh://git@gitlab.com/test/test-data.git',
description: '',
homepage: 'http://gitlab.com/test/test-data'
},
commits: [
{
id: '97de212e80737a608d939f648d959671fb0a0142',
message: 'Test commit message',
timestamp: '2014-09-23T13:12:25+02:00',
url: 'https://gitlab.com/test/test-data/commit/97de212e80737a608d939f648d959671fb0a0142',
author: {
name: 'Git User',
email: 'git@user.com'
}
}
],
total_commits_count: 1,
ci_yaml_file: File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
}
end
gl_project factory: :empty_project
factory :ci_commit_without_jobs do
after(:create) do |commit, evaluator|
commit.push_data[:ci_yaml_file] = YAML.dump({})
commit.save
allow(commit).to receive(:ci_yaml_file) { YAML.dump({}) }
end
end
factory :ci_commit_with_one_job do
after(:create) do |commit, evaluator|
commit.push_data[:ci_yaml_file] = YAML.dump({ rspec: { script: "ls" } })
commit.save
allow(commit).to receive(:ci_yaml_file) { YAML.dump({ rspec: { script: "ls" } }) }
end
end
factory :ci_commit_with_two_jobs do
after(:create) do |commit, evaluator|
commit.push_data[:ci_yaml_file] = YAML.dump({ rspec: { script: "ls" }, spinach: { script: "ls" } })
commit.save
allow(commit).to receive(:ci_yaml_file) { YAML.dump({ rspec: { script: "ls" }, spinach: { script: "ls" } }) }
end
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