#!/usr/bin/env python
"""A script to generate the initial draft of release notes for a milestone.

Usage:

    {executable} <MILESTONE_ID>

"""

import requests

URL = 'https://api.github.com/repos/akvo/akvo-rsr/issues?milestone={milestone}&state=closed'
RELEASE_NOTES = """
## New and noteworthy

{features}

## Resolved issues

{bugs}

## Code maintenance

{chores}
"""

LABELS = {
    'bugs': 'Bug',
    'chores': 'Chore',
    'features': None,
}


def get_issues(milestone):
    """Return the issues in the milestone."""
    url = URL.format(milestone=milestone)
    entries = requests.get(url).json()
    issues = [entry for entry in entries if 'pull_request' not in entry]
    return issues


def format_issue(issue):
    return '- {title} [#{number}]({html_url})'.format(**issue)


def format_issues(issues):
    return '\n\n'.join(map(format_issue, issues))


def issue_labels(issue):
    return set([label['name'] for label in issue['labels']])


def filter_issues(issues, label, labels):
    other_labels = set(labels) - {label}
    if label is None:
        issues_ = [
            issue for issue in issues
            if not other_labels.intersection(issue_labels(issue))
        ]
    else:
        issues_ = [
            issue for issue in issues
            if label in issue_labels(issue)
        ]

    return issues_


def release_notes(issues):
    grouped_issues = {
        key: format_issues(filter_issues(issues, label, LABELS.values()))
        for key, label in LABELS.items()
    }
    return RELEASE_NOTES.format(**grouped_issues)


def main(milestone):
    issues = get_issues(milestone)
    print(release_notes(issues))


if __name__ == '__main__':
    import sys
    if len(sys.argv) != 2:
        sys.exit(__doc__.format(executable=sys.argv[0]))

    milestone = sys.argv[1]
    main(milestone)

@@ -518,16 +518,32 @@
 one.%22%22%22%0A
+    issues = %5B%5D%0A
     url 
@@ -584,43 +584,102 @@
     
-entries = requests.get(url)
+while url is not None:%0A        response = requests.get(url)%0A        entries = response
 .json()%0A
     
@@ -674,16 +674,20 @@
 .json()%0A
+    
     issu
@@ -688,19 +688,24 @@
   issues
- = 
+.extend(
 %5Bentry f
@@ -755,16 +755,201 @@
 n entry%5D
+)%0A%0A        successive, end = response.headers.get('link').split(',')%0A        url_, direction = successive.split(';')%0A        url = None if 'next' not in direction else url_.strip('%3C%3E')%0A
 %0A    ret

Improve script to generate release notes