#!/usr/bin/python

import suds
import urllib2
import atexit

project = "FRWA"
jiraHost = "http://localhost:8080/"
jiraUser = "user"
jiraPassword = "pw"
proxy = "localhost:3128"

proxy = urllib2.ProxyHandler({"http": proxy, "https": proxy})
t = suds.transport.http.HttpTransport()
t.urlopener = urllib2.build_opener(proxy)
#soap = suds.client.Client(jiraHost + "rpc/soap/jirasoapservice-v2?wsdl", transport=t)
soap = suds.client.Client(jiraHost + "rpc/soap/jirasoapservice-v2?wsdl")
auth = soap.service.login(jiraUser, jiraPassword)
atexit.register(lambda: soap.service.logout())

users = {}
statuses = {}
priorities = {}
resolutions = {}
types = {}

def createVersion(name, sequence):
    version = soap.factory.create("ns0:RemoteVersion")
    version.name = name
    version.archived = False
    version.released = False
    version.sequence = sequence
    return version

def saveVersion(version):
    soap.service.addVersion(auth, project, version)

def getStatus(statusCode):
    if statuses:
        return statuses[statusCode]

    jiraStatuses = soap.service.getStatuses(auth)
    for jiraStatus in jiraStatuses:
        statuses[jiraStatus.id] = jiraStatus.name

    return statuses[statusCode]

def getPriority(priorityCode):
    if priorities:
        return priorities[priorityCode]

    jiraPriorities = soap.service.getPriorities(auth)
    for jiraPriority in jiraPriorities:
        priorities[jiraPriority.id] = jiraPriority.name

    return priorities[priorityCode]

def getType(typeCode):
    if types:
        return types[typeCode]

    jiraTypes = soap.service.getIssueTypes(auth)
    for jiraType in jiraTypes:
        types[jiraType.id] = jiraType.name
    jiraTypes = soap.service.getSubTaskIssueTypes(auth)
    for jiraType in jiraTypes:
        types[jiraType.id] = jiraType.name

    return types[typeCode]

def getResolution(resolutionCode):
    if resolutionCode is None:
                return "Unresolved"
    if resolutions:
        return resolutions[resolutionCode]

    jiraResolutions = soap.service.getResolutions(auth)

    for jiraResolution in jiraResolutions:
        resolutions[jiraResolution.id] = jiraResolution.name

    return resolutions[resolutionCode]

def getUser(userCode):
    if userCode in users:
        return users[userCode]

    user = soap.service.getUser(auth, userCode)
    users[user.name] = user.fullname
    return users[userCode]

def getJiraLink(issue):
    return jiraHost + "browse/" + issue

def getVersion(name):
    versions = filter (lambda version: version.name.strip() == name, \
        soap.service.getVersions(auth, project))
    return versions[0]

def getRequestedIssues(version):
    return soap.service.getIssuesFromJqlSearch(auth,
        "project = " + project + " AND \"Requested Version\" in (\"" + version.name + "\") ORDER BY key DESC", 100)

def getFixedIssues(version):
    return soap.service.getIssuesFromJqlSearch(auth,
        "project = " + project + " AND \"fixVersion\" in (\"" + version.name + "\") ORDER BY key DESC", 100)

def getSpesialTasks(jql_query):
    return soap.service.getIssuesFromJqlSearch(auth,
            jql_query, 100)

#        'project = FRWA AND resolution in (Unresolved, Reopened) AND assignee = mishnik', 100)    
def getIssue(id):
    return soap.service.getIssue(auth, id)

def getParticipants(jira):
    userCodes = filter (lambda field: field.customfieldId == "customfield_10652", \
        jira.customFieldValues)[0].values
    return  ','.join(map (lambda code: getUser(code), userCodes))
Hosted by uCoz