diff options
author | Valery Sizov <vsv2711@gmail.com> | 2015-05-28 14:29:21 +0300 |
---|---|---|
committer | Valery Sizov <vsv2711@gmail.com> | 2015-06-03 16:35:57 +0300 |
commit | 17a8fe8a9011cfde0a9dbe70d726c769819a9dfd (patch) | |
tree | 10b048c8c2dab844f82779a2a6efdaf3811bb9f0 | |
parent | 54bd76399ddf5fb925df01d9f86ed020c475b3ba (diff) | |
download | gitlab-ci-17a8fe8a9011cfde0a9dbe70d726c769819a9dfd.tar.gz |
proof of concept yml configuration
-rw-r--r-- | app/models/build.rb | 10 | ||||
-rw-r--r-- | app/models/commit.rb | 45 | ||||
-rw-r--r-- | app/models/job.rb | 12 | ||||
-rw-r--r-- | app/models/project_services/hip_chat_message.rb | 2 | ||||
-rw-r--r-- | app/models/project_services/slack_message.rb | 2 | ||||
-rw-r--r-- | app/services/create_commit_service.rb | 9 | ||||
-rw-r--r-- | app/services/web_hook_service.rb | 2 | ||||
-rw-r--r-- | app/views/builds/_build.html.haml | 2 | ||||
-rw-r--r-- | app/views/builds/show.html.haml | 8 | ||||
-rw-r--r-- | db/migrate/20150528011001_add_fields_to_builds.rb | 6 | ||||
-rw-r--r-- | db/schema.rb | 5 | ||||
-rw-r--r-- | lib/gitlab_ci_yaml_parser.rb | 31 | ||||
-rw-r--r-- | spec/models/commit_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/project_services/slack_message_spec.rb | 2 |
14 files changed, 78 insertions, 60 deletions
diff --git a/app/models/build.rb b/app/models/build.rb index c9f1230..e03cd21 100644 --- a/app/models/build.rb +++ b/app/models/build.rb @@ -109,8 +109,8 @@ class Build < ActiveRecord::Base WebHookService.new.build_end(build) end - if build.commit.success? && !(build.job && build.job.deploy?) - build.commit.create_deploy_builds(build.ref) + if build.commit.success? && !build.deploy? + build.commit.create_deploy_builds end project.execute_services(build) @@ -207,12 +207,6 @@ class Build < ActiveRecord::Base end end - def job_name - if job - job.name - end - end - def for_tag? if job && job.build_tags true diff --git a/app/models/commit.rb b/app/models/commit.rb index 6c876bd..4657564 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -93,30 +93,15 @@ class Commit < ActiveRecord::Base end def create_builds - project.jobs.where(build_branches: true).active.parallel.map do |job| - create_build_from_job(job) + builds_config.builds.each do |build_attrs| + builds.create!({project: project}.merge(build_attrs)) end end - def create_builds_for_tag(ref = '') - project.jobs.where(build_tags: true).active.parallel.map do |job| - create_build_from_job(job, ref) - end - end - - def create_build_from_job(job, ref = '') - build = builds.new(commands: job.commands) - build.tag_list = job.tag_list - build.project_id = project_id - build.job = job - build.save - build - end - def builds_without_retry @builds_without_retry ||= begin - grouped_builds = builds.group_by(&:job) + grouped_builds = builds.group_by(&:name) grouped_builds.map do |job, builds| builds.sort_by(&:id).last end @@ -127,14 +112,24 @@ class Commit < ActiveRecord::Base @retried_builds ||= (builds - builds_without_retry) end - def create_deploy_builds(ref) - project.jobs.deploy.active.each do |job| - if job.run_for_ref?(ref) - create_build_from_job(job) - end + def create_deploy_builds + builds_config.deploy_builds.each do |build_attrs| + refs = build_attrs.delete(:refs) + next unless refs.empty? || refs_matches?(refs, ref) + builds.create!({project: project}.merge(build_attrs)) end end + # refs - list of refs. Glob syntax is supported. Ex. ["feature*", "bug"] + # ref - ref that should be checked + def refs_matches?(refs, ref) + refs.map(&:strip).each do |ref_pattern| + return true if File.fnmatch(ref_pattern, ref) + end + + false + end + def status if success? 'success' @@ -194,4 +189,8 @@ class Commit < ActiveRecord::Base def matrix? builds_without_retry.size > 1 end + + def builds_config + @builds_config ||= GitlabCiYamlParser.new(push_data[:ci_yaml_file]) + end end diff --git a/app/models/job.rb b/app/models/job.rb index 110c96d..b036d78 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -34,16 +34,4 @@ class Job < ActiveRecord::Base def deploy? job_type == "deploy" end - - def run_for_ref?(ref) - if refs.present? - refs.split(",").map(&:strip).each do |refs_val| - return true if File.fnmatch(refs_val, ref) - end - - false - else - true - end - end end diff --git a/app/models/project_services/hip_chat_message.rb b/app/models/project_services/hip_chat_message.rb index a5e4aee..3ceed94 100644 --- a/app/models/project_services/hip_chat_message.rb +++ b/app/models/project_services/hip_chat_message.rb @@ -12,7 +12,7 @@ class HipChatMessage lines.push("<a href=\"#{RoutesHelper.project_ref_commit_url(project, commit.ref, commit.sha)}\">Commit ##{commit.id}</a></br>") else first_build = commit.builds_without_retry.first - lines.push("<a href=\"#{RoutesHelper.project_build_url(project, first_build)}\">Build '#{first_build.job_name}' ##{first_build.id}</a></br>") + lines.push("<a href=\"#{RoutesHelper.project_build_url(project, first_build)}\">Build '#{first_build.name}' ##{first_build.id}</a></br>") end lines.push("#{commit.short_sha} #{commit.git_author_name} - #{commit.git_commit_message}</br>") lines.push("#{humanized_status(commit_status)} in #{commit.duration} second(s).") diff --git a/app/models/project_services/slack_message.rb b/app/models/project_services/slack_message.rb index c95499e..15d6ee3 100644 --- a/app/models/project_services/slack_message.rb +++ b/app/models/project_services/slack_message.rb @@ -24,7 +24,7 @@ class SlackMessage commit.builds_without_retry.each do |build| next unless build.failed? fields << { - title: build.job_name, + title: build.name, value: "Build <#{RoutesHelper.project_build_url(project, build)}|\##{build.id}> failed in #{build.duration.to_i} second(s)." } end diff --git a/app/services/create_commit_service.rb b/app/services/create_commit_service.rb index 646b7c5..efe70c9 100644 --- a/app/services/create_commit_service.rb +++ b/app/services/create_commit_service.rb @@ -43,18 +43,15 @@ class CreateCommitService user_email: params[:user_email], repository: params[:repository], commits: params[:commits], - total_commits_count: params[:total_commits_count] + total_commits_count: params[:total_commits_count], + ci_yaml_file: params[:ci_yaml_file] } } commit = project.commits.create(data) end - if origin_ref.start_with?('refs/tags/') - commit.create_builds_for_tag(ref) - else - commit.create_builds - end + commit.create_builds if commit.builds.empty? commit.create_deploy_builds(ref) diff --git a/app/services/web_hook_service.rb b/app/services/web_hook_service.rb index 0918be8..4de268a 100644 --- a/app/services/web_hook_service.rb +++ b/app/services/web_hook_service.rb @@ -18,7 +18,7 @@ class WebHookService data = {} data.merge!({ build_id: build.id, - build_name: build.job_name, + build_name: build.name, build_status: build.status, build_started_at: build.started_at, build_finished_at: build.finished_at, diff --git a/app/views/builds/_build.html.haml b/app/views/builds/_build.html.haml index 1c93576..3751c30 100644 --- a/app/views/builds/_build.html.haml +++ b/app/views/builds/_build.html.haml @@ -17,7 +17,7 @@ · #{build.short_sha} %td - = build.job_name + = build.name - if build.tags.any? .pull-right - build.tag_list.each do |tag| diff --git a/app/views/builds/show.html.haml b/app/views/builds/show.html.haml index afca8ce..cf633b1 100644 --- a/app/views/builds/show.html.haml +++ b/app/views/builds/show.html.haml @@ -21,9 +21,9 @@ %i{class: build_icon_css_class(build)} %span Build ##{build.id} - - if build.job_name + - if build.name · - = build.job_name + = build.name - unless @commit.builds_without_retry.include?(@build) %li.active @@ -150,8 +150,8 @@ = link_to build_url(build) do %span ##{build.id} %td - - if build.job_name - = build.job_name + - if build.name + = build.name %td.status= build.status diff --git a/db/migrate/20150528011001_add_fields_to_builds.rb b/db/migrate/20150528011001_add_fields_to_builds.rb new file mode 100644 index 0000000..394dd8b --- /dev/null +++ b/db/migrate/20150528011001_add_fields_to_builds.rb @@ -0,0 +1,6 @@ +class AddFieldsToBuilds < ActiveRecord::Migration + def change + add_column :builds, :name, :string + add_column :builds, :deploy, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e4f9cd..9876a2e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,8 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150602000240) do + +ActiveRecord::Schema.define(version: 20150528011001) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +30,8 @@ ActiveRecord::Schema.define(version: 20150602000240) do t.float "coverage" t.text "commands" t.integer "job_id" + t.string "name" + t.boolean "deploy", default: false end add_index "builds", ["commit_id"], name: "index_builds_on_commit_id", using: :btree diff --git a/lib/gitlab_ci_yaml_parser.rb b/lib/gitlab_ci_yaml_parser.rb new file mode 100644 index 0000000..5a63458 --- /dev/null +++ b/lib/gitlab_ci_yaml_parser.rb @@ -0,0 +1,31 @@ +# Prototype of parser + +class GitlabCiYamlParser + attr_reader :before_script, :jobs, :on_success + + def initialize(config) + @before_script = ["pwd"] + @jobs = [{script: "ls -la", runner: "", name: "Rspec"}] + @on_success = [script: "cap deploy production", refs: [], name: "Deploy"] + end + + def builds + @jobs.map do |job| + { + name: job[:name], + commands: "#{@before_script.join("\n")}\n#{job[:script]}" + } + end + end + + def deploy_builds + @on_success.map do |job| + { + name: job[:name], + commands: "#{@before_script.join("\n")}\n#{job[:script]}", + deploy: true, + refs: job[:refs] + } + end + end +end
\ No newline at end of file diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb index 75d5610..c4056e3 100644 --- a/spec/models/commit_spec.rb +++ b/spec/models/commit_spec.rb @@ -135,7 +135,7 @@ describe Commit do FactoryGirl.create :job, job_type: :deploy, project: project project.reload - commit.create_deploy_builds(commit.ref) + commit.create_deploy_builds commit.builds.reload commit.builds.size.should == 1 diff --git a/spec/models/project_services/slack_message_spec.rb b/spec/models/project_services/slack_message_spec.rb index 1fa2e31..83a6352 100644 --- a/spec/models/project_services/slack_message_spec.rb +++ b/spec/models/project_services/slack_message_spec.rb @@ -72,7 +72,7 @@ describe SlackMessage do subject.fallback.should include("\##{commit.id}") subject.fallback.should include('failed') subject.attachments.first[:fields].size.should == 1 - subject.attachments.first[:fields].first[:title].should == build2.job_name + subject.attachments.first[:fields].first[:title].should == build2.name subject.attachments.first[:fields].first[:value].should include("\##{build2.id}") end end |