U
    Pe                     @   s^   d Z ddlmZ ddlZddlmZ ddlZdZdd Zdd	 Z	e
d
krZe	ejdd   dS )af  
Changelog parser
================

This generates a changelog from a json file of the PRs of a given milestone,
dumped to json, using the [GitHub CLI](https://github.com/cli/cli).

First, in the command line, create the following alias::

    gh alias set --shell viewMilestone "gh api graphql -F owner='kivy' -F name='kivy' -F number=\$1 -f query='
        query GetMilestones(\$name: String!, \$owner: String!, \$number: Int!) {
            repository(owner: \$owner, name: \$name) {
                milestone(number: \$number) {
                    pullRequests(states: MERGED, first: 1000) {
                        nodes {
                            number
                            title
                            labels (first: 25) {
                                nodes {
                                    name
                                }
                            }
                        }
                    }
                }
            }
        }
    '"

Then, log in using ``gh`` and run::

    gh viewMilestone 26 > prs.json

This will generate ``prs.json``. Then, to generate the changelog, run::

    python -m kivy.tools.changelog_parser prs.json changelog.md

to generate a markdown changelog at ``changelog.md``. Then, edit as desired
and paste into the
[changelog here](https://github.com/kivy/kivy/blob/master/doc/sources/changelog.rst).
    )existsN)defaultdict)process_changelogc                 C   sf   t |dd d}|rb| | ddt|  d |D ] \}}| d| d| d q6| d d S )	Nc                 S   s   | d S Nr    xr   r   ?/tmp/pip-unpacked-wheel-xzebddm3/kivy/tools/changelog_parser.py<lambda>6       z'write_special_section.<locals>.<lambda>key
-z

z
- [:repo:`z`]: )sortedwritelen)fhitemsheaderntitler   r   r	   write_special_section5   s    r   c              	      s  t |rtd|t| d}t|}W 5 Q R X |d d d d d }d}tt}g }g }g }	|D ]}
|
d	 }|
d
 }dd |
d d D }d|k}d|k}d|k}d  fdd|D }|std|  d}qjt	|dkrtd|  d}qj||d  
||f |r$|
||f |r8|
||f |rj|	
||f qj|rZtdt|dZ}t||d t||	d t||d t| dd dD ]\}}t|||  qW 5 Q R X d S ) Nz*{} already exists and would be overwrittenrdata
repositoryZ	milestoneZpullRequestsZnodesFnumberr   c                 S   s   g | ]}|d  qS )namer   .0labelr   r   r	   
<listcomp>O   s     z%process_changelog.<locals>.<listcomp>labelszNotes: API-breakzNotes: Release-highlightzNotes: API-deprecationzComponent: c                    s&   g | ]}|  r|t d  qS )N)
startswithr   r   Zcomponent_strr   r	   r!   T   s    
zFound no component label for #T   z)Found more than one component label for #r   z9One or more PRs have no, or more than one component labelwZ
HighlightsZ
DeprecatedzBreaking changesc                 S   s   | d S r   r   r   r   r   r	   r
   s   r   z#process_changelog.<locals>.<lambda>r   )r   
ValueErrorformatopenjsonloadr   listprintr   appendr   r   r   
capitalize)Zfilename_inZfilename_outr   r   ZprsZbad_prZgroupedZhighlightedZ
api_breaksZ
deprecatesitemr   r   r"   Z	api_breakZ	highlight
deprecated
componentsgroupr   r   r$   r	   r   >   s`    
r   __main__r%   )__doc__os.pathr   syscollectionsr   r*   __all__r   r   __name__argvr   r   r   r	   <module>   s   ,	9