Esempio n. 1
0
    def test_identity_matcher_instance_with_strict(self):
        """Test if the factory function adds the strict mode to the matcher instance"""

        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(matcher.strict, True)

        matcher = create_identity_matcher('default', strict=False)
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(matcher.strict, False)
Esempio n. 2
0
    def test_identity_matcher_instance_with_strict(self):
        """Test if the factory function adds the strict mode to the matcher instance"""

        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(matcher.strict, True)

        matcher = create_identity_matcher('default', strict=False)
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(matcher.strict, False)
Esempio n. 3
0
    def test_identity_matcher_instance(self):
        """Test if the factory function returns an identity matcher instance"""

        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)

        matcher = create_identity_matcher('email')
        self.assertIsInstance(matcher, EmailMatcher)

        matcher = create_identity_matcher('email-name')
        self.assertIsInstance(matcher, EmailNameMatcher)
Esempio n. 4
0
    def test_identity_matcher_instance(self):
        """Test if the factory function returns an identity matcher instance"""

        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)

        matcher = create_identity_matcher('email')
        self.assertIsInstance(matcher, EmailMatcher)

        matcher = create_identity_matcher('email-name')
        self.assertIsInstance(matcher, EmailNameMatcher)
Esempio n. 5
0
    def test_identity_matcher_instance_with_sources_list(self):
        """Test if the factory function adds a sources list to the matcher instance"""

        # The sources list is None
        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(matcher.sources, None)

        # Create a matcher with a sources list
        sources = ['git', 'jira', 'github']

        matcher = create_identity_matcher('default', sources=sources)
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(len(matcher.sources), 3)
Esempio n. 6
0
    def test_identity_matcher_instance_with_sources_list(self):
        """Test if the factory function adds a sources list to the matcher instance"""

        # The sources list is None
        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(matcher.sources, None)

        # Create a matcher with a sources list
        sources = ['git', 'jira', 'github']

        matcher = create_identity_matcher('default', sources=sources)
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(len(matcher.sources), 3)
Esempio n. 7
0
    def test_identity_matcher_instance_with_blacklist(self):
        """Test if the factory function adds a blacklist to the matcher instance"""

        # The blacklist is empty
        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(len(matcher.blacklist), 0)

        # Create a matcher with a blacklist
        blacklist = [MatchingBlacklist(excluded='*****@*****.**'),
                     MatchingBlacklist(excluded='*****@*****.**'),
                     MatchingBlacklist(excluded='*****@*****.**'),
                     MatchingBlacklist(excluded='John Smith'),
                     MatchingBlacklist(excluded='root')]

        matcher = create_identity_matcher('default', blacklist=blacklist)
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(len(matcher.blacklist), 5)
Esempio n. 8
0
    def test_identity_matcher_instance_with_blacklist(self):
        """Test if the factory function adds a blacklist to the matcher instance"""

        # The blacklist is empty
        matcher = create_identity_matcher('default')
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(len(matcher.blacklist), 0)

        # Create a matcher with a blacklist
        blacklist = [MatchingBlacklist(excluded='*****@*****.**'),
                     MatchingBlacklist(excluded='*****@*****.**'),
                     MatchingBlacklist(excluded='*****@*****.**'),
                     MatchingBlacklist(excluded='John Smith'),
                     MatchingBlacklist(excluded='root')]

        matcher = create_identity_matcher('default', blacklist=blacklist)
        self.assertIsInstance(matcher, IdentityMatcher)
        self.assertEqual(len(matcher.blacklist), 5)
