From: "cron2 (Code Review)" Date: Sun, 22 Oct 2023 11:10:26 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., heiko@...515..., flichtenheld79@...277..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <7765c799a8a7e27472faf6fcc3d2c847a2d4698f-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="U/L2TLq8BNA="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patch... List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: d12fk , openvpn-devel , unauthorized --U/L2TLq8BNA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#6) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+1 by plaisthos, Code-Review+2 by cron2 Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchset= s to Patchwork ...................................................................... dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork Since we're trying to use Gerrit for patch reviews, but the actual merge process is still implemented against the ML and Patchwork, I wrote a script that attempts to bridge the gap. It extracts all relevant information about a patch from Gerrit and converts it into a mail compatible to git-am. Mostly this work is done by Gerrit already, since we can get the original patch in git format-patch format. But we add Acked-by information according to the approvals in Gerrit and some other metadata. This should allow the merge to happen based on this one mail alone. v3: - handle missing display_name and email fields for reviewers gracefully - handle missing Signed-off-by line gracefully v4: - use formatted string consistently Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Message-Id: <20231022105919.21779-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg27= 279.html Signed-off-by: Gert Doering --- A dev-tools/gerrit-send-mail.py 1 file changed, 132 insertions(+), 0 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/61/361/6 diff --git a/dev-tools/gerrit-send-mail.py b/dev-tools/gerrit-send-mail.py new file mode 100755 index 0000000..851a20a --- /dev/null +++ b/dev-tools/gerrit-send-mail.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc +# Copyright (C) 2023 Frank Lichtenheld +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Extract a patch from Gerrit and transform it in a file suitable as input +# for git send-email. + +import argparse +import base64 +from datetime import timezone +import json +import sys +from urllib.parse import urlparse + +import dateutil.parser +import requests + + +def get_details(args): + params =3D {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r =3D requests.get(f"{args.url}/changes/{args.changeid}", params=3Dpar= ams) + print(r.url) + json_txt =3D r.text.removeprefix(")]}'\n") + json_data =3D json.loads(json_txt) + assert len(json_data["revisions"]) =3D=3D 1 # CURRENT_REVISION works = as expected + revision =3D json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by =3D [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >=3D 0 # no NACK + if reviewer["value"] =3D=3D 2: + # fall back to user name if optional fields are not set + reviewer_name =3D reviewer.get("display_name", reviewer["n= ame"]) + reviewer_mail =3D reviewer.get("email", reviewer["name"]) + ack =3D f"{reviewer_name} <{reviewer_mail}>" + print(f"Acked-by: {ack}") + acked_by.append(ack) + change_id =3D json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp =3D ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=3Dtimezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp =3D int(utc_stamp * 1000) + hostname =3D urlparse(args.url).hostname + msg_id =3D f"gerrit.{created_stamp}.{change_id}@{hostname}" + return { + "revision": revision, + "project": json_data["project"], + "target": json_data["branch"], + "msg_id": msg_id, + "acked_by": acked_by, + } + + +def get_patch(details, args): + r =3D requests.get( + f"{args.url}/changes/{args.changeid}/revisions/{details['revision'= ]}/patch?download" + ) + print(r.url) + patch_text =3D base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + comment_start =3D patch_text.index("\n---\n") + len("\n---\n") + try: + signed_off_start =3D patch_text.rindex("\nSigned-off-by: ") + signed_off_end =3D patch_text.index("\n", signed_off_start + 1) + 1 + except ValueError: # Signed-off missing + signed_off_end =3D patch_text.index("\n---\n") + 1 + assert comment_start > signed_off_end + acked_by_text =3D "" + acked_by_names =3D "" + for ack in details["acked_by"]: + acked_by_text +=3D f"Acked-by: {ack}\n" + acked_by_names +=3D f"{ack}\n" + patch_text_mod =3D ( + patch_text[:signed_off_end] + + acked_by_text + + patch_text[signed_off_end:comment_start] + + f""" +This change was reviewed on Gerrit and approved by at least one +developer. I request to merge it to {details["target"]}. + +Gerrit URL: {args.url}/c/{details["project"]}/+/{args.changeid} +This mail reflects revision {details["revision"]} of this Change. +Acked-by according to Gerrit (reflected above): +{acked_by_names} + """ + + patch_text[comment_start:] + ) + filename =3D f"gerrit-{args.changeid}-{details['revision']}.patch" + with open(filename, "w") as patch_file: + patch_file.write(patch_text_mod) + print("send with:") + print(f"git send-email --in-reply-to {details['msg_id']} {filename}") + + +def main(): + parser =3D argparse.ArgumentParser( + prog=3D"gerrit-send-mail", + description=3D"Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default=3D"https://gerrit.openvpn.n= et") + args =3D parser.parse_args() + + details =3D get_details(args) + patch =3D get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ =3D=3D "__main__": + sys.exit(main()) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160 Gerrit-Change-Number: 361 Gerrit-PatchSet: 6 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: d12fk Gerrit-CC: openvpn-devel Gerrit-CC: unauthorized Gerrit-MessageType: newpatchset --U/L2TLq8BNA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #6 to the change originally cr= eated by flichtenheld.

