From b4d3310b50a3a0e33647687955906d5d76d372eb Mon Sep 17 00:00:00 2001 From: Novak Zaballa <41410593+novakzaballa@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:26:31 -0400 Subject: [PATCH] fix: Solve delete GitHub integration issue (#4622) --- api/integrations/github/client.py | 17 ++++++++++++++--- .../github/test_unit_github_views.py | 12 ++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/api/integrations/github/client.py b/api/integrations/github/client.py index caf6ca13deba..910fb85bf7eb 100644 --- a/api/integrations/github/client.py +++ b/api/integrations/github/client.py @@ -116,9 +116,20 @@ def post_comment_to_github( def delete_github_installation(installation_id: str) -> requests.Response: url = f"{GITHUB_API_URL}app/installations/{installation_id}" headers = build_request_headers(installation_id, use_jwt=True) - response = requests.delete(url, headers=headers, timeout=GITHUB_API_CALLS_TIMEOUT) - response.raise_for_status() - return response + try: + response = requests.delete( + url, headers=headers, timeout=GITHUB_API_CALLS_TIMEOUT + ) + response.raise_for_status() + return response + except HTTPError: + response_content = response.content.decode("utf-8") + error_data = json.loads(response_content) + if error_data.get("message") == "Not Found" and response.status_code == 404: + logger.info( + f"The GitHub application with the installation ID: {installation_id} was not found." + ) + return response def fetch_search_github_resource( diff --git a/api/tests/unit/integrations/github/test_unit_github_views.py b/api/tests/unit/integrations/github/test_unit_github_views.py index ede44b4b6ddf..1348b7929c83 100644 --- a/api/tests/unit/integrations/github/test_unit_github_views.py +++ b/api/tests/unit/integrations/github/test_unit_github_views.py @@ -189,7 +189,7 @@ def test_delete_github_configuration( @responses.activate -def test_cannot_delete_github_configuration_when_delete_github_installation_response_was_404( +def test_can_delete_github_configuration_when_delete_github_installation_response_was_404( admin_client_new: APIClient, organisation: Organisation, github_configuration: GithubConfiguration, @@ -213,18 +213,14 @@ def test_cannot_delete_github_configuration_when_delete_github_installation_resp method="DELETE", url=f"{GITHUB_API_URL}app/installations/{github_configuration.installation_id}", status=404, - json={"message": "not found"}, + json={"message": "Not Found", "status": "404"}, ) # When response = admin_client_new.delete(url) # Then - assert response.status_code == status.HTTP_502_BAD_GATEWAY - assert ( - response.json()["detail"] - == "Failed to delete GitHub Installation. Error: 404 Client Error: Not Found for url: https://api.github.com/app/installations/1234567" # noqa: E501 - ) - assert GithubConfiguration.objects.filter(id=github_configuration.id).exists() + assert response.status_code == status.HTTP_204_NO_CONTENT + assert not GithubConfiguration.objects.filter(id=github_configuration.id).exists() def test_get_github_repository(