Exemplo n.º 1
0
 def check_authorization(auth_method, jira_host, credentials, base_check=None):
     """
     Used to get the connection object.
     If the connection object is not needed - add the base_check flag (the object will be destroyed)
     :param auth_method: basic or oauth
     :param jira_host: https://jira.somecompany.com
     :param credentials: different for each methods
     :param base_check: to destroy the object
     :return: Jira connection object
     """
     try:
         if auth_method == 'basic':
             jira_conn = jira.JIRA(
                 server=jira_host,
                 basic_auth=credentials,
                 max_retries=0
             )
         else:
             jira_conn = jira.JIRA(
                 server=jira_host,
                 oauth=credentials,
                 max_retries=0
             )
     except jira.JIRAError as e:
         raise JiraLoginError(e.status_code, jira_host, auth_method, credentials)
     except ConnectionError:
         raise JiraConnectionError(jira_host)
     else:
         if base_check:
             jira_conn.kill_session()
         else:
             return jira_conn
Exemplo n.º 2
0
    def __init__(self, mail: ProcessedMail, config: JiraConfig):
        self.mail = mail  # type: ProcessedMail
        self.config = config  # type: JiraConfig

        self.jira = jira.JIRA(self.config.jiraHost,
                              basic_auth=(self.config.jiraUser,
                                          self.config.jiraPass))
Exemplo n.º 3
0
 def __init__(self, config, issue_id):
     self.config = config
     self.client = jira.JIRA(
         server=self.config['server'],
         basic_auth=(self.config['username'], self.config['password']),
     )
     self.issue = self.client.issue(issue_id)
Exemplo n.º 4
0
    def add_attachment(self, request):
        args = list()

        if request.args:
            args = ast.literal_eval(request.args)

        # Because Attachments can be quite large,
        # then we tighten them with individual requests from Jira
        client = jira.JIRA(
            server=request.config_id.location,
            basic_auth=(
                request.config_id.username, request.config_id.password)
        )

        for arg in args:
            task = self.env["project.task"].search([
                ("key", "=", arg["issue"])
            ])
            attachment = client.attachment(arg["jira_id"])

            data = {
                "datas": base64.b64encode(attachment.get()),
                "datas_fname": attachment.filename,
                "name": attachment.filename,
                "res_id": task.id,
                "res_model": "project.task"
            }

            ir_attachment = self.env['ir.attachment']
            ir_attachment.create(data)
Exemplo n.º 5
0
def test_creds(url, credentials):
    # We don't need an api call, this will fail if the creds are invalid
    jira.JIRA(url,
              validate=True,
              get_server_info=True,
              timeout=5,
              oauth=credentials)
def run_using_cli(args):
    config_logger(args.verbose)

    logger.debug('Starting issue resolver task using CLI with args=%s',
                 pprint.pformat(args.__dict__))

    jira_client = jira.JIRA(consts.JIRA_SERVER,
                            token_auth=args.jira_access_token)

    if args.filters_json:
        filters_json = read_filters_file(args.filters_json)
        filters = get_filters_from_json(filters_json, jira_client)
    else:
        filters = get_filters_from_args(args, jira_client)

    issues = get_issues(jira_client,
                        args.issue,
                        only_recent=args.recent_issues)

    dry_run_stdout = get_dry_run_stdout(args)
    try:
        close_tickets_by_filters(
            jira_client=jira_client,
            filters=filters,
            issues=issues,
            dry_run_stdout=dry_run_stdout,
        )
    finally:
        if dry_run_stdout:
            dry_run_stdout.close()
Exemplo n.º 7
0
    def __init__(  # pylint: disable=too-many-arguments
            self,
            server,
            username=None,
            password=None,
            access_token=None,
            access_token_secret=None,
            consumer_key=None,
            key_cert=None):
        """Initialize the JiraClient with the server URL and user credentials."""
        opts = {"server": server, "verify": True}
        basic_auth = None
        oauth_dict = None
        if access_token and access_token_secret and consumer_key and key_cert:
            oauth_dict = {
                "access_token": access_token,
                "access_token_secret": access_token_secret,
                "consumer_key": consumer_key,
                "key_cert": key_cert
            }
        elif username and password:
            basic_auth = (username, password)
        else:
            raise TypeError(
                "Must specify Basic Auth (using arguments username & password)"
                " or OAuth (using arguments access_token, access_token_secret,"
                " consumer_key & key_cert_file) credentials")
        self._jira = jira.JIRA(options=opts,
                               basic_auth=basic_auth,
                               oauth=oauth_dict,
                               validate=True)

        self._transitions = {}
        self._resolutions = {}
