From a04cae1fb0528b1daad569c1fc7c1e568e1df2b8 Mon Sep 17 00:00:00 2001 From: Tino Wehe Date: Wed, 1 Apr 2015 12:32:36 +0200 Subject: Jobs checking "run_for_ref" by RegExp now +- increased max length of refs in db +- changed ref-check to RegExp + added method to distinguish between RegExp given or normal string + added more tests +- changed "refs" description in FrontEnd --- app/models/job.rb | 23 ++++++++++++++++++++--- app/views/jobs/_deploy_job_edit.html.haml | 8 ++++++-- spec/models/job_spec.rb | 10 ++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/models/job.rb b/app/models/job.rb index 106e0d3..b14cc33 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -26,13 +26,30 @@ class Job < ActiveRecord::Base scope :parallel, ->(){ where(job_type: "parallel") } scope :deploy, ->(){ where(job_type: "deploy") } - validate :refs, length: { maximum: 100 } - + validate :refs, length: { maximum: 255 } + def deploy? job_type == "deploy" end def run_for_ref?(ref) - refs.blank? || refs.split(",").map{|ref| ref.strip}.include?(ref) + refs.blank? || refs_include_ref?(ref) + end + + def refs_include_ref?(ref) + includes = false + # extract the refs - split by "," + # is tricky because they can be in a regex too + refs.scan(/\w+|\/+.*?\/+/).map{|re| re.strip}.each do |re| + # is regexp or not + if re.start_with?("/") && re.end_with?("/") + includes = !ref.match(/#{re.delete("/")}/i).nil? + else + includes = ref == re + end + + break if includes == true + end + return includes end end diff --git a/app/views/jobs/_deploy_job_edit.html.haml b/app/views/jobs/_deploy_job_edit.html.haml index f04029a..3b802d4 100644 --- a/app/views/jobs/_deploy_job_edit.html.haml +++ b/app/views/jobs/_deploy_job_edit.html.haml @@ -27,10 +27,14 @@ .form-group = label_tag :refs, class: 'control-label' do Refs - .col-sm-10 - = job_form.text_field :refs, class: 'form-control', placeholder: "master, staging" + .col-sm-10 + = job_form.text_field :refs, class: 'form-control', placeholder: "master, staging, /testing-v.*/" .help-block Run only when the above git refs strings match the branch or tag that was pushed. + %br + You can use RegExp like "/stable-v.*/" to match a ref like "stable-v0.1.0". The RegExp is always case insensitive. + %br + NOTE: It's important to surround you regex with "/"! .form-group = f.label :commands, 'Script', class: 'control-label' diff --git a/spec/models/job_spec.rb b/spec/models/job_spec.rb index f80519d..643a23c 100644 --- a/spec/models/job_spec.rb +++ b/spec/models/job_spec.rb @@ -22,7 +22,7 @@ describe Job do it { should belong_to(:project) } it { should have_many(:builds) } - + describe "run_for_ref?" do it "allows run for any ref if refs params is empty" do job = FactoryGirl.create :job, project: project @@ -30,9 +30,15 @@ describe Job do end it "allows run for any ref in refs params" do - job = FactoryGirl.create :job, project: project, refs: "master, staging" + job = FactoryGirl.create :job, project: project, refs: "master, staging, /testing.*/, /^unstable$/, /unstable-v[0-9]{1,}.[0-9]{1,}.[0-9]{1,}/" job.run_for_ref?("master").should be_true job.run_for_ref?("staging").should be_true + job.run_for_ref?("staging-v0.1.0").should be_false + job.run_for_ref?("testing").should be_true + job.run_for_ref?("testing-v0.1.0").should be_true + job.run_for_ref?("unstable").should be_true + job.run_for_ref?("unstable-v0.1.0").should be_true + job.run_for_ref?("unstable-0.1.0").should be_false job.run_for_ref?("anything").should be_false end end -- cgit v1.2.1