Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: presubmit_canned_checks.py

Issue 6730020: Clean up the parsing of approvals for OWNERS checks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: rebase to head Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/presubmit_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Generic presubmit checks that can be reused by other presubmit checks.""" 5 """Generic presubmit checks that can be reused by other presubmit checks."""
6 6
7 import time 7 import time
8 8
9 9
10 ### Description checks 10 ### Description checks
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 out.append('%s has %d build(s) pending' % 622 out.append('%s has %d build(s) pending' %
623 (builder_name, pending_builds_len)) 623 (builder_name, pending_builds_len))
624 if out: 624 if out:
625 return [output_api.PresubmitPromptWarning( 625 return [output_api.PresubmitPromptWarning(
626 'Build(s) pending. It is suggested to wait that no more than %d ' 626 'Build(s) pending. It is suggested to wait that no more than %d '
627 'builds are pending.' % max_pendings, 627 'builds are pending.' % max_pendings,
628 long_text='\n'.join(out))] 628 long_text='\n'.join(out))]
629 return [] 629 return []
630 630
631 631
632 def CheckOwners(input_api, output_api, email_regexp=None, 632 def CheckOwners(input_api, output_api, source_file_filter=None):
633 source_file_filter=None): 633 if not input_api.is_committing:
634 return []
635 if input_api.tbr:
636 return [output_api.PresubmitNotifyResult('--tbr was specified, '
637 'skipping OWNERS check')]
638 if not input_api.change.issue:
639 return [output_api.PresubmitError('Change not uploaded for review')]
640
634 affected_files = set([f.LocalPath() for f in 641 affected_files = set([f.LocalPath() for f in
635 input_api.change.AffectedFiles(source_file_filter)]) 642 input_api.change.AffectedFiles(source_file_filter)])
643
636 owners_db = input_api.owners_db 644 owners_db = input_api.owners_db
637 if email_regexp: 645 owner_email, approvers = _RietveldOwnerAndApprovers(input_api,
638 owners_db.email_regexp = input_api.re.compile(email_regexp) 646 owners_db.email_regexp)
647 approvers_plus_owner = approvers.union(set([owner_email]))
639 648
640 if input_api.is_committing and input_api.tbr: 649 missing_files = owners_db.files_not_covered_by(affected_files,
641 return [output_api.PresubmitNotifyResult( 650 approvers_plus_owner)
642 '--tbr was specified, skipping OWNERS check')] 651 if missing_files:
643 elif input_api.is_committing: 652 return [output_api.PresubmitError('Missing LGTM from an OWNER for: %s' %
644 approvers = _Approvers(input_api, owners_db.email_regexp) 653 ','.join(missing_files))]
645 missing_files = owners_db.files_not_covered_by(affected_files, approvers)
646 if missing_files:
647 return [output_api.PresubmitError('Missing LGTM from an OWNER for: %s' %
648 ','.join(missing_files))]
649 return []
650 elif input_api.change.tags.get('R'):
651 return []
652 654
653 suggested_reviewers = owners_db.reviewers_for(affected_files) 655 if not approvers:
654 return [output_api.PresubmitAddReviewers(suggested_reviewers)] 656 return [output_api.PresubmitError('Missing LGTM from someone other than %s'
657 % owner_email)]
658 return []
655 659
656 660
657 def _Approvers(input_api, email_regexp): 661 def _RietveldOwnerAndApprovers(input_api, email_regexp):
658 if not input_api.change.issue: 662 """Return the owner and approvers of a change, if any."""
659 return []
660
661 # TODO(dpranke): Should figure out if input_api.host_url is supposed to 663 # TODO(dpranke): Should figure out if input_api.host_url is supposed to
662 # be a host or a scheme+host and normalize it there. 664 # be a host or a scheme+host and normalize it there.
663 host = input_api.host_url 665 host = input_api.host_url
664 if not host.startswith('http://') and not host.startswith('https://'): 666 if not host.startswith('http://') and not host.startswith('https://'):
665 host = 'http://' + host 667 host = 'http://' + host
666 url = '%s/api/%s?messages=true' % (host, input_api.change.issue) 668 url = '%s/api/%s?messages=true' % (host, input_api.change.issue)
667 669
668 f = input_api.urllib2.urlopen(url) 670 f = input_api.urllib2.urlopen(url)
669 issue_props = input_api.json.load(f) 671 issue_props = input_api.json.load(f)
670 owner = input_api.re.escape(issue_props['owner']) 672 messages = issue_props.get('messages', [])
673 owner_email = issue_props['owner_email']
674 owner_regexp = input_api.re.compile(input_api.re.escape(owner_email))
675 approvers = _GetApprovers(messages, email_regexp, owner_regexp)
676
677 return (owner_email, set(approvers))
678
679
680 def _IsApprovingComment(text):
681 """Implements the logic for parsing a change comment for approval."""
682
683 # Any comment that contains a non-quoted line containing an 'lgtm' is an
684 # approval.
685 #
686 # TODO(dpranke): this differs from the logic used inside Google in a few
687 # ways. Inside Google,
688 #
689 # 1) the approving phrase must appear at the beginning of the first non
690 # quoted-line in the comment.'
691 # 2) "LG", "Looks Good", and "Looks Good to Me" are also acceptable.
692 # 3) Subsequent comments from the reviewer can rescind approval, unless
693 # the phrase "LGTM++" was used.
694 # We should consider implementing some or all of this here.
695 for l in text.splitlines():
696 l = l.strip().lower()
697 if l.startswith('>'):
698 continue
699
700 if 'lgtm' in l:
701 return True
702 return False
703
704
705 def _GetApprovers(messages, email_regexp, owner_regexp):
706 """Returns the set of approvers for a change given the owner and messages.
707
708 Messages should be a list of dicts containing 'sender' and 'text' keys."""
671 709
672 # TODO(dpranke): This mimics the logic in 710 # TODO(dpranke): This mimics the logic in
673 # /tools/commit-queue/verifiers/reviewer_lgtm.py 711 # /tools/commit-queue/verifiers/reviewer_lgtm.py
674 # We should share the code and/or remove the check there where it is 712 # We should share the code and/or remove the check there where it is
675 # redundant (since the commit queue also enforces the presubmit checks). 713 # redundant (since the commit queue also enforces the presubmit checks).
676 def match_reviewer(r): 714 def match_reviewer(r):
677 return email_regexp.match(r) and not input_api.re.match(owner, r) 715 return email_regexp.match(r) and not owner_regexp.match(r)
678 716
679 approvers = [] 717 approvers = []
680 for m in issue_props.get('messages', []): 718 for m in messages:
681 if 'lgtm' in m['text'].lower() and match_reviewer(m['sender']): 719 sender = m['sender']
682 approvers.append(m['sender']) 720 if _IsApprovingComment(m['text']) and match_reviewer(sender):
721 approvers.append(sender)
683 return set(approvers) 722 return set(approvers)
684 723
685 724
686 def _CheckConstNSObject(input_api, output_api, source_file_filter): 725 def _CheckConstNSObject(input_api, output_api, source_file_filter):
687 """Checks to make sure no objective-c files have |const NSSomeClass*|.""" 726 """Checks to make sure no objective-c files have |const NSSomeClass*|."""
688 pattern = input_api.re.compile(r'const\s+NS\w*\s*\*') 727 pattern = input_api.re.compile(r'const\s+NS\w*\s*\*')
689 728
690 def objective_c_filter(f): 729 def objective_c_filter(f):
691 return (source_file_filter(f) and 730 return (source_file_filter(f) and
692 input_api.os_path.splitext(f.LocalPath())[1] in ('.h', '.mm')) 731 input_api.os_path.splitext(f.LocalPath())[1] in ('.h', '.mm'))
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 # This code loads the default black list (e.g. third_party, experimental, etc) 808 # This code loads the default black list (e.g. third_party, experimental, etc)
770 # and add our black list (breakpad, skia and v8 are still not following 809 # and add our black list (breakpad, skia and v8 are still not following
771 # google style and are not really living this repository). 810 # google style and are not really living this repository).
772 # See presubmit_support.py InputApi.FilterSourceFile for the (simple) usage. 811 # See presubmit_support.py InputApi.FilterSourceFile for the (simple) usage.
773 black_list = input_api.DEFAULT_BLACK_LIST + excluded_paths 812 black_list = input_api.DEFAULT_BLACK_LIST + excluded_paths
774 white_list = input_api.DEFAULT_WHITE_LIST + text_files 813 white_list = input_api.DEFAULT_WHITE_LIST + text_files
775 sources = lambda x: input_api.FilterSourceFile(x, black_list=black_list) 814 sources = lambda x: input_api.FilterSourceFile(x, black_list=black_list)
776 text_files = lambda x: input_api.FilterSourceFile(x, black_list=black_list, 815 text_files = lambda x: input_api.FilterSourceFile(x, black_list=black_list,
777 white_list=white_list) 816 white_list=white_list)
778 817
779 # TODO(dpranke): enable upload as well 818 results.extend(input_api.canned_checks.CheckOwners(
780 if input_api.is_committing: 819 input_api, output_api, source_file_filter=sources))
781 results.extend(input_api.canned_checks.CheckOwners(
782 input_api, output_api, source_file_filter=sources))
783 820
784 results.extend(input_api.canned_checks.CheckLongLines( 821 results.extend(input_api.canned_checks.CheckLongLines(
785 input_api, output_api, source_file_filter=sources)) 822 input_api, output_api, source_file_filter=sources))
786 results.extend(input_api.canned_checks.CheckChangeHasNoTabs( 823 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
787 input_api, output_api, source_file_filter=sources)) 824 input_api, output_api, source_file_filter=sources))
788 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( 825 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
789 input_api, output_api, source_file_filter=sources)) 826 input_api, output_api, source_file_filter=sources))
790 results.extend(input_api.canned_checks.CheckChangeSvnEolStyle( 827 results.extend(input_api.canned_checks.CheckChangeSvnEolStyle(
791 input_api, output_api, source_file_filter=text_files)) 828 input_api, output_api, source_file_filter=text_files))
792 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes( 829 results.extend(input_api.canned_checks.CheckSvnForCommonMimeTypes(
793 input_api, output_api)) 830 input_api, output_api))
794 results.extend(input_api.canned_checks.CheckLicense( 831 results.extend(input_api.canned_checks.CheckLicense(
795 input_api, output_api, license_header, source_file_filter=sources)) 832 input_api, output_api, license_header, source_file_filter=sources))
796 results.extend(_CheckConstNSObject( 833 results.extend(_CheckConstNSObject(
797 input_api, output_api, source_file_filter=sources)) 834 input_api, output_api, source_file_filter=sources))
798 results.extend(_CheckSingletonInHeaders( 835 results.extend(_CheckSingletonInHeaders(
799 input_api, output_api, source_file_filter=sources)) 836 input_api, output_api, source_file_filter=sources))
800 return results 837 return results
OLDNEW
« no previous file with comments | « no previous file | tests/presubmit_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698