7676import json
7777import logging
7878import os
79+ import sys
7980from collections .abc import Callable , Mapping
8081from contextlib import nullcontext
8182from datetime import date
9697 Unset ,
9798)
9899
100+ if sys .version_info < (3 , 13 ):
101+ from typing_extensions import deprecated
102+ else :
103+ from warnings import deprecated
104+
105+
99106if TYPE_CHECKING :
100107 from collections .abc import Iterable
101108
@@ -1386,8 +1393,8 @@ def __init__(
13861393 self ._provider_branch : str | None = None
13871394 "The provider branch."
13881395
1389- self ._consumer_versions : list [str ] | None = None
1390- "List of consumer version regex patterns ."
1396+ self ._consumer_versions : list [str | dict [ str , Any ] ] | None = None
1397+ "List of consumer version selectors ."
13911398
13921399 self ._consumer_tags : list [str ] | None = None
13931400 "List of consumer tags to match."
@@ -1442,11 +1449,174 @@ def provider_branch(self, branch: str) -> Self:
14421449 self ._verifier ._branch = branch # type: ignore # noqa: PGH003, SLF001
14431450 return self
14441451
1452+ def consumer_version ( # noqa: PLR0913
1453+ self ,
1454+ * ,
1455+ consumer : str | None = None ,
1456+ tag : str | None = None ,
1457+ fallback_tag : str | None = None ,
1458+ latest : bool | None = None ,
1459+ deployed_or_released : Literal [True ] | None = None ,
1460+ deployed : Literal [True ] | None = None ,
1461+ released : Literal [True ] | None = None ,
1462+ environment : str | None = None ,
1463+ main_branch : Literal [True ] | None = None ,
1464+ branch : str | None = None ,
1465+ matching_branch : Literal [True ] | None = None ,
1466+ fallback_branch : str | None = None ,
1467+ ) -> Self :
1468+ """
1469+ Add a consumer version selector.
1470+
1471+ This method allows specifying consumer version selection criteria to
1472+ filter which consumer pacts are verified from the broker.
1473+
1474+ This function can be called multiple times to add multiple selectors.
1475+ The resulting selectors are combined with a logical OR, meaning that
1476+ pacts matching any of the selectors will be included in the
1477+ verification.
1478+
1479+ Args:
1480+ consumer:
1481+ Application name to filter the results on.
1482+
1483+ Allows a selector to only be applied to a certain consumer.
1484+
1485+ tag:
1486+ The tag name(s) of the consumer versions to get the pacts for.
1487+
1488+ This field is still supported but it is recommended to use the
1489+ `branch` in preference now.
1490+
1491+ fallback_tag:
1492+ The name of the tag to fallback to if the specified `tag` does
1493+ not exist.
1494+
1495+ This is useful when the consumer and provider use matching
1496+ branch names to coordinate the development of new features. This
1497+ field is still supported but it is recommended to use two
1498+ separate selectors - one with the main branch name and one with
1499+ the feature branch name.
1500+
1501+ latest:
1502+ Only select the latest (if false, this selects all pacts for a
1503+ tag).
1504+
1505+ Used in conjunction with the tag property. If a tag is
1506+ specified, and latest is true, then the latest pact for each of
1507+ the consumers with that tag will be returned. If a tag is
1508+ specified and the latest flag is not set to true, all the pacts
1509+ with the specified tag will be returned.
1510+
1511+ deployed_or_released:
1512+ Applications that have been deployed or released.
1513+
1514+ If the key is specified, can only be set to `True`. Returns the
1515+ pacts for all versions of the consumer that are currently
1516+ deployed or released and currently supported in any environment.
1517+ Use of this selector requires that the deployment of the
1518+ consumer application is recorded in the Pact Broker using the
1519+ `pact-broker record-deployment` or `pact-broker record-release`
1520+ CLI.
1521+
1522+ deployed:
1523+ Applications that have been deployed.
1524+
1525+ If the key is specified, can only be set to `True`. Returns the
1526+ pacts for all versions of the consumer that are currently
1527+ deployed to any environment. Use of this selector requires that
1528+ the deployment of the consumer application is recorded in the
1529+ Pact Broker using the `pact-broker record-deployment` CLI.
1530+
1531+ released:
1532+ Applications that have been released.
1533+
1534+ If the key is specified, can only be set to `True`. Returns the
1535+ pacts for all versions of the consumer that are released and
1536+ currently supported in any environment. Use of this selector
1537+ requires that the deployment of the consumer application is
1538+ recorded in the Pact Broker using the `pact-broker
1539+ record-release` CLI.
1540+
1541+ environment:
1542+ Applications in a given environment.
1543+
1544+ The name of the environment containing the consumer versions for
1545+ which to return the pacts. Used to further qualify `{
1546+ "deployed": true }` or `{ "released": true }`. Normally, this
1547+ would not be needed, as it is recommended to verify the pacts
1548+ for all currently deployed/currently supported released
1549+ versions.
1550+
1551+ main_branch:
1552+ Applications with the default branch set in the broker.
1553+
1554+ If the key is specified, can only be set to `True`. Return the
1555+ pacts for the configured `mainBranch` of each consumer. Use of
1556+ this selector requires that the consumer has configured the
1557+ `mainBranch` property, and has set a branch name when publishing
1558+ the pacts.
1559+
1560+ branch:
1561+ Applications with the given branch.
1562+
1563+ The branch name of the consumer versions to get the pacts for.
1564+ Use of this selector requires that the consumer has configured a
1565+ branch name when publishing the pacts.
1566+
1567+ matching_branch:
1568+ Applications that match the provider version branch sent during
1569+ verification.
1570+
1571+ If the key is specified, can only be set to `True`. When true,
1572+ returns the latest pact for any branch with the same name as the
1573+ specified `provider_version_branch`.
1574+
1575+ fallback_branch:
1576+ Fallback branch if branch doesn't exist.
1577+
1578+ The name of the branch to fallback to if the specified branch
1579+ does not exist. Use of this property is discouraged as it may
1580+ allow a pact to pass on a feature branch while breaking
1581+ backwards compatibility with the main branch, which is generally
1582+ not desired. It is better to use two separate consumer version
1583+ selectors, one with the main branch name, and one with the
1584+ feature branch name, rather than use this property.
1585+
1586+ Returns:
1587+ The builder instance for method chaining.
1588+ """
1589+ if self ._consumer_versions is None :
1590+ self ._consumer_versions = []
1591+
1592+ param_mapping = [
1593+ ("consumer" , consumer ),
1594+ ("tag" , tag ),
1595+ ("fallbackTag" , fallback_tag ),
1596+ ("latest" , latest ),
1597+ ("deployedOrReleased" , deployed_or_released ),
1598+ ("deployed" , deployed ),
1599+ ("released" , released ),
1600+ ("environment" , environment ),
1601+ ("mainBranch" , main_branch ),
1602+ ("branch" , branch ),
1603+ ("matchingBranch" , matching_branch ),
1604+ ("fallbackBranch" , fallback_branch ),
1605+ ]
1606+
1607+ self ._consumer_versions .append ({
1608+ key : value for key , value in param_mapping if value is not None
1609+ })
1610+ return self
1611+
1612+ @deprecated ("Use `consumer_version` method with keyword arguments instead." )
14451613 def consumer_versions (self , * versions : str ) -> Self :
14461614 """
14471615 Set the consumer versions.
14481616 """
1449- self ._consumer_versions = list (versions )
1617+ if self ._consumer_versions is None :
1618+ self ._consumer_versions = []
1619+ self ._consumer_versions .extend (versions )
14501620 return self
14511621
14521622 def consumer_tags (self , * tags : str ) -> Self :
@@ -1463,6 +1633,11 @@ def build(self) -> Verifier:
14631633 Returns:
14641634 The Verifier instance with the broker source added.
14651635 """
1636+ consumer_versions = [
1637+ json .dumps (cv ) if not isinstance (cv , str ) else cv
1638+ for cv in (self ._consumer_versions or [])
1639+ ]
1640+
14661641 self ._verifier ._broker_source_hook = ( # noqa: SLF001
14671642 lambda : pact_ffi .verifier_broker_source_with_selectors (
14681643 self ._verifier ._handle , # noqa: SLF001
@@ -1474,7 +1649,7 @@ def build(self) -> Verifier:
14741649 self ._include_wip_since ,
14751650 self ._provider_tags or [],
14761651 self ._provider_branch or self ._verifier ._branch , # noqa: SLF001
1477- self . _consumer_versions or [] ,
1652+ consumer_versions ,
14781653 self ._consumer_tags or [],
14791654 )
14801655 )
0 commit comments