#!/usr/bin/env python3
"""Fetch the full change history for the Jira issues whose export kept only the first page.

The July 2026 export saved at most 40 changelog entries per issue; 650 issues have more.
This writes issues/<KEY>/changelog.json (all entries, paged) next to the existing issue.json.

    export JIRA_SITE=https://esperantotech.atlassian.net JIRA_EMAIL=you@example.com JIRA_TOKEN=...
    python3 jira_changelog.py /archive/atlassian/jira            # dry run: lists what it would fetch
    python3 jira_changelog.py /archive/atlassian/jira --write    # fetches and writes

Needs only read access (an API token from id.atlassian.com → Security → API tokens). Pure stdlib.
"""
import base64, json, os, sys, time, urllib.request

root = sys.argv[1] if len(sys.argv) > 1 else '/archive/atlassian/jira'
write = '--write' in sys.argv
site, email, token = (os.environ.get(k, '') for k in ('JIRA_SITE', 'JIRA_EMAIL', 'JIRA_TOKEN'))
auth = base64.b64encode(f'{email}:{token}'.encode()).decode()

def get(url):
    req = urllib.request.Request(url, headers={'Authorization': f'Basic {auth}', 'Accept': 'application/json'})
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.load(r)

todo = []
for key in sorted(os.listdir(os.path.join(root, 'issues'))):
    p = os.path.join(root, 'issues', key, 'issue.json')
    if not os.path.isfile(p): continue
    with open(p, 'rb') as f: ch = (json.load(f).get('changelog') or {})
    total, have = ch.get('total') or 0, len(ch.get('histories') or [])
    if total > have and not os.path.isfile(os.path.join(root, 'issues', key, 'changelog.json')):
        todo.append((key, total, have))
print(f'{len(todo)} issues need their full changelog ({sum(t - h for _, t, h in todo)} missing entries)')
if not write:
    for key, total, have in todo[:20]: print(f'  {key}: {have} of {total}')
    sys.exit(0)
if not (site and email and token): sys.exit('set JIRA_SITE, JIRA_EMAIL and JIRA_TOKEN')
for i, (key, total, have) in enumerate(todo, 1):
    out, start = [], 0
    while True:
        page = get(f'{site}/rest/api/3/issue/{key}/changelog?startAt={start}&maxResults=100')
        out += page.get('values') or []
        start += page.get('maxResults') or 100
        if page.get('isLast') or start >= (page.get('total') or 0): break
        time.sleep(0.2)
    with open(os.path.join(root, 'issues', key, 'changelog.json'), 'w') as f:
        json.dump({'total': len(out), 'histories': out}, f)
    print(f'{i}/{len(todo)} {key}: {len(out)} entries')
