示例#1
0
def _upload_file(manager, issue, upload_file, filename):
    # This method will fix original method jira.JIRA.add_attachment (jira/client.py line 591)
    url = manager._get_url('issue/' + str(issue) + '/attachments')
    files = {
        'file': (filename, upload_file),
    }
    headers = {
        'X-Atlassian-Token': 'no-check',
    }
    req = Request('POST',
                  url,
                  headers=headers,
                  files=files,
                  auth=manager._session.auth)
    prepped = req.prepare()
    prepped.body = re.sub(b'filename=.*',
                          b'filename="%s"\r' % filename.encode('utf-8'),
                          prepped.body)
    r = manager._session.send(prepped)

    js = utils.json_loads(r)

    if not js or not isinstance(js, collections.Iterable):
        raise JIRAError("Unable to parse JSON: %s" % js)

    attachment = Attachment(manager._options, manager._session, js[0])

    if attachment.size == 0:
        raise JIRAError("Added empty attachment?!: r: %s\nattachment: %s" %
                        (r, attachment))

    return attachment
示例#2
0
def create_customer_request(self,
                            fields=None,
                            prefetch=True,
                            use_old_api=False,
                            **fieldargs):
    """The code for this function is almost completely copied from
    function create_customer_request of the JIRA library"""
    data = fields

    p = data['serviceDeskId']
    service_desk = None

    if isinstance(p, str) or isinstance(p, int):
        service_desk = self.service_desk(p)
    elif isinstance(p, ServiceDesk):
        service_desk = p

    data['serviceDeskId'] = service_desk.id

    p = data['requestTypeId']
    if isinstance(p, int):
        data['requestTypeId'] = p
    elif isinstance(p, str):
        data['requestTypeId'] = self.request_type_by_name(service_desk, p).id

    requestParticipants = data.pop('requestParticipants', None)

    url = self._options['server'] + '/rest/servicedeskapi/request'
    headers = {'X-ExperimentalApi': 'opt-in'}
    r = self._session.post(url, headers=headers, data=json.dumps(data))

    raw_issue_json = json_loads(r)
    if 'issueKey' not in raw_issue_json:
        raise JIRAError(r.status_code, request=r)

    if requestParticipants:
        url = (self._options['server'] +
               '/rest/servicedeskapi/request/%s/participant' %
               raw_issue_json['issueKey'])
        headers = {'X-ExperimentalApi': 'opt-in'}

        if use_old_api:
            data = {'usernames': requestParticipants}
        else:
            data = {'accountIds': requestParticipants}

        r = self._session.post(url, headers=headers, json=data)

    if r.status_code != status.HTTP_200_OK:
        raise JIRAError(r.status_code, request=r)

    if prefetch:
        return self.issue(raw_issue_json['issueKey'])
    else:
        return Issue(self._options, self._session, raw=raw_issue_json)
示例#3
0
def _get_filename(path):
    limit = CHARS_LIMIT - PADDING
    fname = os.path.basename(path)
    filename = fname.split('.')[0]
    filename_extension = fname.split('.')[1:]
    count = (
        len('.'.join(filename_extension).encode('utf-8')) + 1
        if filename_extension
        else 0
    )
    char_limit = 0

    for char in filename:
        count += len(char.encode('utf-8'))
        if count > limit:
            break
        else:
            char_limit += 1

    if not char_limit:
        raise JIRAError('Attachment filename is very long.')

    tmp = [filename[:char_limit]]
    tmp.extend(filename_extension)
    filename = '.'.join(tmp)
    return filename
示例#4
0
 def test_create_returns_JIRAError(self, mock_get_users: Mock,
                                   mock_JIRA_client: Mock) -> None:
     mock_JIRA_client.return_value.create_issue.side_effect = JIRAError(
         'Some exception')
     with app.test_request_context():
         mock_get_users.return_value = [self.mock_user]
         try:
             jira_client = JiraClient(
                 issue_labels=[],
                 issue_tracker_url=app.config['ISSUE_TRACKER_URL'],
                 issue_tracker_user=app.config['ISSUE_TRACKER_USER'],
                 issue_tracker_password=app.
                 config['ISSUE_TRACKER_PASSWORD'],
                 issue_tracker_project_id=app.
                 config['ISSUE_TRACKER_PROJECT_ID'],
                 issue_tracker_max_results=app.
                 config['ISSUE_TRACKER_MAX_RESULTS'])
             jira_client.create_issue(description='desc',
                                      owner_ids=['test_email'],
                                      frequent_user_ids=['test_email'],
                                      priority_level='P2',
                                      project_key='',
                                      table_uri='key',
                                      title='title',
                                      table_url='http://table')
         except JIRAError as e:
             self.assertTrue(type(e), type(JIRAError))
             self.assertTrue(e, 'Some exception')
