delete_dev_releases.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # --------------------------------------------------------------------------------------------------
  12. # Deletes dev releases and tags for a specific version from a GitHub repository.
  13. # --------------------------------------------------------------------------------------------------
  14. import argparse
  15. import requests
  16. from common import Git, Version, fetch_github_releases
  17. def main(arg_strings=None):
  18. parser = argparse.ArgumentParser(
  19. description="Deletes Taipy package dev releases and tags from GitHub.",
  20. formatter_class=argparse.RawTextHelpFormatter,
  21. )
  22. parser.add_argument(
  23. "version",
  24. action="store",
  25. type=Version.check_argument,
  26. help="""The version (M.m.p) of the releases to be deleted.
  27. The indicated version must not have extensions.""",
  28. )
  29. def _check_repository_path(value: str):
  30. if len(value.split("/")) != 2:
  31. raise argparse.ArgumentTypeError(f"'{value}' is not a valid '<owner>/<repo>' path.")
  32. return value
  33. parser.add_argument(
  34. "-r",
  35. "--repository_path",
  36. type=_check_repository_path,
  37. help="""The '<owner>/<repo>' string that identifies the repository where releases are fetched.
  38. The default is the current repository.""",
  39. )
  40. parser.add_argument(
  41. "-y",
  42. "--yes",
  43. action="store_true",
  44. help="""Do not ask for confirmation of the deletion of the releases and tags.""",
  45. )
  46. args = parser.parse_args(arg_strings)
  47. headers = {"Accept": "application/vnd.github+json"}
  48. repository_path = args.repository_path if args.repository_path else Git.get_github_path()
  49. all_releases = fetch_github_releases(repository_path)
  50. found = False
  51. if all_releases:
  52. for package, releases in all_releases.items():
  53. for release in releases:
  54. release_version = release["version"]
  55. release_id = release["id"]
  56. release_tag = release["tag"]
  57. if release_version.validate_extension() and args.version.match(release_version):
  58. found = True
  59. confirm = True if args.yes else False
  60. if not args.yes:
  61. print(f"\n➡️ Release: package: {package.name}, version: {release_version}") # noqa: T201
  62. confirm = (
  63. input("❓ Do you want to delete this release and its tag? (y/N): ").strip().lower() != "y"
  64. )
  65. if confirm:
  66. # Delete release
  67. url = f"https://api.github.com/repos/{repository_path}/releases/{release_id}"
  68. response = requests.delete(url, headers=headers)
  69. if response.status_code == 204:
  70. print(f"✅ Successfully deleted release {release_version} for package '{package.name}'.") # noqa: T201
  71. else:
  72. print( # noqa: T201
  73. f"❌ Failed to delete release {release_version} for package '{package.name}':"
  74. + f" {response.status_code} - {response.text}"
  75. )
  76. # Delete tag
  77. url = f"https://api.github.com/repos/{repository_path}/git/refs/tags/{release_tag}'"
  78. response = requests.delete(url, headers=headers)
  79. if response.status_code == 204:
  80. print(f"✅ Successfully deleted tag {release_tag}.") # noqa: T201
  81. else:
  82. print(f"❌ Failed to delete tag {release_tag}: {response.status_code} - {response.text}") # noqa: T201
  83. else:
  84. print("ℹ️ Skipped.") # noqa: T201
  85. if not found:
  86. print(f"No dev releases found for version {args.version}.") # noqa: T201
  87. if __name__ == "__main__":
  88. main()