View Change

The following approvals got outda= ted and were removed: Code-Review+1 by plaisthos, Code-Review+2 by cron2

dev-tools/gerrit-send-ma=
il.py: tool to send Gerrit patchsets to Patchwork

Since we're tr= ying to use Gerrit for patch reviews, but the actual
merge process is st= ill implemented against the ML and Patchwork,
I wrote a script that atte= mpts to bridge the gap.

It extracts all relevant information about a= patch from Gerrit
and converts it into a mail compatible to git-am. Mos= tly this
work is done by Gerrit already, since we can get the originalpatch in git format-patch format. But we add Acked-by information
acco= rding to the approvals in Gerrit and some other metadata.

This shoul= d allow the merge to happen based on this one mail
alone.

v3:
= - handle missing display_name and email fields for reviewers
gracefu= lly
- handle missing Signed-off-by line gracefully
v4:
- use for= matted string consistently

Change-Id: If4e9c2e58441efb3fd00872cd62d1= cc6c607f160
Signed-off-by: Frank Lichtenheld <frank@...2641...>Acked-by: Gert Doering <gert@...1296...>
Message-Id: <20231022= 105919.21779-1-gert@...1296...>
URL: https://www.mail-archive.com/ope= nvpn-devel@lists.sourceforge.net/msg27279.html
Signed-off-by: Gert Doeri= ng <gert@...1296...>
---
A dev-tools/gerrit-send-mail.py
1 f= ile changed, 132 insertions(+), 0 deletions(-)

