def _create_config(self, job_name='job_1', with_local_site=False): """Create an integration config. Args: job_name (string, optional): The Jenkins job name, which is used in constructing the URL to start a build on Jenkins. """ choice = ReviewRequestRepositoriesChoice() condition_set = ConditionSet(conditions=[ Condition(choice=choice, operator=choice.get_operator('any')) ]) if with_local_site: local_site = self.get_local_site(name=self.local_site_name) else: local_site = None config = self.integration.create_config(name='Config 1', enabled=True, local_site=local_site) config.set('conditions', condition_set.serialize()) config.set('jenkins_job_name', job_name) config.set('jenkins_endpoint', 'http://localhost:8000') config.set('jenkins_username', 'admin') config.set('jenkins_password', 'admin') config.save() return config
def _create_config(self, with_local_site=False): """Create an integration config. Args: with_local_site (bool): Whether to limit the config to a local site. """ choice = ReviewRequestRepositoriesChoice() condition_set = ConditionSet(conditions=[ Condition(choice=choice, operator=choice.get_operator('any')) ]) if with_local_site: local_site = self.get_local_site(name=self.local_site_name) else: local_site = None config = self.integration.create_config(name='Config 1', enabled=True, local_site=local_site) config.set('conditions', condition_set.serialize()) config.set('branch_name', 'review-requests') config.save() return config
def test_matches_with_none_op(self): """Testing RepositoriesChoice.matches with "none" operator""" condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('none')), ]) self.assertTrue(condition_set.matches(repository=None)) self.assertFalse(condition_set.matches( repository=self.create_repository()))
def test_matches_with_is_private_op(self): """Testing RepositoriesChoice.matches with "is-private" operator""" condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('is-private')), ]) self.assertTrue(condition_set.matches( repository=self.create_repository(name='repo1', public=False))) self.assertFalse(condition_set.matches( repository=self.create_repository(name='repo2', public=True)))
def test_matches_with_any_op(self): """Testing ReviewRequestRepositoriesChoice.matches with "any" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('any')), ]) self.assertTrue(condition_set.matches( review_request=self.create_review_request(create_repository=True))) self.assertFalse(condition_set.matches( review_request=self.create_review_request()))
def test_matches_with_is_op(self): """Testing ReviewRequestAllDiffFilesChoice.matches with "is" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('is'), 'file1'), ]) self.assertFalse( condition_set.matches(review_request=self.review_request)) self.filediff2.delete() self.assertTrue( condition_set.matches(review_request=self.review_request))
def test_matches_with_none_op(self): """Testing ReviewGroupsChoice.matches with "none" operator""" self.create_review_group(name='group1') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('none')), ]) self.assertTrue(condition_set.matches( review_groups=Group.objects.none())) self.assertFalse(condition_set.matches( review_groups=Group.objects.all()))
def test_matches_with_is_op(self): """Testing ReviewRequestAllDiffFilesChoice.matches with "is" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('is'), 'file1'), ]) self.assertFalse(condition_set.matches( review_request=self.review_request)) self.filediff2.delete() self.assertTrue(condition_set.matches( review_request=self.review_request))
def test_matches_with_none_op(self): """Testing ReviewGroupsChoice.matches with "none" operator""" self.create_review_group(name='group1') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('none')), ]) self.assertTrue( condition_set.matches(review_groups=Group.objects.none())) self.assertFalse( condition_set.matches(review_groups=Group.objects.all()))
def test_matches_with_none_op(self): """Testing ReviewRequestReviewGroupsChoice.matches with "none" operator """ group = self.create_review_group(name='group1') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('none')), ]) review_request = self.create_review_request() self.assertTrue(condition_set.matches(review_request=review_request)) review_request.target_groups = [group] self.assertFalse(condition_set.matches(review_request=review_request))
def test_matches_with_not_one_of_op(self): """Testing RepositoriesChoice.matches with "not-one-of" operator""" repository1 = self.create_repository(name='repo1') repository2 = self.create_repository(name='repo2') repository3 = self.create_repository(name='repo3') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('not-one-of'), [repository1, repository2]) ]) self.assertFalse(condition_set.matches(repository=repository1)) self.assertFalse(condition_set.matches(repository=repository2)) self.assertTrue(condition_set.matches(repository=repository3))
def test_matches_with_any_public_op(self): """Testing ReviewGroupsChoice.matches with "any-public" operator""" group1 = self.create_review_group(name='group1', invite_only=False) group2 = self.create_review_group(name='group2', invite_only=True) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('any-public')), ]) self.assertTrue(condition_set.matches( review_groups=Group.objects.filter(pk=group1.pk))) self.assertFalse(condition_set.matches( review_groups=Group.objects.filter(pk=group2.pk))) self.assertFalse(condition_set.matches( review_groups=Group.objects.none()))
def test_matches_with_not_one_of_op(self): """Testing RepositoryTypeChoice.matches with "not-one-of" operator""" repository1 = self.create_repository(name='repo1', tool_name='Git') repository2 = self.create_repository(name='repo2', tool_name='Subversion') repository3 = self.create_repository(name='repo3', tool_name='CVS') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('not-one-of'), [repository1.tool, repository2.tool]) ]) self.assertFalse(condition_set.matches(repository=repository1)) self.assertFalse(condition_set.matches(repository=repository2)) self.assertTrue(condition_set.matches(repository=repository3))
def test_matches_with_contains_any_op(self): """Testing ReviewRequestParticipantChoice.matches with "contains-any" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains-any'), [self.user1, self.user2]), ]) review_request = self.create_review_request() self.assertFalse(condition_set.matches(review_request=review_request)) review_request = self.create_review_request() self.create_review(review_request, user=self.user1, public=True) self.assertTrue(condition_set.matches(review_request=review_request))
def test_matches_with_not_one_of_op(self): """Testing ReviewRequestSubmitterChoice.matches with "not-one-of" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('not-one-of'), [self.user1, self.user2]), ]) self.assertFalse(condition_set.matches( review_request=self.create_review_request(submitter=self.user1))) self.assertFalse(condition_set.matches( review_request=self.create_review_request(submitter=self.user2))) self.assertTrue(condition_set.matches( review_request=self.create_review_request(submitter=self.user3)))
def test_matches_with_not_one_of_op(self): """Testing ReviewRequestOwnerChoice.matches with "not-one-of" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('not-one-of'), [self.user1, self.user2]), ]) self.assertFalse(condition_set.matches( review_request=self.create_review_request(submitter=self.user1))) self.assertFalse(condition_set.matches( review_request=self.create_review_request(submitter=self.user2))) self.assertTrue(condition_set.matches( review_request=self.create_review_request(submitter=self.user3)))
def test_matches_with_matches_regex_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "matches-regex" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('matches-regex'), re.compile(r'^[Ff]ile1$')), ]) self.assertTrue(condition_set.matches( review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('matches-regex'), re.compile('^\d')), ]) self.assertFalse(condition_set.matches( review_request=self.review_request))
def test_matches_with_ends_with_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "ends-with" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('ends-with'), 'le1'), ]) self.assertTrue(condition_set.matches( review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('ends-with'), 'xyz'), ]) self.assertFalse(condition_set.matches( review_request=self.review_request))
def test_matches_with_starts_with_op(self): """Testing ReviewRequestAllDiffFilesChoice.matches with "starts-with" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('starts-with'), 'file'), ]) self.assertTrue(condition_set.matches( review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('starts-with'), 'file1'), ]) self.assertFalse(condition_set.matches( review_request=self.review_request))
def test_matches_with_contains_any_op(self): """Testing ReviewGroupsChoice.matches with "contains-any" operator""" group1 = self.create_review_group(name='group1') group2 = self.create_review_group(name='group2') group3 = self.create_review_group(name='group3') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains-any'), [group1, group2]) ]) self.assertTrue(condition_set.matches( review_groups=Group.objects.filter(pk=group1.pk))) self.assertFalse(condition_set.matches( review_groups=Group.objects.filter(pk=group3.pk))) self.assertFalse(condition_set.matches( review_groups=Group.objects.none()))
def test_matches_with_contains_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "contains" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains'), '1'), ]) self.assertTrue(condition_set.matches( review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains'), '3'), ]) self.assertFalse(condition_set.matches( review_request=self.review_request))
def test_matches_with_contains_any_op(self): """Testing ReviewRequestReviewerChoice.matches with "contains-any" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains-any'), [self.user1, self.user2]), ]) review_request = self.create_review_request(target_people=[self.user1]) self.assertTrue(condition_set.matches(review_request=review_request)) review_request = self.create_review_request(target_people=[self.user2]) self.assertTrue(condition_set.matches(review_request=review_request)) review_request = self.create_review_request(target_people=[self.user3]) self.assertFalse(condition_set.matches(review_request=review_request))
def test_matches_with_does_not_contain_any_op(self): """Testing ReviewRequestReviewerChoice.matches with "does-not-contain-any" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain-any'), [self.user1, self.user2]), ]) review_request = self.create_review_request(target_people=[self.user1]) self.assertFalse(condition_set.matches(review_request=review_request)) review_request = self.create_review_request(target_people=[self.user2]) self.assertFalse(condition_set.matches(review_request=review_request)) review_request = self.create_review_request(target_people=[self.user3]) self.assertTrue(condition_set.matches(review_request=review_request))
def test_matches_with_does_not_contain_any_op(self): """Testing ReviewRequestParticipantChoice.matches with "does-not-contain-any" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain-any'), [self.user1, self.user2]), ]) review_request = self.create_review_request() self.assertTrue(condition_set.matches(review_request=review_request)) review_request = self.create_review_request() self.create_review(review_request, user=self.user1, public=True) self.assertFalse(condition_set.matches(review_request=review_request))
def test_matches_with_any_public_op(self): """Testing ReviewRequestReviewGroupsChoice.matches with "any-public" operator""" group1 = self.create_review_group(name='group1', invite_only=False) group2 = self.create_review_group(name='group2', invite_only=True) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('any-public')), ]) review_request = self.create_review_request() review_request.target_groups = [group1] self.assertTrue(condition_set.matches(review_request=review_request)) review_request.target_groups = [group2] self.assertFalse(condition_set.matches(review_request=review_request)) review_request.target_groups = [] self.assertFalse(condition_set.matches(review_request=review_request))
def test_matches_with_does_not_match_regex_op(self): """Testing ReviewRequestAllDiffFilesChoice.matches with "does-not-match-regex" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-match-regex'), re.compile(r'^[Ff]ile3$')), ]) self.assertTrue(condition_set.matches( review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-match-regex'), re.compile('[Ff]ile1')), ]) self.assertFalse(condition_set.matches( review_request=self.review_request))
def test_matches_with_does_not_contain_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "does-not-contain" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain'), 'xyz'), ]) self.assertTrue(condition_set.matches( review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain'), 'file'), ]) self.assertFalse(condition_set.matches( review_request=self.review_request))
def _create_config(self, enterprise=False, with_local_site=False): """Create an integration config. Args: enterprise (bool, optional): Whether to use an enterprise endpoint or the default open-source endpoint. with_local_site (bool, optional): Whether to limit the config to a local site. """ choice = ReviewRequestRepositoriesChoice() condition_set = ConditionSet(conditions=[ Condition(choice=choice, operator=choice.get_operator('any')) ]) if with_local_site: local_site = self.get_local_site(name=self.local_site_name) else: local_site = None config = self.integration.create_config(name='Config 1', enabled=True, local_site=local_site) config.set('conditions', condition_set.serialize()) config.set('travis_yml', 'script:\n python ./tests/runtests.py') config.set('branch_name', 'review-requests') if enterprise: config.set('travis_endpoint', TravisAPI.ENTERPRISE_ENDPOINT) config.set('travis_custom_endpoint', 'https://travis.example.com/') else: config.set('travis_endpoint', TravisAPI.OPEN_SOURCE_ENDPOINT) config.save() return config
def test_matches_with_does_not_contain_any_op(self): """Testing ReviewRequestReviewGroupsChoice.matches with "does-not-contain-any" operator """ group1 = self.create_review_group(name='group1') group2 = self.create_review_group(name='group2') group3 = self.create_review_group(name='group3') condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain-any'), [group1, group2]) ]) review_request = self.create_review_request() self.assertTrue(condition_set.matches(review_request=review_request)) review_request.target_groups = [group3] self.assertTrue(condition_set.matches(review_request=review_request)) review_request.target_groups = [group1] self.assertFalse(condition_set.matches(review_request=review_request))
def load_conditions(self, form_cls, conditions_key='conditions'): """Load a set of conditions from the configuration. This loads and deserializes a set of conditions from the configuration stored in the provided key. Those conditions can then be matched by the caller. If the conditions are not found, this will return ``None``. If the conditions cannot be deserialized, this will log some diagnostic output and error messages and return ``None``. Args: form_cls (type): The configuration form class that owns the condition field. This will generally be ``my_integration.form_cls``, but can be another form in more complex integrations. conditions_key (unicode, optional): The key for the conditions data in the configuration. Defaults to "conditions". Returns: djblets.conditions.conditions.ConditionSet: The condition set based on the data, if found and if it could be loaded. Otherwise, ``None`` will be returned. """ conditions_data = self.get(conditions_key) if not conditions_data: return None try: return ConditionSet.deserialize( form_cls.base_fields[conditions_key].choices, conditions_data, choice_kwargs={ 'local_site': self.local_site, 'matching': True, }) except: logging.exception( 'Unable to load bad condition set data for ' 'integration configuration ID=%s for key="%s"', self.pk, conditions_key) logging.debug('Bad conditions data = %r', conditions_data) return None
def load_conditions(self, form_cls, conditions_key='conditions'): """Load a set of conditions from the configuration. This loads and deserializes a set of conditions from the configuration stored in the provided key. Those conditions can then be matched by the caller. If the conditions are not found, this will return ``None``. If the conditions cannot be deserialized, this will log some diagnostic output and error messages and return ``None``. Args: form_cls (type): The configuration form class that owns the condition field. This will generally be ``my_integration.form_cls``, but can be another form in more complex integrations. conditions_key (unicode, optional): The key for the conditions data in the configuration. Defaults to "conditions". Returns: djblets.conditions.conditions.ConditionSet: The condition set based on the data, if found and if it could be loaded. Otherwise, ``None`` will be returned. """ conditions_data = self.get(conditions_key) if not conditions_data: return None try: return ConditionSet.deserialize( form_cls.base_fields[conditions_key].choices, conditions_data, choice_kwargs={ 'local_site': self.local_site, }) except: logging.exception('Unable to load bad condition set data for ' 'integration configuration ID=%s for key="%s"', self.pk, conditions_key) logging.debug('Bad conditions data = %r', conditions_data) return None
def test_matches_with_contains_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "contains" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains'), '1'), ]) self.assertTrue( condition_set.matches(review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('contains'), '3'), ]) self.assertFalse( condition_set.matches(review_request=self.review_request))
def test_matches_with_starts_with_op(self): """Testing ReviewRequestAllDiffFilesChoice.matches with "starts-with" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('starts-with'), 'file'), ]) self.assertTrue( condition_set.matches(review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('starts-with'), 'file1'), ]) self.assertFalse( condition_set.matches(review_request=self.review_request))
def test_matches_with_matches_regex_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "matches-regex" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('matches-regex'), re.compile(r'^[Ff]ile1$')), ]) self.assertTrue( condition_set.matches(review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('matches-regex'), re.compile('^\d')), ]) self.assertFalse( condition_set.matches(review_request=self.review_request))
def test_matches_with_ends_with_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "ends-with" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('ends-with'), 'le1'), ]) self.assertTrue( condition_set.matches(review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('ends-with'), 'xyz'), ]) self.assertFalse( condition_set.matches(review_request=self.review_request))
def test_matches_with_does_not_contain_op(self): """Testing ReviewRequestAnyDiffFileChoice.matches with "does-not-contain" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain'), 'xyz'), ]) self.assertTrue( condition_set.matches(review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-contain'), 'file'), ]) self.assertFalse( condition_set.matches(review_request=self.review_request))
def test_matches_with_does_not_match_regex_op(self): """Testing ReviewRequestAllDiffFilesChoice.matches with "does-not-match-regex" operator """ condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-match-regex'), re.compile(r'^[Ff]ile3$')), ]) self.assertTrue( condition_set.matches(review_request=self.review_request)) condition_set = ConditionSet(ConditionSet.MODE_ALL, [ Condition(self.choice, self.choice.get_operator('does-not-match-regex'), re.compile('[Ff]ile1')), ]) self.assertFalse( condition_set.matches(review_request=self.review_request))