-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add support for custom context to task-context #971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bhearsum
wants to merge
1
commit into
taskcluster:main
Choose a base branch
from
bhearsum:feat-add-support-for-custom-co
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−30
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| from taskgraph.transforms.base import TransformSequence | ||
| from taskgraph.util.schema import Schema, resolve_keyed_by | ||
| from taskgraph.util.task_context import CUSTOM_CONTEXT_MAP | ||
| from taskgraph.util.templates import deep_get, substitute_task_fields | ||
| from taskgraph.util.yaml import load_yaml | ||
|
|
||
|
|
@@ -28,6 +29,12 @@ class TaskContextConfig(Schema): | |
| from_file: Optional[str] = None | ||
| # Key/value pairs to be used as task context | ||
| from_object: Optional[object] = None | ||
| # Retrieve task context values from registered custom providers. Each | ||
| # entry is the name of a provider registered via | ||
| # ``taskgraph.util.task_context.custom_context``. Providers are called | ||
| # with the ``TransformConfig`` and the task being rendered, and must | ||
| # return a dict of key/value pairs to add to the context. | ||
| from_custom: Optional[list[str]] = None | ||
| resolve_keyed_by_options: Optional[ResolveKeyedByConfigOptions] = None | ||
|
|
||
|
|
||
|
|
@@ -51,6 +58,7 @@ class TaskContextSchema(Schema, forbid_unknown_fields=False, kw_only=True): | |
| # is as follows: | ||
| # - Parameters | ||
| # - `from-object` keys | ||
| # - Custom providers (`from-custom`) | ||
| # - File | ||
| # | ||
| # That is to say: parameters will always override anything else. | ||
|
|
@@ -89,10 +97,15 @@ def render_task(config, tasks): | |
| if from_file: | ||
| file_context = load_yaml(from_file) | ||
|
|
||
| custom_context = {} | ||
| for name in sub_config.pop("from-custom", None) or []: | ||
| custom_context.update(CUSTOM_CONTEXT_MAP[name](config, task)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check that |
||
|
|
||
| fields = sub_config.pop("substitution-fields") | ||
|
|
||
| subs = {} | ||
| subs.update(file_context) | ||
| subs.update(custom_context) | ||
| # We've popped away the configuration; everything left in `sub_config` is | ||
| # substitution key/value pairs. | ||
| subs.update(sub_config.pop("from-object", {})) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| # Define a collection of custom task-context providers. | ||
| # Note: this is stored here instead of where it is used in the | ||
| # `task_context` transform to give consumers a chance to register their own | ||
| # providers before the `task_context` schema is created. | ||
| CUSTOM_CONTEXT_MAP = {} | ||
|
|
||
|
|
||
| def custom_context(name): | ||
| def wrapper(func): | ||
| assert name not in CUSTOM_CONTEXT_MAP, ( | ||
| f"duplicate custom_context function name {name} ({func} and {CUSTOM_CONTEXT_MAP[name]})" | ||
| ) | ||
| CUSTOM_CONTEXT_MAP[name] = func | ||
| return func | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the tests cover the order of precedence?