Exemplo n.º 8
0
    def jira(self):
        '''If the credentials are invalid, the jira connect call will fail. So this doubles as a test routine'''
        merged_creds = {}
        try:
            merged_creds['consumer_key'] = self.config_data[
                self.url]['consumer_key']
            merged_creds['key_cert'] = self.config_data[self.url]['rsa_key']
        except KeyError as err:
            logging.critical('Missing shared auth data\n')
            raise err

        try:
            merged_creds['access_token'] = self.auth_data[
                self.url]['oauth_token']
            merged_creds['access_token_secret'] = self.auth_data[
                self.url]['oauth_token_secret']
        except KeyError as err:
            logging.critical(
                'Missing auth tokens. Do you need to oauth-dance?\n')
            raise err

        jira_obj = jira.JIRA(self.url,
                             validate=True,
                             get_server_info=True,
                             timeout=5,
                             oauth=merged_creds)

        return jira_obj
Exemplo n.º 9
0
    def jira(self):
        """
        lazily get a jira client.
        """
        if not self._jira:
            while True:
                try:
                    _jira = jira.JIRA(
                        options={'server': JIRA_URL},
                        basic_auth=(self.username, self.jira_pwd),
                        validate=True,
                        logging=False,
                        max_retries=3,
                        timeout=5,  # I think the unit is seconds.
                    )
                    if _jira:
                        self._jira = _jira
                        break
                except jira.exceptions.JIRAError as e:
                    get_logger().warning(
                        'Failed to login to Jira. Please re-enter your username and password. '
                        'If the failure persists, please login to Jira manually in a browser. '
                        'If that still doesn\'t work, seek help in #new-server-eng-help'
                    )
                    get_logger().debug(e)
                    self.reset_jira_credentials()

        return self._jira
def jira_client_from_options(options):
    kwargs = {}

    jira_server = options.jira_server

    if not jira_server:
        logger.debug("Not using JIRA client")
        return None

    kwargs["server"] = jira_server

    jira_cookie_username = options.jira_cookie_username
    jira_cookie_password = options.jira_cookie_password
    jira_http_basic_username = options.jira_http_basic_username
    jira_http_basic_password = options.jira_http_basic_password
    jira_oauth_access_token = options.jira_oauth_access_token
    jira_oauth_access_token_secret = options.jira_oauth_access_token_secret
    jira_oauth_consumer_key = options.jira_oauth_consumer_key
    jira_oauth_key_cert_file = options.jira_oauth_key_cert_file
    jira_kerberos = options.jira_kerberos

    is_cookie_auth = any([jira_cookie_username, jira_cookie_password])
    is_basic_auth = any([jira_http_basic_username, jira_http_basic_password])
    is_oauth = any(
        [jira_oauth_access_token, jira_oauth_access_token_secret, jira_oauth_consumer_key, jira_oauth_key_cert_file]
    )
    is_kerberos = jira_kerberos

    auth_methods_requested = {
        "Cookie Auth": is_cookie_auth,
        "Basic Auth": is_basic_auth,
        "OAuth": is_oauth,
        "Kerberos": is_kerberos,
    }

    num_auth_methods_requested = sum(auth_methods_requested.values())
    if num_auth_methods_requested == 0:
        raise ValueError("No JIRA authentication details provided")
    elif num_auth_methods_requested > 1:
        names_of_auth_methods_requested = [name for name, was_requested in auth_methods_requested if was_requested]
        raise ValueError(
            f"Too many JIRA authentication methods requested: {', '.join(names_of_auth_methods_requested)}"
        )
    elif is_cookie_auth:
        kwargs["auth"] = (jira_cookie_username, jira_cookie_password)
    elif is_basic_auth:
        kwargs["basic_auth"] = (jira_http_basic_username, jira_http_basic_password)
    elif is_oauth:
        kwargs["oauth"] = {
            "access_token": jira_oauth_access_token,
            "access_token_secret": jira_oauth_access_token_secret,
            "consumer_key": jira_oauth_consumer_key,
            "key_cert": pathlib.Path(jira_oauth_key_cert_file).read_text(),
        }
    elif is_kerberos:
        kwargs["kerberos"] = True
    else:
        raise RuntimeError("Programmer error - unhandled case")

    return JiraClient(jira.JIRA(**kwargs))
