Skip to content

Commit

Permalink
Add a build_id function to RepositoryArchitecture
Browse files Browse the repository at this point in the history
This is a direct mapping to view=status in the backend
  • Loading branch information
coolo committed Nov 14, 2018
1 parent 758b12e commit fa6b35b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/api/app/models/repository_architecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ class RepositoryArchitecture < ApplicationRecord
validates :repository, :architecture, :position, presence: true
validates :repository, uniqueness: { scope: :architecture }

has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy do
def for_uuid(uuid)
where(status_reports: { uuid: uuid })
end
has_many :status_reports, as: :checkable, class_name: 'Status::Report', dependent: :destroy

def latest
for_uuid(proxy_association.owner.build_id)
end
def build_id
Backend::Api::Build::Repository.build_id(repository.project.name, repository.name, architecture.name)
end
end

Expand Down
17 changes: 17 additions & 0 deletions src/api/lib/backend/api/build/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Backend
module Api
module Build
# Class that connect to endpoints related to projects
class Repository
extend Backend::ConnectionHelper

# Returns the build id for a repository
# @return [String]
def self.build_id(project_name, repository_name, architecture)
response = http_get(['/build/:project/:repository/:architecture', project_name, repository_name, architecture], params: { view: 'status' })
Xmlhash.parse(response).value('buildid')
end
end
end
end
end
25 changes: 25 additions & 0 deletions src/api/spec/models/repository_architecture_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

RSpec.describe RepositoryArchitecture do
describe '.build_id' do
let(:repository_architecture) { create(:repository_architecture) }
let(:repository) { repository_architecture.repository }
let(:project) { repository.project.name }
let(:architecture) { repository_architecture.architecture.name }

let(:good_response) { '<status code="finished"><buildid>1</buildid></status>' }
let(:busy_response) { '<status code="building"/>' }

subject { repository_architecture.build_id }

it 'fetches from backend' do
stub_request(:get, "#{CONFIG['source_url']}/build/#{project}/#{repository.name}/#{architecture}?view=status").and_return(body: good_response)
expect(subject).to eq('1')
end

it 'does not crash' do
stub_request(:get, "#{CONFIG['source_url']}/build/#{project}/#{repository.name}/#{architecture}?view=status").and_return(body: busy_response)
expect(subject).to be_nil
end
end
end

0 comments on commit fa6b35b

Please sign in to comment.