-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add config-driven release_level utility #220
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,38 @@ | ||
| # 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/. | ||
|
|
||
| from typing import Optional, Union | ||
|
|
||
| from taskgraph.parameters import extend_parameters_schema | ||
| from taskgraph.util.schema import Schema | ||
|
|
||
|
|
||
| class MozillaParametersSchema(Schema, kw_only=True): | ||
| # Branches of the current project that are considered "production" | ||
| # releases. Resolved from the graph config's `release-branches` mapping by | ||
| # `set_release_branches`. `True` means all branches of the project are | ||
| # release branches, a list restricts releases to the named branches, and | ||
| # `None` means the project has no release branches. | ||
| release_branches: Optional[Union[bool, list[str]]] = None | ||
|
|
||
|
|
||
| def get_defaults(repo_root=None): | ||
| return { | ||
| "release_branches": None, | ||
| } | ||
|
|
||
|
|
||
| def register_parameters(): | ||
| extend_parameters_schema(MozillaParametersSchema, defaults_fn=get_defaults) | ||
|
|
||
|
|
||
| def set_release_branches(graph_config, parameters): | ||
| """Resolve the current project's release branches from the graph config. | ||
|
|
||
| Projects should call this from their `decision-parameters` function so that | ||
| `release_branches` is persisted into `parameters.yml` and available to | ||
| consumers that only have a `Parameters` object (e.g. action tasks). | ||
| """ | ||
| mapping = graph_config._config.get("release-branches") or {} | ||
| parameters["release_branches"] = mapping.get(parameters["project"]) |
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,34 @@ | ||
| # 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/. | ||
|
|
||
| import re | ||
|
|
||
|
|
||
| def release_level(params): | ||
| """Whether this is a production release or not. | ||
|
|
||
| The set of branches considered "production" is project specific and comes | ||
| from the ``release_branches`` parameter, which is resolved at decision time | ||
| from the graph config's ``release-branches`` mapping. A value of ``True`` | ||
| means every branch of the project is a release branch (the model used by | ||
| Mercurial based projects), while a list restricts releases to the named | ||
| branches. | ||
|
|
||
| :return str: One of "production" or "staging". | ||
| """ | ||
| if params["level"] != "3": | ||
| return "staging" | ||
|
|
||
| branches = params.get("release_branches") | ||
| if not branches: | ||
| return "staging" | ||
|
|
||
| if branches is True: | ||
| return "production" | ||
|
|
||
| m = re.match(r"refs/heads/(\S+)$", params["head_ref"]) | ||
|
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. Is there a way to write this function such that we only have two returns? It would be easier to read if eg: we returned |
||
| if m is not None and m.group(1) in branches: | ||
| return "production" | ||
|
|
||
| return "staging" | ||
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,34 @@ | ||
| # 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/. | ||
|
|
||
| import pytest | ||
|
|
||
| from mozilla_taskgraph.parameters import set_release_branches | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "release_branches,project,expected", | ||
| ( | ||
| # No `release-branches` config at all. | ||
| (None, "some-project", None), | ||
| # Project not listed. | ||
| ({"firefox": ["main"]}, "autoland", None), | ||
| # Whole project is a release project. | ||
| ({"mozilla-central": True}, "mozilla-central", True), | ||
| # Project mapped to a list of branches. | ||
| ({"firefox": ["main", "beta"]}, "firefox", ["main", "beta"]), | ||
| ), | ||
| ) | ||
| def test_set_release_branches( | ||
| make_graph_config, parameters, release_branches, project, expected | ||
| ): | ||
| extra_config = {} | ||
| if release_branches is not None: | ||
| extra_config["release-branches"] = release_branches | ||
|
|
||
| graph_config = make_graph_config(extra_config=extra_config) | ||
| parameters["project"] = project | ||
|
|
||
| set_release_branches(graph_config, parameters) | ||
| assert parameters["release_branches"] == expected |
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,58 @@ | ||
| # 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/. | ||
|
|
||
| import pytest | ||
|
|
||
| from mozilla_taskgraph.util.attributes import release_level | ||
|
|
||
| FIREFOX_BRANCHES = ["main", "beta", "release", "esr140"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "params,expected", | ||
| ( | ||
| # Not level 3 -> always staging, regardless of branch. | ||
| ({"level": "1", "release_branches": True}, "staging"), | ||
| ( | ||
| { | ||
| "level": "1", | ||
| "release_branches": FIREFOX_BRANCHES, | ||
| "head_ref": "refs/heads/beta", | ||
| }, | ||
| "staging", | ||
| ), | ||
| # No release branches for the project -> staging (e.g. autoland). | ||
| ({"level": "3", "release_branches": None}, "staging"), | ||
| # Whole project is a release project (Mercurial model). | ||
| ({"level": "3", "release_branches": True}, "production"), | ||
| # Git monorepo model: only listed branches are production. | ||
| ( | ||
| { | ||
| "level": "3", | ||
| "release_branches": FIREFOX_BRANCHES, | ||
| "head_ref": "refs/heads/beta", | ||
| }, | ||
| "production", | ||
| ), | ||
| ( | ||
| { | ||
| "level": "3", | ||
| "release_branches": FIREFOX_BRANCHES, | ||
| "head_ref": "refs/heads/test", | ||
| }, | ||
| "staging", | ||
| ), | ||
| # Only refs/heads/* match, not tags. | ||
| ( | ||
| { | ||
| "level": "3", | ||
| "release_branches": FIREFOX_BRANCHES, | ||
| "head_ref": "refs/tags/beta", | ||
| }, | ||
| "staging", | ||
| ), | ||
| ), | ||
| ) | ||
| def test_release_level(params, expected): | ||
| assert release_level(params) == expected |
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.
Is there a reason we want to route these through parameters? Parameters are typically for things that are associated with a specific event; these are clearly not. IMO it would be preferable to have callers pass along the graph config here (or simply the
release-branchesfrom the graph config) instead of adding this indirection.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.
The reason is that there are callers that don't have
graph_configavailable. For example theRUN_ON_PROJECT_ALIASESpath.https://searchfox.org/firefox-main/source/taskcluster/gecko_taskgraph/target_tasks.py#117
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.
I'm not seeing any consumers of
release_levelintarget_tasks.py? (In any case, the target task handler functions do get passed the graph config if it is needed by anything they call, eg: https://searchfox.org/firefox-main/rev/bbdd22a8b58a05e5b269fe341b02ce11426c550f/taskcluster/gecko_taskgraph/target_tasks.py#475-477)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.
match_run_on_projectsis called intarget_tasks.py, which in turn calls(RUN_ON_PROJECT_ALIASES[alias](params):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.
Ah, thank you - I missed that indirection. In any case, graph config is available to be fed through as mentioned, so there should be no need to add a new parameter here.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, I double checked. You're right. The task handlers do have
graph_configin scope, only the plumbing is not there. So that would need to be updated.If we go down that path, then we can avoid setting
release_branchesin parameters here, and makerelease_level's signature as:I believe we still need
paramsin order to accesslevelandhead_ref?Does that make sense?
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.
Personally, I would pass the specific things that are needed from them instead of the entire data structures (it makes the signature more readable, and reduces the temptation to depend on more and more parts of those data structures), but I wouldn't r- this either way.
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.
Hey @bhearsum, I've just a pushed a new commit where I implemented you suggestions. Let me know what you think.
release_levelnow takesrelease_branches(directly, instead of the wholegraph_config), however I decided to keepparamsin the function signature instead of specifying the other 3 pieces of data it requires (level,head_refandproject).