1
0
Fork 0
mirror of https://gitlab.wikimedia.org/ladsgroup/Phabricator-maintenance-bot synced 2024-11-28 06:52:38 +01:00

patchforreview_remover.py: Handle bot comments for Gitlab status updates

Handle change status report for both Gerrit and Gitlab.

Bug: T325297
This commit is contained in:
Ahmon Dancy 2023-01-19 12:10:41 -08:00
parent 10c6aace45
commit ae3f603e50

View file

@ -19,6 +19,37 @@ class Checker():
phid = self.client.lookupPhid(t_id) phid = self.client.lookupPhid(t_id)
return self.phid_check(phid) return self.phid_check(phid)
def get_change_url(self, raw_comment):
m = re.search(r'https://gerrit(?:-test|)\.wikimedia\.org/r/\d+', raw_comment)
if m:
return m[0]
m = re.search(r'https://gitlab\.wikimedia\.org/repos/.*/-/merge_requests/\d+', raw_comment)
if m:
return m[0]
return None
def get_operation_type(self, raw_comment, url):
"""
If the operation type can be determined from raw_comment, return
it. It will be the string "open" (first patchset created or merge
request created) or "close" (merge or abandon)
"""
# Gitlab style
if re.search(r"opened " + re.escape(url), raw_comment):
return "open"
if re.search(r"(merged|closed) " + re.escape(url), raw_comment):
return "close"
# Gerrit style
if re.search(r"Change \d+ had a related patch set uploaded", raw_comment):
return "open"
if re.search(r'Change \d+ \*\*(?:merged|abandoned)\*\* by ', raw_comment):
return "close"
return None
def phid_check(self, phid) -> bool: def phid_check(self, phid) -> bool:
""" """
Returns true if the Patch-For-Review project should be removed from the Phabricator Returns true if the Patch-For-Review project should be removed from the Phabricator
@ -50,15 +81,14 @@ class Checker():
if len(case['comments']) != 1: if len(case['comments']) != 1:
return False return False
raw_comment = case['comments'][0]['content']['raw'] raw_comment = case['comments'][0]['content']['raw']
gerrit_patch_id = re.findall(
r'https://gerrit(?:-test|)\.wikimedia\.org/r/(\d+)(?:$|\]\])', raw_comment)[0] change_url = self.get_change_url(raw_comment)
merged = re.findall( if change_url:
r'Change \d+ \*\*(?:merged|abandoned)\*\* by ', op = self.get_operation_type(raw_comment, change_url)
raw_comment)
# Append True or False depending on whether the action was to # Append True or False depending on whether the action was to
# open a patch (True) or merge a patch (False) # open/reopen a change (True) or merge a change (False)
gerrit_patch_status[gerrit_patch_id].append(not(bool(merged))) gerrit_patch_status[change_url].append(op in ["open", "reopen"])
for patch in gerrit_patch_status: for patch in gerrit_patch_status:
# The normal sequence of GerritBot transactions for a Gerrit change is "Change # The normal sequence of GerritBot transactions for a Gerrit change is "Change
@ -67,11 +97,12 @@ class Checker():
# by whoever" (indicated by False in gerrit_patch_status). The transactions # by whoever" (indicated by False in gerrit_patch_status). The transactions
# are returned in reverse order so the opened/merged pattern will appear as # are returned in reverse order so the opened/merged pattern will appear as
# the reverse of [True, False], which is [False, True]. # the reverse of [True, False], which is [False, True].
# FIXME: This logic can't handle a open/close/reopen/merge situation.
if gerrit_patch_status[patch] != [False, True]: if gerrit_patch_status[patch] != [False, True]:
return False return False
return True return True
if __name__ == "__main__":
client = Client.newFromCreds() client = Client.newFromCreds()
gerrit_bot_phid = 'PHID-USER-idceizaw6elwiwm5xshb' gerrit_bot_phid = 'PHID-USER-idceizaw6elwiwm5xshb'