summaryrefslogtreecommitdiff
path: root/roadmap/management/commands/roadmap_import.py
blob: 854ac3067a230c8e7b21d6360cb69798f3f554a8 (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
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")