From 022089adc85104abca4d8ecaf18f5b7735150125 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 12:04:06 -1000 Subject: [PATCH 01/16] Implement bare-bones /jobs/:job_id/debug endpoint --- lib/travis/api/app/endpoint/jobs.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index 9bb2fbc9..d06489f3 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -124,6 +124,13 @@ class Travis::Api::App end end + post "/:job_id/debug" do + job = service(:find_job, params).run + cfg = job.config + cfg.merge! debug_data + job.save! + end + def archive_url(path) "https://s3.amazonaws.com/#{hostname('archive')}#{path}" end @@ -131,6 +138,16 @@ class Travis::Api::App def hostname(name) "#{name}#{'-staging' if Travis.env == 'staging'}.#{Travis.config.host.split('.')[-2, 2].join('.')}" end + + def debug_data + { + debug: { + stage: 'before_install', + previous_status: 'failed', + created_by: current_user.login + } + } + end end end end From 8490e9f9cea5ddd972ad494e803d82241e875923 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 12:26:19 -1000 Subject: [PATCH 02/16] Debug output --- lib/travis/api/app/endpoint/jobs.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index d06489f3..64b2a154 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -125,10 +125,13 @@ class Travis::Api::App end post "/:job_id/debug" do + Travis.logger.debug "Reached endpoint" job = service(:find_job, params).run + Travis.logger.debug "found job: #{job}" cfg = job.config cfg.merge! debug_data job.save! + status 200 end def archive_url(path) From 2196acb36d2eb927bc1f3b248b3c33eb4cc87824 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 12:40:51 -1000 Subject: [PATCH 03/16] Use V3 services for debug route --- lib/travis/api/app/endpoint/jobs.rb | 10 ---------- lib/travis/api/v3/permissions/job.rb | 4 ++++ lib/travis/api/v3/routes.rb | 1 + lib/travis/api/v3/services/job/debug.rb | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 lib/travis/api/v3/services/job/debug.rb diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index 64b2a154..cb01401b 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -124,16 +124,6 @@ class Travis::Api::App end end - post "/:job_id/debug" do - Travis.logger.debug "Reached endpoint" - job = service(:find_job, params).run - Travis.logger.debug "found job: #{job}" - cfg = job.config - cfg.merge! debug_data - job.save! - status 200 - end - def archive_url(path) "https://s3.amazonaws.com/#{hostname('archive')}#{path}" end diff --git a/lib/travis/api/v3/permissions/job.rb b/lib/travis/api/v3/permissions/job.rb index 3055257e..b2193a50 100644 --- a/lib/travis/api/v3/permissions/job.rb +++ b/lib/travis/api/v3/permissions/job.rb @@ -9,5 +9,9 @@ module Travis::API::V3 def restart? write? end + + def debug? + write? + end end end diff --git a/lib/travis/api/v3/routes.rb b/lib/travis/api/v3/routes.rb index 4243edb4..f70fbacd 100644 --- a/lib/travis/api/v3/routes.rb +++ b/lib/travis/api/v3/routes.rb @@ -34,6 +34,7 @@ module Travis::API::V3 post :cancel, '/cancel' post :restart, '/restart' + post :debug, '/debug' end resource :organization do diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb new file mode 100644 index 00000000..dd98a1bf --- /dev/null +++ b/lib/travis/api/v3/services/job/debug.rb @@ -0,0 +1,19 @@ +module Travis::API::V3 + class Services::Job::Debug < Service + + def run + raise LoginRequired unless access_control.logged_in? or access_control.full_access? + raise NotFound unless job = find(:job) + access_control.permissions(job).debug! + + Travis.logger.debug "Reached endpoint" + job = service(:find_job, params).run + Travis.logger.debug "found job: #{job}" + cfg = job.config + cfg.merge! debug_data + job.save! + + accepted(job: job, state_change: :created) + end + end +end From 1ee5421d3fd88612ee59a74e69480bc2bd0aeec4 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 12:44:17 -1000 Subject: [PATCH 04/16] Fix up Debug service --- lib/travis/api/v3/services/job/debug.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index dd98a1bf..ef6ff7fd 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -6,14 +6,20 @@ module Travis::API::V3 raise NotFound unless job = find(:job) access_control.permissions(job).debug! - Travis.logger.debug "Reached endpoint" - job = service(:find_job, params).run - Travis.logger.debug "found job: #{job}" - cfg = job.config - cfg.merge! debug_data + job.config.merge! debug_data job.save! accepted(job: job, state_change: :created) end + + def debug_data + { + debug: { + stage: 'before_install', + previous_status: 'failed', + created_by: access_control.user + } + } + end end end From e6d39c2a529df46b1c9324969866bc278f8254f1 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 14:24:09 -1000 Subject: [PATCH 05/16] Restart job via V3 query --- lib/travis/api/v3/services/job/debug.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index ef6ff7fd..3c4686cd 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -9,6 +9,7 @@ module Travis::API::V3 job.config.merge! debug_data job.save! + query.restart(access_control.user) accepted(job: job, state_change: :created) end @@ -17,7 +18,7 @@ module Travis::API::V3 debug: { stage: 'before_install', previous_status: 'failed', - created_by: access_control.user + created_by: access_control.user.login } } end From 80cb9455577fdec5bb9e343e84ded357b6feb6d5 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 14:27:57 -1000 Subject: [PATCH 06/16] Remove superfulous method --- lib/travis/api/app/endpoint/jobs.rb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index cb01401b..9bb2fbc9 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -131,16 +131,6 @@ class Travis::Api::App def hostname(name) "#{name}#{'-staging' if Travis.env == 'staging'}.#{Travis.config.host.split('.')[-2, 2].join('.')}" end - - def debug_data - { - debug: { - stage: 'before_install', - previous_status: 'failed', - created_by: current_user.login - } - } - end end end end From 89487c09cab282ef761e0d61ae5ee1802843f38a Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 14:42:34 -1000 Subject: [PATCH 07/16] Fix specs to account for new 'debug' permissions --- spec/v3/services/job/find_spec.rb | 6 +- spec/v3/services/jobs/find_spec.rb | 520 +++++++++++++++-------------- 2 files changed, 268 insertions(+), 258 deletions(-) diff --git a/spec/v3/services/job/find_spec.rb b/spec/v3/services/job/find_spec.rb index bf366386..cc2e6967 100644 --- a/spec/v3/services/job/find_spec.rb +++ b/spec/v3/services/job/find_spec.rb @@ -23,7 +23,8 @@ describe Travis::API::V3::Services::Job::Find do "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => job.id, "number" => job.number, "state" => job.state, @@ -93,7 +94,8 @@ describe Travis::API::V3::Services::Job::Find do "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => job.id, "number" => job.number, "state" => job.state, diff --git a/spec/v3/services/jobs/find_spec.rb b/spec/v3/services/jobs/find_spec.rb index f7b4c851..12ffa4fa 100644 --- a/spec/v3/services/jobs/find_spec.rb +++ b/spec/v3/services/jobs/find_spec.rb @@ -21,7 +21,8 @@ describe Travis::API::V3::Services::Jobs::Find do "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[0].id, "number" => "#{jobs[0].number}", "state" => "configured", @@ -30,45 +31,46 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}}, + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}}, {"@type" => "job", "@href" => "/v3/job/#{jobs[1].id}", "@representation" => "standard", "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[1].id, "number" => "#{jobs[1].number}", "state" => "configured", @@ -77,45 +79,46 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}}, + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}}, {"@type" => "job", "@href" => "/v3/job/#{jobs[2].id}", "@representation" => "standard", "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[2].id, "number" => "#{jobs[2].number}", "state" => "configured", @@ -124,45 +127,46 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}}, + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}}, {"@type" => "job", "@href" => "/v3/job/#{jobs[3].id}", "@representation" => "standard", "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[3].id, "number" => "#{jobs[3].number}", "state" => "configured", @@ -171,38 +175,38 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}} + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}} ] } } @@ -227,7 +231,8 @@ describe Travis::API::V3::Services::Jobs::Find do "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[0].id, "number" => "#{jobs[0].number}", "state" => "configured", @@ -236,45 +241,46 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}}, + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}}, {"@type" => "job", "@href" => "/v3/job/#{jobs[1].id}", "@representation" => "standard", "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[1].id, "number" => "#{jobs[1].number}", "state" => "configured", @@ -283,45 +289,46 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}}, + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}}, {"@type" => "job", "@href" => "/v3/job/#{jobs[2].id}", "@representation" => "standard", "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[2].id, "number" => "#{jobs[2].number}", "state" => "configured", @@ -330,45 +337,46 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}}, + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}}, {"@type" => "job", "@href" => "/v3/job/#{jobs[3].id}", "@representation" => "standard", "@permissions" => { "read" => true, "cancel" => false, - "restart" => false }, + "restart" => false, + "debug" => false }, "id" => jobs[3].id, "number" => "#{jobs[3].number}", "state" => "configured", @@ -377,38 +385,38 @@ describe Travis::API::V3::Services::Jobs::Find do "build" => { "@type" => "build", "@href" => "/v3/build/#{build.id}", - "@representation"=> "minimal", - "id" => build.id, - "number" => build.number, - "state" => "configured", - "duration" => nil, - "event_type" => "push", - "previous_state" => "passed", - "started_at" => "2010-11-12T13:00:00Z", - "finished_at" => nil}, - "queue" => "builds.linux", - "repository" =>{ - "@type" => "repository", - "@href" => "/v3/repo/1", - "@representation"=>"minimal", - "id" => repo.id, + "@representation"=> "minimal", + "id" => build.id, + "number" => build.number, + "state" => "configured", + "duration" => nil, + "event_type" => "push", + "previous_state" => "passed", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil}, + "queue" => "builds.linux", + "repository" =>{ + "@type" => "repository", + "@href" => "/v3/repo/1", + "@representation"=>"minimal", + "id" => repo.id, "name" => "minimal", - "slug" => "svenfuchs/minimal"}, - "commit" =>{ - "@type" => "commit", - "@representation"=> "minimal", - "id" => commit.id, - "sha" => commit.commit, - "ref" => commit.ref, - "message" => commit.message, - "compare_url" => commit.compare_url, - "committed_at" =>"2010-11-12T12:55:00Z"}, - "owner" =>{ - "@type" => "user", - "@href" => "/v3/user/1", - "@representation"=> "minimal", - "id" => 1, - "login" => "svenfuchs"}} + "slug" => "svenfuchs/minimal"}, + "commit" =>{ + "@type" => "commit", + "@representation"=> "minimal", + "id" => commit.id, + "sha" => commit.commit, + "ref" => commit.ref, + "message" => commit.message, + "compare_url" => commit.compare_url, + "committed_at" =>"2010-11-12T12:55:00Z"}, + "owner" =>{ + "@type" => "user", + "@href" => "/v3/user/1", + "@representation"=> "minimal", + "id" => 1, + "login" => "svenfuchs"}} ] } } From 82b95440b29a61612a2778332efbf92cbbe5cf1f Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 29 Feb 2016 16:15:01 -1000 Subject: [PATCH 08/16] =?UTF-8?q?Fix=20previous=5Fstatus=20=E2=86=92=20pre?= =?UTF-8?q?vious=5Fstate,=20and=20set=20value=20correctly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/travis/api/v3/services/job/debug.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index 3c4686cd..bd3ea411 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -17,7 +17,7 @@ module Travis::API::V3 { debug: { stage: 'before_install', - previous_status: 'failed', + previous_state: job.state, created_by: access_control.user.login } } From d1e07f10f041d9df9623a46bff3e89bb0eec1e17 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Wed, 2 Mar 2016 09:11:12 -1000 Subject: [PATCH 09/16] Accept 'quiet' param for /job/:job_id/debug endpoint --- lib/travis/api/v3/services/job/debug.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index bd3ea411..5dac9cc8 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -1,5 +1,6 @@ module Travis::API::V3 class Services::Job::Debug < Service + params "quiet" def run raise LoginRequired unless access_control.logged_in? or access_control.full_access? @@ -13,12 +14,13 @@ module Travis::API::V3 accepted(job: job, state_change: :created) end - def debug_data + def debug_data(j) { debug: { stage: 'before_install', - previous_state: job.state, - created_by: access_control.user.login + previous_state: j.state, + created_by: access_control.user.login, + quiet: params["quiet"] || false } } end From ca09547452c824b45d348c016add90d4ff7a2078 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Wed, 2 Mar 2016 14:44:14 -1000 Subject: [PATCH 10/16] Access job via attr_reader --- lib/travis/api/v3/services/job/debug.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index 5dac9cc8..d67b53e5 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -2,9 +2,11 @@ module Travis::API::V3 class Services::Job::Debug < Service params "quiet" + attr_reader :job + def run raise LoginRequired unless access_control.logged_in? or access_control.full_access? - raise NotFound unless job = find(:job) + raise NotFound unless @job = find(:job) access_control.permissions(job).debug! job.config.merge! debug_data @@ -14,11 +16,11 @@ module Travis::API::V3 accepted(job: job, state_change: :created) end - def debug_data(j) + def debug_data { debug: { stage: 'before_install', - previous_state: j.state, + previous_state: job.state, created_by: access_control.user.login, quiet: params["quiet"] || false } From 56ea1d666311aeacae5bb705514efd904dce9561 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Thu, 3 Mar 2016 10:12:48 -1000 Subject: [PATCH 11/16] Raise if debug feature is unavailable --- lib/travis/api/v3/services/job/debug.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index d67b53e5..e3c37ab2 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -7,6 +7,7 @@ module Travis::API::V3 def run raise LoginRequired unless access_control.logged_in? or access_control.full_access? raise NotFound unless @job = find(:job) + raise WrongCredentials unless Travis.config.debug_tools_enabled or Travis::Features.active?(:debug_tools, access_control.user) access_control.permissions(job).debug! job.config.merge! debug_data From 05e860dd35e317452903e7275f0e669f5a79a004 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Thu, 3 Mar 2016 15:21:37 -1000 Subject: [PATCH 12/16] Fix `#active?` invocation Second arg is repository --- lib/travis/api/v3/services/job/debug.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index e3c37ab2..7dd1c280 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -7,7 +7,7 @@ module Travis::API::V3 def run raise LoginRequired unless access_control.logged_in? or access_control.full_access? raise NotFound unless @job = find(:job) - raise WrongCredentials unless Travis.config.debug_tools_enabled or Travis::Features.active?(:debug_tools, access_control.user) + raise WrongCredentials unless Travis.config.debug_tools_enabled or Travis::Features.active?(:debug_tools, job.repository) access_control.permissions(job).debug! job.config.merge! debug_data From ba142b84c73a24344a47aa5b36f73591088b85e6 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Fri, 4 Mar 2016 09:04:58 -1000 Subject: [PATCH 13/16] Put debug options in debug_otions This depends on https://github.com/travis-ci/travis-migrations/pull/7 --- lib/travis/api/v3/models/job.rb | 3 ++- lib/travis/api/v3/services/job/debug.rb | 12 +++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/travis/api/v3/models/job.rb b/lib/travis/api/v3/models/job.rb index 852f515c..f8f9bfb4 100644 --- a/lib/travis/api/v3/models/job.rb +++ b/lib/travis/api/v3/models/job.rb @@ -2,12 +2,13 @@ module Travis::API::V3 class Models::Job < Model self.inheritance_column = :_type_disabled - + has_one :log, dependent: :destroy belongs_to :repository belongs_to :commit belongs_to :build, autosave: true, foreign_key: 'source_id' belongs_to :owner, polymorphic: true serialize :config + serialize :debug_options end end diff --git a/lib/travis/api/v3/services/job/debug.rb b/lib/travis/api/v3/services/job/debug.rb index 7dd1c280..63c1939c 100644 --- a/lib/travis/api/v3/services/job/debug.rb +++ b/lib/travis/api/v3/services/job/debug.rb @@ -10,7 +10,7 @@ module Travis::API::V3 raise WrongCredentials unless Travis.config.debug_tools_enabled or Travis::Features.active?(:debug_tools, job.repository) access_control.permissions(job).debug! - job.config.merge! debug_data + job.debug_options = debug_data job.save! query.restart(access_control.user) @@ -19,12 +19,10 @@ module Travis::API::V3 def debug_data { - debug: { - stage: 'before_install', - previous_state: job.state, - created_by: access_control.user.login, - quiet: params["quiet"] || false - } + stage: 'before_install', + previous_state: job.state, + created_by: access_control.user.login, + quiet: params["quiet"] || false } end end From d944fe347a5fbd0e91f1ae753ae32772cf258f0f Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 7 Mar 2016 11:51:26 -1000 Subject: [PATCH 14/16] Add /job/:job_id/debug spec --- spec/v3/services/job/debug_sepc.rb | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 spec/v3/services/job/debug_sepc.rb diff --git a/spec/v3/services/job/debug_sepc.rb b/spec/v3/services/job/debug_sepc.rb new file mode 100644 index 00000000..3e3376e9 --- /dev/null +++ b/spec/v3/services/job/debug_sepc.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Travis::API::V3::Services::Job::Debug do + let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } + let(:sidekiq_payload) { JSON.load(Sidekiq::Client.last['args'].last[:payload]).deep_symbolize_keys } + let(:sidekiq_params) { Sidekiq::Client.last['args'].last.deep_symbolize_keys } + before { repo.requests.each(&:delete) } + + before do + Travis::Features.stubs(:owner_active?).returns(true) + @original_sidekiq = Sidekiq::Client + Sidekiq.send(:remove_const, :Client) # to avoid a warning + Sidekiq::Client = [] + end + + after do + Sidekiq.send(:remove_const, :Client) # to avoid a warning + Sidekiq::Client = @original_sidekiq + end + + describe "not authenticated" do + before { post("/v3/job/#{job.id}/debug") } + example { expect(last_response.status).to be == 403 } + example { expect(JSON.load(body)).to be == { + "@type" => "error", + "error_type" => "login_required", + "error_message" => "login required" + }} + end + +end \ No newline at end of file From 73f724c58cac86a6ecf5c3d350b266e7d62f1e06 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 7 Mar 2016 16:36:33 -1000 Subject: [PATCH 15/16] Add more specs for `/job/:job_id/debug` We now rely on https://github.com/travis-ci/travis-core/pull/519. --- Gemfile | 2 +- Gemfile.lock | 7 ++-- spec/v3/services/job/debug_sepc.rb | 61 ++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 9893ebf5..0a612122 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ ruby '2.1.7' if ENV.key?('DYNO') gem 's3', github: 'travis-ci/s3' -gem 'travis-core', github: 'travis-ci/travis-core' +gem 'travis-core', github: 'travis-ci/travis-core', branch: 'ha-feature-debug-endpoint' gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-config', '~> 0.1.0' gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil diff --git a/Gemfile.lock b/Gemfile.lock index 29745665..06399e45 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,7 +50,8 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: 96ee8c449ebe305c5c95633cea13eb88fe978abb + revision: 501f31a40af589b5ea8a677e7f8b067949a322b2 + branch: ha-feature-debug-endpoint specs: travis-core (0.0.1) actionmailer (~> 3.2.19) @@ -193,9 +194,9 @@ GEM httparty (0.11.0) multi_json (~> 1.0) multi_xml (>= 0.5.2) - httpclient (2.7.0.1) + httpclient (2.7.1) i18n (0.7.0) - ice_nine (0.11.1) + ice_nine (0.11.2) jemalloc (1.0.1) journey (1.0.4) json (1.8.3) diff --git a/spec/v3/services/job/debug_sepc.rb b/spec/v3/services/job/debug_sepc.rb index 3e3376e9..03fe80fb 100644 --- a/spec/v3/services/job/debug_sepc.rb +++ b/spec/v3/services/job/debug_sepc.rb @@ -2,8 +2,12 @@ require 'spec_helper' describe Travis::API::V3::Services::Job::Debug do let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } - let(:sidekiq_payload) { JSON.load(Sidekiq::Client.last['args'].last[:payload]).deep_symbolize_keys } - let(:sidekiq_params) { Sidekiq::Client.last['args'].last.deep_symbolize_keys } + let(:owner_type) { repo.owner_type.constantize } + let(:owner) { owner_type.find(repo.owner_id)} + let(:build) { repo.builds.last } + let(:jobs) { Travis::API::V3::Models::Build.find(build.id).jobs } + let(:job) { jobs.last } + before { repo.requests.each(&:delete) } before do @@ -11,6 +15,8 @@ describe Travis::API::V3::Services::Job::Debug do @original_sidekiq = Sidekiq::Client Sidekiq.send(:remove_const, :Client) # to avoid a warning Sidekiq::Client = [] + + Travis.config.stubs(:debug_tools_enabled).returns true end after do @@ -18,14 +24,47 @@ describe Travis::API::V3::Services::Job::Debug do Sidekiq::Client = @original_sidekiq end - describe "not authenticated" do - before { post("/v3/job/#{job.id}/debug") } - example { expect(last_response.status).to be == 403 } - example { expect(JSON.load(body)).to be == { - "@type" => "error", - "error_type" => "login_required", - "error_message" => "login required" - }} - end + describe "#run" do + context "when unauthenticated" do + before { post("/v3/job/#{job.id}/debug") } + example { expect(last_response.status).to be == 403 } + example { expect(JSON.load(body)).to be == { + "@type" => "error", + "error_type" => "login_required", + "error_message" => "login required" + }} + end + context "when authenticated" do + let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) } + let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }} + + context "without sufficient authorization" do + before { post("/v3/job/#{job.id}/debug", {}, headers) } + + example { expect(last_response.status).to be == 403 } + example { expect(JSON.load(body)).to include( + "@type" => "error", + "error_type" => "insufficient_access", + "error_message" => "operation requires debug access to job", + "resource_type" => "job", + )} + end + + context "with sufficient authorization" do + let(:params) {{}} + + before { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, push: true) } + before { post("/v3/job/#{job.id}/debug", {}, headers) } + + example { expect(last_response.status).to be == 202 } + + example { expect(job.reload.debug_options).to include( + stage: "before_install", + created_by: owner.login, + quiet: false + ) } + end + end + end end \ No newline at end of file From aaed416238bd88284d64e0c6c30d527d61ade8f6 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 7 Mar 2016 16:51:35 -1000 Subject: [PATCH 16/16] Point to travis-core master branch https://github.com/travis-ci/travis-core/pull/519 has been merged, so there is no need for a special branch --- Gemfile | 2 +- Gemfile.lock | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 0a612122..9893ebf5 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ ruby '2.1.7' if ENV.key?('DYNO') gem 's3', github: 'travis-ci/s3' -gem 'travis-core', github: 'travis-ci/travis-core', branch: 'ha-feature-debug-endpoint' +gem 'travis-core', github: 'travis-ci/travis-core' gem 'travis-support', github: 'travis-ci/travis-support' gem 'travis-config', '~> 0.1.0' gem 'travis-sidekiqs', github: 'travis-ci/travis-sidekiqs', require: nil diff --git a/Gemfile.lock b/Gemfile.lock index 06399e45..dcf56948 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,8 +50,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: 501f31a40af589b5ea8a677e7f8b067949a322b2 - branch: ha-feature-debug-endpoint + revision: fb39f8af5628444e2d7f5893a9f09fde7b0796e2 specs: travis-core (0.0.1) actionmailer (~> 3.2.19)