From 546a3c6561fbe967cc37ccc3229b71893cd20c34 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 2 Oct 2015 13:46:38 +0200 Subject: Refactor commit and build --- app/controllers/ci/builds_controller.rb | 2 +- app/controllers/ci/commits_controller.rb | 6 +- app/controllers/ci/projects_controller.rb | 3 +- app/helpers/ci/commits_helper.rb | 2 +- app/helpers/ci/gitlab_helper.rb | 2 +- app/helpers/ci_status_helper.rb | 2 +- app/models/ci/build.rb | 16 +++ app/models/ci/commit.rb | 137 +++++++-------------- app/models/ci/project_status.rb | 12 -- app/models/project.rb | 6 +- app/models/project_services/ci/hip_chat_message.rb | 2 +- app/models/project_services/ci/mail_service.rb | 2 +- app/models/project_services/ci/slack_message.rb | 2 +- app/models/project_services/gitlab_ci_service.rb | 4 +- app/services/ci/create_builds_service.rb | 27 ++++ app/services/ci/create_commit_service.rb | 39 +++--- app/services/ci/create_trigger_request_service.rb | 6 +- app/views/ci/builds/_build.html.haml | 4 + app/views/ci/builds/show.html.haml | 6 +- app/views/ci/commits/_commit.html.haml | 4 +- app/views/ci/commits/show.html.haml | 57 +++++---- app/views/ci/notify/build_fail_email.html.haml | 2 +- app/views/ci/notify/build_fail_email.text.erb | 2 +- app/views/ci/notify/build_success_email.html.haml | 2 +- app/views/ci/notify/build_success_email.text.erb | 2 +- 25 files changed, 163 insertions(+), 186 deletions(-) create mode 100644 app/services/ci/create_builds_service.rb (limited to 'app') diff --git a/app/controllers/ci/builds_controller.rb b/app/controllers/ci/builds_controller.rb index 80ee8666792..bf87f81439a 100644 --- a/app/controllers/ci/builds_controller.rb +++ b/app/controllers/ci/builds_controller.rb @@ -18,7 +18,7 @@ module Ci if commit # Redirect to commit page - redirect_to ci_project_ref_commit_path(@project, @build.commit.ref, @build.commit.sha) + redirect_to ci_project_commit_path(@project, @build.commit) return end end diff --git a/app/controllers/ci/commits_controller.rb b/app/controllers/ci/commits_controller.rb index 7a0a500fbe6..acf9189572c 100644 --- a/app/controllers/ci/commits_controller.rb +++ b/app/controllers/ci/commits_controller.rb @@ -13,7 +13,7 @@ module Ci end def status - commit = Ci::Project.find(params[:project_id]).commits.find_by_sha_and_ref!(params[:id], params[:ref_id]) + commit = Ci::Project.find(params[:project_id]).commits.find_by_sha!(params[:id], params[:ref_id]) render json: commit.to_json(only: [:id, :sha], methods: [:status, :coverage]) rescue ActiveRecord::RecordNotFound render json: { status: "not_found" } @@ -22,7 +22,7 @@ module Ci def cancel commit.builds.running_or_pending.each(&:cancel) - redirect_to ci_project_ref_commits_path(project, commit.ref, commit.sha) + redirect_to ci_project_commits_path(project, commit.sha) end private @@ -32,7 +32,7 @@ module Ci end def commit - @commit ||= Ci::Project.find(params[:project_id]).commits.find_by_sha_and_ref!(params[:id], params[:ref_id]) + @commit ||= Ci::Project.find(params[:project_id]).commits.find_by_sha!(params[:id]) end end end diff --git a/app/controllers/ci/projects_controller.rb b/app/controllers/ci/projects_controller.rb index e8788955eba..20e6c2c2ba7 100644 --- a/app/controllers/ci/projects_controller.rb +++ b/app/controllers/ci/projects_controller.rb @@ -19,7 +19,8 @@ module Ci @ref = params[:ref] @commits = @project.commits.reverse_order - @commits = @commits.where(ref: @ref) if @ref + # TODO: this is broken + # @commits = @commits.where(ref: @ref) if @ref @commits = @commits.page(params[:page]).per(20) end diff --git a/app/helpers/ci/commits_helper.rb b/app/helpers/ci/commits_helper.rb index 9069aed5b4d..a0df4c3d72d 100644 --- a/app/helpers/ci/commits_helper.rb +++ b/app/helpers/ci/commits_helper.rb @@ -1,7 +1,7 @@ module Ci module CommitsHelper def ci_commit_path(commit) - ci_project_ref_commits_path(commit.project, commit.ref, commit.sha) + ci_project_commits_path(commit.project, commit) end def commit_link(commit) diff --git a/app/helpers/ci/gitlab_helper.rb b/app/helpers/ci/gitlab_helper.rb index 13e4d0fd9c3..baddbc806f2 100644 --- a/app/helpers/ci/gitlab_helper.rb +++ b/app/helpers/ci/gitlab_helper.rb @@ -26,7 +26,7 @@ module Ci def yaml_web_editor_link(project) commits = project.commits - if commits.any? && commits.last.push_data[:ci_yaml_file] + if commits.any? && commits.last.ci_yaml_file "#{project.gitlab_url}/edit/master/.gitlab-ci.yml" else "#{project.gitlab_url}/new/master" diff --git a/app/helpers/ci_status_helper.rb b/app/helpers/ci_status_helper.rb index 3a88ed7107e..794bdc2530e 100644 --- a/app/helpers/ci_status_helper.rb +++ b/app/helpers/ci_status_helper.rb @@ -1,6 +1,6 @@ module CiStatusHelper def ci_status_path(ci_commit) - ci_project_ref_commits_path(ci_commit.project, ci_commit.ref, ci_commit) + ci_project_commits_path(ci_commit.project, ci_commit) end def ci_status_icon(ci_commit) diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 645ad68e1b3..bf3e8915205 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -34,10 +34,12 @@ module Ci belongs_to :trigger_request, class_name: 'Ci::TriggerRequest' serialize :options + serialize :push_data validates :commit, presence: true validates :status, presence: true validates :coverage, numericality: true, allow_blank: true + validates_presence_of :ref scope :running, ->() { where(status: "running") } scope :pending, ->() { where(status: "pending") } @@ -45,6 +47,9 @@ module Ci scope :failed, ->() { where(status: "failed") } scope :unstarted, ->() { where(runner_id: nil) } scope :running_or_pending, ->() { where(status:[:running, :pending]) } + scope :latest, ->() { group(:name).order(stage_idx: :asc, created_at: :desc) } + scope :ignore_failures, ->() { where(allow_failure: false) } + scope :for_ref, ->(ref) { where(ref: ref) } acts_as_taggable @@ -82,6 +87,7 @@ module Ci new_build.name = build.name new_build.allow_failure = build.allow_failure new_build.stage = build.stage + new_build.stage_idx = build.stage_idx new_build.trigger_request = build.trigger_request new_build.save new_build @@ -187,6 +193,16 @@ module Ci project.name end + def project_recipients + recipients = project.email_recipients.split(' ') + + if project.email_add_pusher? && push_data[:user_email].present? + recipients << push_data[:user_email] + end + + recipients.uniq + end + def repo_url project.repo_url_with_auth end diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 6d048779cde..35134b6628e 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -23,9 +23,7 @@ module Ci has_many :builds, dependent: :destroy, class_name: 'Ci::Build' has_many :trigger_requests, dependent: :destroy, class_name: 'Ci::TriggerRequest' - serialize :push_data - - validates_presence_of :ref, :sha, :before_sha, :push_data + validates_presence_of :sha validate :valid_commit_sha def self.truncate_sha(sha) @@ -69,15 +67,15 @@ module Ci end def git_author_name - commit_data[:author][:name] if commit_data && commit_data[:author] + commit_data.author_name if commit_data end def git_author_email - commit_data[:author][:email] if commit_data && commit_data[:author] + commit_data.author_email if commit_data end def git_commit_message - commit_data[:message] if commit_data && commit_data[:message] + commit_data.message if commit_data end def short_before_sha @@ -89,84 +87,31 @@ module Ci end def commit_data - push_data[:commits].find do |commit| - commit[:id] == sha - end + @commit ||= gl_project.commit(sha) rescue nil end - def project_recipients - recipients = project.email_recipients.split(' ') - - if project.email_add_pusher? && push_data[:user_email].present? - recipients << push_data[:user_email] - end - - recipients.uniq - end - def stage - return unless config_processor - stages = builds_without_retry.select(&:active?).map(&:stage) - config_processor.stages.find { |stage| stages.include? stage } + builds_without_retry.group(:stage_idx).select(:stage).last end - def create_builds_for_stage(stage, trigger_request) + def create_builds(ref, tag, push_data, trigger_request = nil) return if skip_ci? && trigger_request.blank? return unless config_processor - - builds_attrs = config_processor.builds_for_stage_and_ref(stage, ref, tag) - builds_attrs.map do |build_attrs| - builds.create!({ - name: build_attrs[:name], - commands: build_attrs[:script], - tag_list: build_attrs[:tags], - options: build_attrs[:options], - allow_failure: build_attrs[:allow_failure], - stage: build_attrs[:stage], - trigger_request: trigger_request, - }) - end + CreateBuildsService.new.execute(self, config_processor, ref, tag, push_data, trigger_request) end - def create_next_builds(trigger_request) - return if skip_ci? && trigger_request.blank? - return unless config_processor - - stages = builds.where(trigger_request: trigger_request).group_by(&:stage) - - config_processor.stages.any? do |stage| - !stages.include?(stage) && create_builds_for_stage(stage, trigger_request).present? - end + def refs + builds.group(:ref).pluck(:ref) end - def create_builds(trigger_request = nil) - return if skip_ci? && trigger_request.blank? - return unless config_processor - - config_processor.stages.any? do |stage| - create_builds_for_stage(stage, trigger_request).present? - end + def last_ref + builds.latest.first.try(:ref) end def builds_without_retry - @builds_without_retry ||= - begin - grouped_builds = builds.group_by(&:name) - grouped_builds.map do |name, builds| - builds.sort_by(&:id).last - end - end - end - - def builds_without_retry_sorted - return builds_without_retry unless config_processor - - stages = config_processor.stages - builds_without_retry.sort_by do |build| - [stages.index(build.stage) || -1, build.name || ""] - end + builds.latest end def retried_builds @@ -180,35 +125,32 @@ module Ci return 'failed' elsif builds.none? return 'skipped' - elsif success? - 'success' - elsif pending? - 'pending' - elsif running? - 'running' - elsif canceled? - 'canceled' + end + + statuses = builds_without_retry.ignore_failures.pluck(:status) + if statuses.all? { |status| status == 'success' } + return 'success' + elsif statuses.all? { |status| status == 'pending' } + return 'pending' + elsif statuses.include?('running') || statuses.include?('pending') + return 'running' + elsif statuses.all? { |status| status == 'canceled' } + return 'canceled' else - 'failed' + return 'failed' end end def pending? - builds_without_retry.all? do |build| - build.pending? - end + status == 'pending' end def running? - builds_without_retry.any? do |build| - build.running? || build.pending? - end + status == 'running' end def success? - builds_without_retry.all? do |build| - build.success? || build.ignored? - end + status == 'success' end def failed? @@ -216,15 +158,17 @@ module Ci end def canceled? - builds_without_retry.all? do |build| - build.canceled? - end + status == 'canceled' end def duration @duration ||= builds_without_retry.select(&:duration).sum(&:duration).to_i end + def duration_for_ref(ref) + builds_without_retry.for_ref(ref).select(&:duration).sum(&:duration).to_i + end + def finished_at @finished_at ||= builds.order('finished_at DESC').first.try(:finished_at) end @@ -238,12 +182,12 @@ module Ci end end - def matrix? - builds_without_retry.size > 1 + def matrix_for_ref?(ref) + builds_without_retry.for_ref(ref).pluck(:id).size > 1 end def config_processor - @config_processor ||= Ci::GitlabCiYamlProcessor.new(push_data[:ci_yaml_file]) + @config_processor ||= Ci::GitlabCiYamlProcessor.new(ci_yaml_file) rescue Ci::GitlabCiYamlProcessor::ValidationError => e save_yaml_error(e.message) nil @@ -253,10 +197,15 @@ module Ci nil end + def ci_yaml_file + gl_project.repository.blob_at(sha, '.gitlab-ci.yml') + rescue + nil + end + def skip_ci? return false if builds.any? - commits = push_data[:commits] - commits.present? && commits.last[:message] =~ /(\[ci skip\])/ + git_commit_message =~ /(\[ci skip\])/ if git_commit_message end def update_committed! diff --git a/app/models/ci/project_status.rb b/app/models/ci/project_status.rb index 6d5cafe81a2..b66f1212f23 100644 --- a/app/models/ci/project_status.rb +++ b/app/models/ci/project_status.rb @@ -28,18 +28,6 @@ module Ci status end - # only check for toggling build status within same ref. - def last_commit_changed_status? - ref = last_commit.ref - last_commits = commits.where(ref: ref).last(2) - - if last_commits.size < 2 - false - else - last_commits[0].status != last_commits[1].status - end - end - def last_commit_for_ref(ref) commits.where(ref: ref).last end diff --git a/app/models/project.rb b/app/models/project.rb index b90a82da9f2..bb47b9abb03 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -744,7 +744,11 @@ class Project < ActiveRecord::Base end def ci_commit(sha) - gitlab_ci_project.commits.find_by(sha: sha) if gitlab_ci? + ci_commits.find_by(sha: sha) + end + + def ensure_ci_commit(sha) + ci_commit(sha) || ci_commits.create(sha: sha) end def ensure_gitlab_ci_project diff --git a/app/models/project_services/ci/hip_chat_message.rb b/app/models/project_services/ci/hip_chat_message.rb index 25c72033eac..73fdbe801c0 100644 --- a/app/models/project_services/ci/hip_chat_message.rb +++ b/app/models/project_services/ci/hip_chat_message.rb @@ -13,7 +13,7 @@ module Ci lines.push("#{project.name} - ") if commit.matrix? - lines.push("Commit ##{commit.id}
") + lines.push("Commit ##{commit.id}
") else first_build = commit.builds_without_retry.first lines.push("Build '#{first_build.name}' ##{first_build.id}
") diff --git a/app/models/project_services/ci/mail_service.rb b/app/models/project_services/ci/mail_service.rb index 1bd2f33612b..11a2743f969 100644 --- a/app/models/project_services/ci/mail_service.rb +++ b/app/models/project_services/ci/mail_service.rb @@ -61,7 +61,7 @@ module Ci end def execute(build) - build.commit.project_recipients.each do |recipient| + build.project_recipients.each do |recipient| case build.status.to_sym when :success mailer.build_success_email(build.id, recipient) diff --git a/app/models/project_services/ci/slack_message.rb b/app/models/project_services/ci/slack_message.rb index 757b1961143..7d254836fb5 100644 --- a/app/models/project_services/ci/slack_message.rb +++ b/app/models/project_services/ci/slack_message.rb @@ -48,7 +48,7 @@ module Ci def attachment_message out = "<#{ci_project_url(project)}|#{project_name}>: " if commit.matrix? - out << "Commit <#{ci_project_ref_commits_url(project, commit.ref, commit.sha)}|\##{commit.id}> " + out << "Commit <#{ci_project_commits_url(project, commit.sha)}|\##{commit.id}> " else build = commit.builds_without_retry.first out << "Build <#{ci_project_build_url(project, build)}|\##{build.id}> " diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb index 6d2cf79b691..fd108516530 100644 --- a/app/models/project_services/gitlab_ci_service.rb +++ b/app/models/project_services/gitlab_ci_service.rb @@ -63,7 +63,7 @@ class GitlabCiService < CiService end def get_ci_commit(sha, ref) - Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha_and_ref!(sha, ref) + Ci::Project.find(project.gitlab_ci_project).commits.find_by_sha!(sha) end def commit_status(sha, ref) @@ -80,7 +80,7 @@ class GitlabCiService < CiService def build_page(sha, ref) if project.gitlab_ci_project.present? - ci_project_ref_commits_url(project.gitlab_ci_project, ref, sha) + ci_project_commits_url(project.gitlab_ci_project, sha) end end diff --git a/app/services/ci/create_builds_service.rb b/app/services/ci/create_builds_service.rb new file mode 100644 index 00000000000..e9c85410e5c --- /dev/null +++ b/app/services/ci/create_builds_service.rb @@ -0,0 +1,27 @@ +module Ci + class CreateBuildsService + def execute(commit, ref, tag, push_data, 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| + # don't create the same build twice + unless commit.builds.find_by_name_and_trigger_request(name: build_attrs[:name], ref: ref, tag: tag, trigger_request: trigger_request) + commit.builds.create!({ + name: build_attrs[:name], + commands: build_attrs[:script], + tag_list: build_attrs[:tags], + options: build_attrs[:options], + allow_failure: build_attrs[:allow_failure], + stage: build_attrs[:stage], + stage_idx: build_attrs[:stage_idx], + trigger_request: trigger_request, + ref: ref, + tag: tag, + push_data: push_data, + }) + end + end + end + end + end +end diff --git a/app/services/ci/create_commit_service.rb b/app/services/ci/create_commit_service.rb index 0a1abf89a95..9120a82edcd 100644 --- a/app/services/ci/create_commit_service.rb +++ b/app/services/ci/create_commit_service.rb @@ -16,33 +16,22 @@ module Ci return false end - commit = project.commits.find_by_sha_and_ref(sha, ref) - - # Create commit if not exists yet - unless commit - data = { - ref: ref, - sha: sha, - tag: origin_ref.start_with?('refs/tags/'), - before_sha: before_sha, - 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.commits.create(data) - 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 unless commit.builds.any? + commit.create_builds(ref, tag, push_data) commit end diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb index 9bad09f2f54..f13ed787ed2 100644 --- a/app/services/ci/create_trigger_request_service.rb +++ b/app/services/ci/create_trigger_request_service.rb @@ -1,15 +1,15 @@ module Ci class CreateTriggerRequestService def execute(project, trigger, ref, variables = nil) - commit = project.commits.where(ref: ref).last + commit = project.gl_project.commit(ref) return unless commit + ci_commit = project.gl_project.ensure_ci_commit(commit.sha) trigger_request = trigger.trigger_requests.create!( - commit: commit, variables: variables ) - if commit.create_builds(trigger_request) + if ci_commit.create_builds(ref, tag, nil, trigger_request) trigger_request end end diff --git a/app/views/ci/builds/_build.html.haml b/app/views/ci/builds/_build.html.haml index 515b862e992..8ccc0dff2fb 100644 --- a/app/views/ci/builds/_build.html.haml +++ b/app/views/ci/builds/_build.html.haml @@ -6,6 +6,10 @@ = link_to ci_project_build_path(build.project, build) do %strong Build ##{build.id} + - if defined?(ref) + %td + = build.ref + %td = build.stage diff --git a/app/views/ci/builds/show.html.haml b/app/views/ci/builds/show.html.haml index 839dbf5c554..f941143e2e0 100644 --- a/app/views/ci/builds/show.html.haml +++ b/app/views/ci/builds/show.html.haml @@ -1,7 +1,7 @@ #up-build-trace -- if @commit.matrix? +- if @commit.matrix_for_ref?(@build.ref) %ul.center-top-menu - - @commit.builds_without_retry_sorted.each do |build| + - @commit.builds_without_retry.for_ref(build.ref).each do |build| %li{class: ('active' if build == @build) } = link_to ci_project_build_url(@project, build) do = ci_icon_for_status(build.status) @@ -12,7 +12,7 @@ = build.id - - unless @commit.builds_without_retry.include?(@build) + - unless @commit.builds_without_retry.for_ref(@build.ref).include?(@build) %li.active %a Build ##{@build.id} diff --git a/app/views/ci/commits/_commit.html.haml b/app/views/ci/commits/_commit.html.haml index 1eacfca944f..f8a1fa50851 100644 --- a/app/views/ci/commits/_commit.html.haml +++ b/app/views/ci/commits/_commit.html.haml @@ -7,7 +7,7 @@ %td.build-link - = link_to ci_project_ref_commits_path(commit.project, commit.ref, commit.sha) do + = link_to ci_project_commits_path(commit.project, commit.sha) do %strong #{commit.short_sha} %td.build-message @@ -16,7 +16,7 @@ %td.build-branch - unless @ref %span - = link_to truncate(commit.ref, length: 25), ci_project_path(@project, ref: commit.ref) + = link_to truncate(commit.last_ref, length: 25), ci_project_path(@project, ref: commit.last_ref) %td.duration - if commit.duration > 0 diff --git a/app/views/ci/commits/show.html.haml b/app/views/ci/commits/show.html.haml index 8f38aa84676..4badb671287 100644 --- a/app/views/ci/commits/show.html.haml +++ b/app/views/ci/commits/show.html.haml @@ -17,14 +17,11 @@ %p %span.attr-name Commit: #{gitlab_commit_link(@project, @commit.sha)} - - %p - %span.attr-name Branch: - #{gitlab_ref_link(@project, @commit.ref)} .col-sm-6 - %p - %span.attr-name Author: - #{@commit.git_author_name} (#{@commit.git_author_email}) + - if @commit.git_author_name || @commit.git_author_email + %p + %span.attr-name Author: + #{@commit.git_author_name} (#{@commit.git_author_email}) - if @commit.created_at %p %span.attr-name Created at: @@ -33,7 +30,7 @@ - if current_user && can?(current_user, :manage_builds, gl_project) .pull-right - if @commit.builds.running_or_pending.any? - = link_to "Cancel", cancel_ci_project_ref_commits_path(@project, @commit.ref, @commit.sha), class: 'btn btn-sm btn-danger' + = link_to "Cancel", cancel_ci_project_commits_path(@project, @commit), class: 'btn btn-sm btn-danger' - if @commit.yaml_errors.present? @@ -43,30 +40,31 @@ - @commit.yaml_errors.split(",").each do |error| %li= error -- unless @commit.push_data[:ci_yaml_file] +- unless @commit.ci_yaml_file .bs-callout.bs-callout-warning \.gitlab-ci.yml not found in this commit -%h3 - Builds - - if @commit.duration > 0 - %small.pull-right - %i.fa.fa-time - #{time_interval_in_words @commit.duration} +- @commit.refs.each do |ref| + %h3 + Builds for #{ref} + - if @commit.duration_for_ref(ref) > 0 + %small.pull-right + %i.fa.fa-time + #{time_interval_in_words @commit.duration_for_ref(ref)} -%table.table.builds - %thead - %tr - %th Status - %th Build ID - %th Stage - %th Name - %th Duration - %th Finished at - - if @project.coverage_enabled? - %th Coverage - %th - = render @commit.builds_without_retry_sorted, controls: true + %table.table.builds + %thead + %tr + %th Status + %th Build ID + %th Stage + %th Name + %th Duration + %th Finished at + - if @project.coverage_enabled? + %th Coverage + %th + = render @commit.builds_without_retry.for_ref(ref), controls: true - if @commit.retried_builds.any? %h3 @@ -77,6 +75,7 @@ %tr %th Status %th Build ID + %th Ref %th Stage %th Name %th Duration @@ -84,4 +83,4 @@ - if @project.coverage_enabled? %th Coverage %th - = render @commit.retried_builds + = render @commit.retried_builds, ref: true diff --git a/app/views/ci/notify/build_fail_email.html.haml b/app/views/ci/notify/build_fail_email.html.haml index d818e8b6756..4ebdfa1b6c0 100644 --- a/app/views/ci/notify/build_fail_email.html.haml +++ b/app/views/ci/notify/build_fail_email.html.haml @@ -11,7 +11,7 @@ %p Author: #{@build.commit.git_author_name} %p - Branch: #{@build.commit.ref} + Branch: #{@build.ref} %p Message: #{@build.commit.git_commit_message} diff --git a/app/views/ci/notify/build_fail_email.text.erb b/app/views/ci/notify/build_fail_email.text.erb index 1add215a1c8..177827f9a3c 100644 --- a/app/views/ci/notify/build_fail_email.text.erb +++ b/app/views/ci/notify/build_fail_email.text.erb @@ -3,7 +3,7 @@ Build failed for <%= @project.name %> Status: <%= @build.status %> Commit: <%= @build.commit.short_sha %> Author: <%= @build.commit.git_author_name %> -Branch: <%= @build.commit.ref %> +Branch: <%= @build.ref %> Message: <%= @build.commit.git_commit_message %> Url: <%= ci_project_build_url(@build.project, @build) %> diff --git a/app/views/ci/notify/build_success_email.html.haml b/app/views/ci/notify/build_success_email.html.haml index a20dcaee24e..7cc43300e88 100644 --- a/app/views/ci/notify/build_success_email.html.haml +++ b/app/views/ci/notify/build_success_email.html.haml @@ -12,7 +12,7 @@ %p Author: #{@build.commit.git_author_name} %p - Branch: #{@build.commit.ref} + Branch: #{@build.ref} %p Message: #{@build.commit.git_commit_message} diff --git a/app/views/ci/notify/build_success_email.text.erb b/app/views/ci/notify/build_success_email.text.erb index 7ebd17e7270..4d55c39b0fb 100644 --- a/app/views/ci/notify/build_success_email.text.erb +++ b/app/views/ci/notify/build_success_email.text.erb @@ -3,7 +3,7 @@ Build successful for <%= @project.name %> Status: <%= @build.status %> Commit: <%= @build.commit.short_sha %> Author: <%= @build.commit.git_author_name %> -Branch: <%= @build.commit.ref %> +Branch: <%= @build.ref %> Message: <%= @build.commit.git_commit_message %> Url: <%= ci_project_build_url(@build.project, @build) %> -- cgit v1.2.1 From e3d870d7fc282a1f0a1028996c8b44e5d32b9cbf Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 10:14:33 +0200 Subject: Add user to Ci::Build to have pusher email address --- app/models/ci/build.rb | 6 +++--- app/models/ci/commit.rb | 4 ++-- app/models/project_services/gitlab_ci_service.rb | 2 +- app/models/user.rb | 1 + app/services/ci/create_builds_service.rb | 4 ++-- app/services/ci/create_commit_service.rb | 16 ++-------------- 6 files changed, 11 insertions(+), 22 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index bf3e8915205..bfdc1c7486e 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -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 diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 35134b6628e..3c577e3f081 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -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 diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb index fd108516530..8e2b395494e 100644 --- a/app/models/project_services/gitlab_ci_service.rb +++ b/app/models/project_services/gitlab_ci_service.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 1069f8e3664..c7e3992b6a1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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' # diff --git a/app/services/ci/create_builds_service.rb b/app/services/ci/create_builds_service.rb index e9c85410e5c..77a4305071c 100644 --- a/app/services/ci/create_builds_service.rb +++ b/app/services/ci/create_builds_service.rb @@ -1,6 +1,6 @@ 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 diff --git a/app/services/ci/create_commit_service.rb b/app/services/ci/create_commit_service.rb index 9120a82edcd..edbb07580c9 100644 --- a/app/services/ci/create_commit_service.rb +++ b/app/services/ci/create_commit_service.rb @@ -1,6 +1,6 @@ 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 -- cgit v1.2.1 From 317a7469545d0e9a70e54a87a540b8aabe4c418b Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 12:02:26 +0200 Subject: Make commit_spec run --- app/models/ci/build.rb | 4 +- app/models/ci/commit.rb | 75 +++++++++++++++++++------------- app/services/ci/create_builds_service.rb | 39 ++++++++--------- app/services/ci/web_hook_service.rb | 2 - app/views/ci/builds/show.html.haml | 5 --- app/views/ci/commits/show.html.haml | 11 ++--- 6 files changed, 69 insertions(+), 67 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index bfdc1c7486e..79f040b8954 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -80,6 +80,8 @@ module Ci def retry(build) new_build = Ci::Build.new(status: :pending) + new_build.ref = build.ref + new_build.tag = build.tag new_build.options = build.options new_build.commands = build.commands new_build.tag_list = build.tag_list @@ -141,7 +143,7 @@ module Ci state :canceled, value: 'canceled' end - delegate :sha, :short_sha, :before_sha, :ref, :project, + delegate :sha, :short_sha, :project, to: :commit, prefix: false def trace_html diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 3c577e3f081..59d4932d434 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -58,14 +58,6 @@ module Ci end end - def new_branch? - before_sha == Ci::Git::BLANK_SHA - end - - def compare? - !new_branch? - end - def git_author_name commit_data.author_name if commit_data end @@ -78,10 +70,6 @@ module Ci commit_data.message if commit_data end - def short_before_sha - Ci::Commit.truncate_sha(before_sha) - end - def short_sha Ci::Commit.truncate_sha(sha) end @@ -99,7 +87,22 @@ module Ci 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, user, trigger_request) + config_processor.stages.any? do |stage| + CreateBuildsService.new.execute(self, stage, ref, tag, user, trigger_request).present? + end + end + + def create_next_builds(ref, tag, user, trigger_request) + return if skip_ci? && trigger_request.blank? + return unless config_processor + + stages = builds.where(ref: ref, tag: tag, trigger_request: trigger_request).group_by(&:stage) + + config_processor.stages.any? do |stage| + unless stages.include?(stage) + CreateBuildsService.new.execute(self, stage, ref, tag, user, trigger_request).present? + end + end end def refs @@ -111,7 +114,14 @@ module Ci end def builds_without_retry - builds.latest + @builds_without_retry ||= + begin + grouped_builds = builds.group_by(&:name) + latest_builds = grouped_builds.map do |name, builds| + builds.sort_by(&:id).last + end + latest_builds.sort_by(&:stage_idx) + end end def retried_builds @@ -125,32 +135,35 @@ module Ci return 'failed' elsif builds.none? return 'skipped' - end - - statuses = builds_without_retry.ignore_failures.pluck(:status) - if statuses.all? { |status| status == 'success' } - return 'success' - elsif statuses.all? { |status| status == 'pending' } - return 'pending' - elsif statuses.include?('running') || statuses.include?('pending') - return 'running' - elsif statuses.all? { |status| status == 'canceled' } - return 'canceled' + elsif success? + 'success' + elsif pending? + 'pending' + elsif running? + 'running' + elsif canceled? + 'canceled' else - return 'failed' + 'failed' end end def pending? - status == 'pending' + builds_without_retry.all? do |build| + build.pending? + end end def running? - status == 'running' + builds_without_retry.any? do |build| + build.running? || build.pending? + end end def success? - status == 'success' + builds_without_retry.all? do |build| + build.success? || build.ignored? + end end def failed? @@ -158,7 +171,9 @@ module Ci end def canceled? - status == 'canceled' + builds_without_retry.all? do |build| + build.canceled? + end end def duration diff --git a/app/services/ci/create_builds_service.rb b/app/services/ci/create_builds_service.rb index 77a4305071c..255fdc41103 100644 --- a/app/services/ci/create_builds_service.rb +++ b/app/services/ci/create_builds_service.rb @@ -1,26 +1,23 @@ module Ci class CreateBuildsService - 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| - # don't create the same build twice - unless commit.builds.find_by_name_and_trigger_request(name: build_attrs[:name], ref: ref, tag: tag, trigger_request: trigger_request) - commit.builds.create!({ - name: build_attrs[:name], - commands: build_attrs[:script], - tag_list: build_attrs[:tags], - options: build_attrs[:options], - allow_failure: build_attrs[:allow_failure], - stage: build_attrs[:stage], - stage_idx: build_attrs[:stage_idx], - trigger_request: trigger_request, - ref: ref, - tag: tag, - user: user, - }) - end - end + def execute(commit, stage, ref, tag, user, trigger_request) + builds_attrs = commit.config_processor.builds_for_stage_and_ref(stage, ref, tag) + + builds_attrs.map do |build_attrs| + build_attrs.slice!(:name, + :commands, + :tag_list, + :options, + :allow_failure, + :stage, + :stage_idx) + + build_attrs.merge!(ref: ref, + tag: tag, + trigger_request: trigger_request, + user: user) + + commit.builds.create!(build_attrs) end end end diff --git a/app/services/ci/web_hook_service.rb b/app/services/ci/web_hook_service.rb index 87984b20fa1..4bbca5c7da1 100644 --- a/app/services/ci/web_hook_service.rb +++ b/app/services/ci/web_hook_service.rb @@ -28,8 +28,6 @@ module Ci gitlab_url: project.gitlab_url, ref: build.ref, sha: build.sha, - before_sha: build.before_sha, - push_data: build.commit.push_data }) end end diff --git a/app/views/ci/builds/show.html.haml b/app/views/ci/builds/show.html.haml index f941143e2e0..e4ec190ada5 100644 --- a/app/views/ci/builds/show.html.haml +++ b/app/views/ci/builds/show.html.haml @@ -122,11 +122,6 @@ Commit .pull-right %small #{build_commit_link @build} - - - if @build.commit.compare? - %p - %span.attr-name Compare: - #{build_compare_link @build} %p %span.attr-name Branch: #{build_ref_link @build} diff --git a/app/views/ci/commits/show.html.haml b/app/views/ci/commits/show.html.haml index 4badb671287..69487320175 100644 --- a/app/views/ci/commits/show.html.haml +++ b/app/views/ci/commits/show.html.haml @@ -9,14 +9,9 @@ .gray-content-block.second-block .row .col-sm-6 - - if @commit.compare? - %p - %span.attr-name Compare: - #{gitlab_compare_link(@project, @commit.short_before_sha, @commit.short_sha)} - - else - %p - %span.attr-name Commit: - #{gitlab_commit_link(@project, @commit.sha)} + %p + %span.attr-name Commit: + #{gitlab_commit_link(@project, @commit.sha)} .col-sm-6 - if @commit.git_author_name || @commit.git_author_email %p -- cgit v1.2.1 From 361dc3641dd28c4ecefbda94f7a8dad299c349aa Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 12:38:00 +0200 Subject: Fix builds_without_retry --- app/models/ci/build.rb | 2 +- app/models/ci/commit.rb | 17 +++++++---------- app/views/ci/builds/show.html.haml | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 79f040b8954..30a8b5aa816 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -47,7 +47,7 @@ module Ci scope :failed, ->() { where(status: "failed") } scope :unstarted, ->() { where(runner_id: nil) } scope :running_or_pending, ->() { where(status:[:running, :pending]) } - scope :latest, ->() { group(:name).order(stage_idx: :asc, created_at: :desc) } + scope :latest, ->() { where(id: unscope(:select).select('max(id)').group(:name)).order(stage_idx: :asc) } scope :ignore_failures, ->() { where(allow_failure: false) } scope :for_ref, ->(ref) { where(ref: ref) } diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 59d4932d434..31da7e8f2b6 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -114,14 +114,11 @@ module Ci end def builds_without_retry - @builds_without_retry ||= - begin - grouped_builds = builds.group_by(&:name) - latest_builds = grouped_builds.map do |name, builds| - builds.sort_by(&:id).last - end - latest_builds.sort_by(&:stage_idx) - end + builds.latest + end + + def builds_without_retry_for_ref(ref) + builds.for_ref(ref).latest end def retried_builds @@ -181,7 +178,7 @@ module Ci end def duration_for_ref(ref) - builds_without_retry.for_ref(ref).select(&:duration).sum(&:duration).to_i + builds_without_retry_for_ref(ref).select(&:duration).sum(&:duration).to_i end def finished_at @@ -198,7 +195,7 @@ module Ci end def matrix_for_ref?(ref) - builds_without_retry.for_ref(ref).pluck(:id).size > 1 + builds_without_retry_for_ref(ref).pluck(:id).size > 1 end def config_processor diff --git a/app/views/ci/builds/show.html.haml b/app/views/ci/builds/show.html.haml index e4ec190ada5..c42d11bf05d 100644 --- a/app/views/ci/builds/show.html.haml +++ b/app/views/ci/builds/show.html.haml @@ -1,7 +1,7 @@ #up-build-trace - if @commit.matrix_for_ref?(@build.ref) %ul.center-top-menu - - @commit.builds_without_retry.for_ref(build.ref).each do |build| + - @commit.builds_without_retry_for_ref(build.ref).each do |build| %li{class: ('active' if build == @build) } = link_to ci_project_build_url(@project, build) do = ci_icon_for_status(build.status) @@ -12,7 +12,7 @@ = build.id - - unless @commit.builds_without_retry.for_ref(@build.ref).include?(@build) + - unless @commit.builds_without_retry_for_ref(@build.ref).include?(@build) %li.active %a Build ##{@build.id} -- cgit v1.2.1 From d2d2df0738f3cd8311963c34d90ebc8ce4081aa6 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 13:12:00 +0200 Subject: Fix next round of tests --- app/controllers/ci/commits_controller.rb | 2 +- app/helpers/builds_helper.rb | 4 ---- app/models/project_services/ci/hip_chat_message.rb | 9 +------- app/models/project_services/ci/slack_message.rb | 23 +++++++----------- app/models/project_services/gitlab_ci_service.rb | 2 +- app/services/ci/create_builds_service.rb | 27 ++++++++++++---------- app/services/ci/create_commit_service.rb | 3 +-- app/views/ci/commits/show.html.haml | 6 ++--- 8 files changed, 30 insertions(+), 46 deletions(-) (limited to 'app') diff --git a/app/controllers/ci/commits_controller.rb b/app/controllers/ci/commits_controller.rb index acf9189572c..887e92f84cf 100644 --- a/app/controllers/ci/commits_controller.rb +++ b/app/controllers/ci/commits_controller.rb @@ -13,7 +13,7 @@ module Ci end def status - commit = Ci::Project.find(params[:project_id]).commits.find_by_sha!(params[:id], params[:ref_id]) + commit = Ci::Project.find(params[:project_id]).commits.find_by_sha!(params[:id]) render json: commit.to_json(only: [:id, :sha], methods: [:status, :coverage]) rescue ActiveRecord::RecordNotFound render json: { status: "not_found" } diff --git a/app/helpers/builds_helper.rb b/app/helpers/builds_helper.rb index b6658e52d09..626f4e2f4c0 100644 --- a/app/helpers/builds_helper.rb +++ b/app/helpers/builds_helper.rb @@ -3,10 +3,6 @@ module BuildsHelper gitlab_ref_link build.project, build.ref end - def build_compare_link build - gitlab_compare_link build.project, build.commit.short_before_sha, build.short_sha - end - def build_commit_link build gitlab_commit_link build.project, build.short_sha end diff --git a/app/models/project_services/ci/hip_chat_message.rb b/app/models/project_services/ci/hip_chat_message.rb index 73fdbe801c0..0bf448d47f2 100644 --- a/app/models/project_services/ci/hip_chat_message.rb +++ b/app/models/project_services/ci/hip_chat_message.rb @@ -11,14 +11,7 @@ module Ci def to_s lines = Array.new lines.push("#{project.name} - ") - - if commit.matrix? - lines.push("Commit ##{commit.id}
") - else - first_build = commit.builds_without_retry.first - lines.push("Build '#{first_build.name}' ##{first_build.id}
") - end - + lines.push("Commit ##{commit.id}
") lines.push("#{commit.short_sha} #{commit.git_author_name} - #{commit.git_commit_message}
") lines.push("#{humanized_status(commit_status)} in #{commit.duration} second(s).") lines.join('') diff --git a/app/models/project_services/ci/slack_message.rb b/app/models/project_services/ci/slack_message.rb index 7d254836fb5..a89c01517b7 100644 --- a/app/models/project_services/ci/slack_message.rb +++ b/app/models/project_services/ci/slack_message.rb @@ -23,15 +23,13 @@ module Ci def attachments fields = [] - if commit.matrix? - commit.builds_without_retry.each do |build| - next if build.allow_failure? - next unless build.failed? - fields << { - title: build.name, - value: "Build <#{ci_project_build_url(project, build)}|\##{build.id}> failed in #{build.duration.to_i} second(s)." - } - end + commit.builds_without_retry.each do |build| + next if build.allow_failure? + next unless build.failed? + fields << { + title: build.name, + value: "Build <#{ci_project_build_url(project, build)}|\##{build.id}> failed in #{build.duration.to_i} second(s)." + } end [{ @@ -47,12 +45,7 @@ module Ci def attachment_message out = "<#{ci_project_url(project)}|#{project_name}>: " - if commit.matrix? - out << "Commit <#{ci_project_commits_url(project, commit.sha)}|\##{commit.id}> " - else - build = commit.builds_without_retry.first - out << "Build <#{ci_project_build_url(project, build)}|\##{build.id}> " - end + out << "Commit <#{ci_project_commits_url(project, commit.sha)}|\##{commit.id}> " out << "(<#{commit_sha_link}|#{commit.short_sha}>) " out << "of <#{commit_ref_link}|#{commit.ref}> " out << "by #{commit.git_author_name} " if commit.git_author_name diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb index 8e2b395494e..17d1ef71945 100644 --- a/app/models/project_services/gitlab_ci_service.rb +++ b/app/models/project_services/gitlab_ci_service.rb @@ -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, current_user) + Ci::CreateCommitService.new.execute(ci_project, current_user, data) end end diff --git a/app/services/ci/create_builds_service.rb b/app/services/ci/create_builds_service.rb index 255fdc41103..c420f3268fd 100644 --- a/app/services/ci/create_builds_service.rb +++ b/app/services/ci/create_builds_service.rb @@ -4,20 +4,23 @@ module Ci builds_attrs = commit.config_processor.builds_for_stage_and_ref(stage, ref, tag) builds_attrs.map do |build_attrs| - build_attrs.slice!(:name, - :commands, - :tag_list, - :options, - :allow_failure, - :stage, - :stage_idx) + # don't create the same build twice + unless commit.builds.find_by(ref: ref, tag: tag, trigger_request: trigger_request, name: build_attrs[:name]) + build_attrs.slice!(:name, + :commands, + :tag_list, + :options, + :allow_failure, + :stage, + :stage_idx) - build_attrs.merge!(ref: ref, - tag: tag, - trigger_request: trigger_request, - user: user) + build_attrs.merge!(ref: ref, + tag: tag, + trigger_request: trigger_request, + user: user) - commit.builds.create!(build_attrs) + commit.builds.create!(build_attrs) + end end end end diff --git a/app/services/ci/create_commit_service.rb b/app/services/ci/create_commit_service.rb index edbb07580c9..fc1ae5774d5 100644 --- a/app/services/ci/create_commit_service.rb +++ b/app/services/ci/create_commit_service.rb @@ -1,7 +1,6 @@ module Ci class CreateCommitService - def execute(project, params, user) - before_sha = params[:before] + def execute(project, user, params) sha = params[:checkout_sha] || params[:after] origin_ref = params[:ref] diff --git a/app/views/ci/commits/show.html.haml b/app/views/ci/commits/show.html.haml index 69487320175..7217671fe95 100644 --- a/app/views/ci/commits/show.html.haml +++ b/app/views/ci/commits/show.html.haml @@ -9,9 +9,9 @@ .gray-content-block.second-block .row .col-sm-6 - %p - %span.attr-name Commit: - #{gitlab_commit_link(@project, @commit.sha)} + %p + %span.attr-name Commit: + #{gitlab_commit_link(@project, @commit.sha)} .col-sm-6 - if @commit.git_author_name || @commit.git_author_email %p -- cgit v1.2.1 From 782c8f9aa0a32def807da126e9f07f278772b6a2 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 13:37:50 +0200 Subject: Fix triggers spec --- app/services/ci/create_trigger_request_service.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'app') diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb index f13ed787ed2..3597372528b 100644 --- a/app/services/ci/create_trigger_request_service.rb +++ b/app/services/ci/create_trigger_request_service.rb @@ -1,10 +1,14 @@ module Ci class CreateTriggerRequestService def execute(project, trigger, ref, variables = nil) - commit = project.gl_project.commit(ref) - return unless commit + target = project.gl_project.repository.rev_parse_target(ref) + return unless target - ci_commit = project.gl_project.ensure_ci_commit(commit.sha) + # check if ref is tag + sha = target.oid + tag = target.is_a?(Rugged::Tag) || target.is_a?(Rugged::Tag::Annotation) + + ci_commit = project.gl_project.ensure_ci_commit(sha) trigger_request = trigger.trigger_requests.create!( variables: variables ) -- cgit v1.2.1 From 5064c9038c1ae2fa6c48bc46c58f49c72ff1963a Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 13:51:28 +0200 Subject: Fix next bunch of tests --- app/models/ci/build.rb | 4 ++++ app/services/ci/create_trigger_request_service.rb | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 30a8b5aa816..89af83d8efc 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -146,6 +146,10 @@ module Ci delegate :sha, :short_sha, :project, to: :commit, prefix: false + def before_sha + Gitlab::Git::BLANK_SHA + end + def trace_html html = Ci::Ansi2html::convert(trace) if trace.present? html || '' diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb index 3597372528b..083cea77202 100644 --- a/app/services/ci/create_trigger_request_service.rb +++ b/app/services/ci/create_trigger_request_service.rb @@ -1,10 +1,11 @@ module Ci class CreateTriggerRequestService def execute(project, trigger, ref, variables = nil) - target = project.gl_project.repository.rev_parse_target(ref) - return unless target + return unless project.gl_project + return unless project.gl_project.repository # check if ref is tag + target = project.gl_project.repository.rev_parse_target(ref) sha = target.oid tag = target.is_a?(Rugged::Tag) || target.is_a?(Rugged::Tag::Annotation) @@ -16,6 +17,8 @@ module Ci if ci_commit.create_builds(ref, tag, nil, trigger_request) trigger_request end + rescue Rugged::OdbError + nil end end end -- cgit v1.2.1 From 0367dbf04392a200b5a2e0fcbab6269ff283fa54 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 14:15:15 +0200 Subject: Fix build pipelining --- app/models/ci/build.rb | 5 +++-- app/models/ci/commit.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 89af83d8efc..f35224916ed 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -50,6 +50,7 @@ module Ci scope :latest, ->() { where(id: unscope(:select).select('max(id)').group(:name)).order(stage_idx: :asc) } scope :ignore_failures, ->() { where(allow_failure: false) } scope :for_ref, ->(ref) { where(ref: ref) } + scope :similar, ->(build) { where(ref: build.ref, tag: build.tag, trigger_request_id: build.trigger_request_id) } acts_as_taggable @@ -125,8 +126,8 @@ module Ci Ci::WebHookService.new.build_end(build) end - if build.commit.success? - build.commit.create_next_builds(build.trigger_request) + if build.commit.should_create_next_builds?(build) + build.commit.create_next_builds(build.ref, build.tag, build.user, build.trigger_request) end project.execute_services(build) diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index 31da7e8f2b6..c77921979a6 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -224,6 +224,16 @@ module Ci update!(committed_at: DateTime.now) end + def should_create_next_builds?(build) + # don't create other builds if this one is retried + other_builds = builds.similar(build).latest + return false unless other_builds.include?(build) + + other_builds.all? do |build| + build.success? || build.ignored? + end + end + private def save_yaml_error(error) -- cgit v1.2.1 From f42078f7c14b3a8a32f486ab0e3ec7ef791fc097 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 14:31:51 +0200 Subject: Fix rest of tests --- app/services/ci/create_trigger_request_service.rb | 13 +++++-------- app/services/ci/web_hook_service.rb | 1 + 2 files changed, 6 insertions(+), 8 deletions(-) (limited to 'app') diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb index 083cea77202..ea82dbb2bf4 100644 --- a/app/services/ci/create_trigger_request_service.rb +++ b/app/services/ci/create_trigger_request_service.rb @@ -1,15 +1,14 @@ module Ci class CreateTriggerRequestService def execute(project, trigger, ref, variables = nil) - return unless project.gl_project - return unless project.gl_project.repository + commit = project.gl_project.commit(ref) + return unless commit # check if ref is tag - target = project.gl_project.repository.rev_parse_target(ref) - sha = target.oid - tag = target.is_a?(Rugged::Tag) || target.is_a?(Rugged::Tag::Annotation) + tag = project.gl_project.repository.find_tag(ref).present? + + ci_commit = project.gl_project.ensure_ci_commit(commit.sha) - ci_commit = project.gl_project.ensure_ci_commit(sha) trigger_request = trigger.trigger_requests.create!( variables: variables ) @@ -17,8 +16,6 @@ module Ci if ci_commit.create_builds(ref, tag, nil, trigger_request) trigger_request end - rescue Rugged::OdbError - nil end end end diff --git a/app/services/ci/web_hook_service.rb b/app/services/ci/web_hook_service.rb index 4bbca5c7da1..92e6df442b4 100644 --- a/app/services/ci/web_hook_service.rb +++ b/app/services/ci/web_hook_service.rb @@ -27,6 +27,7 @@ module Ci project_name: project.name, gitlab_url: project.gitlab_url, ref: build.ref, + before_sha: build.before_sha, sha: build.sha, }) end -- cgit v1.2.1 From c9853897229ca5585c69c4675cbeefd9ca53147d Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 15:59:31 +0200 Subject: Add stage tests --- app/models/ci/commit.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb index c77921979a6..46370034f9a 100644 --- a/app/models/ci/commit.rb +++ b/app/models/ci/commit.rb @@ -81,7 +81,8 @@ module Ci end def stage - builds_without_retry.group(:stage_idx).select(:stage).last + running_or_pending = builds_without_retry.running_or_pending + running_or_pending.limit(1).pluck(:stage).first end def create_builds(ref, tag, user, trigger_request = nil) -- cgit v1.2.1 From 29a7c6796e75d3a36c613622c7c7507646fd93a0 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 16:06:35 +0200 Subject: Fix GitLabCiService and remove ci_yaml_file from CI push data --- app/models/project_services/gitlab_ci_service.rb | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) (limited to 'app') diff --git a/app/models/project_services/gitlab_ci_service.rb b/app/models/project_services/gitlab_ci_service.rb index 17d1ef71945..b63a75cf3af 100644 --- a/app/models/project_services/gitlab_ci_service.rb +++ b/app/models/project_services/gitlab_ci_service.rb @@ -40,18 +40,9 @@ class GitlabCiService < CiService def execute(data) return unless supported_events.include?(data[:object_kind]) - sha = data[:checkout_sha] - - if sha.present? - file = ci_yaml_file(sha) - - if file && file.data - data.merge!(ci_yaml_file: file.data) - end - end - - ci_project = Ci::Project.find_by(gitlab_id: project.id) + ci_project = project.gitlab_ci_project if ci_project + current_user = User.find_by(id: data[:user_id]) Ci::CreateCommitService.new.execute(ci_project, current_user, data) end end @@ -99,14 +90,4 @@ class GitlabCiService < CiService def fields [] end - - private - - def ci_yaml_file(sha) - repository.blob_at(sha, '.gitlab-ci.yml') - end - - def repository - project.repository - end end -- cgit v1.2.1 From 97a11136d3e7d7ed57c7571d296d7b50617dce16 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Mon, 5 Oct 2015 16:21:18 +0200 Subject: Fix create_trigger_request_service_spec --- app/services/ci/create_trigger_request_service.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/services/ci/create_trigger_request_service.rb b/app/services/ci/create_trigger_request_service.rb index ea82dbb2bf4..4b86cb0a1f5 100644 --- a/app/services/ci/create_trigger_request_service.rb +++ b/app/services/ci/create_trigger_request_service.rb @@ -10,7 +10,8 @@ module Ci ci_commit = project.gl_project.ensure_ci_commit(commit.sha) trigger_request = trigger.trigger_requests.create!( - variables: variables + variables: variables, + commit: ci_commit, ) if ci_commit.create_builds(ref, tag, nil, trigger_request) -- cgit v1.2.1