Skip to content
Permalink
Browse files
[webkitscmpy] Filter approvers by status
https://bugs.webkit.org/show_bug.cgi?id=231843
<rdar://problem/84320934>

Reviewed by Dewei Zhu.

If a project defines statuses for it's contributors, we only want
to allow reviewers to approve changes.

* Scripts/libraries/webkitscmpy/webkitscmpy/contributor.py:
(Contributor): Add reviewer status.
(Contributor.Mapping.load): Keep track of possible statuses.
(Contributor.Mapping.__init__): Ditto.
(Contributor.Mapping.add): Ditto.
* Scripts/libraries/webkitscmpy/webkitscmpy/remote/bitbucket.py:
(BitBucket.PRGenerator.PullRequest): If a project defines a reviewer status, enforce it.
* Scripts/libraries/webkitscmpy/webkitscmpy/remote/git_hub.py:
(GitHub.PRGenerator._contributor): Handle case where no name is defined.
(GitHub.PRGenerator.reviewers): If a project defines a reviewer status, enforce it.
* Scripts/libraries/webkitscmpy/webkitscmpy/test/pull_request_unittest.py:


Canonical link: https://commits.webkit.org/243238@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@284479 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
JonWBedard committed Oct 19, 2021
1 parent a13071d commit ba6e5c5910938ea1d51eb57551e1792580fcb41b
Showing 7 changed files with 69 additions and 8 deletions.
@@ -1,3 +1,26 @@
2021-10-19 Jonathan Bedard <jbedard@apple.com>

[webkitscmpy] Filter approvers by status
https://bugs.webkit.org/show_bug.cgi?id=231843
<rdar://problem/84320934>

Reviewed by Dewei Zhu.

If a project defines statuses for it's contributors, we only want
to allow reviewers to approve changes.

* Scripts/libraries/webkitscmpy/webkitscmpy/contributor.py:
(Contributor): Add reviewer status.
(Contributor.Mapping.load): Keep track of possible statuses.
(Contributor.Mapping.__init__): Ditto.
(Contributor.Mapping.add): Ditto.
* Scripts/libraries/webkitscmpy/webkitscmpy/remote/bitbucket.py:
(BitBucket.PRGenerator.PullRequest): If a project defines a reviewer status, enforce it.
* Scripts/libraries/webkitscmpy/webkitscmpy/remote/git_hub.py:
(GitHub.PRGenerator._contributor): Handle case where no name is defined.
(GitHub.PRGenerator.reviewers): If a project defines a reviewer status, enforce it.
* Scripts/libraries/webkitscmpy/webkitscmpy/test/pull_request_unittest.py:

2021-10-19 Alex Christensen <achristensen@webkit.org>

Fix iOS API tests after r284304
@@ -29,7 +29,7 @@ def readme():