git pull ssh://ger=
rit.openvpn.net:29418/openvpn refs/changes/61/361/6
diff --git a/de=
v-tools/gerrit-send-mail.py b/dev-tools/gerrit-send-mail.py
new file mode 100755
index 0000000..851a20a
--- /dev/null
+++ b/dev-tools/gerrit-send-mail.py<= br>@@ -0,0 +1,132 @@
+#!/usr/bin/env python3
+
+# Copyri= ght (C) 2023 OpenVPN Inc <sales@...515...>
+# Copyright (C) 2023 Frank Lichtenheld <fr= ank.lichtenheld@...515...>
+#
+# This = program is free software; you can redistribute it and/or modify
<= span style=3D"color: hsl(120, 100%, 40%);">+# it under the terms of the GN= U General Public License version 2
+# as published by the Free Software Foundation.
<= span style=3D"color: hsl(120, 100%, 40%);">+#
+# This program is distributed in the hope that i= t will be useful,
+# = but WITHOUT ANY WARRANTY; without even the implied warranty of
<= span style=3D"color: hsl(120, 100%, 40%);">+# MERCHANTABILITY or FITNESS F= OR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Gener= al Public License along
+# with this program; if not, write to the Free Software Foundation, In= c.,
+# 51 Franklin S= treet, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Extract a patch from Gerrit and transform it in a file suitable a= s input
+# for git se= nd-email.
++import argparse
+import base64
+from datetime import timezone+import json
+import sys
+from urllib.parse import urlparse
+
+import dateutil.parser
+import requests
+
+
+def get_details= (args):
+ params = =3D {"o": ["CURRENT_REVISION", "LABELS", &quo= t;DETAILED_ACCOUNTS"]}
+ r =3D requests.get(f"{args.url}/changes/{args.changeid}&qu= ot;, params=3Dparams)
+ print(r.url)
+ = json_txt =3D r.text.removeprefix(")]}'\n")
+ json_data =3D json.loads(json_t= xt)
+ assert len(j= son_data["revisions"]) =3D=3D 1 # CURRENT_REVISION works as expe= cted
+ revision = =3D json_data["revisions"].popitem()[1]["_number"]

+ assert "Code-Re= view" in json_data["labels"]
+ acked_by =3D []
+ for reviewer in json_data["labels"][&= quot;Code-Review"]["all"]:
+ if "value" in reviewer:
<= span style=3D"color: hsl(120, 100%, 40%);">+ assert reviewer[&qu= ot;value"] >=3D 0 # no NACK
+ if reviewer["value"] =3D=3D 2:

+ # fall ba= ck to user name if optional fields are not set
+ reviewer_name =3D reviewer.get(&= quot;display_name", reviewer["name"])
+ reviewer_mail =3D reviewe= r.get("email", reviewer["name"])
+ ack =3D f"{reviewer_= name} <{reviewer_mail}>"
+ print(f"Acked-by: {ack}")<= br>+ acked_by.ap= pend(ack)
+ change= _id =3D json_data["change_id"]
+ # assumes that the created date in Gerrit is in UT= C
+ utc_stamp =3D = (
+ dateutil.p= arser.parse(json_data["created"])
+ .replace(tzinfo=3Dtimezone.utc)
<= span style=3D"color: hsl(120, 100%, 40%);">+ .timestamp()
= + )
+ # convert to milliseconds as used in mes= sage id
+ created_= stamp =3D int(utc_stamp * 1000)
+ hostname =3D urlparse(args.url).hostname
+ msg_id =3D f"gerrit.{created_= stamp}.{change_id}@{hostname}"
+ return {
+ "revision": revision,
+ "project": json_data["p= roject"],
+ = "target": json_data["branch"],
+ "msg_id": msg_id,
+ "acked_by&qu= ot;: acked_by,
+ }=
+
+
+def get_patch(details, args):
+ r =3D requests.get(
+ f"{args.url}/changes/{args.c= hangeid}/revisions/{details['revision']}/patch?download"
+ )
+ print(r.url)
+ patch_text =3D base64.b64decode(r.tex= t).decode()
+ retu= rn patch_text
+
+
+def apply_patch_mods(patch_text, details, ar= gs):
+ comment_sta= rt =3D patch_text.index("\n---\n") + len("\n---\n")
+ try:
+ signed_off_start =3D patch= _text.rindex("\nSigned-off-by: ")

+ signed_off_end =3D patch_text.index("\= n", signed_off_start + 1) + 1
+ except ValueError: # Signed-off missing
+ signed_off_end =3D patch_te= xt.index("\n---\n") + 1

+ assert comment_start > signed_off_end
+ acked_by_text =3D ""
+ acked_by_names = =3D ""
+ = for ack in details["acked_by"]:
+ acked_by_text +=3D f"Acked-by: {ack}\n&= quot;
+ acked_= by_names +=3D f"{ack}\n"
+ patch_text_mod =3D (
+ patch_text[:signed_off_end]
+ + acked_by_text
+ + patch_text[signed_off_end= :comment_start]

+ = + f"""
+This change was reviewed on Gerrit and approved by at least one
+developer. I request to m= erge it to {details["target"]}.
+
+This mail reflects= revision {details["revision"]} of this Change.
+Acked-by according to Gerrit (reflect= ed above):
+{acked_by= _names}
+ &quo= t;""
+ = + patch_text[comment_start:]
+ )
+ = filename =3D f"gerrit-{args.changeid}-{details['revision']}.= patch"
+ with= open(filename, "w") as patch_file:
+ patch_file.write(patch_text_mod)+ print("send with:&q= uot;)
+ print(f&qu= ot;git send-email --in-reply-to {details['msg_id']} {filename}"= ;)
+
+
+def main():
+ parser =3D argparse.ArgumentParser(
+ prog=3D"gerrit-send-mail&quo= t;,
+ descript= ion=3D"Send patchset from Gerrit to mailing list",
+ )

+ parser.add_argument("changeid")
+ parser.add_argumen= t("-u", "--url", default=3D"https://gerrit.openvpn= .net")
+ args= =3D parser.parse_args()
+
+ details = =3D get_details(args)
+ patch =3D get_patch(details, args)
+ apply_patch_mods(patch, details, args)
<= span style=3D"color: hsl(120, 100%, 40%);">+

+
+if __name__ =3D=3D "__main__":
+ sys.exit(main())

=

To view, visit change 361. To unsubscribe, or for help writing mail fil= ters, visit settings.

<= meta itemprop=3D"name" content=3D"View Change"/>
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62= d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 6
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: d12fk <heiko@...515...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-CC: unauthorized <flichtenheld79@...493...= 277...>
Gerrit-MessageType: newpatchset
--U/L2TLq8BNA=--