From f356beaccfd59a4c7a54945d0b2efe91aed828f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ko=CC=88tte?= Date: Thu, 19 Nov 2015 13:39:13 +0100 Subject: [PATCH] add time and disable_by_push options for cron --- lib/travis/api/v3/renderer/cron.rb | 2 +- lib/travis/api/v3/services/crons/create.rb | 17 +++++++- spec/v3/services/cron/delete_spec.rb | 21 +++++++--- spec/v3/services/cron/find_spec.rb | 42 +++++++++++++------ spec/v3/services/crons/create_spec.rb | 26 ++++++++---- spec/v3/services/crons/find_spec.rb | 21 +++++++--- spec/v3/services/crons/for_repository_spec.rb | 21 +++++++--- 7 files changed, 109 insertions(+), 41 deletions(-) diff --git a/lib/travis/api/v3/renderer/cron.rb b/lib/travis/api/v3/renderer/cron.rb index 6a760fa9..4d337e74 100644 --- a/lib/travis/api/v3/renderer/cron.rb +++ b/lib/travis/api/v3/renderer/cron.rb @@ -3,7 +3,7 @@ require 'travis/api/v3/renderer/model_renderer' module Travis::API::V3 class Renderer::Cron < Renderer::ModelRenderer representation(:minimal, :id) - representation(:standard, :id, :branch, :repository) + representation(:standard, :id, :repository, :branch, :hour, :mon, :tue, :wed, :thu, :fri, :sat, :sun, :disable_by_push) def repository model.branch.repository diff --git a/lib/travis/api/v3/services/crons/create.rb b/lib/travis/api/v3/services/crons/create.rb index c7f6d229..b5fba683 100644 --- a/lib/travis/api/v3/services/crons/create.rb +++ b/lib/travis/api/v3/services/crons/create.rb @@ -1,15 +1,28 @@ module Travis::API::V3 class Services::Crons::Create < Service result_type :cron + params :mon, :tue, :wed, :thu, :fri, :sat, :sun, :disable_by_push, :hour def run! raise LoginRequired unless access_control.logged_in? or access_control.full_access? raise NotFound unless repository = find(:repository) raise NotFound unless branch = find(:branch, repository) access_control.permissions(repository).create_cron! + Models::Cron.create(branch: branch, + mon: value("mon"), + tue: value("tue"), + wed: value("wed"), + thu: value("thu"), + fri: value("fri"), + sat: value("sat"), + sun: value("sun"), + hour: params["hour"], + disable_by_push: value("disable_by_push")) + end - Models::Cron.create(branch: branch) - end + def value s + params[s] ? params[s] : false + end end end diff --git a/spec/v3/services/cron/delete_spec.rb b/spec/v3/services/cron/delete_spec.rb index 0613a0bb..f97c68bc 100644 --- a/spec/v3/services/cron/delete_spec.rb +++ b/spec/v3/services/cron/delete_spec.rb @@ -21,18 +21,27 @@ describe Travis::API::V3::Services::Cron::Delete do "read" => true, "delete" => true }, "id" => cron.id, - "branch" => { - "@type" => "branch", - "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", - "@representation" => "minimal", - "name" => branch.name }, "repository" => { "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", "@representation" => "minimal", "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal" } + "slug" => "svenfuchs/minimal" }, + "branch" => { + "@type" => "branch", + "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", + "@representation" => "minimal", + "name" => branch.name }, + "hour" => nil, + "mon" => false, + "tue" => false, + "wed" => false, + "thu" => false, + "fri" => false, + "sat" => false, + "sun" => false, + "disable_by_push" => true }} end diff --git a/spec/v3/services/cron/find_spec.rb b/spec/v3/services/cron/find_spec.rb index 3ee0af53..64c62461 100644 --- a/spec/v3/services/cron/find_spec.rb +++ b/spec/v3/services/cron/find_spec.rb @@ -17,18 +17,27 @@ describe Travis::API::V3::Services::Cron::Find do "read" => true, "delete" => false }, "id" => cron.id, - "branch" => { - "@type" => "branch", - "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", - "@representation" => "minimal", - "name" => branch.name }, "repository" => { "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", "@representation" => "minimal", "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal" } + "slug" => "svenfuchs/minimal" }, + "branch" => { + "@type" => "branch", + "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", + "@representation" => "minimal", + "name" => branch.name }, + "hour" => nil, + "mon" => false, + "tue" => false, + "wed" => false, + "thu" => false, + "fri" => false, + "sat" => false, + "sun" => false, + "disable_by_push" => true }} end @@ -72,18 +81,27 @@ describe Travis::API::V3::Services::Cron::Find do "read" => true, "delete" => false }, "id" => cron.id, - "branch" => { - "@type" => "branch", - "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", - "@representation" => "minimal", - "name" => branch.name }, "repository" => { "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", "@representation" => "minimal", "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal" } + "slug" => "svenfuchs/minimal" }, + "branch" => { + "@type" => "branch", + "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", + "@representation" => "minimal", + "name" => branch.name }, + "hour" => nil, + "mon" => false, + "tue" => false, + "wed" => false, + "thu" => false, + "fri" => false, + "sat" => false, + "sun" => false, + "disable_by_push" => true }} end diff --git a/spec/v3/services/crons/create_spec.rb b/spec/v3/services/crons/create_spec.rb index c4b710b5..79fbd15e 100644 --- a/spec/v3/services/crons/create_spec.rb +++ b/spec/v3/services/crons/create_spec.rb @@ -6,13 +6,14 @@ describe Travis::API::V3::Services::Crons::Create do let(:last_cron) {Travis::API::V3::Models::Cron.where(branch_id: branch.id).last} let(:current_cron) {Travis::API::V3::Models::Cron.where(branch_id: branch.id).last} let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) } - let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }} + let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}", "Content-Type" => "application/json" }} + let(:options) {{ "tue" => true, "sat" => true, "sun" => false, "disable_by_push" => true, "hour" => 12 }} let(:parsed_body) { JSON.load(body) } describe "creating a cron job" do before { last_cron } before { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, push: true) } - before { post("/v3/repo/#{repo.id}/branch/#{branch.name}/crons/create", {}, headers) } + before { post("/v3/repo/#{repo.id}/branch/#{branch.name}/crons/create", options, headers) } example { expect(current_cron == last_cron).to be_falsey } example { expect(last_response).to be_ok } example { expect(parsed_body).to be == { @@ -23,18 +24,27 @@ describe Travis::API::V3::Services::Crons::Create do "read" => true, "delete" => true }, "id" => current_cron.id, - "branch" => { - "@type" => "branch", - "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", - "@representation" => "minimal", - "name" => "#{branch.name}" }, "repository" => { "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", "@representation" => "minimal", "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal" } + "slug" => "svenfuchs/minimal" }, + "branch" => { + "@type" => "branch", + "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", + "@representation" => "minimal", + "name" => "#{branch.name}" }, + "hour" => 12, + "mon" => false, + "tue" => true, + "wed" => false, + "thu" => false, + "fri" => false, + "sat" => true, + "sun" => false, + "disable_by_push" => true }} end diff --git a/spec/v3/services/crons/find_spec.rb b/spec/v3/services/crons/find_spec.rb index 7a8b36ab..3738cece 100644 --- a/spec/v3/services/crons/find_spec.rb +++ b/spec/v3/services/crons/find_spec.rb @@ -39,18 +39,27 @@ describe Travis::API::V3::Services::Crons::Find do "read" => true, "delete" => false }, "id" => cron.id, - "branch" => { - "@type" => "branch", - "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", - "@representation" => "minimal", - "name" => "#{branch.name}" }, "repository" => { "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", "@representation" => "minimal", "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal" } + "slug" => "svenfuchs/minimal" }, + "branch" => { + "@type" => "branch", + "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", + "@representation" => "minimal", + "name" => "#{branch.name}" }, + "hour" => nil, + "mon" => false, + "tue" => false, + "wed" => false, + "thu" => false, + "fri" => false, + "sat" => false, + "sun" => false, + "disable_by_push" => true } ] }} diff --git a/spec/v3/services/crons/for_repository_spec.rb b/spec/v3/services/crons/for_repository_spec.rb index 1174c5ea..97b713d2 100644 --- a/spec/v3/services/crons/for_repository_spec.rb +++ b/spec/v3/services/crons/for_repository_spec.rb @@ -39,18 +39,27 @@ describe Travis::API::V3::Services::Crons::ForRepository do "read" => true, "delete" => false }, "id" => cron.id, - "branch" => { - "@type" => "branch", - "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", - "@representation" => "minimal", - "name" => "#{branch.name}" }, "repository" => { "@type" => "repository", "@href" => "/v3/repo/#{repo.id}", "@representation" => "minimal", "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal" } + "slug" => "svenfuchs/minimal" }, + "branch" => { + "@type" => "branch", + "@href" => "/v3/repo/#{repo.id}/branch/#{branch.name}", + "@representation" => "minimal", + "name" => "#{branch.name}" }, + "hour" => nil, + "mon" => false, + "tue" => false, + "wed" => false, + "thu" => false, + "fri" => false, + "sat" => false, + "sun" => false, + "disable_by_push" => true } ] }}