From 98ecee29475a4b8152f1cf2f446b4e5da6afac4d Mon Sep 17 00:00:00 2001 From: seylu <98249191+seyLu@users.noreply.github.com> Date: Mon, 28 Aug 2023 12:46:52 +0800 Subject: [PATCH 1/6] feat: add fetching of existing github labels --- config/labels.yaml | 1 - scripts/setup_issue_label.py | 61 ++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/config/labels.yaml b/config/labels.yaml index 9e36f48..18ebbfd 100644 --- a/config/labels.yaml +++ b/config/labels.yaml @@ -24,4 +24,3 @@ maintainability). - name: 'Type: Suggestion' color: '#ac8daf' - diff --git a/scripts/setup_issue_label.py b/scripts/setup_issue_label.py index 342d529..144fb0b 100644 --- a/scripts/setup_issue_label.py +++ b/scripts/setup_issue_label.py @@ -65,6 +65,7 @@ def __init__(self) -> None: "Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28", } + self._github_label_names: list[str] = self._fetch_github_label_names() self._labels: list[dict[str, str]] = self._load_labels() @property @@ -75,10 +76,22 @@ def url(self) -> str: def headers(self) -> dict[str, str]: return self._headers + @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_label_names(self) -> list[str]: + res: Response = requests.get( + self.url, + headers=self.headers, + ) + + return [github_label["name"] for github_label in res.json()] + def _load_labels(self) -> list[dict[str, str]]: labels: list[dict[str, str]] label_file: str = "" @@ -114,37 +127,39 @@ def delete_default_labels(self) -> None: ] for default_label_name in DEFAULT_LABEL_NAMES: - url: str = f"{self.url}/{default_label_name}" - - res: Response = requests.delete( - url, - headers=self.headers, - ) + if default_label_name in self.github_label_names: + url: str = f"{self.url}/{default_label_name}" - 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, - ) - - 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.info(f"Label `{label['name']}` created successfully.") + else: + logging.error( + f"Status {res.status_code}. Failed to create label `{label['name']}`." + ) + def main() -> None: github_issue_label = GithubIssueLabel() From a11002465e9b309a3cd6ce3eb06e67c011b11f82 Mon Sep 17 00:00:00 2001 From: seylu <98249191+seyLu@users.noreply.github.com> Date: Mon, 28 Aug 2023 13:19:49 +0800 Subject: [PATCH 2/6] chore: lint --- scripts/setup_issue_label.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/scripts/setup_issue_label.py b/scripts/setup_issue_label.py index 144fb0b..59aa046 100644 --- a/scripts/setup_issue_label.py +++ b/scripts/setup_issue_label.py @@ -65,7 +65,10 @@ def __init__(self) -> None: "Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28", } - self._github_label_names: list[str] = self._fetch_github_label_names() + 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 @@ -76,6 +79,10 @@ 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 @@ -84,13 +91,20 @@ def github_label_names(self) -> list[str]: def labels(self) -> list[dict[str, str]]: return self._labels - def _fetch_github_label_names(self) -> list[str]: + def _fetch_github_labels(self) -> list[dict[str, str]]: res: Response = requests.get( self.url, headers=self.headers, ) - return [github_label["name"] for github_label in res.json()] + 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]] @@ -159,6 +173,8 @@ def create_labels(self) -> None: logging.error( f"Status {res.status_code}. Failed to create label `{label['name']}`." ) + else: + pass def main() -> None: From 6dd5c113f5daa16e4747dc3bc7a27a51c0015532 Mon Sep 17 00:00:00 2001 From: seylu <98249191+seyLu@users.noreply.github.com> Date: Mon, 28 Aug 2023 14:18:00 +0800 Subject: [PATCH 3/6] feat: add logic to update label if label already exists --- scripts/setup_issue_label.py | 43 +++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/scripts/setup_issue_label.py b/scripts/setup_issue_label.py index 59aa046..1cb4e35 100644 --- a/scripts/setup_issue_label.py +++ b/scripts/setup_issue_label.py @@ -107,7 +107,7 @@ def _fetch_github_labels(self) -> list[dict[str, str]]: ] def _load_labels(self) -> list[dict[str, str]]: - labels: list[dict[str, str]] + labels: list[dict[str, str]] = [] label_file: str = "" if os.path.isfile(LabelFile.YAML): @@ -126,10 +126,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)): + 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.info(f"Label `{label['new_name']}` updated successfully.") + else: + logging.error( + f"Status {res.status_code}. Failed to update label `{label['new_name']}`." + ) + def delete_default_labels(self) -> None: DEFAULT_LABEL_NAMES: list[str] = [ "bug", @@ -158,9 +184,16 @@ def delete_default_labels(self) -> None: def create_labels(self) -> None: for label in self.labels: - if label["name"] not in self.github_label_names: - label["color"] = label["color"].replace("#", "") + if label["name"] in self.github_label_names: + i: int = self.github_label_names.index(label["name"]) + + if ( + label["color"] != self.github_labels[i]["color"] + or label["description"] != self.github_labels[i]["description"] + ): + self._update_label(label) + else: res: Response = requests.post( self.url, headers=self.headers, @@ -173,8 +206,6 @@ def create_labels(self) -> None: logging.error( f"Status {res.status_code}. Failed to create label `{label['name']}`." ) - else: - pass def main() -> None: From 2d1d5b8d4a01fa956dabb25a3dc7dc1094a0ac45 Mon Sep 17 00:00:00 2001 From: seylu <98249191+seyLu@users.noreply.github.com> Date: Mon, 28 Aug 2023 14:20:38 +0800 Subject: [PATCH 4/6] chore: rename method _load_labels to more descriptive _load_labels_from_config --- scripts/setup_issue_label.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup_issue_label.py b/scripts/setup_issue_label.py index 1cb4e35..56c2c96 100644 --- a/scripts/setup_issue_label.py +++ b/scripts/setup_issue_label.py @@ -69,7 +69,7 @@ def __init__(self) -> None: 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() + self._labels: list[dict[str, str]] = self._load_labels_from_config() @property def url(self) -> str: @@ -106,7 +106,7 @@ def _fetch_github_labels(self) -> list[dict[str, str]]: for github_label in res.json() ] - def _load_labels(self) -> list[dict[str, str]]: + def _load_labels_from_config(self) -> list[dict[str, str]]: labels: list[dict[str, str]] = [] label_file: str = "" From 49eca45a58a6743bc4bd8fc6b9f4a30d643a0ae0 Mon Sep 17 00:00:00 2001 From: seylu <98249191+seyLu@users.noreply.github.com> Date: Mon, 28 Aug 2023 14:24:27 +0800 Subject: [PATCH 5/6] chore: when logging loading labels from config error, label index starts at 1 --- scripts/setup_issue_label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/setup_issue_label.py b/scripts/setup_issue_label.py index 56c2c96..eea171d 100644 --- a/scripts/setup_issue_label.py +++ b/scripts/setup_issue_label.py @@ -126,7 +126,7 @@ def _load_labels_from_config(self) -> list[dict[str, str]]: with open(label_file, "r") as f: if label_file == LabelFile.YAML: - for i, label in enumerate(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')}`." From 495421a02cc7c27d5d3166d4f4dbbd97bcb69a98 Mon Sep 17 00:00:00 2001 From: seylu <98249191+seyLu@users.noreply.github.com> Date: Mon, 28 Aug 2023 14:34:45 +0800 Subject: [PATCH 6/6] feat: add logging for fetching list of github labels --- scripts/setup_issue_label.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/scripts/setup_issue_label.py b/scripts/setup_issue_label.py index eea171d..5cac2e7 100644 --- a/scripts/setup_issue_label.py +++ b/scripts/setup_issue_label.py @@ -97,6 +97,13 @@ def _fetch_github_labels(self) -> list[dict[str, str]]: 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"], @@ -149,12 +156,12 @@ def _update_label(self, label: dict[str, str]) -> None: res: Response = requests.patch(url, headers=self.headers, json=label) - if res.status_code == 200: - logging.info(f"Label `{label['new_name']}` updated successfully.") - else: + 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] = [ @@ -175,12 +182,12 @@ def delete_default_labels(self) -> None: headers=self.headers, ) - if res.status_code == 204: - logging.info(f"Label `{default_label_name}` deleted successfully.") - else: + 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: @@ -200,12 +207,12 @@ def create_labels(self) -> None: json=label, ) - if res.status_code == 201: - logging.info(f"Label `{label['name']}` created successfully.") - else: + 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: