File indexing completed on 2026-12-02 10:29:06
0001
0002 """Create What's new items from a reviewed YAML ingest file.
0003
0004 Each record carries the item fields of docs/WHATS_NEW.md (The item):
0005 title, summary, details, refs (a list, written as Markdown lines), tags,
0006 importance, shipped, contact and builds_on. Other keys, such as the
0007 evidence a record carries for review, are not item fields and are not
0008 written. Every item is created through help/whats_new.py item_create, the
0009 call the New item form makes, as the person who approved the ingest. An
0010 item whose slug exists is left as it is. An item that builds on another
0011 record of the file is created after it.
0012
0013 By hand, on swf-testbed:
0014
0015 cd /data/wenauseic/github/swf-monitor/src
0016 ../../swf-testbed/.venv/bin/python ../scripts/whats-new-ingest.py \\
0017 ../scripts/whats-new-backfill-2026-10-30.yaml --as wenaus [--dry-run]
0018 """
0019 import argparse
0020 import json
0021 import logging
0022 import os
0023 import sys
0024
0025 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
0026 sys.path.insert(0, os.path.join(THIS_DIR, '..', 'src'))
0027 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'swf_monitor_project.settings')
0028
0029 import django
0030 django.setup()
0031
0032 import yaml
0033
0034 from help import whats_new as wn
0035 from monitor_app.epicprod_logging import log_epicprod_action
0036
0037 log = logging.getLogger('whats-new-ingest')
0038
0039
0040 def _fields(record):
0041 """The item fields of one ingest record."""
0042 fields = {k: record[k] for k in wn.FIELDS if record.get(k) is not None}
0043 refs = fields.get('refs')
0044 if isinstance(refs, list):
0045 fields['refs'] = '\n'.join(f'- {r}' for r in refs)
0046 if fields.get('shipped') is not None:
0047 fields['shipped'] = str(fields['shipped'])
0048 return fields
0049
0050
0051 def _ordered(records):
0052 """Records in an order where every builds_on target in the file comes
0053 first; the file's own order otherwise, oldest last becomes oldest first."""
0054 pending = list(reversed(records))
0055 slugs = {wn.slug_for(r.get('title', '')) for r in records}
0056 done, out = set(), []
0057 while pending:
0058 ready = [r for r in pending
0059 if all(s in done or s not in slugs
0060 for s in (r.get('builds_on') or []))]
0061 if not ready:
0062 raise ValueError('builds_on links in the file form a cycle: '
0063 + ', '.join(r.get('title', '') for r in pending))
0064 for r in ready:
0065 pending.remove(r)
0066 done.add(wn.slug_for(r.get('title', '')))
0067 out.append(r)
0068 return out
0069
0070
0071 def main():
0072 parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
0073 parser.add_argument('path', help='the YAML ingest file, a list of records')
0074 parser.add_argument('--as', dest='person', required=True,
0075 help='the account of the person who approved the ingest')
0076 parser.add_argument('--dry-run', action='store_true',
0077 help='validate and print what would be created; write nothing')
0078 args = parser.parse_args()
0079 logging.basicConfig(level=logging.INFO, stream=sys.stderr,
0080 format='%(levelname)s %(message)s')
0081
0082 created, present, errors = [], [], []
0083 try:
0084 with open(args.path) as fh:
0085 records = yaml.safe_load(fh)
0086 if not isinstance(records, list):
0087 raise ValueError(f'{args.path}: expected a list of records')
0088 batch = {wn.slug_for(r.get('title', '')) for r in records}
0089 for record in _ordered(records):
0090 fields = _fields(record)
0091 slug = wn.slug_for(fields.get('title', ''))
0092 if wn.get_entry(slug) is not None:
0093 log.info('present, left as it is: %s', slug)
0094 present.append(slug)
0095 continue
0096 try:
0097 if args.dry_run:
0098 check = dict(fields, builds_on=[
0099 s for s in fields.get('builds_on') or [] if s not in batch])
0100 wn.validate_new(check, changed_by=args.person)
0101 log.info('would create: %s (%s)', slug, fields.get('shipped'))
0102 else:
0103 wn.item_create(fields, changed_by=args.person)
0104 log.info('created: %s (%s)', slug, fields.get('shipped'))
0105 created.append(slug)
0106 except wn.WhatsNewError as exc:
0107 log.error('refused: %s: %s', slug, exc.detail)
0108 errors.append({'slug': slug, 'error': exc.detail})
0109 except (OSError, ValueError, yaml.YAMLError) as exc:
0110 log.error('%s', exc)
0111 errors.append({'slug': '', 'error': str(exc)})
0112
0113 summary = {'dry_run': args.dry_run, 'created': len(created),
0114 'present': len(present), 'errors': errors}
0115 if not args.dry_run and (created or errors):
0116 log_epicprod_action(
0117 'script', 'whats_new_ingest',
0118 outcome='error' if errors else 'ok', sublevel='normal',
0119 live_default=False, username=args.person,
0120 level=logging.ERROR if errors else logging.INFO,
0121 message=(f"What's new ingest from {os.path.basename(args.path)}: "
0122 f'{len(created)} created, {len(present)} present, '
0123 f'{len(errors)} refused'))
0124 print(json.dumps(summary))
0125 return 1 if errors else 0
0126
0127
0128 if __name__ == '__main__':
0129 sys.exit(main())