Skip to content

Commit ccb29cb

Browse files
committed
try again
1 parent 93ddee4 commit ccb29cb

File tree

3 files changed

+125
-116
lines changed

3 files changed

+125
-116
lines changed

hooli-data-eng/hooli_data_eng/definitions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dagster as dg
22
from dagster.components import load_defs
33
import hooli_data_eng.defs
4+
from hooli_data_eng.defs.dbt.slim_ci_job import dbt_slim_ci_with_github_job
45

56

67
# ---------------------------------------------------
@@ -15,5 +16,6 @@
1516
dg.Definitions(
1617
# only apply this setting once
1718
executor=dg.multiprocess_executor.configured({"max_concurrent": 3}),
19+
jobs=[dbt_slim_ci_with_github_job],
1820
),
1921
)

hooli-data-eng/hooli_data_eng/defs/dbt/component.py

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
import textwrap
3-
import os
43
from typing import Any, Mapping, Union, Literal, Optional
54
import dagster as dg
65
from hooli_data_eng.utils import get_env
@@ -17,7 +16,6 @@
1716
from datetime import datetime
1817
from hooli_data_eng.defs.dbt.dbt_code_version import get_current_dbt_code_version
1918
from hooli_data_eng.defs.dbt.resources import resource_def
20-
from github import Github
2119

2220
from datetime import timedelta
2321
from dagster.preview.freshness import apply_freshness_policy
@@ -242,7 +240,6 @@ def _dbt_asset(context: dg.AssetExecutionContext, dbt: DbtCliResource):
242240
asset_checks=checks,
243241
sensors=sensors,
244242
resources=resource_def[get_env()],
245-
jobs=[get_slim_ci_job()],
246243
)
247244

