From: "flichtenheld (Code Review)" Date: Wed, 20 Sep 2023 12:41:26 +0000 In-Reply-To: References: Reply-To: frank@...2641..., openvpn-devel@lists.sourceforge.net Message-ID: <69ffd2c29cd5c53ec2085a09121757ca365c4b8b-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="J9uW4jhR/+k="; 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: , Cc: openvpn-devel --J9uW4jhR/+k= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2. Hello cron2, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=3Demail to look at the new patch set (#2). 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. Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160 Signed-off-by: Frank Lichtenheld --- A dev-tools/gerrit-send-mail.py 1 file changed, 133 insertions(+), 0 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/61/361/2 diff --git a/dev-tools/gerrit-send-mail.py b/dev-tools/gerrit-send-mail.py new file mode 100644 index 0000000..74bd272 --- /dev/null +++ b/dev-tools/gerrit-send-mail.py @@ -0,0 +1,133 @@ +#!/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(args.url + "/changes/" + args.changeid, params=3Dpa= rams) + 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: + ack =3D "{} <{}>".format(reviewer["display_name"], reviewe= r["email"]) + print("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( + "{}/changes/{}/revisions/{}/patch?download".format( + args.url, args.changeid, details["revision"] + ) + ) + print(r.url) + patch_text =3D base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + signed_off_start =3D patch_text.rindex("\nSigned-off-by: ") + signed_off_end =3D patch_text.index("\n", signed_off_start + 1) + 1 + comment_start =3D patch_text.index("\n---\n") + len("\n---\n") + 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] + + """ +This change was reviewed on Gerrit and approved by at least one +developer. I request to merge it to {target}. + +Gerrit URL: {url}/c/{project}/+/{changeid} +This mail reflects revision {revision} of this Change. +Acked-by according to Gerrit (reflected above): +{acked_by_names} + """.format( + url=3Dargs.url, + changeid=3Dargs.changeid, + acked_by_names=3Dacked_by_names, + **details, + ) + + patch_text[comment_start:] + ) + filename =3D "gerrit-{}-{}.patch".format(args.changeid, details["revis= ion"]) + with open(filename, "w") as patch_file: + patch_file.write(patch_text_mod) + print("send with:") + print("git send-email --in-reply-to {} {}".format(details["msg_id"], f= ilename)) + + +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: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-CC: openvpn-devel Gerrit-Attention: cron2 Gerrit-MessageType: newpatchset --J9uW4jhR/+k= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2.

flichtenheld uploaded patch set #2 to this change.

<= p>View Ch= ange

dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Pa=
tchwork

Since we're trying to use Gerrit for patch reviews, but = the actual
merge process is still implemented against the ML and Patchwo= rk,
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.

Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f= 160
Signed-off-by: Frank Lichtenheld <frank@...2641...>
---
= A dev-tools/gerrit-send-mail.py
1 file changed, 133 insertions(+), 0 del= etions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/ch=
anges/61/361/2
diff --git a/dev-tools/gerrit-send-mail.py b/dev-too=
ls/gerrit-send-mail.py
new file mode 100644
index 0000000..74bd272
--- /dev/null
+++ b= /dev-tools/gerrit-send-mail.py
@@ -0,0 +1,133 @@
= +#!/usr/bin/env python3<= br>+
+# Copyright (C) 2023 OpenVPN Inc <sales@...1186...= .515...>
+# Copyr= ight (C) 2023 Frank Lichtenheld <frank.lichtenheld@...515...><= br>+#
+# This program is free software; you can red= istribute it and/or modify
+# it under the terms of the GNU General Public License version 2
+# as published by the= Free Software Foundation.
+#
+# This pro= gram is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without ev= en the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public L= icense for more details.
+#
+# You should= have received a copy of the GNU General Public License along
+# with this program; if not, wri= te 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
+f= rom datetime import timezone
+import json
= +import sys
+from url= lib.parse import urlparse
+
+import dateut= il.parser
+import req= uests
+
+

+def get_details(args):
+ params =3D {"o": ["CURRENT_REV= ISION", "LABELS", "DETAILED_ACCOUNTS"]}
= + r =3D requests.get(args.ur= l + "/changes/" + args.changeid, params=3Dparams)
+ print(r.url)
+ json_txt =3D r.text.removeprefix(&= quot;)]}'\n")
+ assert len(json_data["revisions"]) =3D= =3D 1 # CURRENT_REVISION works as expected
+ revision =3D json_data["revisions"].p= opitem()[1]["_number"]
+ assert "Code-Review" in json_data["labels&q= uot;]
+ acked_by = =3D []
+ for revie= wer in json_data["labels"]["Code-Review"]["all&quo= t;]:
+ if &quo= t;value" in reviewer:
+ assert reviewer["value"] >=3D 0 # no NACK<= /span>
+ if revie= wer["value"] =3D=3D 2:
+ ack =3D "{} <{}>".format(revie= wer["display_name"], reviewer["email"])
+ print("Acked-b= y: " + 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"])<= br>+ .replace(tzinfo=3Dt= imezone.utc)
+ = .timestamp()
+ )<= /span>
+ # convert to mil= liseconds as used in message id
+ created_stamp =3D int(utc_stamp * 1000)
+ hostname =3D urlparse(args.url).hos= tname
+ msg_id =3D= f"gerrit.{created_stamp}.{change_id}@{hostname}"
+ return {

+ "revision": revision,
+ "project= ": json_data["project"],
+ "target": json_data["branch"= ;],
+ "ms= g_id": msg_id,
+= "acked_by": acked_by,
+ }
+
+
+def get_patch(details, args):
+ r =3D requests.get= (
+ "{}/c= hanges/{}/revisions/{}/patch?download".format(
+ args.url, args.changeid, deta= ils["revision"]
+ )
+ = )
+ print(r.url)
+ patch_text =3D ba= se64.b64decode(r.text).decode()
+ return patch_text
+
+
+def apply_patch_mods(pat= ch_text, details, args):
+ signed_off_start =3D patch_text.rindex("\nSigned-off-by: &quo= t;)
+ signed_off_e= nd =3D patch_text.index("\n", signed_off_start + 1) + 1+ comment_start =3D patch_t= ext.index("\n---\n") + len("\n---\n")
+ assert comment_start > signed_= off_end
+ acked_by= _text =3D ""
+ for ack in details["acked_by"]:
= + acked_by_text +=3D f&q= uot;Acked-by: {ack}\n"
+ acked_by_names +=3D f"{ack}\n"
+ patch_text_mod =3D (
+ patch_text[:signed_off_en= d]

+ + acked_b= y_text
+ + pat= ch_text[signed_off_end:comment_start]
+ + """
+This change was reviewed on Gerrit and approved = by at least one
+deve= loper. I request to merge it to {target}.
+
+This mail reflects revision {revision} of this Ch= ange.
+Acked-by accor= ding to Gerrit (reflected above):
+{acked_by_names}
+ """.format(
+ url=3Dargs.url,
+ changeid=3Dargs.changeid,
+ acked_by_na= mes=3Dacked_by_names,
+ **details,
+ )
+ = + patch_text[comment_start:]
+ )
+ = filename =3D "gerrit-{}-{}.patch".format(args.changeid, details= ["revision"])
+ with open(filename, "w") as patch_file:
+ patch_file.write(patch_text_m= od)
+ print("= send with:")
+ = print("git send-email --in-reply-to {} {}".format(details["= ;msg_id"], filename))
+
+
<= span style=3D"color: hsl(120, 100%, 40%);">+def main():

+ parser =3D argparse.ArgumentParser(=
+ prog=3D&quo= t;gerrit-send-mail",
+ description=3D"Send patchset from Gerrit to mailing list= ",
+ )=
+ parser.add_argument(&q= uot;changeid")
+= parser.add_argument("-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, detail= s, args)

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

To view, visit change 361. To unsubscribe, or for h= elp writing mail filters, visit settings.

<= /div>
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62= d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: cron2
Gerrit-MessageType: newpatchset
--J9uW4jhR/+k=--