Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools/hydracomment: Add tool for simultaneous commenting #20001

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dist/tools/hydracomment/README.md
Original file line number Diff line number Diff line change
@@ -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
88 changes: 88 additions & 0 deletions dist/tools/hydracomment/hydracomment.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>

"""
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default="Bors merge",
default="bors merge",

I'm not sure if it is case sensitive, but so far we always used the lower case version.

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()
1 change: 1 addition & 0 deletions dist/tools/hydracomment/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
agithub==2.2.2
Loading