Commit 46c9d5ef authored by kyle Ju's avatar kyle Ju Committed by Commit Bot

Add an edge case to PR clenaup tool.

Address the edge case where when a CL no longer has exportable changes, the PR should
be closed and the branch should be deleted.


Bug: 852014, 750942
Change-Id: I86adf85a3dcec7a9fac088fa42e2e357bbf1285a
Reviewed-on: https://chromium-review.googlesource.com/c/1349471
Commit-Queue: Robert Ma <robertma@chromium.org>
Reviewed-by: default avatarRobert Ma <robertma@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611164}
parent 163dcc7f
...@@ -53,11 +53,17 @@ class PrCleanupTool(object): ...@@ -53,11 +53,17 @@ class PrCleanupTool(object):
change_id = self.wpt_github.extract_metadata('Change-Id: ', pull_request.body) change_id = self.wpt_github.extract_metadata('Change-Id: ', pull_request.body)
if not change_id: if not change_id:
continue continue
cl_status = self.gerrit.query_cl(change_id).status cl = self.gerrit.query_cl(change_id)
cl_status = cl.status
if cl_status == 'ABANDONED': if cl_status == 'ABANDONED':
_log.info('https://github.com/web-platform-tests/wpt/pull/%s', pull_request.number) comment = 'Close this PR because the Chromium CL has been abandoned.'
_log.info(self.wpt_github.extract_metadata('Reviewed-on: ', pull_request.body)) self.log_affected_pr_details(pull_request, comment)
self.close_abandoned_pr(pull_request) self.close_pr_and_delete_branch(pull_request.number, comment)
elif cl_status == 'MERGED' and (not cl.is_exportable()):
comment = 'Close this PR because the Chromium CL does not have exportable changes.'
self.log_affected_pr_details(pull_request, comment)
self.close_pr_and_delete_branch(pull_request.number, comment)
return True return True
def parse_args(self, argv): def parse_args(self, argv):
...@@ -73,11 +79,18 @@ class PrCleanupTool(object): ...@@ -73,11 +79,18 @@ class PrCleanupTool(object):
return parser.parse_args(argv) return parser.parse_args(argv)
def retrieve_all_prs(self): def retrieve_all_prs(self):
"""Retrieve last 1000 PRs.""" """Retrieves last 1000 PRs."""
return self.wpt_github.all_pull_requests() return self.wpt_github.all_pull_requests()
def close_abandoned_pr(self, pull_request): def close_pr_and_delete_branch(self, pull_request_number, comment):
"""Closes a PR if the original CL is abandoned.""" """Closes a PR with a comment and delete the corresponding branch."""
comment = 'Close this PR because the Chromium CL has been abandoned.' self.wpt_github.add_comment(pull_request_number, comment)
self.wpt_github.add_comment(pull_request.number, comment) self.wpt_github.update_pr(pull_request_number, state='closed')
self.wpt_github.update_pr(pull_request.number, state='closed') branch = self.wpt_github.get_pr_branch(pull_request_number)
self.wpt_github.delete_remote_branch(branch)
def log_affected_pr_details(self, pull_request, comment):
"""Logs details of an affected PR."""
_log.info(comment)
_log.info('https://github.com/web-platform-tests/wpt/pull/%s', pull_request.number)
_log.info(self.wpt_github.extract_metadata('Reviewed-on: ', pull_request.body))
...@@ -3,6 +3,7 @@ import json ...@@ -3,6 +3,7 @@ import json
from blinkpy.common.host_mock import MockHost from blinkpy.common.host_mock import MockHost
from blinkpy.common.system.log_testing import LoggingTestCase from blinkpy.common.system.log_testing import LoggingTestCase
from blinkpy.common.path_finder import RELATIVE_WEB_TESTS
from blinkpy.w3c.gerrit_mock import MockGerritAPI, MockGerritCL from blinkpy.w3c.gerrit_mock import MockGerritAPI, MockGerritCL
from blinkpy.w3c.pr_cleanup_tool import PrCleanupTool from blinkpy.w3c.pr_cleanup_tool import PrCleanupTool
from blinkpy.w3c.wpt_github import PullRequest from blinkpy.w3c.wpt_github import PullRequest
...@@ -24,7 +25,7 @@ class PrCleanupToolTest(LoggingTestCase): ...@@ -24,7 +25,7 @@ class PrCleanupToolTest(LoggingTestCase):
})) }))
self.host = host self.host = host
def test_main_successful_delete(self): def test_main_successful_close_abandoned_cl(self):
pr_cleanup = PrCleanupTool(self.host) pr_cleanup = PrCleanupTool(self.host)
pr_cleanup.wpt_github = MockWPTGitHub(pull_requests=[ pr_cleanup.wpt_github = MockWPTGitHub(pull_requests=[
PullRequest(title='title1', number=1234, body='Change-Id: 88', state='open', labels=[]), PullRequest(title='title1', number=1234, body='Change-Id: 88', state='open', labels=[]),
...@@ -48,4 +49,36 @@ class PrCleanupToolTest(LoggingTestCase): ...@@ -48,4 +49,36 @@ class PrCleanupToolTest(LoggingTestCase):
self.assertEqual(pr_cleanup.gerrit.cls_queried, ['88']) self.assertEqual(pr_cleanup.gerrit.cls_queried, ['88'])
self.assertEqual(pr_cleanup.wpt_github.calls, [ self.assertEqual(pr_cleanup.wpt_github.calls, [
'all_pull_requests', 'all_pull_requests',
'add_comment "Close this PR because the Chromium CL has been abandoned."', 'update_pr']) 'add_comment "Close this PR because the Chromium CL has been abandoned."',
'update_pr', 'get_pr_branch', 'delete_remote_branch'])
def test_main_successful_close_no_exportable_changes(self):
pr_cleanup = PrCleanupTool(self.host)
pr_cleanup.wpt_github = MockWPTGitHub(pull_requests=[
PullRequest(title='title1', number=1234, body='Change-Id: 99', state='open', labels=[]),
])
pr_cleanup.gerrit = MockGerritAPI()
pr_cleanup.gerrit.cl = MockGerritCL(
data={
'change_id': 'I001',
'subject': 'subject',
'_number': 1234,
'status': 'MERGED',
'current_revision': '1',
'has_review_started': True,
'revisions': {
'1': {'commit_with_footers': 'a commit with footers',
'files': {
RELATIVE_WEB_TESTS + 'foo/bar.html': '',
}}
},
'owner': {'email': 'test@chromium.org'},
},
api=pr_cleanup.gerrit)
pr_cleanup.main(['--credentials-json', '/tmp/credentials.json'])
self.assertEqual(pr_cleanup.gerrit.cls_queried, ['99'])
self.assertEqual(pr_cleanup.wpt_github.calls, [
'all_pull_requests',
'add_comment "Close this PR because the Chromium'
' CL does not have exportable changes."',
'update_pr', 'get_pr_branch', 'delete_remote_branch'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment