Skip to content

Commit f69538d

Browse files
Restrict cache on forked prs (#480)
## 📝 Description renderedtext/project-tasks#2647 ## ✅ Checklist - [x] I have tested this change - [ ] This change requires documentation update: To be done
1 parent f916423 commit f69538d

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

zebra/lib/zebra/workers/job_request_factory.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ defmodule Zebra.Workers.JobRequestFactory do
6363
{:ok, rsa} <- Task.await(gen_rsa),
6464
{:ok, project} <- Task.await(find_project),
6565
find_repository <- Task.async(fn -> Repository.find(project.repository_id) end),
66-
find_cache <-
67-
Task.async(fn ->
68-
if Job.self_hosted?(job.machine_type),
69-
do: {:ok, nil},
70-
else: Cache.find(project.cache_id)
71-
end),
7266
find_artifact_token <-
7367
Task.async(fn ->
7468
Artifacthub.generate_token(project.artifact_store_id, job.id, project.id, spec)
7569
end),
7670
{:ok, repo_proxy} <- Task.await(find_repo_proxy),
71+
find_cache <-
72+
Task.async(fn ->
73+
if Job.self_hosted?(job.machine_type),
74+
do: {:ok, nil},
75+
else: Cache.find(project.cache_id, repo_proxy)
76+
end),
7777
find_secrets <-
7878
Task.async(fn -> Secrets.load(org_id, job.id, spec, project, repo_proxy) end),
7979
{:ok, repository, private_git_key} <- Task.await(find_repository),

zebra/lib/zebra/workers/job_request_factory/cache.ex

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ defmodule Zebra.Workers.JobRequestFactory.Cache do
1313
# Overall, if cache system is down, we ignore every issue.
1414
#
1515

16-
def find(nil) do
16+
def find(nil, _repo_proxy) do
1717
# We don't fail any jobs due to a missing cache connection,
1818
# but we should make sure we are aware of any issues in this area.
1919
Watchman.increment("external.cachehub.describe.failed")
2020

2121
{:ok, nil}
2222
end
2323

24-
def find(cache_id) do
24+
def find(cache_id, repo_proxy) do
2525
Watchman.benchmark("external.cachehub.describe", fn ->
2626
req = Request.new(cache_id: cache_id)
2727

28-
with {:ok, endpoint} <- Application.fetch_env(:zebra, :cachehub_api_endpoint),
28+
with false <- forked_pr?(repo_proxy),
29+
{:ok, endpoint} <- Application.fetch_env(:zebra, :cachehub_api_endpoint),
2930
{:ok, channel} <- GRPC.Stub.connect(endpoint),
3031
{:ok, response} <- Stub.describe(channel, req, timeout: 30_000) do
3132
if response.status.code == InternalApi.ResponseStatus.Code.value(:OK) do
@@ -38,6 +39,10 @@ defmodule Zebra.Workers.JobRequestFactory.Cache do
3839
{:ok, nil}
3940
end
4041
else
42+
true ->
43+
Logger.info("Skipping fetching of the cache as the job is part of Forked PR build.")
44+
{:ok, nil}
45+
4146
e ->
4247
Watchman.increment("external.cachehub.describe.failed")
4348
Logger.info("Failed to fetch info from cachehub #{cache_id}, #{inspect(e)}")
@@ -82,4 +87,12 @@ defmodule Zebra.Workers.JobRequestFactory.Cache do
8287
{:ok, vars}
8388
end
8489
end
90+
91+
defp forked_pr?(_repo = %{pr_slug: ""}), do: false
92+
93+
defp forked_pr?(repo) do
94+
[pr_repo | _rest] = repo.pr_slug |> String.split("/")
95+
[base_repo | _rest] = repo.repo_slug |> String.split("/")
96+
pr_repo != base_repo
97+
end
8598
end

0 commit comments

Comments
 (0)