示例#5
0
def _get_filename(path):
    # JIRA does not support composite symbols from Latin-1 charset.
    # Hence we need to use NFD normalization which translates
    # each character into its decomposed form.
    path = unicodedata.normalize('NFD', path)
    limit = CHARS_LIMIT - PADDING
    fname = os.path.basename(path)
    filename = fname.split('.')[0]
    filename_extension = fname.split('.')[1:]
    count = (len('.'.join(filename_extension).encode('utf-8')) +
             1 if filename_extension else 0)
    char_limit = 0

    for char in filename:
        count += len(char.encode('utf-8'))
        if count > limit:
            break
        else:
            char_limit += 1

    if not char_limit:
        raise JIRAError('Attachment filename is very long.')

    tmp = [filename[:char_limit]]
    tmp.extend(filename_extension)
    filename = '.'.join(tmp)
    return filename
示例#6
0
        def create_custom_field(fields=None):
            url = self._get_url('field')
            r = self._session.post(url, data=json.dumps(fields))

            if r.status_code != 201:
                raise JIRAError(r.status_code, request=r)

            return r
示例#7
0
def update_description(username, password, issue, new_description):
    try:
        Jira = JIRA(JIRA_URL, basic_auth=(username, password), max_retries=0)
        existing_issue = Jira.issue(issue)
        existing_issue.update(description=new_description)
        Jira.kill_session()
    except JIRAError as e:
        raise JIRAError()
示例#8
0
def add_jira_comment(username, password, issue, comment):
    '''
    Given a comment, add it to the provided JIRA Issue
    '''
    Jira = JIRA(JIRA_URL, basic_auth=(username, password), max_retries=0)
    try:
        jira_issue = Jira.issue(issue)
    except JIRAError as e:
        raise JIRAError(text="JIRA Error {}: {}".format(
            e.response.status_code, e.response.reason))
    Jira.add_comment(jira_issue, comment)
示例#9
0
def add_watchers(username, password, issue, watchers):
    '''
    Given a list of watchers, add them to the provided JIRA Issue
    '''
    Jira = JIRA(JIRA_URL, basic_auth=(username, password), max_retries=0)
    try:
        jira_issue = Jira.issue(issue)
    except JIRAError as e:
        raise JIRAError()
    for watcher in watchers:
        Jira.add_watcher(jira_issue, watcher)
示例#10
0
def get_project_id_from_name(username, password, name):
    '''
    Gets a project ID from the provided project Name
    Example: 'Single Cell' returns 11220
    '''
    try:
        Jira = JIRA(JIRA_URL, basic_auth=(username, password), max_retries=0)
        projects = sorted(Jira.projects(),
                          key=lambda project: project.name.strip())
        for project in projects:
            if (project.name == name):
                return project.id
        return None
    except JIRAError as e:
        raise JIRAError()
示例#11
0
 def test_get_issues_returns_JIRAError(self, mock_remaining_issues: Mock, mock_JIRA_client: Mock) -> None:
     mock_JIRA_client.return_value.get_issues.side_effect = JIRAError('Some exception')
     mock_remaining_issues.return_value = 0
     with app.test_request_context():
         try:
             jira_client = JiraClient(issue_labels=[],
                                      issue_tracker_url=app.config['ISSUE_TRACKER_URL'],
                                      issue_tracker_user=app.config['ISSUE_TRACKER_USER'],
                                      issue_tracker_password=app.config['ISSUE_TRACKER_PASSWORD'],
                                      issue_tracker_project_id=app.config['ISSUE_TRACKER_PROJECT_ID'],
                                      issue_tracker_max_results=app.config['ISSUE_TRACKER_MAX_RESULTS'])
             jira_client.get_issues('key')
         except JIRAError as e:
             self.assertTrue(type(e), type(JIRAError))
             self.assertTrue(e, 'Some exception')