Exemplo n.º 11
0
def get_repos_data(user='******', jira_url=r"http://issues.apache.org/jira"):
    gh = github3.login(
        token=os.environ['GITHUB_TOKEN'])  # [email protected]
    repos = list(gh.search_repositories('org:{0} language:Java'.format(user)))
    conn = jira.JIRA(jira_url)
    jira_projects = conn.projects()
    github_repos = list(
        map(lambda repo: repo.as_dict()['name'].strip().lower(), repos))
    _repos = list(
        map(lambda x: x
            if '-' not in x else "-".join(x.split('-')[1:]), github_repos))
    jira_keys = list(map(lambda p: p.key.strip().lower(), jira_projects))
    jira_names = list(
        map(lambda p: "-".join(p.name.strip().lower().split()), jira_projects))
    jira_elements = list(set(jira_names + jira_keys))
    _elements = list(
        map(lambda x: x
            if '-' not in x else "-".join(x.split('-')[1:]), jira_elements))
    jira_elements = list(set(_elements + jira_elements))
    jira_and_github = list(
        map(
            lambda x: x[0],
            filter(
                lambda x: x[1] > 1,
                Counter(list(set(github_repos + _repos)) +
                        jira_elements).most_common())))
    for key in jira_and_github:
        for repo in find_repo_and_jira(key, repos, jira_projects):
            if repo:
                print(repo)
Exemplo n.º 12
0
def create_report(**kwargs):
    j_host = kwargs.get('jira_host')
    j_user = kwargs.get('jira_user_id')
    j_user_pwd = kwargs.get('jira_user_password')
    t_host = kwargs.get('testrail_host')
    t_user = kwargs.get('testrail_user')
    t_user_key = kwargs.get('testrail_user_key')
    t_plan = kwargs.get('testrail_plan')
    t_project = kwargs.get('testrail_project')
    t_a_run = kwargs.get('testrail_only_run')
    o_type = kwargs.get('out_type')
    push_report_flag = kwargs.get('push_report_flag')
    sort_by = kwargs.get('sort_by')

    t_client = TestRail(email=t_user, key=t_user_key, url=t_host)
    t_client.set_project_id(t_client.project(t_project).id)

    j_client = jira.JIRA(j_host, basic_auth=(j_user, j_user_pwd))

    runs = get_runs(t_client, t_plan, t_a_run)
    results = get_all_results(t_client, runs)
    table = get_defects_table(j_client, results, sort_by)
    out_table(o_type, table)
    if push_report_flag:
        push_report(t_client, t_plan, table)
Exemplo n.º 13
0
 def get_cnx(self):
     if self._cnx:
         return self._cnx
     self._cnx = jira.JIRA(
         server=self.config['server'],
         basic_auth=(self.config['username'], self.config['password']))
     return self._cnx
Exemplo n.º 14
0
    def send_notification(self, notification):
        """Creates or Updates an issue in Jira
        """

        jira_fields = self._build_jira_message(notification)

        parsed_url = urllib.parse.urlsplit(notification.address)
        query_params = urllib.parse.parse_qs(parsed_url.query)
        # URL without query params
        url = urllib.parse.urljoin(
            notification.address,
            urllib.parse.urlparse(notification.address).path)

        jira_fields["project"] = query_params["project"][0]
        if query_params.get("component"):
            jira_fields["component"] = query_params["component"][0]

        auth = (CONF.jira_notifier.user, CONF.jira_notifier.password)
        proxy = CONF.jira_notifier.proxy
        proxy_dict = None
        if proxy is not None:
            proxy_dict = {"https": proxy}

        try:
            jira_obj = jira.JIRA(url, basic_auth=auth, proxies=proxy_dict)

            self.jira_workflow(jira_fields, jira_obj, notification)
        except Exception:
            self._log.exception(
                "Error creating issue in Jira at URL {}".format(url))
            return False

        return True
