diff --git a/src/taskgraph/config.py b/src/taskgraph/config.py index 3c1a2762e..ac384eab8 100644 --- a/src/taskgraph/config.py +++ b/src/taskgraph/config.py @@ -66,6 +66,10 @@ description="The taskcluster index prefix to use for caching tasks. " "Defaults to `trust-domain`.", ): str, + Optional( + "cache-pull-requests", + description="Should tasks from pull requests populate the cache", + ): bool, Optional( "index-path-regexes", description="Regular expressions matching index paths to be summarized.", diff --git a/src/taskgraph/util/cached_tasks.py b/src/taskgraph/util/cached_tasks.py index edaf31908..4105b05fd 100644 --- a/src/taskgraph/util/cached_tasks.py +++ b/src/taskgraph/util/cached_tasks.py @@ -65,7 +65,9 @@ def add_optimization( # Pull requests use a different target cache index route. This way we can # be confident they won't be used by anything other than the pull request # that created the cache in the first place. - if config.params["tasks_for"].startswith("github-pull-request"): + if config.params["tasks_for"].startswith( + "github-pull-request" + ) and config.graph_config["taskgraph"].get("cache-pull-requests", True): subs["head_ref"] = config.params["head_ref"] if subs["head_ref"].startswith("refs/heads/"): subs["head_ref"] = subs["head_ref"][11:] @@ -77,9 +79,10 @@ def add_optimization( subs["level"] = config.params["level"] if config.params["tasks_for"].startswith("github-pull-request"): - taskdesc.setdefault("routes", []).append( - f"index.{TARGET_PR_CACHE_INDEX.format(**subs)}" - ) + if config.graph_config["taskgraph"].get("cache-pull-requests", True): + taskdesc.setdefault("routes", []).append( + f"index.{TARGET_PR_CACHE_INDEX.format(**subs)}" + ) else: taskdesc.setdefault("routes", []).append( f"index.{TARGET_CACHE_INDEX.format(**subs)}" diff --git a/test/test_util_cached_tasks.py b/test/test_util_cached_tasks.py index 8583739dc..049a80520 100644 --- a/test/test_util_cached_tasks.py +++ b/test/test_util_cached_tasks.py @@ -93,6 +93,22 @@ def assert_pull_request(task): } +def assert_pull_request_nocache(task): + handle_exception(task) + assert task == { + "attributes": { + "cached_task": {"digest": "abc", "name": "cache-name", "type": "cache-type"} + }, + "optimization": { + "index-search": [ + "test-domain.cache.level-3.cache-type.cache-name.hash.abc", + "test-domain.cache.level-2.cache-type.cache-name.hash.abc", + "test-domain.cache.level-1.cache-type.cache-name.hash.abc", + ] + }, + } + + @pytest.mark.parametrize( "extra_params,extra_graph_config,digest,digest_data", ( @@ -151,6 +167,17 @@ def assert_pull_request(task): None, id="pull_request", ), + pytest.param( + # extra_params + {"tasks_for": "github-pull-request"}, + # extra_graph_config + {"taskgraph": {"cache-pull-requests": False}}, + # digest + "abc", + # digest_data + None, + id="pull_request_nocache", + ), ), ) def test_add_optimization(