setup(
name='webkitscmpy',
version='2.2.12',
version='2.2.13',
description='Library designed to interact with git and svn repositories.',
long_description=readme(),
classifiers=[
@@ -46,7 +46,7 @@ def _maybe_add_webkitcorepy_path():
"Please install webkitcorepy with `pip install webkitcorepy --extra-index-url <package index URL>`"
)

version = Version(2, 2, 12)
version = Version(2, 2, 13)

AutoInstall.register(Package('fasteners', Version(0, 15, 0)))
AutoInstall.register(Package('monotonic', Version(1, 5)))
@@ -35,6 +35,7 @@ class Contributor(object):
SVN_AUTHOR_RE = re.compile(r'r(?P<revision>\d+) \| (?P<email>.*) \| (?P<date>.*) \| \d+ lines?')
SVN_AUTHOR_Q_RE = re.compile(r'r(?P<revision>\d+) \| (?P<email>.*) \| (?P<date>.*)')
SVN_PATCH_FROM_RE = re.compile(r'Patch by (?P<author>.*) <(?P<email>.*)> on \d+-\d+-\d+')
REVIEWER = 'reviewer'

class Encoder(json.JSONEncoder):

@@ -68,6 +69,8 @@ def load(cls, file):
created.github = contributor.get('github', created.github)
created.bitbucket = contributor.get('bitbucket', created.bitbucket)

result.statuses.add(created.status)

if created.github:
result[created.github] = created
if created.bitbucket:
@@ -85,6 +88,7 @@ def load(cls, file):

def __init__(self):
super(Contributor.Mapping, self).__init__(lambda: None)
self.statuses = set()

def save(self, file):
alias_to_name = defaultdict(list)
@@ -111,6 +115,8 @@ def add(self, contributor):
result.github = contributor.github or result.github
result.bitbucket = contributor.bitbucket or result.bitbucket

self.statuses.add(result.status)

if result.github:
self[result.github] = result
if result.bitbucket:
@@ -25,10 +25,8 @@
import six
import sys

import json

from webkitcorepy import decorators
from webkitscmpy import Commit, PullRequest
from webkitscmpy import Commit, Contributor, PullRequest
from webkitscmpy.remote.scm import Scm


@@ -58,13 +56,14 @@ def PullRequest(self, data):
result._reviewers = []
result._approvers = []
result._blockers = []
needs_status = Contributor.REVIEWER in self.repository.contributors.statuses
for rdata in data.get('reviewers', []):
reviewer = self.repository.contributors.create(
rdata['user']['displayName'],
rdata['user'].get('emailAddress', None),
)
result._reviewers.append(reviewer)
if rdata.get('approved', False):
if rdata.get('approved', False) and (not needs_status or reviewer.status == Contributor.REVIEWER):
result._approvers.append(reviewer)
if rdata.get('status') == 'NEEDS_WORK':
result._blockers.append(reviewer)
@@ -162,7 +162,7 @@ def _contributor(self, username):
return Contributor(username)

data = response.json()
result = self.repository.contributors.create(data.get('name', username), data.get('email'))
result = self.repository.contributors.create(data.get('name', username) or username, data.get('email'))
result.github = username
self.repository.contributors[username] = result
return result
@@ -172,10 +172,11 @@ def reviewers(self, pull_request):
pull_request._reviewers = [self._contributor(user['login']) for user in response.get('users', [])]
pull_request._approvers = []
pull_request._blockers = []
needs_status = Contributor.REVIEWER in self.repository.contributors.statuses
for review in self.repository.request('pulls/{}/reviews'.format(pull_request.number)):
contributor = self._contributor(review['user']['login'])
pull_request._reviewers.append(contributor)
if review.get('state') == 'APPROVED':
if review.get('state') == 'APPROVED' and (not needs_status or contributor.status == Contributor.REVIEWER):
pull_request._approvers.append(contributor)
elif review.get('state') == 'CHANGES_REQUESTED':
pull_request._blockers.append(contributor)
@@ -493,6 +493,22 @@ def test_reviewers(self):
self.assertEqual(pr.approvers, [Contributor('Eager Reviewer', ['ereviewer@webkit.org'])])
self.assertEqual(pr.blockers, [Contributor('Suspicious Reviewer', ['sreviewer@webkit.org'])])

def test_approvers_status(self):
with self.webserver():
repo = remote.GitHub(self.remote)
repo.contributors.add(Contributor(
'Suspicious Reviewer', ['sreviewer@webkit.org'],
github='sreviewer', status=Contributor.REVIEWER,
))
pr = repo.pull_requests.get(1)
self.assertEqual(pr.reviewers, [
Contributor('Eager Reviewer', ['ereviewer@webkit.org']),
Contributor('Reluctant Reviewer', ['rreviewer@webkit.org']),
Contributor('Suspicious Reviewer', ['sreviewer@webkit.org']),
])
self.assertEqual(pr.approvers, [])
self.assertEqual(pr.blockers, [Contributor('Suspicious Reviewer', ['sreviewer@webkit.org'])])


class TestNetworkPullRequestBitBucket(unittest.TestCase):
remote = 'https://bitbucket.example.com/projects/WEBKIT/repos/webkit'
@@ -574,3 +590,19 @@ def test_reviewers(self):
])
self.assertEqual(pr.approvers, [Contributor('Eager Reviewer', ['ereviewer@webkit.org'])])
self.assertEqual(pr.blockers, [Contributor('Suspicious Reviewer', ['sreviewer@webkit.org'])])

def test_approvers_status(self):
with self.webserver():
repo = remote.BitBucket(self.remote)
repo.contributors.add(Contributor(
'Suspicious Reviewer', ['sreviewer@webkit.org'],
github='sreviewer', status=Contributor.REVIEWER,
))
pr = repo.pull_requests.get(1)
self.assertEqual(pr.reviewers, [
Contributor('Eager Reviewer', ['ereviewer@webkit.org']),
Contributor('Reluctant Reviewer', ['rreviewer@webkit.org']),
Contributor('Suspicious Reviewer', ['sreviewer@webkit.org']),
])
self.assertEqual(pr.approvers, [])
self.assertEqual(pr.blockers, [Contributor('Suspicious Reviewer', ['sreviewer@webkit.org'])])

0 comments on commit ba6e5c5

Please sign in to comment.