8181from datetime import date
8282from pathlib import Path
8383from typing import TYPE_CHECKING , Any , Literal , TypedDict , overload
84+ from warnings import deprecated
8485
8586from yarl import URL
8687
@@ -1386,7 +1387,7 @@ def __init__(
13861387 self ._provider_branch : str | None = None
13871388 "The provider branch."
13881389
1389- self ._consumer_versions : list [str ] | None = None
1390+ self ._consumer_versions : list [str | dict [ str , Any ] ] | None = None
13901391 "List of consumer version regex patterns."
13911392
13921393 self ._consumer_tags : list [str ] | None = None
@@ -1442,11 +1443,174 @@ def provider_branch(self, branch: str) -> Self:
14421443 self ._verifier ._branch = branch # type: ignore # noqa: PGH003, SLF001
14431444 return self
14441445
1446+ def consumer_version ( # noqa: PLR0913
1447+ self ,
1448+ * ,
1449+ consumer : str | None = None ,
1450+ tag : str | None = None ,
1451+ fallback_tag : str | None = None ,
1452+ latest : bool | None = None ,
1453+ deployed_or_released : Literal [True ] | None = None ,
1454+ deployed : Literal [True ] | None = None ,
1455+ released : Literal [True ] | None = None ,
1456+ environment : str | None = None ,
1457+ main_branch : Literal [True ] | None = None ,
1458+ branch : str | None = None ,
1459+ matching_branch : Literal [True ] | None = None ,
1460+ fallback_branch : str | None = None ,
1461+ ) -> Self :
1462+ """
1463+ Add a consumer version selector.
1464+
1465+ This method allows specifying consumer version selection criteria to
1466+ filter which consumer pacts are verified from the broker.
1467+
1468+ This function can be called multiple times to add multiple selectors.
1469+ The resulting selectors are combined with a logical OR, meaning that
1470+ pacts matching any of the selectors will be included in the
1471+ verification.
1472+
1473+ Args:
1474+ consumer:
1475+ Application name to filter the results on.
1476+
1477+ Allows a selector to only be applied to a certain consumer.
1478+
1479+ tag:
1480+ The tag name(s) of the consumer versions to get the pacts for.
1481+
1482+ This field is still supported but it is recommended to use the
1483+ `branch` in preference now.
1484+
1485+ fallback_tag:
1486+ The name of the tag to fallback to if the specified `tag` does
1487+ not exist.
1488+
1489+ This is useful when the consumer and provider use matching
1490+ branch names to coordinate the development of new features. This
1491+ field is still supported but it is recommended to use two
1492+ separate selectors - one with the main branch name and one with
1493+ the feature branch name.
1494+
1495+ latest:
1496+ Only select the latest (if false, this selects all pacts for a
1497+ tag).
1498+
1499+ Used in conjunction with the tag property. If a tag is
1500+ specified, and latest is true, then the latest pact for each of
1501+ the consumers with that tag will be returned. If a tag is
1502+ specified and the latest flag is not set to true, all the pacts
1503+ with the specified tag will be returned.
1504+
1505+ deployed_or_released:
1506+ Applications that have been deployed or released.
1507+
1508+ If the key is specified, can only be set to `True`. Returns the
1509+ pacts for all versions of the consumer that are currently
1510+ deployed or released and currently supported in any environment.
1511+ Use of this selector requires that the deployment of the
1512+ consumer application is recorded in the Pact Broker using the
1513+ `pact-broker record-deployment` or `pact-broker record-release`
1514+ CLI.
1515+
1516+ deployed:
1517+ Applications that have been deployed.
1518+
1519+ If the key is specified, can only be set to `True`. Returns the
1520+ pacts for all versions of the consumer that are currently
1521+ deployed to any environment. Use of this selector requires that
1522+ the deployment of the consumer application is recorded in the
1523+ Pact Broker using the `pact-broker record-deployment` CLI.
1524+
1525+ released:
1526+ Applications that have been released.
1527+
1528+ If the key is specified, can only be set to `True`. Returns the
1529+ pacts for all versions of the consumer that are released and
1530+ currently supported in any environment. Use of this selector
1531+ requires that the deployment of the consumer application is
1532+ recorded in the Pact Broker using the `pact-broker
1533+ record-release` CLI.
1534+
1535+ environment:
1536+ Applications in a given environment.
1537+
1538+ The name of the environment containing the consumer versions for
1539+ which to return the pacts. Used to further qualify `{
1540+ "deployed": true }` or `{ "released": true }`. Normally, this
1541+ would not be needed, as it is recommended to verify the pacts
1542+ for all currently deployed/currently supported released
1543+ versions.
1544+
1545+ main_branch:
1546+ Applications with the default branch set in the broker.
1547+
1548+ If the key is specified, can only be set to `True`. Return the
1549+ pacts for the configured `mainBranch` of each consumer. Use of
1550+ this selector requires that the consumer has configured the
1551+ `mainBranch` property, and has set a branch name when publishing
1552+ the pacts.
1553+
1554+ branch:
1555+ Applications with the given branch.
1556+
1557+ The branch name of the consumer versions to get the pacts for.
1558+ Use of this selector requires that the consumer has configured a
1559+ branch name when publishing the pacts.
1560+
1561+ matching_branch:
1562+ Applications that match the provider version branch sent during
1563+ verification.
1564+
1565+ If the key is specified, can only be set to `True`. When true,
1566+ returns the latest pact for any branch with the same name as the
1567+ specified `provider_version_branch`.
1568+
1569+ fallback_branch:
1570+ Fallback branch if branch doesn't exist.
1571+
1572+ The name of the branch to fallback to if the specified branch
1573+ does not exist. Use of this property is discouraged as it may
1574+ allow a pact to pass on a feature branch while breaking
1575+ backwards compatibility with the main branch, which is generally
1576+ not desired. It is better to use two separate consumer version
1577+ selectors, one with the main branch name, and one with the
1578+ feature branch name, rather than use this property.
1579+
1580+ Returns:
1581+ The builder instance for method chaining.
1582+ """
1583+ if self ._consumer_versions is None :
1584+ self ._consumer_versions = []
1585+
1586+ param_mapping = [
1587+ ("consumer" , consumer ),
1588+ ("tag" , tag ),
1589+ ("fallbackTag" , fallback_tag ),
1590+ ("latest" , latest ),
1591+ ("deployedOrReleased" , deployed_or_released ),
1592+ ("deployed" , deployed ),
1593+ ("released" , released ),
1594+ ("environment" , environment ),
1595+ ("mainBranch" , main_branch ),
1596+ ("branch" , branch ),
1597+ ("matchingBranch" , matching_branch ),
1598+ ("fallbackBranch" , fallback_branch ),
1599+ ]
1600+
1601+ self ._consumer_versions .append ({
1602+ key : value for key , value in param_mapping if value is not None
1603+ })
1604+ return self
1605+
1606+ @deprecated ("Use `consumer_version` method with keyword arguments instead." )
14451607 def consumer_versions (self , * versions : str ) -> Self :
14461608 """
14471609 Set the consumer versions.
14481610 """
1449- self ._consumer_versions = list (versions )
1611+ if self ._consumer_versions is None :
1612+ self ._consumer_versions = []
1613+ self ._consumer_versions .extend (versions )
14501614 return self
14511615
14521616 def consumer_tags (self , * tags : str ) -> Self :
@@ -1463,6 +1627,11 @@ def build(self) -> Verifier:
14631627 Returns:
14641628 The Verifier instance with the broker source added.
14651629 """
1630+ consumer_versions = [
1631+ json .dumps (cv ) if not isinstance (cv , str ) else cv
1632+ for cv in (self ._consumer_versions or [])
1633+ ]
1634+
14661635 self ._verifier ._broker_source_hook = ( # noqa: SLF001
14671636 lambda : pact_ffi .verifier_broker_source_with_selectors (
14681637 self ._verifier ._handle , # noqa: SLF001
@@ -1474,7 +1643,7 @@ def build(self) -> Verifier:
14741643 self ._include_wip_since ,
14751644 self ._provider_tags or [],
14761645 self ._provider_branch or self ._verifier ._branch , # noqa: SLF001
1477- self . _consumer_versions or [] ,
1646+ consumer_versions ,
14781647 self ._consumer_tags or [],
14791648 )
14801649 )
0 commit comments