From 68db62db9904231d70b9a8da7ad94f3b8dc376cb Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Thu, 19 Oct 2023 23:21:30 +0200 Subject: [PATCH] tools/hydracomment: Add tool for simultaneous commenting --- dist/tools/hydracomment/README.md | 18 +++++ dist/tools/hydracomment/hydracomment.py | 88 ++++++++++++++++++++++++ dist/tools/hydracomment/requirements.txt | 1 + 3 files changed, 107 insertions(+) create mode 100644 dist/tools/hydracomment/README.md create mode 100755 dist/tools/hydracomment/hydracomment.py create mode 100644 dist/tools/hydracomment/requirements.txt diff --git a/dist/tools/hydracomment/README.md b/dist/tools/hydracomment/README.md new file mode 100644 index 000000000000..90a0eef31624 --- /dev/null +++ b/dist/tools/hydracomment/README.md @@ -0,0 +1,18 @@ +Comment on multiple PRs +============================= + +This script provides functionality to place the same comment simultaneously on +multiple PRs + +It relies on having a `github` API token, stored in `~/.riotgithubtoken` by +default. + +Usage +----- + +Most common usage would be to run: + + hydracomment.py PR_NUMBER1 PR_NUMBER2 ... + +See the backport_pr README.md for instructions on creating a token with correct +permissions diff --git a/dist/tools/hydracomment/hydracomment.py b/dist/tools/hydracomment/hydracomment.py new file mode 100755 index 000000000000..7af7da59c948 --- /dev/null +++ b/dist/tools/hydracomment/hydracomment.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 Koen Zandberg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. +# +# @author Koen Zandberg + +""" +Place identical comments on multiple PRs. +Note that with great power comes great responsibility +""" + +import os +import sys +import argparse +from agithub.GitHub import GitHub + +ORG = "RIOT-OS" +REPO = "RIOT" +GITHUBTOKEN_FILE = ".riotgithubtoken" + + +def main(): + keyfile = os.path.join(os.environ["HOME"], GITHUBTOKEN_FILE) + parser = argparse.ArgumentParser() + parser.add_argument( + "-k", + "--keyfile", + type=argparse.FileType("r"), + default=keyfile, + help="File containing github token", + ) + parser.add_argument( + "-c", + "--comment", + default="Bors merge", + type=str, + help="Comment to place under all PRs.", + ) + parser.add_argument( + "PR", + nargs='+', + type=int, + help="Pull request number to place comment on", + ) + args = parser.parse_args() + + gittoken = args.keyfile.read().strip() + github_api = GitHub(token=gittoken) + + status, user = github_api.user.get() + if status != 200: + print(f'Could not retrieve user: {user["message"]}') + sys.exit(1) + + response_headers = dict(github_api.getheaders()) + if "X-OAuth-Scopes" in response_headers: + scopes = response_headers["X-OAuth-Scopes"] + else: + scopes = response_headers["x-oauth-scopes"] + scopes_list = [x.strip() for x in scopes.split(",")] + if not ("public_repo" in scopes_list or "repo" in scopes_list): + print( + "missing public_repo scope from token settings." + " Please add it on the GitHub webinterface" + ) + sys.exit(1) + + comment = {"body": args.comment} + for prnum in args.PR: + status, pulldata = github_api.repos[ORG][REPO].pulls[prnum].get() + if status != 200: + print(f'PR #{prnum} not found: {pulldata["message"]}') + continue + status, res = ( + github_api.repos[ORG][REPO].issues[prnum].comments.post(body=comment) + ) + if status != 201: + print(f'Something went wrong adding the comment to #{prnum}: {res["message"]}') + else: + print(f"Added comment to #{prnum}") + + +if __name__ == "__main__": + main() diff --git a/dist/tools/hydracomment/requirements.txt b/dist/tools/hydracomment/requirements.txt new file mode 100644 index 000000000000..f94b59f9f848 --- /dev/null +++ b/dist/tools/hydracomment/requirements.txt @@ -0,0 +1 @@ +agithub==2.2.2