Skip to content

Commit

Permalink
feat: integrate build service (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach authored Mar 19, 2024
1 parent 4383712 commit dec14e7
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/placeos-rest-api/controllers/repositories.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module PlaceOS::Api
end

getter! current_repo : Model::Repository
class_property repository_dir : String = File.expand_path("./repositories")

###############################################################################################

Expand Down Expand Up @@ -118,14 +119,32 @@ module PlaceOS::Api
# lists the drivers in a repository
@[AC::Route::GET("/:id/drivers")]
def drivers : Array(String)
repository_folder = current_repo.folder_name
# Request to core:
# "/api/core/v1/drivers/?repository=#{repository}"
# Returns: `["path/to/file.cr"]`
drivers = Api::Systems.core_for(repository_folder, request_id) do |core_client|
core_client.drivers(repository_folder)
Dir.cd(fetch_repo) do
Dir.glob("drivers/**/*.cr").select do |file|
!file.ends_with?("_spec.cr") && File.open(file) do |f|
f.each_line.any? &.includes?("PlaceOS::Driver")
rescue
false
end
end
end
drivers
end

private def fetch_repo
folder = Path.new(self.class.repository_dir, current_repo.id.as(String), current_repo.folder_name)
downloaded = Dir.exists?(folder)
Dir.mkdir_p(folder) unless downloaded
password = current_repo.decrypt_password if current_repo.password.presence
repo = GitRepository.new(current_repo.uri, current_repo.username, password)
git = GitRepository::Commands.new(folder.to_s)
unless downloaded
git.init
git.add_origin repo.repository
end
git.run_git("fetch", {"--all"})
git.checkout current_repo.branch rescue git.checkout repo.default_branch

folder
end

# Returns the commits for a repository or file
Expand Down Expand Up @@ -167,7 +186,7 @@ module PlaceOS::Api
# Request to core:
# "/api/core/v1/drivers/#{file_name}/details?repository=#{repository}&commit=#{commit_hash}"
details = Api::Systems.core_for(driver_filename, request_id) do |core_client|
core_client.driver_details(driver_filename, commit, current_repo.folder_name, current_repo.branch)
core_client.driver_details(driver_filename, commit, current_repo.id.as(String), current_repo.branch)
end

# The raw JSON string is returned and we proxy that (no need to encode and decode)
Expand Down

0 comments on commit dec14e7

Please sign in to comment.