Change in openvpn[master]: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patch...
A message that is not in the archive (sent off-list or not yet received): gerrit.1695213156000.If4e9c2e58441efb3fd00872cd62d1cc6c607f160@...2715...
flichtenheld has uploaded this change for review. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets 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.
---
A dev-tools/gerrit-send-mail.py
1 file changed, 116 insertions(+), 0 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/61/361/1
dev-tools/gerrit-send-mail.py new file +116 -0
@@ -0,0 +1,116 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = { 'o': [ 'CURRENT_REVISION', 'LABELS', 'DETAILED_ACCOUNTS' ] } + r = requests.get(args.url + '/changes/' + args.changeid, params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + ack = "{} <{}>".format(reviewer["display_name"], reviewer["email"]) + print("Acked-by: " + ack) + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = dateutil.parser.parse(json_data["created"]).replace(tzinfo=timezone.utc).timestamp() + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get("{}/changes/{}/revisions/{}/patch?download".format(args.url, args.changeid, details["revision"])) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + +def apply_patch_mods(patch_text, details, args): + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1; + comment_start = patch_text.index("\n---\n") + len("\n---\n") + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = 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=args.url, + changeid=args.changeid, + acked_by_names=acked_by_names, + **details + ) \ + + patch_text[comment_start:] + filename = "gerrit-{}-{}.patch".format(args.changeid, details["revision"]) + 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"], filename)) + +def main(): + parser = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument('changeid') + parser.add_argument('-u', '--url', default='https://gerrit.openvpn.net') + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + +if __name__ == "__main__": + sys.exit(main())
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newchange
Attention is currently required from: cron2.
flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 1:
(1 comment)
Patchset:
PS1:
Let's see whether this ends up in patchwork
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2
Gerrit-Comment-Date: Wed, 20 Sep 2023 12:36:24 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
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=email
to look at the new patch set (#2).
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets 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.
---
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
dev-tools/gerrit-send-mail.py new file +133 -0
@@ -0,0 +1,133 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r = requests.get(args.url + "/changes/" + args.changeid, params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + ack = "{} <{}>".format(reviewer["display_name"], reviewer["email"]) + print("Acked-by: " + ack) + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=timezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get( + "{}/changes/{}/revisions/{}/patch?download".format( + args.url, args.changeid, details["revision"] + ) + ) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1 + comment_start = patch_text.index("\n---\n") + len("\n---\n") + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = ( + 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=args.url, + changeid=args.changeid, + acked_by_names=acked_by_names, + **details, + ) + + patch_text[comment_start:] + ) + filename = "gerrit-{}-{}.patch".format(args.changeid, details["revision"]) + 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"], filename)) + + +def main(): + parser = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default="https://gerrit.openvpn.net") + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ == "__main__": + sys.exit(main())
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2
Gerrit-MessageType: newpatchset
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=email
to look at the new patch set (#3).
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets 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
---
A dev-tools/gerrit-send-mail.py
1 file changed, 136 insertions(+), 0 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/61/361/3
dev-tools/gerrit-send-mail.py new file +136 -0
@@ -0,0 +1,136 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r = requests.get(args.url + "/changes/" + args.changeid, params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + ack = "{} <{}>".format(reviewer.get("display_name", reviewer["name"]), reviewer.get("email", reviewer["name"])) + print("Acked-by: " + ack) + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=timezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get( + "{}/changes/{}/revisions/{}/patch?download".format( + args.url, args.changeid, details["revision"] + ) + ) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + comment_start = patch_text.index("\n---\n") + len("\n---\n") + try: + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1 + except ValueError: # Signed-off missing + signed_off_end = patch_text.index("\n---\n") + 1 + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = ( + 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=args.url, + changeid=args.changeid, + acked_by_names=acked_by_names, + **details, + ) + + patch_text[comment_start:] + ) + filename = "gerrit-{}-{}.patch".format(args.changeid, details["revision"]) + 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"], filename)) + + +def main(): + parser = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default="https://gerrit.openvpn.net") + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ == "__main__": + sys.exit(main())
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: cron2, flichtenheld.
d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 3:
(1 comment)
File dev-tools/gerrit-send-mail.py:
http://gerrit.openvpn.net/c/openvpn/+/361/comment/350414d1_f3f22a74 :
PS3, Line 47: ack = "{} <{}>".format(reviewer.get("display_name", reviewer["name"]), reviewer.get("email", reviewer["name"]))
I would be in favor of f-string everywhere, they are just so much easier to read and actually faster, not that it would matter in this case.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Mon, 25 Sep 2023 21:44:53 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
Attention is currently required from: cron2, flichtenheld.
Hello cron2,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
to look at the new patch set (#4).
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets 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
---
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/4
dev-tools/gerrit-send-mail.py new file +132 -0
@@ -0,0 +1,132 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r = requests.get(f"{args.url}/changes/{args.changeid}", params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + # fall back to user name if optional fields are not set + reviewer_name = reviewer.get("display_name", reviewer["name"]) + reviewer_mail = reviewer.get("email", reviewer["name"]) + ack = f"{reviewer_name} <{reviewer_mail}>" + print(f"Acked-by: {ack}") + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=timezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get( + f"{args.url}/changes/{args.changeid}/revisions/{details['revision']}/patch?download" + ) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + comment_start = patch_text.index("\n---\n") + len("\n---\n") + try: + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1 + except ValueError: # Signed-off missing + signed_off_end = patch_text.index("\n---\n") + 1 + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = ( + 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 = 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 = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default="https://gerrit.openvpn.net") + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ == "__main__": + sys.exit(main())
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: cron2, d12fk.
flichtenheld has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 4:
(1 comment)
File dev-tools/gerrit-send-mail.py:
http://gerrit.openvpn.net/c/openvpn/+/361/comment/6568c48d_82c7d5d0 :
PS3, Line 47: ack = "{} <{}>".format(reviewer.get("display_name", reviewer["name"]), reviewer.get("email", reviewer["name"]))
Quoted 1 line
I would be in favor of f-string everywhere, they are just so much easier to read and actually faster […]
Done
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Tue, 26 Sep 2023 10:37:52 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: d12fk <[email protected]...>
Gerrit-MessageType: comment
Attention is currently required from: cron2, d12fk, flichtenheld.
plaisthos has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 4: Code-Review+1
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Sun, 08 Oct 2023 11:10:15 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
Attention is currently required from: cron2, d12fk, flichtenheld.
unauthorized has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 4:
(1 comment)
Patchset:
PS4:
Test
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: unauthorized <[email protected]...>
Gerrit-Attention: cron2 <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Mon, 09 Oct 2023 11:47:03 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
Attention is currently required from: d12fk, flichtenheld.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 5: Code-Review+2
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: unauthorized <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Sun, 22 Oct 2023 09:04:16 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
Attention is currently required from: d12fk, flichtenheld.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
......................................................................
Patch Set 5:
(1 comment)
Patchset:
PS5:
I tested this today, and besides needing extra python modules (dateutils) that neither Gentoo nor FreeBSD had before, it nicely did the job on #368 for me.
For extra benefits, the produced "git send-email" line could have the "-v3" for "patch v3"...
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: unauthorized <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Sun, 22 Oct 2023 09:06:38 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
PATCH v5 1/1 dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
In reply to a message not in the archiveFrom: Frank Lichtenheld <[email protected]...>
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
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/361
This mail reflects revision 5 of this Change. Acked-by according to Gerrit (reflected above):
Gert Doering <[email protected]...>
dev-tools/gerrit-send-mail.py new file +132 -0
@@ -0,0 +1,132 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r = requests.get(f"{args.url}/changes/{args.changeid}", params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + # fall back to user name if optional fields are not set + reviewer_name = reviewer.get("display_name", reviewer["name"]) + reviewer_mail = reviewer.get("email", reviewer["name"]) + ack = f"{reviewer_name} <{reviewer_mail}>" + print(f"Acked-by: {ack}") + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=timezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get( + f"{args.url}/changes/{args.changeid}/revisions/{details['revision']}/patch?download" + ) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + comment_start = patch_text.index("\n---\n") + len("\n---\n") + try: + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1 + except ValueError: # Signed-off missing + signed_off_end = patch_text.index("\n---\n") + 1 + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = ( + 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 = 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 = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default="https://gerrit.openvpn.net") + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ == "__main__": + sys.exit(main())
[Openvpn-devel] [PATCH applied] Re: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
Tested this on the last few gerrit-reviewed patches, and it does a good job in "ensure that the mailing list archive has all to-be-committed patches, and the information who reviewed and ACKed it, and where the gerrit info can be found". So, works for me and Frank, and for anyone else interested, here it is...
Could it be optimized, like, "put a v5 right in the [PATCH] line"? Yes, of course :-) - but it's a good start.
Your patch has been applied to the master branch.
commit c827f9d83a7246971f435d0053b0252e49770f11 (master)
Author: Frank Lichtenheld Date: Sun Oct 22 12:59:19 2023 +0200
dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets to Patchwork
Signed-off-by: Frank Lichtenheld <[email protected]...>
Acked-by: Gert Doering <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg27279.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
cron2 has uploaded a new patch set (#6) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
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 patchsets 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
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg27279.html
---
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
dev-tools/gerrit-send-mail.py new file +132 -0
@@ -0,0 +1,132 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r = requests.get(f"{args.url}/changes/{args.changeid}", params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + # fall back to user name if optional fields are not set + reviewer_name = reviewer.get("display_name", reviewer["name"]) + reviewer_mail = reviewer.get("email", reviewer["name"]) + ack = f"{reviewer_name} <{reviewer_mail}>" + print(f"Acked-by: {ack}") + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=timezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get( + f"{args.url}/changes/{args.changeid}/revisions/{details['revision']}/patch?download" + ) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + comment_start = patch_text.index("\n---\n") + len("\n---\n") + try: + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1 + except ValueError: # Signed-off missing + signed_off_end = patch_text.index("\n---\n") + 1 + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = ( + 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 = 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 = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default="https://gerrit.openvpn.net") + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ == "__main__": + sys.exit(main())
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 6
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: unauthorized <[email protected]...>
Gerrit-MessageType: newpatchset
cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/361?usp=email )
Change subject: dev-tools/gerrit-send-mail.py: tool to send Gerrit patchsets 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
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg27279.html
---
A dev-tools/gerrit-send-mail.py
1 file changed, 132 insertions(+), 0 deletions(-)
dev-tools/gerrit-send-mail.py new file +132 -0
@@ -0,0 +1,132 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 OpenVPN Inc <[email protected]...> +# Copyright (C) 2023 Frank Lichtenheld <[email protected]...> +# +# 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 = {"o": ["CURRENT_REVISION", "LABELS", "DETAILED_ACCOUNTS"]} + r = requests.get(f"{args.url}/changes/{args.changeid}", params=params) + print(r.url) + json_txt = r.text.removeprefix(")]}'\n") + json_data = json.loads(json_txt) + assert len(json_data["revisions"]) == 1 # CURRENT_REVISION works as expected + revision = json_data["revisions"].popitem()[1]["_number"] + assert "Code-Review" in json_data["labels"] + acked_by = [] + for reviewer in json_data["labels"]["Code-Review"]["all"]: + if "value" in reviewer: + assert reviewer["value"] >= 0 # no NACK + if reviewer["value"] == 2: + # fall back to user name if optional fields are not set + reviewer_name = reviewer.get("display_name", reviewer["name"]) + reviewer_mail = reviewer.get("email", reviewer["name"]) + ack = f"{reviewer_name} <{reviewer_mail}>" + print(f"Acked-by: {ack}") + acked_by.append(ack) + change_id = json_data["change_id"] + # assumes that the created date in Gerrit is in UTC + utc_stamp = ( + dateutil.parser.parse(json_data["created"]) + .replace(tzinfo=timezone.utc) + .timestamp() + ) + # convert to milliseconds as used in message id + created_stamp = int(utc_stamp * 1000) + hostname = urlparse(args.url).hostname + msg_id = 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 = requests.get( + f"{args.url}/changes/{args.changeid}/revisions/{details['revision']}/patch?download" + ) + print(r.url) + patch_text = base64.b64decode(r.text).decode() + return patch_text + + +def apply_patch_mods(patch_text, details, args): + comment_start = patch_text.index("\n---\n") + len("\n---\n") + try: + signed_off_start = patch_text.rindex("\nSigned-off-by: ") + signed_off_end = patch_text.index("\n", signed_off_start + 1) + 1 + except ValueError: # Signed-off missing + signed_off_end = patch_text.index("\n---\n") + 1 + assert comment_start > signed_off_end + acked_by_text = "" + acked_by_names = "" + for ack in details["acked_by"]: + acked_by_text += f"Acked-by: {ack}\n" + acked_by_names += f"{ack}\n" + patch_text_mod = ( + 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 = 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 = argparse.ArgumentParser( + prog="gerrit-send-mail", + description="Send patchset from Gerrit to mailing list", + ) + parser.add_argument("changeid") + parser.add_argument("-u", "--url", default="https://gerrit.openvpn.net") + args = parser.parse_args() + + details = get_details(args) + patch = get_patch(details, args) + apply_patch_mods(patch, details, args) + + +if __name__ == "__main__": + sys.exit(main())
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/361?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If4e9c2e58441efb3fd00872cd62d1cc6c607f160
Gerrit-Change-Number: 361
Gerrit-PatchSet: 6
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: cron2 <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: d12fk <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: unauthorized <[email protected]...>
Gerrit-MessageType: merged