Completed
Push — master ( 12e882...3eddfa )
by
unknown
8s
created

create_label_groups()   B

Complexity

Conditions 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 16
rs 8.5454
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Copyright (c) The Spyder Development Team
4
#
5
# Licensed under the terms of the MIT License
6
# (See LICENSE.txt for details)
7
# -----------------------------------------------------------------------------
8
"""Build a list of issues and pull requests per Github milestone."""
9
10
# yapf: disable
11
12
from __future__ import print_function
13
14
# Standard library imports
15
import argparse
16
import sys
17
18
# Local imports
19
from loghub.cli.common import add_common_parser_args, parse_password_check_repo
20
from loghub.core.formatter import create_changelog
21
22
# yapf: enable
23
24
PY2 = sys.version[0] == '2'
25
26
27
def main():
28
    """Main script."""
29
    parse_arguments(skip=False)
30
31
32
def create_label_groups(groups):
33
    """Create info dictionaries for label groups."""
34
    group_dicts = []
35
    if groups:
36
        for item in groups:
37
            dic = {}
38
            if len(item) == 1:
39
                dic['label'] = item[0]
40
                dic['name'] = item[0]
41
            elif len(item) == 2:
42
                dic['label'] = item[0]
43
                dic['name'] = item[1]
44
            else:
45
                raise ValueError('Label group takes 1 or 2 arguments')
46
            group_dicts.append(dic)
47
    return group_dicts
48
49
50
def parse_arguments(skip=False):
51
    """Parse CLI arguments."""
52
    parser = argparse.ArgumentParser(
53
        description='Script to print the list of issues and pull requests '
54
        'closed in a given milestone, tag including additional filtering '
55
        'options.')
56
57
    # Get common parser arguments
58
    parser = add_common_parser_args(parser)
59
60
    parser.add_argument(
61
        '-m',
62
        '--milestone',
63
        action="store",
64
        dest="milestone",
65
        default='',
66
        help="Github milestone to get issues and pull requests for")
67
    parser.add_argument(
68
        '-st',
69
        '--since-tag',
70
        action="store",
71
        dest="since_tag",
72
        default='',
73
        help="Github issues and pull requests since tag")
74
    parser.add_argument(
75
        '-ut',
76
        '--until-tag',
77
        action="store",
78
        dest="until_tag",
79
        default='',
80
        help="Github issues and pull requests until tag")
81
    parser.add_argument(
82
        '-b',
83
        '--branch',
84
        action="store",
85
        dest="branch",
86
        default='',
87
        help="Github base branch for merged PRs")
88
    parser.add_argument(
89
        '-ilg',
90
        '--issue-label-group',
91
        action="append",
92
        nargs='+',
93
        default=[],
94
        dest="issue_label_groups",
95
        help="Groups the generated issues by the specified label. This option"
96
        "takes 1 or 2 arguments, where the first one is the label to "
97
        "match and the second one is the label to print on the final"
98
        "output")
99
    parser.add_argument(
100
        '-plg',
101
        '--pr-label-group',
102
        action="append",
103
        nargs='+',
104
        default=[],
105
        dest="pr_label_groups",
106
        help="Groups the generated PRs by the specified label. This option"
107
        "takes 1 or 2 arguments, where the first one is the label to "
108
        "match and the second one is the label to print on the final"
109
        "output")
110
    parser.add_argument(
111
        '-lg',
112
        '--label-group',
113
        action="append",
114
        nargs='+',
115
        default=[],
116
        dest="label_groups",
117
        help="Groups the generated issues and PRs by the specified label. "
118
        "This option takes 1 or 2 arguments, where the first one is the "
119
        "label to match and the second one is the label to print on "
120
        "the final output")
121
    parser.add_argument(
122
        '-ilr',
123
        '--issue-label-regex',
124
        action="store",
125
        dest="issue_label_regex",
126
        default='',
127
        help="Label issue filter using a regular expression filter")
128
    parser.add_argument(
129
        '-plr',
130
        '--pr-label-regex',
131
        action="store",
132
        dest="pr_label_regex",
133
        default='',
134
        help="Label pull request filter using a regular expression filter")
135
    parser.add_argument(
136
        '-f',
137
        '--format',
138
        action="store",
139
        dest="output_format",
140
        default='changelog',
141
        help="Format for print, either 'changelog' (for "
142
        "Changelog.md file) or 'release' (for the Github "
143
        "Releases page). Default is 'changelog'. The "
144
        "'release' option doesn't generate Markdown "
145
        "hyperlinks.")
146
    parser.add_argument(
147
        '--template',
148
        action="store",
149
        dest="template",
150
        default='',
151
        help="Use a custom Jinja2 template file ")
152
    parser.add_argument(
153
        '--batch',
154
        action="store",
155
        dest="batch",
156
        default=None,
157
        choices=['milestones', 'tags'],
158
        help="Run loghub for all milestones or all tags")
159
    parser.add_argument(
160
        '--no-prs',
161
        action="store_false",
162
        dest="show_prs",
163
        default=True,
164
        help="Run loghub without any pull requests output")
165
166
    options = parser.parse_args()
167
168
    milestone = options.milestone
169
    batch = options.batch
170
171
    # Check if milestone or tag given
172
    if not batch:
173
        if not milestone and not options.since_tag:
174
            print('\nLOGHUB: Querying all issues\n')
175
        elif milestone:
176
            print('\nLOGHUB: Querying issues for milestone {0}'
177
                  '\n'.format(milestone))
178
        elif options.since_tag and not options.until_tag:
179
            print('\nLOGHUB: Querying issues since tag {0}'
180
                  '\n'.format(options.since_tag))
181
        elif options.since_tag and options.until_tag:
182
            print('\nLOGHUB: Querying issues since tag {0} until tag {1}'
183
                  '\n'.format(options.since_tag, options.until_tag))
184
    elif batch and any([
185
            bool(options.since_tag), bool(options.until_tag),
186
            bool(options.milestone)
187
    ]):
188
        print('LOGHUB: When using batch mode no tags or milestone arguments '
189
              'are allowed.\n')
190
        sys.exit(1)
191
192
    # Ask for password once input is valid
193
    username = options.username
194
    password = parse_password_check_repo(options)
195
    try:
196
        issue_label_groups = options.label_groups + \
197
                             options.issue_label_groups
198
        new_issue_label_groups = create_label_groups(issue_label_groups)
199
    except ValueError:
200
        print('LOGHUB: Issue label group takes 1 or 2 arguments\n')
201
        sys.exit(1)
202
203
    try:
204
        pr_label_groups = options.label_groups + options.pr_label_groups
205
        new_pr_label_groups = create_label_groups(pr_label_groups)
206
    except ValueError:
207
        print('LOGHUB: PR label group takes 1 or 2 arguments\n')
208
        sys.exit(1)
209
210
    if not skip:
211
        create_changelog(
212
            repo=options.repository,
213
            username=username,
214
            password=password,
215
            token=options.token,
216
            milestone=milestone,
217
            since_tag=options.since_tag,
218
            until_tag=options.until_tag,
219
            branch=options.branch,
220
            issue_label_regex=options.issue_label_regex,
221
            pr_label_regex=options.pr_label_regex,
222
            output_format=options.output_format,
223
            template_file=options.template,
224
            issue_label_groups=new_issue_label_groups,
225
            pr_label_groups=new_pr_label_groups,
226
            batch=batch,
227
            show_prs=options.show_prs)
228
229
    return options
230
231
232
if __name__ == '__main__':  # yapf: disable
233
    main()
234