Skip to content

Commit

Permalink
feat: verify decision task has sufficient scopes before submitting
Browse files Browse the repository at this point in the history
  • Loading branch information
ahal committed Aug 25, 2022
1 parent 42a10da commit fcc38c3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/taskgraph/decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from taskgraph.util.python_path import find_object
from taskgraph.util.schema import Schema, validate_schema
from taskgraph.util.vcs import Repository, get_repository
from taskgraph.util.verify import verifications
from taskgraph.util.yaml import load_yaml

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -123,6 +124,9 @@ def taskgraph_decision(options, parameters=None):
shutil.copy2(RUN_TASK_DIR / "run-task", ARTIFACTS_DIR)
shutil.copy2(RUN_TASK_DIR / "fetch-content", ARTIFACTS_DIR)

# run 'decision' verifications
verifications("decision", tgg.morphed_task_graph, tgg.graph_config, tgg.parameters)

# actually create the graph
create_tasks(
tgg.graph_config,
Expand Down
45 changes: 45 additions & 0 deletions src/taskgraph/util/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import logging
import sys
from abc import ABC, abstractmethod
from textwrap import dedent

import attr

from taskgraph.config import GraphConfig
from taskgraph.parameters import Parameters
from taskgraph.taskgraph import TaskGraph
from taskgraph.util.attributes import match_run_on_projects
from taskgraph.util.taskcluster import get_current_scopes
from taskgraph.util.treeherder import join_symbol

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -281,3 +283,46 @@ def verify_always_optimized(task, taskgraph, scratch_pad, graph_config, paramete
return
if task.task.get("workerType") == "always-optimized":
raise Exception(f"Could not optimize the task {task.label!r}")


@verifications.add("decision")
def verify_scopes_satisfaction(task, taskgraph, scratch_pad, graph_config, parameters):
if task is None:
if not scratch_pad:
return

s = "s" if len(scratch_pad) else ""
are = "are" if len(scratch_pad) else "is"

failstr = ""
for label, scopes in scratch_pad.items():
failstr += "\n" + f" {label}:"
failstr += (
" \n" + "\n ".join([f" {s}" for s in sorted(scopes)]) + "\n"
)

msg = dedent(
f"""
Required scopes are missing!
The Decision task does not have all of the scopes necessary to
perform this request. The following task{s} {are} requesting scopes
the Decision task does not have:
"""
)
msg += failstr
raise Exception(msg)

current_scopes = set(get_current_scopes())
missing = set()
for required in task.task["scopes"]:
for current in current_scopes:
if current == required:
break
if current[-1] == "*" and required.startswith(current[:-1]):
break
else:
missing.add(required)

if missing:
scratch_pad[task.label] = sorted(missing)

0 comments on commit fcc38c3

Please sign in to comment.