diff --git a/tasks/libs/ciproviders/github_api.py b/tasks/libs/ciproviders/github_api.py index 54a3ba49a43f1..f5337fd1333ce 100644 --- a/tasks/libs/ciproviders/github_api.py +++ b/tasks/libs/ciproviders/github_api.py @@ -519,14 +519,37 @@ def get_token_from_app(app_id_env='GITHUB_APP_ID', pkey_env='GITHUB_KEY_B64'): auth_token = integration.get_access_token(install_id) print(auth_token.token) - def create_label(self, name, color, description=""): + def create_label(self, name, color, description="", exist_ok=False): """ Creates a label in the given GitHub repository. """ - return self._repository.create_label(name, color, description) - def create_milestone(self, title): - self._repository.create_milestone(title) + try: + return self._repository.create_label(name, color, description) + except GithubException as e: + if not ( + e.status == 422 + and len(e.data["errors"]) == 1 + and e.data["errors"][0]["code"] == "already_exists" + and exist_ok + ): + raise e + + def create_milestone(self, title, exist_ok=False): + """ + Creates a milestone in the given GitHub repository. + """ + + try: + return self._repository.create_milestone(title) + except GithubException as e: + if not ( + e.status == 422 + and len(e.data["errors"]) == 1 + and e.data["errors"][0]["code"] == "already_exists" + and exist_ok + ): + raise e def create_release(self, tag, message, draft=True): return self._repository.create_git_release( diff --git a/tasks/release.py b/tasks/release.py index c692d5c5a4ffe..ad2de76ecb58b 100644 --- a/tasks/release.py +++ b/tasks/release.py @@ -706,6 +706,7 @@ def create_release_branches(ctx, base_directory="~/dd", major_version: int = 7, f'backport/{release_branch}', BACKPORT_LABEL_COLOR, f'Automatically create a backport PR to {release_branch}', + exist_ok=True, ) # Step 2 - Create PRs with new settings in datadog-agent repository @@ -1207,7 +1208,6 @@ def update_current_milestone(ctx, major_version: int = 7, upstream="origin"): """ Create a PR to bump the current_milestone in the release.json file """ - import github gh = GithubAPI() @@ -1216,14 +1216,7 @@ def update_current_milestone(ctx, major_version: int = 7, upstream="origin"): next.devel = False print(f"Creating the {next} milestone...") - - try: - gh.create_milestone(str(next)) - except github.GithubException as e: - if e.status == 422: - print(f"Milestone {next} already exists") - else: - raise e + gh.create_milestone(str(next), exist_ok=True) with agent_context(ctx, get_default_branch(major=major_version)): milestone_branch = f"release_milestone-{int(time.time())}"