Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/target_tasks.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
6 from taskgraph.util.attributes import (
7 match_run_on_git_branches,
8 match_run_on_projects,
9 match_run_on_tasks_for,
10 )
11
12 _target_task_methods = {}
13
14 _GIT_REFS_HEADS_PREFIX = "refs/heads/"
15
16
17 def register_target_task(name):
18 def wrap(func):
19 _target_task_methods[name] = func
20 return func
21
22 return wrap
23
24
25 def get_method(method):
26 """Get a target_task_method to pass to a TaskGraphGenerator."""
27 return _target_task_methods[method]
28
29
30 def filter_out_cron(task, parameters):
31 """
32 Filter out tasks that run via cron.
33 """
34 return not task.attributes.get("cron")
35
36
37 def filter_for_project(task, parameters):
38 """Filter tasks by project. Optionally enable nightlies."""
39 run_on_projects = set(task.attributes.get("run_on_projects", []))
40 return match_run_on_projects(parameters["project"], run_on_projects)
41
42
43 def filter_for_tasks_for(task, parameters):
44 run_on_tasks_for = set(task.attributes.get("run_on_tasks_for", ["all"]))
45 return match_run_on_tasks_for(parameters["tasks_for"], run_on_tasks_for)
46
47
48 def filter_for_git_branch(task, parameters):
49 """Filter tasks by git branch.
50 If `run_on_git_branch` is not defined, then task runs on all branches"""
51 # We cannot filter out on git branches if we not on a git repository
52 if parameters.get("repository_type") != "git":
53 return True
54
55 # Pull requests usually have arbitrary names, let's not filter git branches on them.
56 if parameters["tasks_for"] == "github-pull-request":
57 return True
58
59 run_on_git_branches = set(task.attributes.get("run_on_git_branches", ["all"]))
60 git_branch = parameters["head_ref"]
61 if git_branch.startswith(_GIT_REFS_HEADS_PREFIX):
62 git_branch = git_branch[len(_GIT_REFS_HEADS_PREFIX) :]
63
64 return match_run_on_git_branches(git_branch, run_on_git_branches)
65
66
67 def filter_out_shipping_phase(task, parameters):
68 return task.attributes.get("shipping_phase") in (None, "build")
69
70
71 def standard_filter(task, parameters):
72 return all(
73 filter_func(task, parameters)
74 for filter_func in (
75 filter_out_cron,
76 filter_out_shipping_phase,
77 filter_for_project,
78 filter_for_tasks_for,
79 filter_for_git_branch,
80 )
81 )
82
83
84 @register_target_task("default")
85 def target_tasks_default(full_task_graph, parameters, graph_config):
86 """Target the tasks which have indicated they should be run on this project
87 via the `run_on_projects` attributes."""
88 return [
89 l for l, t in full_task_graph.tasks.items() if standard_filter(t, parameters)
90 ]
91
92
93 @register_target_task("codereview")
94 def target_tasks_codereview(full_task_graph, parameters, graph_config):
95 """Target the tasks which have indicated they should be run on this project
96 via the `run_on_projects` attributes."""
97 return [
98 l
99 for l, t in full_task_graph.tasks.items()
100 if standard_filter(t, parameters) and t.attributes.get("code-review")
101 ]
102
103
104 @register_target_task("nothing")
105 def target_tasks_nothing(full_task_graph, parameters, graph_config):
106 """Select nothing, for DONTBUILD pushes"""
107 return []