Skip to content

Commit f17b9a9

Browse files
committed
feat(drivers): add endpoint to fetch driver readme files
Add GET /:id/readme endpoint that retrieves readme markdown files for drivers from their git repository. The endpoint constructs the readme path from the driver's file_name, validates file existence at the driver's commit, and returns the file contents.
1 parent 79296fa commit f17b9a9

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/placeos-rest-api/controllers/drivers.cr

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "./application"
2+
require "git-repository"
23

34
module PlaceOS::Api
45
class Drivers < Application
@@ -7,10 +8,10 @@ module PlaceOS::Api
78
# Scopes
89
###############################################################################################
910

10-
before_action :can_read, only: [:index, :show]
11+
before_action :can_read, only: [:index, :show, :readme]
1112
before_action :can_write, only: [:create, :update, :destroy, :remove]
1213

13-
before_action :check_admin, except: [:index, :show]
14+
before_action :check_admin, except: [:index, :show, :readme]
1415

1516
###############################################################################################
1617

@@ -23,6 +24,11 @@ module PlaceOS::Api
2324

2425
getter! current_driver : ::PlaceOS::Model::Driver
2526

27+
getter! current_repo : ::PlaceOS::Model::Repository
28+
# class_property repository_dir : String = File.expand_path("./repositories")
29+
30+
31+
2632
# Response helpers
2733
###############################################################################################
2834

@@ -72,6 +78,41 @@ module PlaceOS::Api
7278
current_driver
7379
end
7480

81+
# get the readme for a driver
82+
@[AC::Route::GET("/:id/readme")]
83+
def readme : String
84+
# Get the repository for the current driver
85+
if (repository = current_driver.repository).nil?
86+
Log.error { {repository_id: current_driver.repository_id, message: "failed to load driver's repository"} }
87+
raise "failed to load driver's repository"
88+
end
89+
90+
# Construct the readme file path from the driver's file_name
91+
# e.g., "drivers/place/auto_release.cr" -> "drivers/place/auto_release_readme.md"
92+
driver_file_name = current_driver.file_name
93+
readme_path = driver_file_name.chomp(".cr") + "_readme.md"
94+
95+
# Create GitRepository instance
96+
repository_path = File.join(Repositories.repository_dir, repository.folder_name)
97+
git_repo = GitRepository.new(repository_path)
98+
99+
# Check if the readme file exists using the driver's commit
100+
files = git_repo.file_list(ref: current_driver.commit, path: readme_path)
101+
file_exists = !files.empty?
102+
103+
if file_exists
104+
# Use file_contents to fetch the readme file
105+
begin
106+
git_repo.file_contents(ref: current_driver.commit, path: readme_path)
107+
rescue e
108+
Log.error(exception: e) { {readme_path: readme_path, message: "failed to fetch readme file contents"} }
109+
raise Error::NotFound.new("Failed to fetch README file contents: #{e.message}")
110+
end
111+
else
112+
raise Error::NotFound.new("README file not found: #{readme_path}")
113+
end
114+
end
115+
75116
# udpate a drivers details
76117
@[AC::Route::PATCH("/:id", body: :driver)]
77118
@[AC::Route::PUT("/:id", body: :driver)]

0 commit comments

Comments
 (0)