Skip to content

Commit 98a9a46

Browse files
lucia-sbAAraKKe
authored andcommitted
Fix commit attribution in ddev size status (#21993)
* Specify commit for the timestamp * Add changelog * Fix Lint * Update ddev/changelog.d/21993.fixed Co-authored-by: Juanpe Araque <[email protected]> * move commit validation to the start of the command * Fix lint --------- Co-authored-by: Juanpe Araque <[email protected]>
1 parent 81ae793 commit 98a9a46

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

ddev/changelog.d/21993.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure correct commit attribution when sending size metrics by requiring an explicit commit in the ddev size status command.

ddev/src/ddev/cli/size/status.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ def status(
103103
from ddev.cli.size.utils.common_funcs import export_format
104104

105105
export_format(app, format, modules_plat_ver, "status", platform, version, compressed)
106-
if to_dd_org or to_dd_key:
106+
if (to_dd_org or to_dd_key) and commit:
107107
from ddev.cli.size.utils.common_funcs import send_metrics_to_dd
108108

109-
send_metrics_to_dd(app, modules_plat_ver, to_dd_org, to_dd_key, compressed)
109+
send_metrics_to_dd(app, commit, modules_plat_ver, to_dd_org, to_dd_key, compressed)
110110
except Exception as e:
111111
app.abort(str(e))
112112

@@ -144,6 +144,9 @@ def validate_parameters(
144144
if to_dd_org and to_dd_key:
145145
errors.append("Specify either --to-dd-org or --to-dd-key, not both")
146146

147+
if (to_dd_org or to_dd_key) and not commit:
148+
errors.append("In order to send metrics to Datadog, you need to provide a commit hash")
149+
147150
if errors:
148151
app.abort("\n".join(errors))
149152

ddev/src/ddev/cli/size/utils/common_funcs.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ def draw_treemap_rects_with_labels(
837837

838838
def send_metrics_to_dd(
839839
app: Application,
840+
commit: str,
840841
modules: list[FileDataEntryPlatformVersion],
841842
org: str | None,
842843
key: str | None,
@@ -852,8 +853,7 @@ def send_metrics_to_dd(
852853
if "site" not in config_file_info:
853854
raise RuntimeError("No site found in config file")
854855

855-
message, tickets, prs = get_last_commit_data()
856-
timestamp = get_last_commit_timestamp()
856+
timestamp, message, tickets, prs = get_commit_data(commit)
857857

858858
metrics = []
859859
n_integrations_metrics = []
@@ -965,24 +965,28 @@ def send_metrics_to_dd(
965965
api.Metric.send(metrics=n_dependencies_metrics)
966966

967967

968-
def get_last_commit_timestamp() -> int:
969-
result = subprocess.run(["git", "log", "-1", "--format=%ct"], capture_output=True, text=True, check=True)
970-
return int(result.stdout.strip())
971-
968+
def get_commit_data(commit: str) -> tuple[int, str, list[str], list[str]]:
969+
'''
970+
Gets the timestamp, message, tickets and PRs of a given commit. If no commit is provided, it uses the last commit.
971+
'''
972+
result = subprocess.run(
973+
["git", "show", "-s", "--format=%ct,%s", commit],
974+
capture_output=True,
975+
text=True,
976+
check=True,
977+
)
972978

973-
def get_last_commit_data() -> tuple[str, list[str], list[str]]:
974-
result = subprocess.run(["git", "log", "-1", "--format=%s"], capture_output=True, text=True, check=True)
979+
timestamp, message = result.stdout.strip().split(',', 1)
975980
ticket_pattern = r'\b(?:DBMON|SAASINT|AGENT|AI)-\d+\b'
976981
pr_pattern = r'#(\d+)'
977982

978-
message = result.stdout.strip()
979983
tickets = re.findall(ticket_pattern, message)
980984
prs = re.findall(pr_pattern, message)
981985
if not tickets:
982986
tickets = [""]
983987
if not prs:
984988
prs = [""]
985-
return message, tickets, prs
989+
return int(timestamp), message, tickets, prs
986990

987991

988992
@cache

0 commit comments

Comments
 (0)