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

[SCRIPT-5] Add fetching of existing github labels and optimize create and delete api calls #13

Merged
merged 6 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion config/labels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@
maintainability).
- name: 'Type: Suggestion'
color: '#ac8daf'

75 changes: 53 additions & 22 deletions scripts/setup_issue_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def __init__(self) -> None:
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
self._github_labels: list[dict[str, str]] = self._fetch_github_labels()
self._github_label_names: list[str] = [
github_label["name"] for github_label in self.github_labels
]
self._labels: list[dict[str, str]] = self._load_labels()

@property
Expand All @@ -75,10 +79,33 @@ def url(self) -> str:
def headers(self) -> dict[str, str]:
return self._headers

@property
def github_labels(self) -> list[dict[str, str]]:
return self._github_labels

@property
def github_label_names(self) -> list[str]:
return self._github_label_names

@property
def labels(self) -> list[dict[str, str]]:
return self._labels

def _fetch_github_labels(self) -> list[dict[str, str]]:
res: Response = requests.get(
self.url,
headers=self.headers,
)

return [
{
"name": github_label["name"],
"color": github_label["color"],
"description": github_label["description"],
}
for github_label in res.json()
]

def _load_labels(self) -> list[dict[str, str]]:
labels: list[dict[str, str]]
label_file: str = ""
Expand Down Expand Up @@ -114,36 +141,40 @@ def delete_default_labels(self) -> None:
]

for default_label_name in DEFAULT_LABEL_NAMES:
url: str = f"{self.url}/{default_label_name}"
if default_label_name in self.github_label_names:
url: str = f"{self.url}/{default_label_name}"

res: Response = requests.delete(
url,
headers=self.headers,
)

if res.status_code == 204:
logging.info(f"Label `{default_label_name}` deleted successfully.")
else:
logging.error(
f"Status {res.status_code}. Failed to delete label `{default_label_name}`."
res: Response = requests.delete(
url,
headers=self.headers,
)

if res.status_code == 204:
logging.info(f"Label `{default_label_name}` deleted successfully.")
else:
logging.error(
f"Status {res.status_code}. Failed to delete label `{default_label_name}`."
)

def create_labels(self) -> None:
for label in self.labels:
label["color"] = label["color"].replace("#", "")
if label["name"] not in self.github_label_names:
label["color"] = label["color"].replace("#", "")

res: Response = requests.post(
self.url,
headers=self.headers,
json=label,
)
res: Response = requests.post(
self.url,
headers=self.headers,
json=label,
)

if res.status_code == 201:
logging.info(f"Label `{label['name']}` created successfully.")
if res.status_code == 201:
logging.info(f"Label `{label['name']}` created successfully.")
else:
logging.error(
f"Status {res.status_code}. Failed to create label `{label['name']}`."
)
else:
logging.error(
f"Status {res.status_code}. Failed to create label `{label['name']}`."
)
pass


def main() -> None:
Expand Down