Skip to content

Commit

Permalink
pw_presubmit: Only apply coverage to primary change
Browse files Browse the repository at this point in the history
Bug: b/390206195
Change-Id: I53d696cee91834fe4e752f5dc88752fefb05006a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/260475
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
Docs-Not-Needed: Rob Mohr <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Pigweed-Auto-Submit: Rob Mohr <[email protected]>
  • Loading branch information
mohrr authored and CQ Bot Account committed Jan 16, 2025
1 parent 704b0fb commit 1cc5512
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pw_presubmit/py/pw_presubmit/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
)
from pw_presubmit.presubmit_context import (
LuciContext,
LuciTrigger,
PresubmitContext,
PresubmitFailure,
)
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions pw_presubmit/py/pw_presubmit/presubmit_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -155,6 +157,7 @@ class LuciTrigger:
ref: str
gerrit_name: str
submitted: bool
primary: bool

@property
def gerrit_host(self):
Expand All @@ -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')
Expand All @@ -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}))
Expand All @@ -203,6 +208,7 @@ def create_from_environment(

@staticmethod
def create_for_testing(**kwargs):
"""Create a LuciTrigger for testing."""
change = {
'number': 123456,
'patchset': 1,
Expand All @@ -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)

Expand Down

0 comments on commit 1cc5512

Please sign in to comment.