diff --git a/Gemfile.lock b/Gemfile.lock index bb404f42..6df423fa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -23,7 +23,7 @@ GIT GIT remote: git://github.com/travis-ci/travis-core.git - revision: 45a2cabe231a06df9ca2e3765bc27fdaffad5d10 + revision: eab89c8ca2407790d9ba23c9150e976f2c3ef211 specs: travis-core (0.0.1) actionmailer (~> 3.2.12) diff --git a/lib/travis/api/app/endpoint/jobs.rb b/lib/travis/api/app/endpoint/jobs.rb index e6722563..dec9ce34 100644 --- a/lib/travis/api/app/endpoint/jobs.rb +++ b/lib/travis/api/app/endpoint/jobs.rb @@ -13,23 +13,6 @@ class Travis::Api::App respond_with service(:find_job, params) end - get '/:job_id/log' do - resource = service(:find_log, params).run - if !resource || resource.archived? - archived_log_path = archive_url("/jobs/#{params[:job_id]}/log.txt") - - if params[:cors_hax] - status 204 - headers['Access-Control-Expose-Headers'] = 'Location' - headers['Location'] = archived_log_path - else - redirect archived_log_path, 307 - end - else - respond_with resource - end - end - post '/:id/cancel' do Metriks.meter("api.request.cancel_job").mark @@ -64,6 +47,39 @@ class Travis::Api::App respond_with service(:reset_model, job_id: params[:id]) end + get '/:job_id/log' do + resource = service(:find_log, params).run + if !resource || resource.archived? + archived_log_path = archive_url("/jobs/#{params[:job_id]}/log.txt") + + if params[:cors_hax] + status 204 + headers['Access-Control-Expose-Headers'] = 'Location' + headers['Location'] = archived_log_path + else + redirect archived_log_path, 307 + end + else + respond_with resource + end + end + + get "/:job_id/annotations" do + respond_with service(:find_annotations, params) + end + + post "/:job_id/annotations" do + if params[:status] && params[:description] + annotation = service(:update_annotation, params).run + + status annotation ? 204 : 401 + else + status 422 + + { "error" => "Must include status and description" } + end + end + def archive_url(path) "https://s3.amazonaws.com/#{hostname('archive')}#{path}" end diff --git a/spec/integration/v2/jobs_spec.rb b/spec/integration/v2/jobs_spec.rb index 77ac9544..6e092e00 100644 --- a/spec/integration/v2/jobs_spec.rb +++ b/spec/integration/v2/jobs_spec.rb @@ -76,6 +76,46 @@ describe 'Jobs' do end end + it "GET /jobs/:id/annotations" do + annotation_provider = Factory(:annotation_provider) + annotation = annotation_provider.annotations.create(job_id: job.id, status: "passed", description: "Foobar") + response = get "/jobs/#{job.id}/annotations", {}, headers + response.should deliver_json_for(Annotation.where(id: annotation.id), version: 'v2') + end + + describe "POST /jobs/:id/annotations" do + context "with valid credentials" do + it "responds with a 204" do + annotation_provider = Factory(:annotation_provider) + response = post "/jobs/#{job.id}/annotations", { username: annotation_provider.api_username, key: annotation_provider.api_key, status: "passed", description: "Foobar" }, headers + response.status.should eq(204) + end + end + + context "without a description" do + it "responds with a 422" do + annotation_provider = Factory(:annotation_provider) + response = post "/jobs/#{job.id}/annotations", { username: annotation_provider.api_username, key: annotation_provider.api_key, status: "errored" }, headers + response.status.should eq(422) + end + end + + context "without a status" do + it "responds with a 422" do + annotation_provider = Factory(:annotation_provider) + response = post "/jobs/#{job.id}/annotations", { username: annotation_provider.api_username, key: annotation_provider.api_key, description: "Foobar" }, headers + response.status.should eq(422) + end + end + + context "with invalid credentials" do + it "responds with a 401" do + response = post "/jobs/#{job.id}/annotations", { username: "invalid-username", key: "invalid-key", status: "passed", description: "Foobar" }, headers + response.status.should eq(401) + end + end + end + describe 'POST /jobs/:id/cancel' do let(:user) { User.where(login: 'svenfuchs').first } let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: -1) } diff --git a/spec/unit/endpoint/jobs_spec.rb b/spec/unit/endpoint/jobs_spec.rb index 763f956a..23dc5636 100644 --- a/spec/unit/endpoint/jobs_spec.rb +++ b/spec/unit/endpoint/jobs_spec.rb @@ -1,5 +1,14 @@ require 'spec_helper' describe Travis::Api::App::Endpoint::Jobs do - it 'has to be tested' + let(:job) { Factory(:test) } + let(:provider) { Factory(:annotation_provider) } + + it "GET /jobs/:id/annotations" do + get("/jobs/#{job.id}/annotations", {}, "HTTP_ACCEPT" => "application/vnd.travis-ci.2+json, */*; q=0.01").should be_ok + end + + it "POST /jobs/:id/annotations" do + response = post("/jobs/#{job.id}/annotations", { "username" => provider.api_username, "key" => provider.api_key, "status" => "passed", "description" => "Foobar" }, "HTTP_ACCEPT" => "application/vnd.travis-ci.2+json, */*; q=0.01").should be_successful + end end