From 1cc55126c2afccdd3b6dde83d5ccee6a31b4cfcd Mon Sep 17 00:00:00 2001 From: Rob Mohr Date: Wed, 15 Jan 2025 16:36:45 -0800 Subject: [PATCH] pw_presubmit: Only apply coverage to primary change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: b/390206195 Change-Id: I53d696cee91834fe4e752f5dc88752fefb05006a Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/260475 Lint: Lint 🤖 Reviewed-by: Ted Pudlik Docs-Not-Needed: Rob Mohr Presubmit-Verified: CQ Bot Account Commit-Queue: Auto-Submit Pigweed-Auto-Submit: Rob Mohr --- pw_presubmit/py/pw_presubmit/build.py | 21 +++++++++++++++++-- .../py/pw_presubmit/presubmit_context.py | 7 +++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pw_presubmit/py/pw_presubmit/build.py b/pw_presubmit/py/pw_presubmit/build.py index d2e1fb1969..5357f2a365 100644 --- a/pw_presubmit/py/pw_presubmit/build.py +++ b/pw_presubmit/py/pw_presubmit/build.py @@ -54,6 +54,7 @@ ) from pw_presubmit.presubmit_context import ( LuciContext, + LuciTrigger, PresubmitContext, PresubmitFailure, ) @@ -1030,13 +1031,29 @@ def _copy_to_gcs(ctx: PresubmitContext, filepath: Path, gcs_dst: str): call(*cmd, tee=outs) +class NoPrimaryTriggerError(Exception): + pass + + +def _get_primary_change(ctx: PresubmitContext) -> LuciTrigger: + assert ctx.luci is not None + + if len(ctx.luci.triggers) == 1: + return ctx.luci.triggers[0] + + for trigger in ctx.luci.triggers: + if trigger.primary: + return trigger + + raise NoPrimaryTriggerError(repr(ctx.luci.triggers)) + + def _write_coverage_metadata( ctx: PresubmitContext, options: CoverageOptions ) -> Sequence[Path]: """Write out Kalypsi coverage metadata file(s) and return their paths.""" assert ctx.luci is not None - assert len(ctx.luci.triggers) == 1 - change = ctx.luci.triggers[0] + change = _get_primary_change(ctx) metadata = { 'trace_type': options.common.trace_type, diff --git a/pw_presubmit/py/pw_presubmit/presubmit_context.py b/pw_presubmit/py/pw_presubmit/presubmit_context.py index d8a31cd4b0..585fe4f326 100644 --- a/pw_presubmit/py/pw_presubmit/presubmit_context.py +++ b/pw_presubmit/py/pw_presubmit/presubmit_context.py @@ -141,6 +141,8 @@ class LuciTrigger: patch for unsubmitted changes and the hash for submitted changes. gerrit_name: The name of the googlesource.com Gerrit host. submitted: Whether the change has been submitted or is still pending. + primary: Whether this change was the change that triggered a build or + if it was imported by that triggering change. gerrit_host: The scheme and hostname of the googlesource.com Gerrit host. gerrit_url: The full URL to this change on the Gerrit host. @@ -155,6 +157,7 @@ class LuciTrigger: ref: str gerrit_name: str submitted: bool + primary: bool @property def gerrit_host(self): @@ -174,6 +177,7 @@ def gitiles_url(self): def create_from_environment( env: dict[str, str] | None = None, ) -> Sequence['LuciTrigger']: + """Create a LuciTrigger from the environment.""" if not env: env = os.environ.copy() raw_path = env.get('TRIGGERING_CHANGES_JSON') @@ -195,6 +199,7 @@ def create_from_environment( 'ref', 'gerrit_name', 'submitted', + 'primary', } if keys <= trigger.keys(): result.append(LuciTrigger(**{x: trigger[x] for x in keys})) @@ -203,6 +208,7 @@ def create_from_environment( @staticmethod def create_for_testing(**kwargs): + """Create a LuciTrigger for testing.""" change = { 'number': 123456, 'patchset': 1, @@ -212,6 +218,7 @@ def create_for_testing(**kwargs): 'ref': 'refs/changes/56/123456/1', 'gerrit_name': 'pigweed', 'submitted': True, + 'primary': True, } change.update(kwargs)