Skip to content

Commit 27e532d

Browse files
committed
Support 'schedule'd workflow runs, not only 'push' runs
Fixes #4
1 parent 3eab57b commit 27e532d

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

github_api.cr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ struct WorkflowRuns
238238
# https://docs.github.com/v3/actions#list-workflow-runs
239239
get_json_list(
240240
WorkflowRuns, "repos/#{repo_owner}/#{repo_name}/actions/workflows/#{workflow}/runs",
241-
params: {branch: branch, event: "push", status: "success"},
241+
params: {branch: branch, status: "success"},
242242
headers: {authorization: token}, max_items: max_items
243243
)
244244
end
@@ -248,6 +248,7 @@ struct WorkflowRun
248248
include JSON::Serializable
249249
property id : Int64
250250
property head_branch : String
251+
property event : String
251252
property workflow_id : Int64
252253
property check_suite_url : String
253254
@[JSON::Field(converter: RFC3339Converter)]

nightly_link.cr

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ record RepoInstallation,
9393
result = nil
9494
unless public_repos.includes?(repo_name) ||
9595
h && private_repos.includes?(repo_name) && h == (result = password(repo_name))
96-
raise ART::Exceptions::NotFound.new("Not found: #{repo_owner}/#{repo_name}")
96+
raise ART::Exceptions::NotFound.new("Repository not found: '#{repo_owner}/#{repo_name}'")
9797
end
9898
result
9999
end
@@ -235,21 +235,16 @@ class DashboardController < ART::Controller
235235
unless workflow.to_i? || workflow.ends_with?(".yml") || workflow.ends_with?(".yaml")
236236
workflow += ".yml"
237237
end
238-
message = nil
238+
239+
run = get_latest_run(repo_owner, repo_name, workflow, branch, token)
240+
if run.updated_at < 90.days.ago
241+
message = "Warning: the latest successful run is older than 90 days, and its artifacts likely expired."
242+
end
239243

240244
artifacts = begin
241-
WorkflowRuns.for_workflow(repo_owner, repo_name, workflow, branch, token, max_items: 1, expires_in: 5.minutes).each do |run|
242-
if run.updated_at < 90.days.ago
243-
message = "Warning: the latest successful run is older than 90 days, and its artifacts likely expired."
244-
end
245-
break Artifacts.for_run(repo_owner, repo_name, run.id, token, expires_in: 3.hours)
246-
end
245+
Artifacts.for_run(repo_owner, repo_name, run.id, token, expires_in: 3.hours)
247246
rescue e : Halite::Exception::ClientError
248-
if e.status_code.in?(401, 404)
249-
raise ART::Exceptions::NotFound.new("No runs found for workflow '#{workflow}' and branch '#{branch}'")
250-
else
251-
raise e
252-
end
247+
raise e unless e.status_code.in?(401, 404)
253248
end
254249
if !artifacts || artifacts.empty?
255250
raise ART::Exceptions::NotFound.new("No artifacts found for workflow '#{workflow}' and branch '#{branch}'")
@@ -266,6 +261,20 @@ class DashboardController < ART::Controller
266261
end
267262
end
268263

264+
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+
end
271+
raise e
272+
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}'")
276+
end
277+
269278
class ArtifactsController < ART::Controller
270279
record Link, url : String, title : String? = nil, ext : Bool = false, zip : String? = nil
271280

@@ -281,25 +290,12 @@ class ArtifactsController < ART::Controller
281290
unless workflow.to_i? || workflow.ends_with?(".yml") || workflow.ends_with?(".yaml")
282291
workflow += ".yml"
283292
end
284-
285-
runs = begin
286-
WorkflowRuns.for_workflow(repo_owner, repo_name, workflow, branch, token, max_items: 1, expires_in: 5.minutes)
287-
rescue e : Halite::Exception::ClientError
288-
if e.status_code.in?(401, 404)
289-
raise ART::Exceptions::NotFound.new("No runs found for workflow '#{workflow}' and branch '#{branch}'")
290-
else
291-
raise e
292-
end
293-
end
294-
if runs.empty?
295-
raise ART::Exceptions::NotFound.new("No artifacts found for workflow '#{workflow}' and branch '#{branch}'")
296-
end
297-
run = runs.first
293+
run = get_latest_run(repo_owner, repo_name, workflow, branch, token)
298294

299295
result = by_run(repo_owner, repo_name, run.id, artifact, run.check_suite_url.rpartition("/").last.to_i64?, h)
300296
result.title = {"Repository #{repo_owner}/#{repo_name}", "Workflow #{workflow} | Branch #{branch} | Artifact #{artifact}"}
301297
result.links << Link.new("https://github.com/#{repo_owner}/#{repo_name}/actions?" + HTTP::Params.encode({
302-
query: "event:push is:success branch:#{branch}",
298+
query: "event:#{run.event} is:success branch:#{branch}",
303299
}), "GitHub: browse workflow runs on branch '#{branch}'", ext: true)
304300
link = "/#{repo_owner}/#{repo_name}/workflows/#{workflow.rchop(".yml")}/#{branch}/#{artifact}"
305301
result.links << Link.new("#{link}#{"?h=#{h}" if h}", result.title[1], zip: "#{link}.zip#{"?h=#{h}" if h}")
@@ -316,9 +312,8 @@ class ArtifactsController < ART::Controller
316312
rescue e : Halite::Exception::ClientError
317313
if e.status_code.in?(401, 404)
318314
raise ART::Exceptions::NotFound.new("No artifacts found for run ##{run_id}")
319-
else
320-
raise e
321315
end
316+
raise e
322317
end
323318
art = artifacts.find { |a| a.name == artifact }
324319
raise ART::Exceptions::NotFound.new("Artifact '#{artifact}' not found for run ##{run_id}") if !art
@@ -345,9 +340,8 @@ class ArtifactsController < ART::Controller
345340
rescue e : Halite::Exception::ClientError
346341
if e.status_code.in?(401, 404)
347342
raise ART::Exceptions::NotFound.new("Artifact ##{artifact_id} not found")
348-
else
349-
raise e
350343
end
344+
raise e
351345
end
352346
result = Result.new
353347
result.title = {"Repository #{repo_owner}/#{repo_name}", "Artifact ##{artifact_id}"}

0 commit comments

Comments
 (0)