Exemplo n.º 15
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        default=False,
                        help='be verbose')
    args = parser.parse_args()

    log_level = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(level=log_level)

    smtp_auth = settings.SMTP_USER, settings.SMTP_PASS

    j = jira.JIRA(server=settings.JIRA_URL,
                  basic_auth=(settings.JIRA_USER, settings.JIRA_PASS))

    date = datetime.date.today() - datetime.timedelta(days=get_day_offset())

    report_subject = settings.DAILY_SUBJECT.format(date.strftime('%Y-%m-%d'), )

    issues = get_issues(j, settings.JIRA_PROJECT, date, settings.FUNC)

    report, total = parse_worklogs(j, date, settings.FUNC, issues)
    scope = {
        'report': report,
        'total': total,
    }
    send_on_email(render(scope), report_subject, settings.EMAIL_FROM,
                  settings.EMAIL_TO, smtp_auth)
    return 0
Exemplo n.º 16
0
def main():
    parser = argparse.ArgumentParser(
        description="Fetch Fake JIRA configuration")
    parser.add_argument("-a",
                        "--address",
                        dest="address",
                        required=True,
                        help="JIRA address")
    parser.add_argument("-u",
                        "--username",
                        dest="username",
                        help="JIRA username")
    parser.add_argument("-p",
                        "--password",
                        dest="password",
                        help="JIRA password")
    parser.add_argument(
        "-o",
        "--out",
        dest="out",
        type=argparse.FileType("w"),
        default=sys.stdout,
        help="output path, default is stdout",
    )
    args = parser.parse_args()
    basic_auth = None
    if args.username:
        while not args.password:
            args.password = getpass.getpass("JIRA password:")
        basic_auth = (args.username, args.password)
    jh = jira.JIRA(server=args.address, basic_auth=basic_auth)
    fetch_config(jh, args.out)
Exemplo n.º 17
0
    def __init__(
        self,
        githubToken: str,
        pull_request_dicts: List[dict],
        repo_owner: str,
        repo_name: str,
        jira_token: str = "",
        jira_username: str = "",
        jira_url: str = "",
    ):
        """
        :param githubToken: GitHub oauth githubToken
        """
        self.githubToken = githubToken
        self.pull_request_dicts = pull_request_dicts
        self.repo_owner = repo_owner
        self.repo_name = repo_name
        self.jira_token = jira_token
        self.jira_url = jira_url
        self.gh = github3.GitHub(token=self.githubToken)

        # documentation claims you need to use username/password combo but username/token works as well
        if jira_token:
            self.jira = jira.JIRA(jira_url,
                                  basic_auth=(jira_username, self.jira_token))
Exemplo n.º 18
0
    def add_attachment(self, request):
        args = list()

        if request.args:
            args = ast.literal_eval(request.args)

        for arg in args:
            task = self.env["project.task"].search([("key", "=", arg["issue"])
                                                    ])
            # Т.к. аттачи могут быть достаточно объемными,
            # то затягиваем их отдельными запросами из Jira
            client = jira.JIRA(server=request.config_id.location,
                               basic_auth=(request.config_id.username,
                                           request.config_id.password))
            attachment = client.attachment(arg["jira_id"])

            data = {
                "datas": base64.b64encode(attachment.get()),
                "datas_fname": attachment.filename,
                "name": attachment.filename,
                "res_id": task.id,
                "res_model": "project.task"
            }

            ir_attachment = self.env['ir.attachment']
            ir_attachment.create(data)
Exemplo n.º 19
0
    def create_jira(cls, config):
        if not config.valid():
            raise Exception

        return jira.JIRA(config.url,
                         basic_auth=(config.username, config.password),
                         validate=True,
                         max_retries=0)
Exemplo n.º 20
0
def _connect_to_jira():
  """Opens a JIRA API connection based on username/pw from env vars."""
  for varname in (_JIRA_PASSWORD_VARNAME, _JIRA_NAME_VARNAME):
    if varname not in os.environ:
      raise RuntimeError('No environment variable value for %r.' % varname)
  return jira.JIRA(
      _JIRA_INSTANCE_URL,
      basic_auth=(os.getenv(_JIRA_NAME_VARNAME), os.getenv(_JIRA_PASSWORD_VARNAME)))