示例#12
0
def create_customer(self, email, displayName):
    """Create a new customer and return an issue Resource for it."""
    url = self._options['server'] + '/rest/servicedeskapi/customer'
    headers = {'X-ExperimentalApi': 'opt-in'}
    r = self._session.post(
        url,
        headers=headers,
        data=json.dumps({
            'email': email,
            'fullName': displayName,  # different property for the server one
        }),
    )

    raw_customer_json = json_loads(r)

    if r.status_code != 201:
        raise JIRAError(r.status_code, request=r)
    return Customer(self._options, self._session, raw=raw_customer_json)
 def test_create_returns_JIRAError(self, mock_JIRA_client: Mock) -> None:
     mock_JIRA_client.return_value.create_issue.side_effect = JIRAError(
         'Some exception')
     with app.test_request_context():
         try:
             jira_client = JiraClient(
                 issue_tracker_url=app.config['ISSUE_TRACKER_URL'],
                 issue_tracker_user=app.config['ISSUE_TRACKER_USER'],
                 issue_tracker_password=app.
                 config['ISSUE_TRACKER_PASSWORD'],
                 issue_tracker_project_id=app.
                 config['ISSUE_TRACKER_PROJECT_ID'],
                 issue_tracker_max_results=app.
                 config['ISSUE_TRACKER_MAX_RESULTS'])
             jira_client.create_issue(description='desc',
                                      table_uri='key',
                                      title='title')
         except JIRAError as e:
             self.assertTrue(type(e), type(JIRAError))
             self.assertTrue(e, 'Some exception')
示例#14
0
def service_desk(manager, id_or_key):
    """In Jira v8.7.1 / SD 4.7.1 a Service Desk ID must be an integer.
    We use a hackish workaround to make it work till Atlassian resolves bug
    https://jira.atlassian.com/browse/JSDSERVER-4877.
    """
    try:
        return manager.service_desk(id_or_key)
    except JIRAError as e:
        if 'java.lang.NumberFormatException' in e.text:
            service_desks = [
                sd for sd in manager.service_desks()
                if sd.projectKey == id_or_key
            ]
            if len(service_desks):
                return service_desks[0]
            else:
                msg = 'The Service Desk with ID {id} does not exist.'.format(
                    id=id_or_key)
                raise JIRAError(text=msg, status_code=404)
        else:
            raise e
示例#15
0
def create_ticket(
    username,
    password,
    project,
    title,
    description,
    reporter,
    assignee=None,
    watchers=None,
):
    '''
    Creates a JIRA Ticket
    Returns the string URL of the JIRA Ticket
    '''
    try:
        Jira = JIRA(JIRA_URL, basic_auth=(username, password), max_retries=0)
        issue_dict = {
            'project': {
                'id': int(project)
            },
            'summary': title,
            'description': description,
            'issuetype': {
                'name': 'Task'
            },
            'reporter': {
                'name': reporter
            },
            'assignee': {
                'name': assignee
            },
        }
        new_issue = Jira.create_issue(fields=issue_dict)
        if watchers is not None:
            for watcher in watchers:
                jira.add_watcher(new_issue.id, watcher)
        Jira.kill_session()
        return str(new_issue)
    except JIRAError as e:
        raise JIRAError()
示例#16
0
    def __init__(
        self,
        server="https://flatironhealth.atlassian.net",
        board="SEGTEST",  # TODO Change this to a board that is monitored
        access_token=None,
        access_token_secret=None,
        consumer_key=None,
        key_cert=None,
        api_token=None,
    ):
        options = {"server": server}
        self.jira = None
        self.board = board

        oauth_dict = {}
        if access_token and access_token_secret and consumer_key and key_cert:
            key_cert_data = (open(os.path.expanduser(key_cert), "r").read()
                             if os.path.isfile(os.path.expanduser(key_cert))
                             else key_cert.replace("\\n", "\n"))

            oauth_dict = {
                "access_token": access_token,
                "access_token_secret": access_token_secret,
                "consumer_key": consumer_key,
                "key_cert": key_cert_data,
            }

            self.jira = JIRA(options, oauth=oauth_dict)

        elif api_token:
            creds = (self._get_api_token(api_token)
                     if os.path.isfile(api_token) else api_token.split(":"))

            self.jira = JIRA(options, basic_auth=(tuple(creds)))

        if not getattr(self, "jira"):
            raise JIRAError("JIRA unable to be initialized")
 def jira_add_watcher_mock(*_):
     raise JIRAError(status_code=401, response=Response('dummy msg'))
示例#18
0
 def issue(self, issue_key):
     try:
         return next(i for i in self._issues if i.key == issue_key)
     except StopIteration:
         raise JIRAError("Issue %s not found" % issue_key)