Skip to content

Commit

Permalink
If there is 1 artifact, don't require clicking through to it
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Feb 13, 2021
1 parent 04c7e07 commit 116e8f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
61 changes: 35 additions & 26 deletions spec/main_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RUN_3 = 987654326
JOB_1 = 9977553311
JOB_2 = 9977553317
ARTIFACT_1 = 87654321
ARTIFACT_2 = 87654325
PRIVATE_REPO = "oprypin/test-private-repo"

require "http"
Expand Down Expand Up @@ -156,7 +157,9 @@ describe "dash_by_branch" do
body: %({"workflow_runs":[
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/UserName/RepoName/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"UserName/RepoName","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/runs/#{RUN_2}/artifacts?per_page=100").to_return(
body: %({"artifacts":[{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_1}"}]}))
body: %({"artifacts":[
{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_1}"},
{"id":#{ARTIFACT_2},"name":"AnotherArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_2}"}]}))
end

test do
Expand Down Expand Up @@ -194,12 +197,14 @@ describe "dash_by_branch" do
test do
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/workflows/SomeWorkflow.yml/runs?per_page=1&branch=SomeBranch&event=push&status=success").to_return(
body: %({"workflow_runs":[
{"id":#{RUN_1},"event":"push","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2020-12-19T22:22:22Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
{"id":#{RUN_1},"event":"push","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2020-12-19T22:22:22Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/workflows/SomeWorkflow.yml/runs?per_page=1&branch=SomeBranch&event=schedule&status=success").to_return(
body: %({"workflow_runs":[
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/runs/#{RUN_2}/artifacts?per_page=100").to_return(
body: %({"artifacts":[{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_1}"}]}))
body: %({"artifacts":[
{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_1}"},
{"id":#{ARTIFACT_2},"name":"AnotherArtifact","url":"https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_2}"}]}))

resp, body = serve("/#{PRIVATE_REPO}/workflows/SomeWorkflow/SomeBranch?h=6c9bf24563d1896f5de321ce6043413f8c75ef16")
assert_canonical "https://nightly.link/#{PRIVATE_REPO}/workflows/SomeWorkflow/SomeBranch?h=6c9bf24563d1896f5de321ce6043413f8c75ef16"
Expand Down Expand Up @@ -248,11 +253,11 @@ describe "dash_by_run" do
resp, body = serve("/uSerName/RepoName/actions/runs/#{RUN_1}")
assert_canonical "https://nightly.link/UserName/RepoName/actions/runs/#{RUN_1}"
assert_contents [
"Repository UserName/RepoName", "Run #987654321",
"Repository UserName/RepoName", "Run #987654321",
"https://nightly.link/UserName/RepoName/actions/runs/987654321/SomeArtifact",
"https://nightly.link/UserName/RepoName/actions/runs/987654321/SomeArtifact.zip",
"https://nightly.link/UserName/RepoName/actions/runs/987654321/SomeArtifact.zip",
"Repository UserName/RepoName", "Run ##{RUN_1}",
"Repository UserName/RepoName", "Run ##{RUN_1}",
"https://nightly.link/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact",
"https://nightly.link/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact.zip",
"https://nightly.link/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact.zip",
]
assert_nofollow
end
Expand All @@ -264,8 +269,8 @@ describe "dash_by_run" do
resp, body = serve("/UserName/RepoName/actions/runs/#{RUN_3}")
assert resp.status == HTTP::Status::NOT_FOUND
assert_contents [
"No artifacts found for run #987654326",
"https://github.com/UserName/RepoName/actions/runs/987654326#artifacts",
"No artifacts found for run ##{RUN_3}",
"https://github.com/UserName/RepoName/actions/runs/#{RUN_3}#artifacts",
]
assert_nofollow
end
Expand All @@ -275,12 +280,14 @@ describe "by_branch" do
before_each do
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/workflows/SomeWorkflow.yml/runs?per_page=1&branch=SomeBranch&event=push&status=success").to_return(
body: %({"workflow_runs":[
{"id":#{RUN_1},"event":"push","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/UserName/RepoName/check-suites/#{CHECK_SUITE_1}","updated_at":"2020-12-19T22:22:22Z","repository":{"full_name":"UserName/RepoName","private":false,"fork":false}}]}))
{"id":#{RUN_1},"event":"push","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/UserName/RepoName/check-suites/#{CHECK_SUITE_1}","updated_at":"2020-12-19T22:22:22Z","repository":{"full_name":"UserName/RepoName","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/workflows/SomeWorkflow.yml/runs?per_page=1&branch=SomeBranch&event=schedule&status=success").to_return(
body: %({"workflow_runs":[
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/UserName/RepoName/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"UserName/RepoName","private":false,"fork":false}}]}))
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/UserName/RepoName/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"UserName/RepoName","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/runs/#{RUN_2}/artifacts?per_page=100").to_return(
body: %({"artifacts":[{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_1}"}]}))
body: %({"artifacts":[
{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_1}"},
{"id":#{ARTIFACT_2},"name":"AnotherArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_2}"}]}))
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/artifacts/#{ARTIFACT_1}/zip").to_return(
headers: HTTP::Headers{"location" => "http://example.org/download1"})
end
Expand Down Expand Up @@ -330,12 +337,14 @@ describe "by_branch" do
test do
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/workflows/SomeWorkflow.yml/runs?per_page=1&branch=SomeBranch&event=push&status=success").to_return(
body: %({"workflow_runs":[
{"id":#{RUN_1},"event":"push","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2020-12-19T22:22:22Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
{"id":#{RUN_1},"event":"push","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2020-12-19T22:22:22Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/workflows/SomeWorkflow.yml/runs?per_page=1&branch=SomeBranch&event=schedule&status=success").to_return(
body: %({"workflow_runs":[
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
{"id":#{RUN_2},"event":"schedule","workflow_id":#{WORKFLOW_1},"check_suite_url":"https://api.github.com/repos/#{PRIVATE_REPO}/check-suites/#{CHECK_SUITE_1}","updated_at":"2021-02-07T07:15:00Z","repository":{"full_name":"#{PRIVATE_REPO}","private":false,"fork":false}}]}))
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/runs/#{RUN_2}/artifacts?per_page=100").to_return(
body: %({"artifacts":[{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_1}"}]}))
body: %({"artifacts":[
{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_1}"},
{"id":#{ARTIFACT_2},"name":"AnotherArtifact","url":"https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_2}"}]}))
WebMock.stub(:get, "https://api.github.com/repos/#{PRIVATE_REPO}/actions/artifacts/#{ARTIFACT_1}/zip").to_return(
headers: HTTP::Headers{"location" => "http://example.org/download2"})

Expand All @@ -358,29 +367,29 @@ end

describe "by_run" do
before_each do
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/runs/#{RUN_2}/artifacts?per_page=100").to_return(
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/runs/#{RUN_1}/artifacts?per_page=100").to_return(
body: %({"artifacts":[{"id":#{ARTIFACT_1},"name":"SomeArtifact","url":"https://api.github.com/repos/UserName/RepoName/actions/artifacts/#{ARTIFACT_1}"}]}))
WebMock.stub(:get, "https://api.github.com/repos/username/reponame/actions/artifacts/#{ARTIFACT_1}/zip").to_return(
headers: HTTP::Headers{"location" => "http://example.org/download1"})
end

test do
resp, body = serve("/UserName/RepoName/actions/runs/#{RUN_2}/SomeArtifact")
assert_canonical "https://nightly.link/UserName/RepoName/actions/runs/#{RUN_2}/SomeArtifact"
resp, body = serve("/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact")
assert_canonical "https://nightly.link/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact"
assert_contents [
"Repository UserName/RepoName", "Run #987654324 | Artifact SomeArtifact",
"Repository UserName/RepoName", "Run ##{RUN_1} | Artifact SomeArtifact",
] * 2 + [
"https://nightly.link/UserName/RepoName/actions/runs/#{RUN_2}/SomeArtifact.zip",
"https://nightly.link/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact.zip",
"https://nightly.link/UserName/RepoName/actions/artifacts/#{ARTIFACT_1}.zip",
].flat_map { |s| [s, s] } + [
"http://example.org/download1",
"https://github.com/UserName/RepoName/actions/runs/#{RUN_2}#artifacts",
"https://github.com/UserName/RepoName/actions/runs/#{RUN_1}#artifacts",
]
assert_nofollow
end

test "redirect" do
resp, body = serve("/UserName/RepoName/actions/runs/#{RUN_2}/SomeArtifact.zip")
resp, body = serve("/UserName/RepoName/actions/runs/#{RUN_1}/SomeArtifact.zip")
assert_redirect "http://example.org/download1"
end
end
Expand Down Expand Up @@ -425,8 +434,8 @@ describe "by_job" do
resp, body = serve("/uSerName/RepoName/runs/#{JOB_1}")
assert_canonical "https://nightly.link/uSerName/RepoName/runs/#{JOB_1}"
assert_contents [
"Repository uSerName/RepoName", "Job #9977553311",
"Repository uSerName/RepoName", "Job #9977553311",
"Repository uSerName/RepoName", "Job ##{JOB_1}",
"Repository uSerName/RepoName", "Job ##{JOB_1}",
"https://nightly.link/uSerName/RepoName/runs/#{JOB_1}.txt",
"http://example.org/download1",
"https://github.com/uSerName/RepoName/runs/#{JOB_1}",
Expand Down
4 changes: 4 additions & 0 deletions src/nightly_link.cr
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ class NightlyLink
repo_owner: repo_owner, repo_name: repo_name, workflow: workflow.rchop(".yml"), branch: branch, artifact: art.name
)), art.name, h: h)
end
if links.size == 1
raise HTTPException.redirect(links[0].url)
end

title = {"Repository #{repo_owner}/#{repo_name}", "Workflow #{workflow} | Branch #{branch}"}
canonical = abs_url(NightlyLink.gen_dash_by_branch(
repo_owner: repo_owner, repo_name: repo_name, workflow: workflow.rchop(".yml"), branch: branch
Expand Down
3 changes: 2 additions & 1 deletion templates/controls.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ <h2>Paste a GitHub link, get a nightly.link!</h2>
<details>
<summary>Link to a repository's latest artifact</summary>
<p>Insert the GitHub URL of a workflow file that uses <a target="_blank" href="https://github.com/actions/upload-artifact#readme">actions/upload-artifact</a>.<br/>
Example: <a class="example" target="_blank" href="<%= HTML.escape(example_workflow) %>"><%= HTML.escape(example_workflow) %></a></p>
Example: <a class="example" target="_blank" href="<%= HTML.escape(example_workflow) %>"><%= HTML.escape(example_workflow) %></a><br>
Note that the <i>branch</i> which you're on also matters.</p>
<p>Following this form (and having selected the "<%= example_art %>" artifact), you will end up at<br><a rel="nofollow" href="<%= example_dest %>"><%= example_dest %></a> [<a rel="nofollow" href="<%= example_dest %>.zip">.zip</a>]<br>
which is a link that always downloads the latest artifact from a <u>succeeding</u> run on that <u>repo</u>+<u>workflow</u>+<u>branch</u>.</p>
<p>If you have several workflows or branches, you can adapt the URL by hand in a predictable way.</p>
Expand Down

0 comments on commit 116e8f0

Please sign in to comment.