From 0920041375e10187f54e38db9392d482ec76c5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ko=CC=88tte?= Date: Mon, 11 Jan 2016 14:14:00 +0100 Subject: [PATCH] further changes for new structure with only one cron per branch --- lib/travis/api/v3/models/branch.rb | 2 +- lib/travis/api/v3/queries/crons.rb | 2 +- lib/travis/api/v3/renderer/cron.rb | 2 +- lib/travis/api/v3/routes.rb | 4 ++-- lib/travis/api/v3/services/{crons => cron}/create.rb | 12 ++++-------- lib/travis/api/v3/services/cron/for_branch.rb | 8 ++++++++ 6 files changed, 17 insertions(+), 13 deletions(-) rename lib/travis/api/v3/services/{crons => cron}/create.rb (60%) create mode 100644 lib/travis/api/v3/services/cron/for_branch.rb diff --git a/lib/travis/api/v3/models/branch.rb b/lib/travis/api/v3/models/branch.rb index fa0d1682..147a8bfe 100644 --- a/lib/travis/api/v3/models/branch.rb +++ b/lib/travis/api/v3/models/branch.rb @@ -4,7 +4,7 @@ module Travis::API::V3 belongs_to :last_build, class_name: 'Travis::API::V3::Models::Build'.freeze has_many :builds, foreign_key: [:repository_id, :branch], primary_key: [:repository_id, :name], order: 'builds.id DESC'.freeze, conditions: { event_type: 'push' } has_many :commits, foreign_key: [:repository_id, :branch], primary_key: [:repository_id, :name], order: 'commits.id DESC'.freeze - has_one :crons, dependent: :delete + has_one :cron, dependent: :delete def default_branch name == repository.default_branch_name diff --git a/lib/travis/api/v3/queries/crons.rb b/lib/travis/api/v3/queries/crons.rb index 6ca4cfff..463336b3 100644 --- a/lib/travis/api/v3/queries/crons.rb +++ b/lib/travis/api/v3/queries/crons.rb @@ -2,7 +2,7 @@ module Travis::API::V3 class Queries::Crons < Query def find(branch) - branch.crons + branch.cron end end end diff --git a/lib/travis/api/v3/renderer/cron.rb b/lib/travis/api/v3/renderer/cron.rb index 4d337e74..fec14f5c 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, :repository, :branch, :hour, :mon, :tue, :wed, :thu, :fri, :sat, :sun, :disable_by_push) + representation(:standard, :id, :repository, :branch, :interval, :disable_by_build) def repository model.branch.repository diff --git a/lib/travis/api/v3/routes.rb b/lib/travis/api/v3/routes.rb index 5fdc8a89..d71f0e53 100644 --- a/lib/travis/api/v3/routes.rb +++ b/lib/travis/api/v3/routes.rb @@ -85,9 +85,9 @@ module Travis::API::V3 route '/branch/{branch.name}' get :find - resource :crons do + resource :cron do route '/cron' - get :find + get :for_branch post :create, '/create' end diff --git a/lib/travis/api/v3/services/crons/create.rb b/lib/travis/api/v3/services/cron/create.rb similarity index 60% rename from lib/travis/api/v3/services/crons/create.rb rename to lib/travis/api/v3/services/cron/create.rb index a8aa1de4..a9ae54a4 100644 --- a/lib/travis/api/v3/services/crons/create.rb +++ b/lib/travis/api/v3/services/cron/create.rb @@ -1,7 +1,7 @@ module Travis::API::V3 - class Services::Crons::Create < Service + class Services::Cron::Create < Service result_type :cron - params :type, :disable_by_build + params :interval, :disable_by_build def run! raise LoginRequired unless access_control.logged_in? or access_control.full_access? @@ -9,12 +9,8 @@ module Travis::API::V3 raise NotFound unless branch = find(:branch, repository) access_control.permissions(repository).create_cron! Models::Cron.create(branch: branch, - type: params["type"], - disable_by_build: value("disable_by_build")) - end - - def value s - params[s] ? params[s] : false + interval: params["interval"], + disable_by_build: params["disable_by_build"] ? params["disable_by_build"] : false) end end diff --git a/lib/travis/api/v3/services/cron/for_branch.rb b/lib/travis/api/v3/services/cron/for_branch.rb new file mode 100644 index 00000000..478e4b2c --- /dev/null +++ b/lib/travis/api/v3/services/cron/for_branch.rb @@ -0,0 +1,8 @@ +module Travis::API::V3 + class Services::Cron::ForBranch < Service + + def run! + Models::Cron.where(:branch_id => find(:branch, find(:repository))).first + end + end +end