diff --git a/lib/travis/api/v3/queries/jobs.rb b/lib/travis/api/v3/queries/jobs.rb new file mode 100644 index 00000000..be4c6395 --- /dev/null +++ b/lib/travis/api/v3/queries/jobs.rb @@ -0,0 +1,21 @@ +module Travis::API::V3 + class Queries::Jobs < Query + #params :state, :event_type, :previous_state, prefix: :job + #params :name, prefix: :branch, method_name: :branch_name + + #sortable_by :id, :started_at, :finished_at + + def find(build) + sort filter(build.jobs) + end + + def filter(list) + #list = list.where(state: list(state)) if state + #list = list.where(previous_state: list(previous_state)) if previous_state + #list = list.where(event_type: list(event_type)) if event_type + #list = list.where(branch: list(branch_name)) if branch_name + + list + end + end +end diff --git a/lib/travis/api/v3/routes.rb b/lib/travis/api/v3/routes.rb index 3ba42268..b9765166 100644 --- a/lib/travis/api/v3/routes.rb +++ b/lib/travis/api/v3/routes.rb @@ -20,6 +20,11 @@ module Travis::API::V3 post :cancel, '/cancel' post :restart, '/restart' + + resource :jobs do + route '/jobs' + get :find + end end resource :job do diff --git a/lib/travis/api/v3/service_index.rb b/lib/travis/api/v3/service_index.rb index 6c76d53d..087e11eb 100644 --- a/lib/travis/api/v3/service_index.rb +++ b/lib/travis/api/v3/service_index.rb @@ -42,7 +42,6 @@ module Travis::API::V3 Routes::Resource.new(:account), # dummy as there are only accounts routes right now Routes::Resource.new(:broadcast), # dummy as there are only broadcasts routes right now Routes::Resource.new(:commit), # dummy as commits can only be embedded - Routes::Resource.new(:jobs), # dummy as there are no direct jobs routes Routes::Resource.new(:request), # dummy as there are only requests routes right now Routes::Resource.new(:error), Routes::Resource.new(:home, attributes: [:config, :errors, :resources], actions: home_actions), diff --git a/lib/travis/api/v3/services/jobs/find.rb b/lib/travis/api/v3/services/jobs/find.rb new file mode 100644 index 00000000..e4288cee --- /dev/null +++ b/lib/travis/api/v3/services/jobs/find.rb @@ -0,0 +1,7 @@ +module Travis::API::V3 + class Services::Jobs::Find < Service + def run! + query.find(find(:build)) + end + end +end diff --git a/spec/v3/services/jobs/find_spec.rb b/spec/v3/services/jobs/find_spec.rb new file mode 100644 index 00000000..f7b4c851 --- /dev/null +++ b/spec/v3/services/jobs/find_spec.rb @@ -0,0 +1,416 @@ +require 'spec_helper' + +describe Travis::API::V3::Services::Jobs::Find do + let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first } + let(:build) { repo.builds.first } + let(:jobs) { Travis::API::V3::Models::Build.find(build.id).jobs } + let(:commit){ build.commit } + let(:parsed_body) { JSON.load(body) } + + describe "jobs on public repository" do + before { get("/v3/build/#{build.id}/jobs") } + example { expect(last_response).to be_ok } + example { expect(parsed_body).to be == { + "@type" => "jobs", + "@href" => "/v3/build/#{build.id}/jobs", + "@representation" => "standard", + "jobs" => [{ + "@type" => "job", + "@href" => "/v3/job/#{jobs[0].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[0].id, + "number" => "#{jobs[0].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}}, + {"@type" => "job", + "@href" => "/v3/job/#{jobs[1].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[1].id, + "number" => "#{jobs[1].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}}, + {"@type" => "job", + "@href" => "/v3/job/#{jobs[2].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[2].id, + "number" => "#{jobs[2].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}}, + {"@type" => "job", + "@href" => "/v3/job/#{jobs[3].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[3].id, + "number" => "#{jobs[3].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}} + ] + } + } + end + + describe "jobs private repository, private API, authenticated as user with access" do + let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) } + let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }} + before { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, pull: true) } + before { repo.update_attribute(:private, true) } + before { get("/v3/build/#{build.id}/jobs", {}, headers) } + after { repo.update_attribute(:private, false) } + example { expect(last_response).to be_ok } + example { expect(parsed_body).to be == { + "@type" => "jobs", + "@href" => "/v3/build/#{build.id}/jobs", + "@representation" => "standard", + "jobs" => [{ + "@type" => "job", + "@href" => "/v3/job/#{jobs[0].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[0].id, + "number" => "#{jobs[0].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}}, + {"@type" => "job", + "@href" => "/v3/job/#{jobs[1].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[1].id, + "number" => "#{jobs[1].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}}, + {"@type" => "job", + "@href" => "/v3/job/#{jobs[2].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[2].id, + "number" => "#{jobs[2].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}}, + {"@type" => "job", + "@href" => "/v3/job/#{jobs[3].id}", + "@representation" => "standard", + "@permissions" => { + "read" => true, + "cancel" => false, + "restart" => false }, + "id" => jobs[3].id, + "number" => "#{jobs[3].number}", + "state" => "configured", + "started_at" => "2010-11-12T13:00:00Z", + "finished_at" => nil, + "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, + "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"}} + ] + } + } + end +end