Exemplo n.º 21
0
 def _create_client(self):
     """Return a client object for querying the issue tracker."""
     config = db_config.get()
     credentials = json.loads(config.jira_credentials)
     jira_url = config.jira_url
     jira_client = jira.JIRA(jira_url,
                             auth=(credentials['username'],
                                   credentials['password']))
     return jira_client
def JiraClient():
    global __JIRA_CLIENT_SINGLETON
    if __JIRA_CLIENT_SINGLETON is None:
        # can thrown requests.exceptions.SSLError
        __JIRA_CLIENT_SINGLETON = jira.JIRA(
            server=JIRA_SERVER,
            basic_auth=(JIRA_BASIC_AUTH_USERNAME, JIRA_BASIC_AUTH_PASSWORD),
        )
    return __JIRA_CLIENT_SINGLETON
Exemplo n.º 23
0
 def run(self):
     j = jira.JIRA(self.CFG.get("server"),
                   basic_auth=(self.CFG.get("username"),
                               self.CFG.get("token")))
     issues = j.search_issues(self.CFG.get("query"))
     for i in issues:
         url = f"{self.CFG.get('server')}/browse/{i.fields.project}-{i.id}"
         msg = f"{Colours.OKGREEN}{i.id}:{Colours.ENDC} {Colours.BLUE}{i.fields.summary}{Colours.ENDC} {Colours.WHITE}{url}{Colours.ENDC} {Colours.BLUE}{i.fields.updated}{Colours.ENDC}"
         print(msg)
Exemplo n.º 24
0
 def jira_handle(self):
     if self._jira_handle is None:
         username, password = zazu.credential_helper.get_user_pass_credentials(
             'Jira')
         self._jira_handle = jira.JIRA(self._base_url,
                                       basic_auth=(username, password),
                                       options={'check_update': False},
                                       max_retries=0)
     return self._jira_handle
Exemplo n.º 25
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-a', '--max-age', required=True, type=int, help='maximum task age (days)')
    parser.add_argument('-p', '--push', required=False, type=int, nargs='+', help='Push this tasks to the queue',
                        default=())
    parser.add_argument('-s', '--skip', required=False, type=int, nargs='+', help='Skip this tasks in the queue',
                        default=())
    parser.add_argument('-q', '--max-queue', required=False, type=int, help='maximum queue size', default=50)
    args = parser.parse_args()

    skip = [key(n) for n in args.skip]

    j = jira.JIRA(
        server=settings.JIRA_URL,
        basic_auth=(
            settings.JIRA_USER,
            settings.JIRA_PASS)
    )

    tasks = collections.OrderedDict()

    critical = get_critical_tasks(j, settings.JIRA_PROJECT, settings.FUNC)
    bugs = get_bugs(j, settings.JIRA_PROJECT, settings.FUNC)

    max_queue = args.max_queue
    old_count = max_queue - len(critical) - len(bugs) - len(args.push)

    old = get_old_tasks(j, settings.JIRA_PROJECT, settings.FUNC, args.max_age, old_count)

    for suite in critical, bugs, old:
        for task in suite:
            if task.key in skip:
                continue
            tasks[task.key] = task

    for i in args.push:
        task_key = key(i)
        try:
            tasks[task_key] = j.issue(task_key)
        except jira.exceptions.JIRAError:
            pass

    scope = {
        'tasks': tasks,
        'max_age': args.max_age,
    }

    smtp_auth = settings.SMTP_USER, settings.SMTP_PASS

    send_on_email(
        render(scope),
        settings.QUEUE_SUBJECT.format(datetime.datetime.now().strftime('%d-%m-%Y')),
        settings.EMAIL_FROM,
        settings.EMAIL_TO,
        smtp_auth
    )
    return 0
Exemplo n.º 26
0
    def __init__(self, github_credentials, jira_credentials):
        options = {"server": jira_credentials.host}
        auth = (jira_credentials.user, jira_credentials.api_token)
        self.jira_connection = jira.JIRA(options, basic_auth=auth)

        self.github_connection = github.Github(github_credentials.access_token)
        self.repo = self.github_connection.get_repo(
            github_credentials.repository_full_name)
        self.repo_full_name = github_credentials.repository_full_name
