Skip to content

Commit

Permalink
fix pre-commit issues
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Jan 10, 2025
1 parent 615f4db commit 3e65316
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions .github/actions/ensure_backporting/add_labels.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
"""Executable used to add backport labels to Github issues."""

import argparse
import os
Expand All @@ -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,
Expand All @@ -29,40 +30,46 @@ 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",
}

# Get PR Labels
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()
main()

0 comments on commit 3e65316

Please sign in to comment.