Skip to content

Commit 8ba0059

Browse files
committed
Fix support for multiple event types
27e532d is broken, it made it so that if the latest event happens to be (e.g.) a pull request, then nothing is found, because max_items is 1.
1 parent d858167 commit 8ba0059

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

github_api.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ struct WorkflowRuns
236236
)
237237
end
238238

239-
cached_array def self.for_workflow(repo_owner : String, repo_name : String, workflow : String, branch : String, token : InstallationToken | UserToken, max_items : Int32, & : WorkflowRun ->)
239+
cached_array def self.for_workflow(repo_owner : String, repo_name : String, workflow : String, branch : String, event : String, token : InstallationToken | UserToken, max_items : Int32, & : WorkflowRun ->)
240240
# https://docs.github.com/v3/actions#list-workflow-runs
241241
get_json_list(
242242
WorkflowRuns, "repos/#{repo_owner}/#{repo_name}/actions/workflows/#{workflow}/runs",
243-
params: {branch: branch, status: "success"},
243+
params: {branch: branch, event: event, status: "success"},
244244
headers: {authorization: token}, max_items: max_items
245245
)
246246
end

nightly_link.cr

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ class DashboardController < ART::Controller
207207
n += 1
208208
end
209209
n.times do
210-
installations << ch.receive
210+
select
211+
when x = ch.receive
212+
installations << x
213+
when timeout(5.seconds)
214+
break
215+
end
211216
end
212217

213218
return ART::StreamedResponse.new(headers: HTML_HEADERS) do |io|
@@ -262,17 +267,39 @@ class DashboardController < ART::Controller
262267
end
263268

264269
private def get_latest_run(repo_owner : String, repo_name : String, workflow : String, branch : String, token : InstallationToken)
265-
artifacts = begin
266-
WorkflowRuns.for_workflow(repo_owner, repo_name, workflow, branch, token, max_items: 1, expires_in: 5.minutes)
267-
rescue e : Halite::Exception::ClientError
268-
if e.status_code.in?(401, 404)
269-
raise ART::Exceptions::NotFound.new("Repository '#{repo_owner}/#{repo_name}' or workflow '#{workflow}' not found")
270+
ch = Channel(Array(WorkflowRun) | Exception).new
271+
{% for event in [{"push", 5}, {"schedule", 60}] %}
272+
spawn do
273+
ch.send begin
274+
WorkflowRuns.for_workflow(repo_owner, repo_name, workflow, branch: branch, event: {{event[0]}}, token: token, max_items: 1, expires_in: {{event[1]}}.minutes)
275+
rescue e
276+
if e.is_a?(Halite::Exception::ClientError) && e.status_code.in?(401, 404)
277+
e = ART::Exceptions::NotFound.new("Repository '#{repo_owner}/#{repo_name}' or workflow '#{workflow}' not found")
278+
end
279+
e
280+
end
270281
end
271-
raise e
282+
{% end %}
283+
284+
runs = Array(WorkflowRun).new
285+
exc = nil
286+
2.times do
287+
select
288+
when x = ch.receive
289+
case x
290+
in Exception
291+
exc ||= x
292+
in Array
293+
runs.concat(x)
294+
end
295+
when timeout(5.seconds)
296+
break
297+
end
298+
end
299+
if runs.empty?
300+
raise exc || ART::Exceptions::NotFound.new("No successful runs found for workflow '#{workflow}' and branch '#{branch}'")
272301
end
273-
artifacts.find do |run|
274-
run.event.in?("push", "schedule")
275-
end || raise ART::Exceptions::NotFound.new("No successful runs found for workflow '#{workflow}' and branch '#{branch}'")
302+
runs.max_by &.updated_at
276303
end
277304

278305
class ArtifactsController < ART::Controller

0 commit comments

Comments
 (0)