From 3e65316ad9046a2ebe7730a7f636846f1444fe2d Mon Sep 17 00:00:00 2001 From: aubin bikouo Date: Fri, 10 Jan 2025 12:05:18 +0100 Subject: [PATCH] fix pre-commit issues --- .../actions/ensure_backporting/add_labels.py | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/.github/actions/ensure_backporting/add_labels.py b/.github/actions/ensure_backporting/add_labels.py index 8d1145a..9a1f035 100644 --- a/.github/actions/ensure_backporting/add_labels.py +++ b/.github/actions/ensure_backporting/add_labels.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +"""Executable used to add backport labels to Github issues.""" import argparse import os @@ -9,16 +10,16 @@ class RequestError(Exception): - pass + """An exception class.""" -def main(): - parser = argparse.ArgumentParser( - description="Ensure an issue contains the backport labels." - ) - parser.add_argument( - "--issue-id", type=int, required=True, help="The pull request number." - ) +def main() -> None: + """Perform operations. + + :raises RequestError: In case an error occurs when calling requests API. + """ + parser = argparse.ArgumentParser(description="Ensure an issue contains the backport labels.") + parser.add_argument("--issue-id", type=int, required=True, help="The pull request number.") parser.add_argument( "--repository", type=str, @@ -29,7 +30,7 @@ def main(): # Get list of labels attached to the pull requests headers = { "Accept": "application/vnd.github+json", - "Authorization": "Bearer {0}".format(os.environ.get("GITHUB_TOKEN")), + "Authorization": f"Bearer {os.environ.get('GITHUB_TOKEN')}", "X-GitHub-Api-Version": "2022-11-28", } @@ -37,32 +38,38 @@ def main(): response = requests.get( f"https://api.github.com/repos/{args.repository}/issues/{args.issue_id}/labels", headers=headers, + timeout=30, ) if not response.ok: raise RequestError(response.reason) - issue_backport_labels = [i['name'] for i in response.json() if re.match("^backport-[0-9]*$", i["name"])] + issue_backport_labels = [ + i["name"] for i in response.json() if re.match("^backport-[0-9]*$", i["name"]) + ] # Get Repository Labels response = requests.get( f"https://api.github.com/repos/{args.repository}/labels", headers=headers, + timeout=30, ) if not response.ok: raise RequestError(response.reason) - repository_backport_labels = [i['name'] for i in response.json() if re.match("^backport-[0-9]*$", i["name"])] + repository_backport_labels = [ + i["name"] for i in response.json() if re.match("^backport-[0-9]*$", i["name"]) + ] labels_to_add = list(set(repository_backport_labels) - set(issue_backport_labels)) - print(f"issue_backport_labels = {issue_backport_labels} - repository_backport_labels = {repository_backport_labels} - labels_to_add = {labels_to_add}") if labels_to_add: data = {"labels": labels_to_add} response = requests.post( f"https://api.github.com/repos/{args.repository}/issues/{args.issue_id}/labels", headers=headers, json=data, + timeout=30, ) if not response.ok: raise RequestError(response.reason) if __name__ == "__main__": - main() \ No newline at end of file + main()