def validate_raw(self, attrs, source): if not attrs[source].strip(): return attrs try: rules = parse_rules(attrs[source]) except ParseError as e: raise serializers.ValidationError( u'Parse error: %r (line %d, column %d)' % ( e.expr.name, e.line(), e.column() )) schema = dump_schema(rules) owners = {o for rule in rules for o in rule.owners} actors = resolve_actors(owners, self.context['ownership'].project_id) bad_actors = [] for owner, actor in six.iteritems(actors): if actor is None: if owner.type == 'user': bad_actors.append(owner.identifier) elif owner.type == 'team': bad_actors.append(u'#{}'.format(owner.identifier)) if bad_actors: raise serializers.ValidationError( u'Invalid rule owners: {}'.format(", ".join(bad_actors)) ) attrs['schema'] = schema return attrs
def validate(self, attrs): if not attrs.get("raw", "").strip(): return attrs try: rules = parse_rules(attrs["raw"]) except ParseError as e: raise serializers.ValidationError({ "raw": "Parse error: %r (line %d, column %d)" % (e.expr.name, e.line(), e.column()) }) schema = dump_schema(rules) owners = {o for rule in rules for o in rule.owners} actors = resolve_actors(owners, self.context["ownership"].project_id) bad_actors = [] for owner, actor in actors.items(): if actor is None: if owner.type == "user": bad_actors.append(owner.identifier) elif owner.type == "team": bad_actors.append(f"#{owner.identifier}") if bad_actors: bad_actors.sort() raise serializers.ValidationError({ "raw": "Invalid rule owners: {}".format(", ".join(bad_actors)) }) attrs["schema"] = schema return attrs
def validate(self, attrs): if not attrs.get('raw', '').strip(): return attrs try: rules = parse_rules(attrs['raw']) except ParseError as e: raise serializers.ValidationError({ 'raw': u'Parse error: %r (line %d, column %d)' % (e.expr.name, e.line(), e.column()) }) schema = dump_schema(rules) owners = {o for rule in rules for o in rule.owners} actors = resolve_actors(owners, self.context['ownership'].project_id) bad_actors = [] for owner, actor in six.iteritems(actors): if actor is None: if owner.type == 'user': bad_actors.append(owner.identifier) elif owner.type == 'team': bad_actors.append(u'#{}'.format(owner.identifier)) if bad_actors: raise serializers.ValidationError({ 'raw': u'Invalid rule owners: {}'.format(", ".join(bad_actors)) }) attrs['schema'] = schema return attrs
def validate_raw(self, attrs, source): if not attrs[source].strip(): return attrs try: rules = parse_rules(attrs[source]) except ParseError as e: raise serializers.ValidationError( u'Parse error: %r (line %d, column %d)' % (e.expr.name, e.line(), e.column())) schema = dump_schema(rules) bad_actors = [] for rule in rules: for owner in rule.owners: try: resolve_actor(owner, self.context['ownership'].project_id) except UnknownActor: if owner.type == 'user': bad_actors.append(owner.identifier) if owner.type == 'team': bad_actors.append(u'#{}'.format(owner.identifier)) if bad_actors: raise serializers.ValidationError( u'Invalid rule owners: {}'.format(", ".join(bad_actors))) attrs['schema'] = schema return attrs
def test_parse_rules(): assert parse_rules(fixture_data) == [ Rule(Matcher("path", "*.js"), [Owner("team", "frontend"), Owner("user", "*****@*****.**")]), Rule(Matcher("url", "http://google.com/*"), [Owner("team", "backend")]), Rule(Matcher("path", "src/sentry/*"), [Owner("user", "*****@*****.**")]), Rule(Matcher("tags.foo", "bar"), [Owner("user", "*****@*****.**")]), Rule(Matcher("tags.foo", "bar baz"), [Owner("user", "*****@*****.**")]), ]
def test_parse_rules(): assert parse_rules(fixture_data) == [ Rule(Matcher('path', '*.js'), [Owner('team', 'frontend'), Owner('user', '*****@*****.**')]), Rule(Matcher('url', 'http://google.com/*'), [Owner('team', 'backend')]), Rule(Matcher('path', 'src/sentry/*'), [Owner('user', '*****@*****.**')]), ]
def validate_raw(self, attrs, source): if not attrs[source].strip(): return attrs try: rules = parse_rules(attrs[source]) except ParseError as e: raise serializers.ValidationError( u'Parse error: %r (line %d, column %d)' % ( e.expr.name, e.line(), e.column() )) attrs['schema'] = dump_schema(rules) return attrs
def test_parse_rules(): assert parse_rules(fixture_data) == [ Rule(Matcher("path", "*.js"), [Owner("team", "frontend"), Owner("user", "*****@*****.**")]), Rule(Matcher("url", "http://google.com/*"), [Owner("team", "backend")]), Rule(Matcher("path", "src/sentry/*"), [Owner("user", "*****@*****.**")]), Rule(Matcher("tags.foo", "bar"), [Owner("user", "*****@*****.**")]), Rule(Matcher("tags.foo", "bar baz"), [Owner("user", "*****@*****.**")]), Rule(Matcher("module", "foo.bar"), [Owner("team", "workflow")]), Rule(Matcher("module", "foo bar"), [Owner("user", "*****@*****.**")]), Rule(Matcher("codeowners", "/src/components/"), [Owner("user", "*****@*****.**")]), Rule(Matcher("codeowners", "frontend/*.ts"), [Owner("user", "*****@*****.**")]), ]
def save(self, *args, **kwargs): if self.raw is None: self.schema = None else: self.schema = dump_schema(parse_rules(self.raw)) return super(ProjectOwnership, self).save(*args, **kwargs)