Skip to content

Commit fd5a56e

Browse files
authored
feat: Github CI/CD Bot Allow Overriding PR Environment Name (#2643)
1 parent 56cbf8b commit fd5a56e

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

docs/integrations/github.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,18 @@ Below is an example of how to define the default config for the bot in either YA
284284

285285
### Configuration Properties
286286

287-
| Option | Description | Type | Required |
288-
|---------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------:|:--------:|
289-
| `invalidate_environment_after_deploy` | Indicates if the PR environment created should be automatically invalidated after changes are deployed. Invalidated environments are cleaned up automatically by the Janitor. Default: `True` | bool | N |
290-
| `merge_method` | The merge method to use when automatically merging a PR after deploying to prod. Defaults to `None` meaning automatic merge is not done. Options: `merge`, `squash`, `rebase` | string | N |
291-
| `enable_deploy_command` | Indicates if the `/deploy` command should be enabled in order to allowed synchronized deploys to production. Default: `False` | bool | N |
292-
| `command_namespace` | The namespace to use for SQLMesh commands. For example if you provide `#SQLMesh` as a value then commands will be expected in the format of `#SQLMesh/<command>`. Default: `None` meaning no namespace is used. | string | N |
293-
| `auto_categorize_changes` | Auto categorization behavior to use for the bot. If not provided then the project-wide categorization behavior is used. See [Auto-categorize model changes](https://sqlmesh.readthedocs.io/en/stable/guides/configuration/#auto-categorize-model-changes) for details. | dict | N |
294-
| `default_pr_start` | Default start when creating PR environment plans. If running in a mode where the bot automatically backfills models (based on `auto_categorize_changes` behavior) then this can be used to limit the amount of data backfilled. Defaults to `None` meaning the start date is set to the earliest model's start or to 1 day ago if [data previews](../concepts/plans.md#data-preview) need to be computed. | str | N |
295-
| `skip_pr_backfill` | Indicates if the bot should skip backfilling models in the PR environment. Default: `True` | bool | N |
296-
| `pr_include_unmodified` | Indicates whether to include unmodified models in the PR environment. Default to the project's config value (which defaults to `False`) | bool | N |
297-
| `run_on_deploy_to_prod` | Indicates whether to run latest intervals when deploying to prod. If set to false, the deployment will backfill only the changed models up to the existing latest interval in production, ignoring any missing intervals beyond this point. Default: `True` | bool | N |
287+
| Option | Description | Type | Required |
288+
|---------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------:|:--------:|
289+
| `invalidate_environment_after_deploy` | Indicates if the PR environment created should be automatically invalidated after changes are deployed. Invalidated environments are cleaned up automatically by the Janitor. Default: `True` | bool | N |
290+
| `merge_method` | The merge method to use when automatically merging a PR after deploying to prod. Defaults to `None` meaning automatic merge is not done. Options: `merge`, `squash`, `rebase` | string | N |
291+
| `enable_deploy_command` | Indicates if the `/deploy` command should be enabled in order to allowed synchronized deploys to production. Default: `False` | bool | N |
292+
| `command_namespace` | The namespace to use for SQLMesh commands. For example if you provide `#SQLMesh` as a value then commands will be expected in the format of `#SQLMesh/<command>`. Default: `None` meaning no namespace is used. | string | N |
293+
| `auto_categorize_changes` | Auto categorization behavior to use for the bot. If not provided then the project-wide categorization behavior is used. See [Auto-categorize model changes](https://sqlmesh.readthedocs.io/en/stable/guides/configuration/#auto-categorize-model-changes) for details. | dict | N |
294+
| `default_pr_start` | Default start when creating PR environment plans. If running in a mode where the bot automatically backfills models (based on `auto_categorize_changes` behavior) then this can be used to limit the amount of data backfilled. Defaults to `None` meaning the start date is set to the earliest model's start or to 1 day ago if [data previews](../concepts/plans.md#data-preview) need to be computed. | str | N |
295+
| `skip_pr_backfill` | Indicates if the bot should skip backfilling models in the PR environment. Default: `True` | bool | N |
296+
| `pr_include_unmodified` | Indicates whether to include unmodified models in the PR environment. Default to the project's config value (which defaults to `False`) | bool | N |
297+
| `run_on_deploy_to_prod` | Indicates whether to run latest intervals when deploying to prod. If set to false, the deployment will backfill only the changed models up to the existing latest interval in production, ignoring any missing intervals beyond this point. Default: `True` | bool | N |
298+
| `pr_environment_name` | The name of the PR environment to create for which a PR number will be appended to. Defaults to the repo name if not provided. Note: The name will be normalized to alphanumeric + underscore and lowercase. | str | N |
298299

299300
Example with all properties defined:
300301

sqlmesh/integrations/github/cicd/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class GithubCICDBotConfig(BaseConfig):
3333
skip_pr_backfill: bool = True
3434
pr_include_unmodified: t.Optional[bool] = None
3535
run_on_deploy_to_prod: bool = True
36+
pr_environment_name: t.Optional[str] = None
3637

3738
@model_validator(mode="before")
3839
@model_validator_v1_args

sqlmesh/integrations/github/cicd/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def pr_environment_name(self) -> str:
360360
return Environment.normalize_name(
361361
"_".join(
362362
[
363-
self._event.pull_request_info.repo,
363+
self.bot_config.pr_environment_name or self._event.pull_request_info.repo,
364364
str(self._event.pull_request_info.pr_number),
365365
]
366366
)

tests/integrations/github/cicd/test_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_load_yaml_config_default(tmp_path):
3838
assert not config.cicd_bot.enable_deploy_command
3939
assert config.cicd_bot.skip_pr_backfill
4040
assert config.cicd_bot.pr_include_unmodified is None
41+
assert config.cicd_bot.pr_environment_name is None
4142

4243

4344
def test_load_yaml_config(tmp_path):
@@ -59,6 +60,7 @@ def test_load_yaml_config(tmp_path):
5960
enable_deploy_command: true
6061
skip_pr_backfill: false
6162
pr_include_unmodified: true
63+
pr_environment_name: "MyOverride"
6264
model_defaults:
6365
dialect: duckdb
6466
""",
@@ -81,6 +83,7 @@ def test_load_yaml_config(tmp_path):
8183
assert config.cicd_bot.enable_deploy_command
8284
assert not config.cicd_bot.skip_pr_backfill
8385
assert config.cicd_bot.pr_include_unmodified
86+
assert config.cicd_bot.pr_environment_name == "MyOverride"
8487

8588

8689
def test_load_python_config_defaults(tmp_path):
@@ -110,6 +113,7 @@ def test_load_python_config_defaults(tmp_path):
110113
assert not config.cicd_bot.enable_deploy_command
111114
assert config.cicd_bot.skip_pr_backfill
112115
assert config.cicd_bot.pr_include_unmodified is None
116+
assert config.cicd_bot.pr_environment_name is None
113117

114118

115119
def test_load_python_config(tmp_path):
@@ -135,6 +139,7 @@ def test_load_python_config(tmp_path):
135139
enable_deploy_command=True,
136140
skip_pr_backfill=False,
137141
pr_include_unmodified=True,
142+
pr_environment_name="MyOverride",
138143
),
139144
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
140145
)
@@ -159,6 +164,7 @@ def test_load_python_config(tmp_path):
159164
assert config.cicd_bot.enable_deploy_command
160165
assert not config.cicd_bot.skip_pr_backfill
161166
assert config.cicd_bot.pr_include_unmodified
167+
assert config.cicd_bot.pr_environment_name == "MyOverride"
162168

163169

164170
def test_validation(tmp_path):

tests/integrations/github/cicd/test_github_commands.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def test_run_all_success_with_approvers_approved(
6666
controller = make_controller(
6767
"tests/fixtures/github/pull_request_synchronized.json",
6868
github_client,
69-
bot_config=GithubCICDBotConfig(invalidate_environment_after_deploy=False),
69+
bot_config=GithubCICDBotConfig(
70+
invalidate_environment_after_deploy=False, pr_environment_name="MyOverride"
71+
),
7072
)
7173
controller._context._run_tests = mocker.MagicMock(
7274
side_effect=lambda **kwargs: (TestResult(), "")
@@ -127,7 +129,7 @@ def test_run_all_success_with_approvers_approved(
127129

128130
assert len(controller._context.apply.call_args_list) == 2
129131
pr_plan = controller._context.apply.call_args_list[0][0]
130-
assert pr_plan[0].environment.name == "hello_world_2"
132+
assert pr_plan[0].environment.name == "myoverride_2"
131133
prod_plan = controller._context.apply.call_args_list[1][0]
132134
assert prod_plan[0].environment.name == c.PROD
133135

@@ -137,7 +139,7 @@ def test_run_all_success_with_approvers_approved(
137139
assert len(created_comments) == 1
138140
assert created_comments[0].body.startswith(
139141
"""**SQLMesh Bot Info**
140-
- PR Virtual Data Environment: hello_world_2
142+
- PR Virtual Data Environment: myoverride_2
141143
<details>
142144
<summary>Prod Plan Being Applied</summary>
143145
@@ -147,7 +149,7 @@ def test_run_all_success_with_approvers_approved(
147149
output = f.read()
148150
assert (
149151
output
150-
== "run_unit_tests=success\nhas_required_approval=success\ncreated_pr_environment=true\npr_environment_name=hello_world_2\npr_environment_synced=success\nprod_plan_preview=success\nprod_environment_synced=success\n"
152+
== "run_unit_tests=success\nhas_required_approval=success\ncreated_pr_environment=true\npr_environment_name=myoverride_2\npr_environment_synced=success\nprod_plan_preview=success\nprod_environment_synced=success\n"
151153
)
152154

153155

0 commit comments

Comments
 (0)