Exemplo n.º 27
0
    def synchronize_projects(self):
        import jira
        import base64
        import requests

        for server in self:
            client = jira.JIRA(server=server.location,
                               basic_auth=(server.username, server.password))

            for simple_project in client.projects():
                project_data = client.project(simple_project.id).raw

                project_type = self.env["project.type"].search(
                    [("name", "ilike", project_data["projectTypeKey"])],
                    limit=1)

                if not project_type:
                    task_types = []
                    for issue_type in project_data["issueTypes"]:

                        task_type = self.env["project.task.type2"].search([
                            ("description", "=", issue_type["name"])
                        ])

                        if not task_type:
                            task_type = self.env["project.task.type2"].create(
                                {"description": issue_type["name"]})
                        task_types.append(task_type.id)

                    project_type = self.env["project.type"].create({
                        "name":
                        project_data["projectTypeKey"],
                        "task_type_ids": [(6, 0, task_types)]
                    })

                project_manager = self.env["res.users"].search(
                    [("name", "ilike", project_data["lead"]["displayName"])],
                    limit=1)

                project = self.env["project.project"].search(
                    [("key", "=", project_data["key"])], limit=1)

                picture_content = client._session.get(
                    project_data["avatarUrls"]["48x48"])

                new_data = {
                    "name": project_data["name"],
                    "key": project_data["key"],
                    "user_id": project_manager and project_manager.id or False,
                    "type_id": project_type.id,
                    # "image" : base64.b64encode(isinstance(picture_content.content, StringIO) and picture_content.content or picture_content.content.getvalue())
                }

                if project:
                    project.write(new_data)
                else:
                    self.env["project.project"].create(new_data)
Exemplo n.º 28
0
    def import_issue(self, request):

        args = list()
        kwargs = dict()

        if request.args:
            args = ast.literal_eval(request.args)

        if request.kwargs:
            kwargs = ast.literal_eval(request.kwargs)

        client = jira.JIRA(
            server=request.config_id.location,
            basic_auth=(
                request.config_id.username,
                request.config_id.password,
            ),
        )

        issue = client.issue(args[0])

        data = {
            "project_id": request.project_id.id,
            "name": issue.raw["fields"]["summary"],
            "description": issue.raw["fields"]["description"],
            "user_id": False,
            "create_uid": False,
        }

        key = issue.raw["key"]

        if issue.raw["fields"]["priority"]:
            task_priority = self.env["project.task.priority"].search([
                ("name", "ilike", issue.raw["fields"]["priority"]["name"])
            ])
            priority_id = task_priority and task_priority.id or False
            data["priority_id"] = priority_id

        if issue.raw["fields"]["assignee"]:
            name = issue.raw["fields"]["assignee"]["displayName"]
            assignee_id = self.env["res.users"].search([("name", "ilike", name)
                                                        ])
            data["user_id"] = assignee_id and assignee_id.id or False

        if issue.raw["fields"]["reporter"]:
            name = issue.raw["fields"]["reporter"]["displayName"]
            reporter_id = self.env["res.users"].search([("name", "ilike", name)
                                                        ])
            data["create_uid"] = reporter_id and reporter_id.id or False

        if issue.raw["fields"]["issuetype"]:
            type_id = kwargs[issue.raw["fields"]["issuetype"]["name"]]
            data["type_id"] = type_id

        task = self.env["project.task"].create(data)
        task.write({"key": key})
Exemplo n.º 29
0
    def getJira(self):
        '''
        jira client: https://pypi.org/project/jira/
        '''
        options = {'server': self.get("/jira/server", mandatory=True)}

        return jira.JIRA(options,
                         basic_auth=(self.get("/jira/username",
                                              mandatory=True),
                                     self.get("/jira/token", mandatory=True)))
Exemplo n.º 30
0
def _get_jira(server=None, username=None, password=None):
    global JIRA
    if not JIRA:
        server, username, password = _get_credentials(server=server,
                                                      username=username,
                                                      password=password)
        JIRA = jira.JIRA(basic_auth=(username, password),
                         server=server,
                         logging=True)  # We want logging
    return JIRA