Example #1
0
class JiraWrapper(object):
    def __init__(self, name, url, username, password, projectKey,
                 issuetypeName, assignee, customFields, actions):

        self.name = name
        self.url = url
        self.username = username
        self.password = password
        self.projectKey = projectKey
        self.issuetypeName = issuetypeName
        self.logger = logging.getLogger("rss2jira")
        self.assignee = assignee
        self.customFields = customFields
        self.action = Action(actions)

        self.options = {
            'server': url,
            'basic_auth': {
                'username': username,
                'password': password,
            },
        }

        self.jira = JIRA(self.options)
        self._authenticate()

    def _authenticate(self):
        # This is a work around for a reported bug reguarding basic auth:
        # https://bitbucket.org/bspeakmon/jira-python/pull-request/4/fix-for-issue-6-basic-authentication
        # https://bitbucket.org/bspeakmon/jira-python/issue/6/basic-authentication-doesnt-actually-use
        auth_url = self.url + '/rest/auth/1/session'
        auth_data = HTTPBasicAuth(self.username, self.password)
        rv = self.jira._session.get(auth_url, auth=auth_data)
        self.logger.info("Authentication result: {} {}".format(
            rv.status_code, rv.text))

    def _issue_dict(self, entry):
        resolvedFields = self._resolve_action(entry)
        self.logger.debug("Title: " + entry.title[:254] +
                          ": Resolved Fields: " + str(resolvedFields))
        return dict(
            {
                'project': {
                    'key': self.projectKey
                },
                'summary':
                entry.title[:254],
                'description':
                "Go to {} ({}).".format(self.name, entry.link) + "\r\n\r\n" +
                self.action.result,
                'issuetype': {
                    'name': self.issuetypeName
                },
                'assignee': {
                    'name':
                    entry.assignee
                    if hasattr(entry, "assignee") else self.assignee
                }
            }, **resolvedFields)

    def _resolve_action(self, entry):
        try:
            return self.action.apply(entry, self.customFields)
        except Exception as e:
            self.logger.exception("Link : " + entry.link +
                                  ", with JIRA data: " +
                                  str(self.customFields))
            raise e

    def create_issue(self, entry):
        fields = self._issue_dict(entry)
        try:
            issue = self.jira.create_issue(fields=fields)
        except Exception as ex:
            self.logger.info(
                "Caught exception while creating JIRA issue. " +
                "Reauthenticating and trying again... %s", ex)
            self._authenticate()
            issue = self.jira.create_issue(fields=fields)
        for link in self.action.links:
            try:
                self.jira.add_simple_link(self.jira.issue(issue), {
                    "url": link,
                    "title": link
                })
            except:
                self.logger.exception(
                    str(issue) + ": Exception: Could not link URL: " + link)
        return issue
Example #2
0
#coding=utf-8
# auther: yubb
from jira.client import JIRA
issue = ['MMMJ-32', 'MMMJ-36']
describe = '来自python的测试'
flow_id = '431'
jira = JIRA('http://jira.m.com/', basic_auth=('gitlab', '123kaibola'))
for issue_id in issue:
    jira.add_comment(issue_id, describe)
    transitions = jira.transitions(issue_id)
    print transitions
    print[(t['id'], t['name']) for t in transitions]
    jira.transition_issue(issue_id, flow_id)
    jira.add_simple_link(issue_id, {
        "url": "http://www.2339.com/",
        "title": "1234"
    })