diff --git a/ddev/changelog.d/21993.fixed b/ddev/changelog.d/21993.fixed new file mode 100644 index 0000000000000..072abe62da962 --- /dev/null +++ b/ddev/changelog.d/21993.fixed @@ -0,0 +1 @@ +Ensure correct commit attribution when sending size metrics by requiring an explicit commit in the ddev size status command. \ No newline at end of file diff --git a/ddev/src/ddev/cli/size/status.py b/ddev/src/ddev/cli/size/status.py index 2e88b08ae3436..b487b19f25b69 100644 --- a/ddev/src/ddev/cli/size/status.py +++ b/ddev/src/ddev/cli/size/status.py @@ -103,10 +103,10 @@ def status( from ddev.cli.size.utils.common_funcs import export_format export_format(app, format, modules_plat_ver, "status", platform, version, compressed) - if to_dd_org or to_dd_key: + if (to_dd_org or to_dd_key) and commit: from ddev.cli.size.utils.common_funcs import send_metrics_to_dd - send_metrics_to_dd(app, modules_plat_ver, to_dd_org, to_dd_key, compressed) + send_metrics_to_dd(app, commit, modules_plat_ver, to_dd_org, to_dd_key, compressed) except Exception as e: app.abort(str(e)) @@ -144,6 +144,9 @@ def validate_parameters( if to_dd_org and to_dd_key: errors.append("Specify either --to-dd-org or --to-dd-key, not both") + if (to_dd_org or to_dd_key) and not commit: + errors.append("In order to send metrics to Datadog, you need to provide a commit hash") + if errors: app.abort("\n".join(errors)) diff --git a/ddev/src/ddev/cli/size/utils/common_funcs.py b/ddev/src/ddev/cli/size/utils/common_funcs.py index 9e33a4562c1c3..5d64d752cf7f5 100644 --- a/ddev/src/ddev/cli/size/utils/common_funcs.py +++ b/ddev/src/ddev/cli/size/utils/common_funcs.py @@ -837,6 +837,7 @@ def draw_treemap_rects_with_labels( def send_metrics_to_dd( app: Application, + commit: str, modules: list[FileDataEntryPlatformVersion], org: str | None, key: str | None, @@ -852,8 +853,7 @@ def send_metrics_to_dd( if "site" not in config_file_info: raise RuntimeError("No site found in config file") - message, tickets, prs = get_last_commit_data() - timestamp = get_last_commit_timestamp() + timestamp, message, tickets, prs = get_commit_data(commit) metrics = [] n_integrations_metrics = [] @@ -965,24 +965,28 @@ def send_metrics_to_dd( api.Metric.send(metrics=n_dependencies_metrics) -def get_last_commit_timestamp() -> int: - result = subprocess.run(["git", "log", "-1", "--format=%ct"], capture_output=True, text=True, check=True) - return int(result.stdout.strip()) - +def get_commit_data(commit: str) -> tuple[int, str, list[str], list[str]]: + ''' + Gets the timestamp, message, tickets and PRs of a given commit. If no commit is provided, it uses the last commit. + ''' + result = subprocess.run( + ["git", "show", "-s", "--format=%ct,%s", commit], + capture_output=True, + text=True, + check=True, + ) -def get_last_commit_data() -> tuple[str, list[str], list[str]]: - result = subprocess.run(["git", "log", "-1", "--format=%s"], capture_output=True, text=True, check=True) + timestamp, message = result.stdout.strip().split(',', 1) ticket_pattern = r'\b(?:DBMON|SAASINT|AGENT|AI)-\d+\b' pr_pattern = r'#(\d+)' - message = result.stdout.strip() tickets = re.findall(ticket_pattern, message) prs = re.findall(pr_pattern, message) if not tickets: tickets = [""] if not prs: prs = [""] - return message, tickets, prs + return int(timestamp), message, tickets, prs @cache