zhuj / estimation-tools

Techniques and Tools which could help you to estimate a scope of work
MIT License
6 stars 2 forks source link

generate jira project based on mindmap #23

Open zhuj opened 6 years ago

zhuj commented 6 years ago
import estimate
import sys

from jira import JIRA

jira = JIRA(basic_auth=('USERNAME', 'PASSWORD'), server='http://jira.url')
PROJECT = 'PROJECTNAME'

#######################

link_types = jira.issue_link_types()
link_type__blocked_by = [ x for x in link_types if x.name == 'Blocks' ][0].inward
link_type__task_of = [ x for x in link_types if x.name == 'Task-Story' ][0].inward

import re
RE_POSTFIX = re.compile("^(.*)\\s*[[]([A-Za-z]+)[]]$")

options = estimate.parse_arguments()
filename = options.filename

processor = estimate.Processor(options)
root = processor.parse(filename)
root = processor.transform(root)

def _stage_to_string(node):
    if (processor._stages):
        stage = node.acquire_custom('stage')
        if (stage): return '.'.join(str(x).strip() for x in stage)
        return ''

def _module_to_string(node):
    if (processor._modules):
        module = node.acquire_custom('module')
        if (module): return module.strip()
        return ''

def _postfix_to_string(node):
    title = node.title().strip()
    match = RE_POSTFIX.match(title)
    if (match): 
        title, postfix = [ match.group(x).strip() for x in (1, 2) ]
        postfox = postfix.upper()
        title = title.strip()
        return (title, postfix)
    return (title, None)

COMPONENTS = {}
lines = estimate.Processor._collect(root)

for l in lines:
    estimates = l.estimates()

    role = l.is_role()
    if (role): 
        pass
        continue

    title, postfix = _postfix_to_string(l)
    if (postfix is None):
        pass
    elif (postfix == 'C'):
        l.set_custom('component', title, error=lambda prev: Exception(parent, prev))
        pass
    elif (postfix == 'S' or postfix == 'T'):
        components = [ x.capitalize() for x in [ l.acquire_custom('module'), l.acquire_custom('component') ] if x is not None ]
        for c in components: COMPONENTS[c] = 1 + COMPONENTS.get(c, 0)

        full_title = " / ".join([ x.title() for x in l.trace(stoppers=[root]) ])
        print full_title
        comment = ';\n'.join([ x for x in l.annotation('comment') if not x.startswith('Requires: ')])

        description = """
%s %s
----
%s
""" % (full_title, estimates and '/'.join([ ("%.2f" % e) for e in estimates ]) or '', comment)

        if (postfix == 'S'): 
            l.set_custom('story', l, error=lambda prev: Exception(parent, prev))
            l._issue = jira.create_issue(
                project=PROJECT,
                summary=title,
                description=description,
                issuetype={'name': 'Story'},
                customfield_10400 = "(none)",
                components=[ { "name": n } for n in components ]
            )
            print l._issue
        elif (postfix == 'T'):
            est = "%sh" % int(0.9 + (estimates[0] + 4.0 * estimates[1] + estimates[2])*8.0/6.0)
            l._issue = jira.create_issue(
                project=PROJECT,
                summary=title,
                description=description,
                issuetype={'name': 'Task'},
                customfield_10400 = "(none)",
                components=[ { "name": n } for n in components ],
                timetracking={ "remainingEstimate": est, "originalEstimate": est }
            )
            story = l.acquire_custom('story')
            if (story and story._issue):
                jira.create_issue_link(link_type__task_of, l._issue, story._issue)
            print l._issue
        else:
            raise Error("Unknown postfix: [" + postfix + "]")
    else:
        raise Error("Unknown postfix: [" + postfix + "]")

from collections import defaultdict
id_to_node = defaultdict(list)

for line in lines:
    for id in line.annotation("node_ids"):
        id_to_node[id].append(line)

for line in lines:
    if (getattr(line, '_issue', None) is not None):
        for id in line.annotation("dependency_ids"):
            for dep in id_to_node[id]:
                if (getattr(dep, '_issue', None) is not None):
                    jira.create_issue_link(link_type__blocked_by, line._issue, dep._issue)