Jeremy White : Refactor to create a function that handles MRs.
Alexandre Julliard
julliard at winehq.org
Thu Apr 28 10:56:35 CDT 2022
Module: tools
Branch: master
Commit: 538a9f53d13c1ad1f503a71f9490c4a9d0e4d33d
URL: https://source.winehq.org/git/tools.git/?a=commit;h=538a9f53d13c1ad1f503a71f9490c4a9d0e4d33d
Author: Jeremy White <jwhite at codeweavers.com>
Date: Thu Apr 28 07:15:36 2022 -0500
Refactor to create a function that handles MRs.
Signed-off-by: Alexandre Julliard <julliard at winehq.org>
---
gitlab/gitlab-to-mail/gitlabtomail.py | 131 ++++++++++++++++++----------------
1 file changed, 69 insertions(+), 62 deletions(-)
diff --git a/gitlab/gitlab-to-mail/gitlabtomail.py b/gitlab/gitlab-to-mail/gitlabtomail.py
index 19eafa3..c46cc98 100755
--- a/gitlab/gitlab-to-mail/gitlabtomail.py
+++ b/gitlab/gitlab-to-mail/gitlabtomail.py
@@ -536,7 +536,75 @@ def create_cover(mr_id, mr_iid, mr_version, versions, nr_patches, author_name, t
return mail
+def process_mr(mr, update_db):
+ iid = mr['iid']
+ log(f"MR{iid} updated - processing")
+
+ if mr['merged_at']:
+ log(f"MR{iid} merged - skipping")
+ return
+
+ # We jump through some hoops because the fetch_mr_patches
+ # entry point on GitLab does not allow one
+ # to specify a particular version. You can only get
+ # a nice set of patches for the most current version.
+ # Out of an abundance of caution, we make sure that the
+ # last sha1 matches a known version and then we use that.
+ updated_at = parse_gitlab_datetime(mr['updated_at'])
+ patches = split_mbox_into_messages(fetch_mr_patches(iid))
+ sha1s = [get_patch_sha1(patch) for patch in patches]
+ versions = fetch_mr_versions(iid)
+ version = get_mr_version(versions, sha1s[-1])
+ if not version:
+ # We can get an unusual case where we do not have a patch
+ # to go with the start of the versions chain. Andrew
+ # was able to do this in wine-demo MR 13, by somehow
+ # having a commit message only commit. Arek believes that
+ # this is not a sensible MR, and we should flag it as such.
+ log(f"Error: MR {iid} patch {sha1s[-1]} not in versions")
+ error = ("This merge request appears to be malformed.\n"
+ "This can be caused by a commit with no content.\n"
+ "Please check your merge request for errors.")
+
+ if not error_in_notes(iid, error):
+ post_note(iid, error)
+ if update_db:
+ db.set_last_mr_updated_at(updated_at)
+ return
+
+ date = get_mr_version_date(versions, version)
+ if db.was_mr_version_processed(iid, version):
+ log(f"MR{iid}v{version} - skipping, already processed")
+ return
+
+ nr_patches = len(patches)
+ author = f"{mr['author']['name']} (@{mr['author']['username']})"
+ cover = create_cover(mr['id'], iid, version, versions, nr_patches, author, mr['title'], mr['description'])
+ if cover is None:
+ log(f"MR{iid}v{version} - skipping, has no changes")
+ return
+
+ fixup_date(cover, date)
+ create_headers_from_mr(cover, mr)
+ send_email(cover)
+ if nr_patches <= settings.MAXIMUM_PATCHES:
+ for nr, mail in enumerate(patches):
+ fixup_patch(mail, iid, version, nr+1)
+ date = date + datetime.timedelta(seconds=1)
+ fixup_date(mail, date)
+ fixup_version(mail, version)
+ create_headers_from_mr(mail, mr)
+ mail['References'] = create_reference(mr['id'], gitlab_hostname)
+ log(f"MR{iid}v{version} - sent email")
+ send_email(mail)
+ if update_db:
+ db.mark_mr_version_processed(iid, version)
+ db.set_last_mr_updated_at(updated_at)
+
+
def main():
+
+ # Process any new merge requests
last_mr_updated_at = db.get_last_mr_updated_at()
if not last_mr_updated_at:
last_mr_updated_at = datetime.datetime.now() - datetime.timedelta(days=settings.INITIAL_BACKLOG_DAYS)
@@ -544,68 +612,7 @@ def main():
db.set_last_mr_updated_at(last_mr_updated_at)
for mr in fetch_recently_updated_mrs(last_mr_updated_at):
- iid = mr['iid']
-
- log(f"MR{iid} updated - processing")
- if mr['merged_at']:
- log(f"MR{iid} merged - skipping")
- continue
-
-
- # We jump through some hoops because the fetch_mr_patches
- # entry point on GitLab does not allow one
- # to specify a particular version. You can only get
- # a nice set of patches for the most current version.
- # Out of an abundance of caution, we make sure that the
- # last sha1 matches a known version and then we use that.
- updated_at = parse_gitlab_datetime(mr['updated_at'])
- patches = split_mbox_into_messages(fetch_mr_patches(iid))
- sha1s = [get_patch_sha1(patch) for patch in patches]
- versions = fetch_mr_versions(iid)
- version = get_mr_version(versions, sha1s[-1])
- if not version:
- # We can get an unusual case where we do not have a patch
- # to go with the start of the versions chain. Andrew
- # was able to do this in wine-demo MR 13, by somehow
- # having a commit message only commit. Arek believes that
- # this is not a sensible MR, and we should flag it as such.
- log(f"Error: MR {iid} patch {sha1s[-1]} not in versions")
- error = ("This merge request appears to be malformed.\n"
- "This can be caused by a commit with no content.\n"
- "Please check your merge request for errors.")
-
- if not error_in_notes(iid, error):
- post_note(iid, error)
- if not settings.READ_ONLY:
- db.set_last_mr_updated_at(updated_at)
- continue
-
- date = get_mr_version_date(versions, version)
- if not db.was_mr_version_processed(iid, version):
- nr_patches = len(patches)
- author = f"{mr['author']['name']} (@{mr['author']['username']})"
- cover = create_cover(mr['id'], iid, version, versions, nr_patches, author, mr['title'], mr['description'])
- if cover is None:
- log(f"MR{iid}v{version} - skipping, has no changes")
- continue
- fixup_date(cover, date)
- create_headers_from_mr(cover, mr)
- send_email(cover)
- if nr_patches <= settings.MAXIMUM_PATCHES:
- for nr, mail in enumerate(patches):
- fixup_patch(mail, iid, version, nr+1)
- date = date + datetime.timedelta(seconds=1)
- fixup_date(mail, date)
- fixup_version(mail, version)
- create_headers_from_mr(mail, mr)
- mail['References'] = create_reference(mr['id'], gitlab_hostname)
- log(f"MR{iid}v{version} - sent email")
- send_email(mail)
- if not settings.READ_ONLY:
- db.mark_mr_version_processed(iid, version)
- db.set_last_mr_updated_at(updated_at)
- else:
- log(f"MR{iid}v{version} - skipping, already processed")
+ process_mr(mr, not settings.READ_ONLY)
date = db.get_last_event_date()
if not date:
More information about the wine-cvs
mailing list