summaryrefslogtreecommitdiff
path: root/roadmap/management/commands/roadmap_import.py
blob: bad3ef535514a7155f5eb8a0f5dc818f5d06afe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright (C) 2013 Linaro
#
# This file is part of roadmap.
#
# roadmap is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# roadmap is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with roadmap.  If not, see <http://www.gnu.org/licenses/>.

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from getpass import getpass
from jira.client import JIRA
from optparse import make_option
from roadmap.helpers import (
    create_or_update,
    JIRA_SERVER,
    JIRA_USER,
    JIRA_VERIFY,
    JIRA_DEPENDSON,
    JIRA_IMPLEMENTS,
    JIRA_KEY,
    SINGLE_UPDATE,
    VERBOSE
)


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--server',
            dest=JIRA_SERVER,
            action='store',
            type='string',
            help='Jira server URL'
        ),
        make_option('--username',
            dest=JIRA_USER,
            action='store',
            type='string',
            help='Jira username'
        ),
        make_option('--key',
            dest=JIRA_KEY,
            action='store',
            type='string',
            help='Jira issue key. Only this issue is synced'
        ),
        make_option('--no-implements',
            dest=JIRA_IMPLEMENTS,
            action='store_false',
            default=True,
            help='If set the "implements" relations are \
                  not retrieved. Default: false'
        ),
        make_option('--depends',
            dest=JIRA_DEPENDSON,
            action='store_true',
            default=False,
            help='If set the "depends" relations are not \
                 retrieved. Default: false. Be careful when \
                 not using --no-implements might run into infinite loop.'
        ),
        make_option('--no-verify',
            dest=JIRA_VERIFY,
            action='store_false',
            default=True,
            help='If set allows to connect to Jira server \
                  secured with self-signed certificate'
        ),
        make_option('--verbose',
            dest=VERBOSE,
            action='store_true',
            default=False,
            help='Enables verbose logging. Default: false.'
        ),

    )
    help = 'Imports current snapshot from Jira server'

    def handle(self, *args, **options):
        if options[JIRA_SERVER] == None:
            raise CommandError("Option `--server=...` must be specified.")
        if options[JIRA_USER] == None:
            raise CommandError("Option `--username=...` must be specified.")
        jira_password = getpass("JIRA password (%s):" % options[JIRA_USER])
        jira_options = {
            'server': options[JIRA_SERVER],
            'verify': options[JIRA_VERIFY]}
        jira = JIRA(
            options=jira_options,
            basic_auth=(options[JIRA_USER], jira_password))
        start = 0
        if options[JIRA_KEY] == None:
            issues = jira.search_issues(
                'project="%s"' % settings.JIRA_PROJECT,
                startAt=start,
                expand="changelog")
        else:
            issues = jira.search_issues(
                'key="%s"' % options[JIRA_KEY],
                startAt=start,
                expand="changelog")

        options.update({SINGLE_UPDATE: False})
        print "Total: ", issues.total
        while len(issues) > 0:
            #allissues.extend(issues)
            for index, issue in enumerate(issues, start=1):
                print "Processing %s/%s (%s/%s) %s" % (
                    index + start,
                    issues.total,
                    index,
                    len(issues),
                    issue.key)
                create_or_update(issue, options, jira)

            start = start + len(issues)
            if options[JIRA_KEY] == None:
                issues = jira.search_issues(
                    'project="%s"' % settings.JIRA_PROJECT,
                    startAt=start,
                    expand="changelog")
            else:
                issues = jira.search_issues(
                    'key="%s"' % options[JIRA_KEY],
                    startAt=start,
                    expand="changelog")