Skip to content

Commit ae2845b

Browse files
authored
feat(drivers): [PPT-2232] add endpoint to fetch driver readme files (#416)
* 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. * chore(shard.lock): update shards * doc(openapi): dock gen * chore(drivers): crystal tool format
1 parent abd4e20 commit ae2845b

2 files changed

Lines changed: 111 additions & 2 deletions

File tree

OPENAPI_DOC.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7476,6 +7476,75 @@ paths:
74767476
application/json:
74777477
schema:
74787478
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
7479+
/api/engine/v2/drivers/{id}/readme:
7480+
get:
7481+
summary: get the readme for a driver
7482+
tags:
7483+
- Drivers
7484+
operationId: PlaceOS::Api::Drivers_readme
7485+
parameters:
7486+
- name: id
7487+
in: path
7488+
required: true
7489+
schema:
7490+
type: string
7491+
responses:
7492+
200:
7493+
description: OK
7494+
content:
7495+
application/json:
7496+
schema:
7497+
$ref: '#/components/schemas/String'
7498+
409:
7499+
description: Conflict
7500+
content:
7501+
application/json:
7502+
schema:
7503+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
7504+
401:
7505+
description: Unauthorized
7506+
content:
7507+
application/json:
7508+
schema:
7509+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
7510+
403:
7511+
description: Forbidden
7512+
404:
7513+
description: Not Found
7514+
content:
7515+
application/json:
7516+
schema:
7517+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
7518+
408:
7519+
description: Request Timeout
7520+
content:
7521+
application/json:
7522+
schema:
7523+
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
7524+
400:
7525+
description: Bad Request
7526+
content:
7527+
application/json:
7528+
schema:
7529+
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
7530+
422:
7531+
description: Unprocessable Entity
7532+
content:
7533+
application/json:
7534+
schema:
7535+
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
7536+
406:
7537+
description: Not Acceptable
7538+
content:
7539+
application/json:
7540+
schema:
7541+
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
7542+
415:
7543+
description: Unsupported Media Type
7544+
content:
7545+
application/json:
7546+
schema:
7547+
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
74797548
/api/engine/v2/drivers/{id}/recompile:
74807549
post:
74817550
summary: force recompile a driver, useful if libraries and supporting files

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

Lines changed: 42 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,10 @@ module PlaceOS::Api
2324

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

27+
getter! current_repo : ::PlaceOS::Model::Repository
28+
29+
# class_property repository_dir : String = File.expand_path("./repositories")
30+
2631
# Response helpers
2732
###############################################################################################
2833

@@ -72,6 +77,41 @@ module PlaceOS::Api
7277
current_driver
7378
end
7479

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

0 commit comments

Comments
 (0)