248245
defs = defs.map_resolved_asset_specs(
@@ -297,116 +294,3 @@ def dbt_code_version_sensor(context: dg.SensorEvaluationContext):
297294
return dbt_code_version_sensor
298295

299296

300-
def get_slim_ci_job():
301-
# This op will be used to run slim CI
302-
@dg.op(out={})
303-
def dbt_slim_ci(context: dg.OpExecutionContext, dbt: DbtCliResource):
304-
dbt_command = [
305-
"build",
306-
"--select",
307-
"state:modified.body+",
308-
"--defer",
309-
"--state",
310-
dbt.state_path,
311-
]
312-
313-
dbt_cli_task = dbt.cli(
314-
args=dbt_command,
315-
manifest=dbt_project.manifest_path,
316-
dagster_dbt_translator=get_hooli_translator(),
317-
)
318-
319-
# Collect results and track successful models
320-
successful_models = []
321-
322-
for event in dbt_cli_task.stream().fetch_row_counts().fetch_column_metadata():
323-
yield event
324-
325-
# Get run results to identify successful models
326-
try:
327-
run_results_json = dbt_cli_task.get_artifact("run_results.json")
328-
for result in run_results_json.get("results", []):
329-
if result.get("status") == "success":
330-
model_name = result.get("unique_id", "").split(".")[-1]
331-
if model_name:
332-
successful_models.append(model_name)
333-
334-
context.log.info(f"Successfully ran {len(successful_models)} dbt models: {successful_models}")
335-
336-
# Post GitHub comment if in CI environment
337-
_post_github_comment(context, successful_models)
338-
339-
except Exception as e:
340-
context.log.warning(f"Could not parse run results or post GitHub comment: {e}")
341-
342-
def _post_github_comment(context: dg.OpExecutionContext, successful_models: list):
343-
"""Post a comment to the GitHub PR about successful dbt models"""
344-
# Only post comments in CI/branch environment
345-
if get_env() != "BRANCH":
346-
context.log.info("Not in branch environment, skipping GitHub comment")
347-
return
348-
349-
# Get GitHub token and repo info from environment
350-
github_token = os.getenv("GITHUB_TOKEN")
351-
github_repo = os.getenv("GITHUB_REPOSITORY") # format: owner/repo - available by default
352-
353-
# Get PR number from GitHub event (available in pull_request events)
354-
github_event_path = os.getenv("GITHUB_EVENT_PATH")
355-
github_pr_number = None
356-
357-
if github_event_path:
358-
try:
359-
with open(github_event_path, 'r') as f:
360-
event_data = json.load(f)
361-
github_pr_number = event_data.get("number") or event_data.get("pull_request", {}).get("number")
362-
except Exception as e:
363-
context.log.warning(f"Could not parse GitHub event data: {e}")
364-
365-
# Fallback to GITHUB_REF for PR number extraction
366-
if not github_pr_number:
367-
github_ref = os.getenv("GITHUB_REF") # format: refs/pull/{pr_number}/merge
368-
if github_ref and github_ref.startswith("refs/pull/") and github_ref.endswith("/merge"):
369-
try:
370-
github_pr_number = int(github_ref.split("/")[2])
371-
except (IndexError, ValueError):
372-
pass
373-
374-
if not all([github_token, github_repo, github_pr_number]):
375-
context.log.warning(
376-
f"Missing required GitHub information. "
377-
f"Token: {'✓' if github_token else '✗'}, "
378-
f"Repo: {'✓' if github_repo else '✗'}, "
379-
f"PR: {'✓' if github_pr_number else '✗'}. "
380-
"Cannot post comment."
381-
)
382-
return
383-
384-
try:
385-
g = Github(github_token)
386-
repo = g.get_repo(github_repo)
387-
pr = repo.get_pull(int(github_pr_number))
388-
389-
if not successful_models:
390-
comment_body = "✅ **dbt Slim CI Results**\n\nNo dbt models were modified or ran successfully in this PR."
391-
else:
392-
model_list = "\n".join([f"- `{model}`" for model in sorted(successful_models)])
393-
comment_body = f"""✅ **dbt Slim CI Results**
394-
395-
Successfully ran **{len(successful_models)}** dbt model(s):
396-
397-
{model_list}
398-
399-
All models passed validation and tests!"""
400-
401-
pr.create_issue_comment(comment_body)
402-
context.log.info(f"Posted GitHub comment to PR #{github_pr_number}")
403-
404-
except Exception as e:
405-
context.log.error(f"Failed to post GitHub comment: {e}")
406-
407-
# This job will be triggered by Pull Request and should only run new or changed dbt models
408-
@dg.job(name="dbt_slim_ci_with_github_job")
409-
def dbt_slim_ci_with_github_job():
410-
dbt_slim_ci()
411-
412-
return dbt_slim_ci_with_github_job
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import json
2+
import os
3+
from typing import Any, Mapping, Union, Literal, Optional
4+
import dagster as dg
5+
from hooli_data_eng.utils import get_env
6+
from dagster_dbt import DbtCliResource
7+
from hooli_data_eng.defs.dbt.resources import dbt_project
8+
from hooli_data_eng.defs.dbt.component import get_hooli_translator
9+
from github import Github
10+
11+
12+
@dg.op(out={})
13+
def dbt_slim_ci(context: dg.OpExecutionContext, dbt: DbtCliResource):
14+
"""Run dbt slim CI and post GitHub comment with successful models"""
15+
dbt_command = [
16+
"build",
17+
"--select",
18+
"state:modified.body+",
19+
"--defer",
20+
"--state",
21+
dbt.state_path,
22+
]
23+
24+
dbt_cli_task = dbt.cli(
25+
args=dbt_command,
26+
manifest=dbt_project.manifest_path,
27+
dagster_dbt_translator=get_hooli_translator(),
28+
)
29+
30+
# Collect results and track successful models
31+
successful_models = []
32+
33+
for event in dbt_cli_task.stream().fetch_row_counts().fetch_column_metadata():
34+
yield event
35+
36+
# Get run results to identify successful models
37+
try:
38+
run_results_json = dbt_cli_task.get_artifact("run_results.json")
39+
for result in run_results_json.get("results", []):
40+
if result.get("status") == "success":
41+
model_name = result.get("unique_id", "").split(".")[-1]
42+
if model_name:
43+
successful_models.append(model_name)
44+
45+
context.log.info(f"Successfully ran {len(successful_models)} dbt models: {successful_models}")
46+
47+
# Post GitHub comment if in CI environment
48+
_post_github_comment(context, successful_models)
49+
50+
except Exception as e:
51+
context.log.warning(f"Could not parse run results or post GitHub comment: {e}")
52+
53+
54+
def _post_github_comment(context: dg.OpExecutionContext, successful_models: list):
55+
"""Post a comment to the GitHub PR about successful dbt models"""
56+
# Only post comments in CI/branch environment
57+
if get_env() != "BRANCH":
58+
context.log.info("Not in branch environment, skipping GitHub comment")
59+
return
60+
61+
# Get GitHub token and repo info from environment
62+
github_token = os.getenv("GITHUB_TOKEN")
63+
github_repo = os.getenv("GITHUB_REPOSITORY") # format: owner/repo - available by default
64+
65+
# Get PR number from GitHub event (available in pull_request events)
66+
github_event_path = os.getenv("GITHUB_EVENT_PATH")
67+
github_pr_number = None
68+
69+
if github_event_path:
70+
try:
71+
with open(github_event_path, 'r') as f:
72+
event_data = json.load(f)
73+
github_pr_number = event_data.get("number") or event_data.get("pull_request", {}).get("number")
74+
except Exception as e:
75+
context.log.warning(f"Could not parse GitHub event data: {e}")
76+
77+
# Fallback to GITHUB_REF for PR number extraction
78+
if not github_pr_number:
79+
github_ref = os.getenv("GITHUB_REF") # format: refs/pull/{pr_number}/merge
80+
if github_ref and github_ref.startswith("refs/pull/") and github_ref.endswith("/merge"):
81+
try:
82+
github_pr_number = int(github_ref.split("/")[2])
83+
except (IndexError, ValueError):
84+
pass
85+
86+
if not all([github_token, github_repo, github_pr_number]):
87+
context.log.warning(
88+
f"Missing required GitHub information. "
89+
f"Token: {'✓' if github_token else '✗'}, "
90+
f"Repo: {'✓' if github_repo else '✗'}, "
91+
f"PR: {'✓' if github_pr_number else '✗'}. "
92+
"Cannot post comment."
93+
)
94+
return
95+
96+
try:
97+
g = Github(github_token)
98+
repo = g.get_repo(github_repo)
99+
pr = repo.get_pull(int(github_pr_number))
100+
101+
if not successful_models:
102+
comment_body = "✅ **dbt Slim CI Results**\n\nNo dbt models were modified or ran successfully in this PR."
103+
else:
104+
model_list = "\n".join([f"- `{model}`" for model in sorted(successful_models)])
105+
comment_body = f"""✅ **dbt Slim CI Results**
106+
107+
Successfully ran **{len(successful_models)}** dbt model(s):
108+
109+
{model_list}
110+
111+
All models passed validation and tests!"""
112+
113+
pr.create_issue_comment(comment_body)
114+
context.log.info(f"Posted GitHub comment to PR #{github_pr_number}")
115+
116+
except Exception as e:
117+
context.log.error(f"Failed to post GitHub comment: {e}")
118+
119+
120+
@dg.job(name="dbt_slim_ci_with_github_job")
121+
def dbt_slim_ci_with_github_job():
122+
"""Job that runs dbt slim CI and posts GitHub comments"""
123+
dbt_slim_ci()

0 commit comments

Comments
 (0)