Skip to content

Modular logistics and supply chain operating system (LSOS)

2.1 KB
Raw Blame
#!/usr/bin/env python3
"""Report {{variables}} a collection references but nothing ever sets.

An unresolved Postman variable does not error — the literal "{{name}}" is sent, and the
API answers with whatever that garbage produces. Those failures read as API problems and
are often diagnosed as such: {{cart_id}} was unset for the entire life of the Storefront
collection, and because the product id in the same URL *did* resolve, the endpoint
answered "Invalid product provided" — an error about the other half of the path.

A variable is considered sourced if a script sets it (pm.environment.set / cv.set) or the
contract action injects it with --env-var. Collection-level `variables:` declarations do
NOT count: the CLI does not resolve them, only --env-var values.

Usage:  find-unsourced-variables.py <collections-root> [action.yml]
Exit code is always 0 — this reports, it does not gate.
"""
import glob
import os
import re
import sys

root = sys.argv[1]
action = sys.argv[2] if len(sys.argv) > 2 else '.github/actions/postman-contract/action.yml'

injected = set()
if os.path.isfile(action):
    injected = set(re.findall(r'--env-var "([A-Za-z0-9_]+)=', open(action).read()))

total = 0
for coll in sorted(os.listdir(root)):
    d = os.path.join(root, coll)
    if not os.path.isdir(d):
        continue

    used, produced = {}, set(injected)
    for p in glob.glob(os.path.join(d, '**', '*.request.yaml'), recursive=True):
        s = open(p).read()
        name = os.path.basename(p)[:-len('.request.yaml')]
        for v in re.findall(r'\{\{([A-Za-z0-9_]+)\}\}', s):
            used.setdefault(v, set()).add(name)
        produced |= set(re.findall(r'\.set\(\s*[\'"]([A-Za-z0-9_]+)[\'"]', s))

    # {{$randomEmail}} and friends are generated by Postman itself.
    missing = {v: n for v, n in used.items() if v not in produced and not v.startswith('$')}
    total += len(missing)

    print(f'\n{coll} — {len(missing)} variable(s) with no source')
    for v, names in sorted(missing.items()):
        print(f'  {{{{{v}}}}}  <- {", ".join(sorted(names))}')

print(f'\n{total} unsourced variable(s) in total.')