Esempio n. 9
0
    def add_identities(cls, db, identities, backend):
        """ Load identities list from backend in Sorting Hat """

        merge_identities = False

        logger.info("Adding the identities to SortingHat")
        if not merge_identities:
            logger.info("Not doing identities merge")

        total = 0
        lidentities = len(identities)

        if merge_identities:
            merged_identities = []  # old identities merged into new ones
            blacklist = api.blacklist(db)
            matching = 'email-name'  # Not active
            matcher = create_identity_matcher(matching, blacklist)

        for identity in identities:
            try:
                uuid = api.add_identity(db, backend, identity['email'],
                                        identity['name'], identity['username'])

                logger.debug("New sortinghat identity %s %s,%s,%s (%i/%i)" % \
                            (uuid, identity['username'], identity['name'], identity['email'],
                            total, lidentities))

                total += 1
                if not merge_identities:
                    continue  # Don't do the merge here. Too slow in large projects

                # Time to  merge
                matches = api.match_identities(db, uuid, matcher)

                if len(matches) > 1:
                    u = api.unique_identities(db, uuid)[0]
                    for m in matches:
                        # First add the old uuid to the list of changed by merge uuids
                        if m.uuid not in merged_identities:
                            merged_identities.append(m.uuid)
                        if m.uuid == uuid:
                            continue
                        # Merge matched identity into added identity
                        api.merge_unique_identities(db, m.uuid, u.uuid)
                        # uuid = m.uuid
                        # u = api.unique_identities(db, uuid, backend)[0]
                        # Include all identities related to this uuid
                        # merged_identities.append(m.uuid)

            except AlreadyExistsError as ex:
                uuid = ex.uuid
                continue
            except WrappedValueError as ex:
                logging.warning("Trying to add a None identity. Ignoring it.")
                continue
            except UnicodeEncodeError as ex:
                logging.warning("UnicodeEncodeError. Ignoring it. %s %s %s" % \
                                (identity['email'], identity['name'],
                                identity['username']))
                continue
            except Exception as ex:
                logging.warning("Unknown exception adding identity. Ignoring it. %s %s %s" % \
                                (identity['email'], identity['name'],
                                identity['username']))
                continue

            if 'company' in identity and identity['company'] is not None:
                try:
                    api.add_organization(db, identity['company'])
                    api.add_enrollment(db, uuid, identity['company'],
                                       datetime(1900, 1, 1),
                                       datetime(2100, 1, 1))
                except AlreadyExistsError:
                    pass

        logger.info("Total NEW identities: %i" % (total))

        if merge_identities:
            logger.info("Total NEW identities merged: %i" % \
                        (len(merged_identities)))
            return merged_identities
        else:
            return []
Esempio n. 10
0
    def add_identities(cls, db, identities, backend):
        """ Load identities list from backend in Sorting Hat """

        merge_identities = False

        logger.info("Adding the identities to SortingHat")
        if not merge_identities:
            logger.info("Not doing identities merge")

        total = 0
        lidentities = len(identities)


        if merge_identities:
            merged_identities = []  # old identities merged into new ones
            blacklist = api.blacklist(db)
            matching = 'email-name'  # Not active
            matcher = create_identity_matcher(matching, blacklist)

        for identity in identities:
            try:
                uuid = api.add_identity(db, backend, identity['email'],
                                        identity['name'], identity['username'])

                logger.debug("New sortinghat identity %s %s,%s,%s (%i/%i)",
                            uuid, identity['username'], identity['name'], identity['email'],
                            total, lidentities)

                profile = {"name": identity['name'] if identity['name'] else identity['username'],
                           "email": identity['email']}

                api.edit_profile(db, uuid, **profile)

                total += 1
                if not merge_identities:
                    continue  # Don't do the merge here. Too slow in large projects

                # Time to  merge
                matches = api.match_identities(db, uuid, matcher)

                if len(matches) > 1:
                    u = api.unique_identities(db, uuid)[0]
                    for m in matches:
                        # First add the old uuid to the list of changed by merge uuids
                        if m.uuid not in merged_identities:
                            merged_identities.append(m.uuid)
                        if m.uuid == uuid:
                            continue
                        # Merge matched identity into added identity
                        api.merge_unique_identities(db, m.uuid, u.uuid)
                        # uuid = m.uuid
                        # u = api.unique_identities(db, uuid, backend)[0]
                        # Include all identities related to this uuid
                        # merged_identities.append(m.uuid)

            except AlreadyExistsError as ex:
                uuid = ex.uuid
                continue
            except WrappedValueError as ex:
                logging.warning("Trying to add a None identity. Ignoring it.")
                continue
            except UnicodeEncodeError as ex:
                logging.warning("UnicodeEncodeError. Ignoring it. %s %s %s" % \
                                (identity['email'], identity['name'],
                                identity['username']))
                continue
            except Exception as ex:
                logging.warning("Unknown exception adding identity. Ignoring it. %s %s %s" % \
                                (identity['email'], identity['name'],
                                identity['username']))
                traceback.print_exc()
                continue

            if 'company' in identity and identity['company'] is not None:
                try:
                    api.add_organization(db, identity['company'])
                    api.add_enrollment(db, uuid, identity['company'],
                                       datetime(1900, 1, 1),
                                       datetime(2100, 1, 1))
                except AlreadyExistsError:
                    pass

        logger.info("Total NEW identities: %i" % (total))

        if merge_identities:
            logger.info("Total NEW identities merged: %i" % \
                        (len(merged_identities)))
            return merged_identities
        else:
            return []