#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: Gensim Contributors
# Copyright (C) 2020 RaRe Technologies s.r.o.
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html

"""Generate changelog entries for all PRs merged since the last release."""
import re
import requests
import time

#
# The releases get sorted in reverse chronological order, so the first release
# in the list is the most recent.
#
get = requests.get('https://api.github.com/repos/RaRe-Technologies/gensim/releases')
get.raise_for_status()
most_recent_release = get.json()[0]
release_timestamp = most_recent_release['published_at']


def iter_merged_prs(since=release_timestamp):
    page = 1
    while True:
        get = requests.get(
            'https://api.github.com/repos/RaRe-Technologies/gensim/pulls',
            params={'state': 'closed', 'page': page},
        )
        get.raise_for_status()
        pulls = get.json()
        if not pulls:
            break

        for i, pr in enumerate(pulls):
            if pr['merged_at'] and pr['merged_at'] > since:
                yield pr

        page += 1
        # Avoid Github API throttling; see https://github.com/RaRe-Technologies/gensim/pull/3203#issuecomment-887453109
        time.sleep(1)


def iter_closed_issues(since=release_timestamp):
    page = 1
    while True:
        get = requests.get(
            'https://api.github.com/repos/RaRe-Technologies/gensim/issues',
            params={'state': 'closed', 'page': page, 'since': since},
        )
        get.raise_for_status()
        issues = get.json()
        if not issues:
            break

        for i, issue in enumerate(issues):
            #
            # In the github API, all pull requests are issues, but not vice versa.
            #
            if 'pull_request' not in issue and issue['closed_at'] > since:
                yield issue
        page += 1
        # Avoid Github API throttling; see https://github.com/RaRe-Technologies/gensim/pull/3203#issuecomment-887453109
        time.sleep(1)


fixed_issue_numbers = set()
for pr in iter_merged_prs(since=release_timestamp):
    pr['user_login'] = pr['user']['login']
    pr['user_html_url'] = pr['user']['html_url']
    print('* [#%(number)d](%(html_url)s): %(title)s, by [@%(user_login)s](%(user_html_url)s)' % pr)

    #
    # Unfortunately, the GitHub API doesn't link PRs to issues that they fix,
    # so we have do it ourselves.
    #
    for match in re.finditer(r'fix(es)? #(?P<number>\d+)\b', pr['body'], flags=re.IGNORECASE):
        fixed_issue_numbers.add(int(match.group('number')))


print()
print('### :question: Closed issues')
print()
print('TODO: move each issue to its appropriate section or delete if irrelevant')
print()

for issue in iter_closed_issues(since=release_timestamp):
    if 'pull_request' in issue or issue['number'] in fixed_issue_numbers:
        continue
    print('* [#%(number)d](%(html_url)s): %(title)s' % issue)

@@ -269,16 +269,26 @@
 ase.%22%22%22%0A
+import os%0A
 import r
@@ -289,16 +289,16 @@
 port re%0A
-
 import r
@@ -318,16 +318,298 @@
 t time%0A%0A
+%0Adef throttle_get(*args, seconds=3, **kwargs):%0A    result = requests.get(*args, **kwargs)%0A    result.raise_for_status()%0A%0A    # Avoid Github API throttling; see https://github.com/RaRe-Technologies/gensim/pull/3203#issuecomment-887453109%0A    time.sleep(seconds)%0A%0A    return result%0A%0A%0A
 #%0A# The 
@@ -719,33 +719,33 @@
 nt.%0A#%0Aget = 
-requests.
+throttle_
 get('https:/
@@ -802,39 +802,16 @@
 eases')%0A
-get.raise_for_status()%0A
 most_rec
@@ -895,16 +895,28 @@
 d_at'%5D%0A%0A
+throttle()%0A%0A
 %0Adef ite
@@ -993,33 +993,33 @@
       get = 
-requests.
+throttle_
 get(%0A       
@@ -1154,39 +1154,9 @@
   )%0A
-        get.raise_for_status()
 %0A
+
     
@@ -1366,150 +1366,8 @@
 = 1%0A
-        # Avoid Github API throttling; see https://github.com/RaRe-Technologies/gensim/pull/3203#issuecomment-887453109%0A        time.sleep(1)%0A
 %0A%0Ade
@@ -1452,33 +1452,33 @@
       get = 
-requests.
+throttle_
 get(%0A       
@@ -1630,39 +1630,8 @@
   )%0A
-        get.raise_for_status()%0A
     
@@ -1975,151 +1975,8 @@
 = 1%0A
-        # Avoid Github API throttling; see https://github.com/RaRe-Technologies/gensim/pull/3203#issuecomment-887453109%0A        time.sleep(1)%0A%0A
 %0Afix
@@ -2363,22 +2363,130 @@
 selves.%0A
-
     #%0A
+    if pr%5B'body'%5D is None:%0A        #%0A        # Weird edge case, PR with no body%0A        #%0A        continue%0A%0A
     for 

update changelog script