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 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
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'

119 changes: 94 additions & 25 deletions scripts/setup_issue_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ def __init__(self) -> None:
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
self._labels: list[dict[str, str]] = self._load_labels()
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_from_config()

@property
def url(self) -> str:
Expand All @@ -75,12 +79,42 @@ 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 _load_labels(self) -> list[dict[str, str]]:
labels: list[dict[str, str]]
def _fetch_github_labels(self) -> list[dict[str, str]]:
res: Response = requests.get(
self.url,
headers=self.headers,
)

if res.status_code != 200:
logging.error(
f"Status {res.status_code}. Failed to fetch list of github labels"
)
sys.exit()

logging.info("Fetching list of github labels.")
return [
{
"name": github_label["name"],
"color": github_label["color"],
"description": github_label["description"],
}
for github_label in res.json()
]

def _load_labels_from_config(self) -> list[dict[str, str]]:
labels: list[dict[str, str]] = []
label_file: str = ""

if os.path.isfile(LabelFile.YAML):
Expand All @@ -99,10 +133,36 @@ def _load_labels(self) -> list[dict[str, str]]:

with open(label_file, "r") as f:
if label_file == LabelFile.YAML:
labels = yaml.safe_load(f)
for i, label in enumerate(yaml.safe_load(f), start=1):
if not label.get("name"):
logging.error(
f"Name not found on `Label #{i}` with color `{label.get('color')}` and description `{label.get('description')}`."
)
sys.exit()

labels.append(
{
"name": label.get("name"),
"color": label.get("color", "").replace("#", ""),
"description": label.get("description", ""),
}
)

return labels

def _update_label(self, label: dict[str, str]) -> None:
url: str = f"{self.url}/{label['name']}"
label["new_name"] = label.pop("name")

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

if res.status_code != 200:
logging.error(
f"Status {res.status_code}. Failed to update label `{label['new_name']}`."
)
else:
logging.info(f"Label `{label['new_name']}` updated successfully.")

def delete_default_labels(self) -> None:
DEFAULT_LABEL_NAMES: list[str] = [
"bug",
Expand All @@ -114,37 +174,46 @@ 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.error(
f"Status {res.status_code}. Failed to delete label `{default_label_name}`."
)
else:
logging.info(f"Label `{default_label_name}` deleted successfully.")

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

res: Response = requests.post(
self.url,
headers=self.headers,
json=label,
)
if (
label["color"] != self.github_labels[i]["color"]
or label["description"] != self.github_labels[i]["description"]
):
self._update_label(label)

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']}`."
res: Response = requests.post(
self.url,
headers=self.headers,
json=label,
)

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


def main() -> None:
github_issue_label = GithubIssueLabel()
Expand Down