Skip to content

Commit fa64b05

Browse files
committed
fix(api:repositories): commit listing
1 parent e6aad27 commit fa64b05

File tree

3 files changed

+77
-49
lines changed

3 files changed

+77
-49
lines changed

spec/helper.cr

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,33 @@ macro test_crd(klass, controller_klass)
225225
{{ klass.id }}.find(id).should be_nil
226226
end
227227
end
228+
229+
class MockServer
230+
include ActionController::Router
231+
232+
def initialize
233+
init_routes
234+
end
235+
236+
private def init_routes
237+
{% for klass in ActionController::Base::CONCRETE_CONTROLLERS %}
238+
{{klass}}.__init_routes__(self)
239+
{% end %}
240+
end
241+
end
242+
243+
MOCK_SERVER = MockServer.new
244+
245+
abstract class PlaceOS::Api::Application < ActionController::Base
246+
def self.with_request(verb, path, expect_failure = false)
247+
io = IO::Memory.new
248+
context = context(verb.upcase, path)
249+
MOCK_SERVER.route_handler.search_route(context)
250+
context.response.output = io
251+
252+
yield new(context)
253+
io.rewind
254+
context.response.status.success?.should (expect_failure ? be_false : be_true)
255+
context
256+
end
257+
end

spec/repositories_spec.cr

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,14 @@ module PlaceOS::Api
6969
end
7070
end
7171

72-
describe "GET /:id/commits" do
73-
context "interface" do
74-
pending "fetches the commits for a repository" do
75-
end
76-
end
77-
78-
context "driver" do
79-
repo = Model::Generator.repository(type: :driver)
80-
repo.uri = "https://github.com/placeOS/private-drivers"
72+
describe "driver only actions" do
73+
repo = Model::Generator.repository(type: :interface)
74+
before_all do
8175
repo.save!
82-
83-
it "fetches the commits for a repository" do
84-
id = repo.id.as(String)
85-
path = "#{base}#{id}/commits"
86-
result = curl(
87-
method: "GET",
88-
path: path,
89-
headers: authorization_header.merge({"Content-Type" => "application/json"}),
90-
)
91-
92-
result.status.should eq HTTP::Status::OK
93-
Array(String).from_json(result.body).should_not be_empty
94-
end
95-
96-
it "fetches the commits for a file" do
97-
id = repo.id.as(String)
98-
params = HTTP::Params{
99-
"driver" => "drivers/place/private_helper.cr",
100-
}
101-
path = "#{base}#{id}/commits?#{params}"
102-
result = curl(
103-
method: "GET",
104-
path: path,
105-
headers: authorization_header.merge({"Content-Type" => "application/json"}),
106-
)
107-
108-
result.status.should eq HTTP::Status::OK
109-
Array(String).from_json(result.body).should_not be_empty
110-
end
11176
end
112-
end
11377

114-
describe "driver only actions" do
11578
it "errors if enumerating drivers in an interface repo" do
116-
repository = Model::Generator.repository(type: :interface).save!
117-
118-
id = repository.id.as(String)
79+
id = repo.id.as(String)
11980
path = "#{base}#{id}/drivers"
12081
result = curl(
12182
method: "GET",
@@ -127,9 +88,7 @@ module PlaceOS::Api
12788
end
12889

12990
it "errors when requesting driver details from an interface repo" do
130-
repository = Model::Generator.repository(type: Model::Repository::Type::Interface).save!
131-
132-
id = repository.id.as(String)
91+
id = repo.id.as(String)
13392
path = "#{base}#{id}/details"
13493
result = curl(
13594
method: "GET",
@@ -141,6 +100,45 @@ module PlaceOS::Api
141100
end
142101
end
143102
end
103+
104+
describe "GET /:id/commits" do
105+
context "interface" do
106+
pending "fetches the commits for a repository" do
107+
end
108+
end
109+
110+
context "driver" do
111+
repo = Model::Generator.repository(type: :driver).tap do |r|
112+
r.uri = "https://github.com/placeOS/private-drivers"
113+
end
114+
115+
before_all do
116+
repo.save!
117+
end
118+
119+
it "fetches the commits for a repository" do
120+
id = repo.id.as(String)
121+
response = Api::Repositories
122+
.with_request("GET", "#{base}#{id}/commits", &.commits)
123+
.response
124+
response.status.should eq HTTP::Status::OK
125+
Array(String).from_json(response.output).should_not be_empty
126+
end
127+
128+
it "fetches the commits for a file" do
129+
id = repo.id.as(String)
130+
params = HTTP::Params{
131+
"driver" => "drivers/place/private_helper.cr",
132+
}
133+
response = Api::Repositories
134+
.with_request("GET", "#{base}#{id}/commits?#{params}", &.commits)
135+
.response
136+
137+
response.status.should eq HTTP::Status::OK
138+
Array(String).from_json(response.output).should_not be_empty
139+
end
140+
end
141+
end
144142
end
145143
end
146144
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ module PlaceOS::Api
123123
render json: commits
124124
end
125125

126-
def self.commits(repository : Model::Repository, request_id : String, file_name : String? = nil, branch : String? = nil, limit : Int32? = nil)
126+
def self.commits(repository : Model::Repository, request_id : String, file_name : String? = nil, branch : String? = nil, limit : Int32? = nil) : Array(String)
127127
limit = 50 if limit.nil?
128128
branch = "master" if branch.nil?
129129

@@ -136,12 +136,12 @@ module PlaceOS::Api
136136
else
137137
client.repository_commits(**args)
138138
end
139-
end
139+
end.map(&.commit)
140140
in .interface?
141141
# Dial the frontends service
142142
Frontends::Client.client(request_id: request_id) do |frontends_client|
143143
frontends_client.commits(repository.folder_name, limit)
144-
end
144+
end.map(&.[:commit])
145145
end
146146
end
147147

0 commit comments

Comments
 (0)