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

fix: GitHub repos unique constraint and delete #4037

Merged
merged 3 commits into from
May 28, 2024

Conversation

novakzaballa
Copy link
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

  • Fix soft-deleted issues with GitHub repos/project uniqueness.
  • Fix when removing a GitHub repo from integration delete only the resources of that repo.
  • Improve unit test.

How did you test this code?

  • Run the GitHub view, and feature external resources unit tests

Copy link

vercel bot commented May 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2024 8:04pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2024 8:04pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2024 8:04pm

Copy link
Contributor

github-actions bot commented May 28, 2024

Uffizzi Preview deployment-52353 was deleted.

Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.41%. Comparing base (50cc369) to head (525a7d8).
Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4037   +/-   ##
=======================================
  Coverage   96.41%   96.41%           
=======================================
  Files        1145     1146    +1     
  Lines       37295    37373   +78     
=======================================
+ Hits        35959    36035   +76     
- Misses       1336     1338    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@novakzaballa novakzaballa requested a review from zachaysan May 28, 2024 16:43
Copy link
Contributor

@zachaysan zachaysan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, but left a number of nits. There is one change I think you should address which is the url__contains bit since it would bleed data.

api/conftest.py Outdated
def post_request_mock(mocker: MockerFixture) -> MagicMock:
def mocked_request(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing typing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

api/conftest.py Outdated
@@ -85,6 +87,25 @@ def pytest_sessionstart(session: pytest.Session) -> None:
fix_issue_3869()


@pytest.fixture()
def post_request_mock(mocker: MockerFixture) -> MagicMock:
def mocked_request(*args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing typing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

api/conftest.py Outdated
def raise_for_status(self) -> None:
pass

def json(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing typing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -25,6 +26,21 @@
logger = logging.getLogger(__name__)


def handle_installation_deleted(payload: dict[str, typing.Any]) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Any has already been imported from typing so this should be like the below

Suggested change
def handle_installation_deleted(payload: dict[str, typing.Any]) -> None:
def handle_installation_deleted(payload: dict[str, Any]) -> None:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 30 to 33
installation_id = payload.get("installation", {}).get("id")
try:
GithubConfiguration.objects.get(installation_id=installation_id).delete()
except GithubConfiguration.DoesNotExist:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

installation_id could be None which may match the queryset if there are installation_id matching records with None in them. I would add an explicit check for installation_id is None and handle that case separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

GithubConfiguration.objects.get(installation_id=installation_id).delete()
except GithubConfiguration.DoesNotExist:
logger.error(
f"Github Configuration with installation_id {installation_id} does not exist"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
f"Github Configuration with installation_id {installation_id} does not exist"
f"GitHub Configuration with installation_id {installation_id} does not exist"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

],
# Filter by url containing the repository owner and name
url__contains=f"{self.repository_owner}/{self.repository_name}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The match criteria isn't good enough. If a repository owner is named foo it would also match to examplefoo. you need to bound the region of the url__contains so that you're always matching the right object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using regex instead of contains

@@ -639,6 +650,32 @@ def test_github_webhook_delete_installation(
assert not GithubConfiguration.objects.filter(installation_id=1234567).exists()


def test_github_webhook_with_non_existing_installation(
github_configuration: GithubConfiguration,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
github_configuration: GithubConfiguration,
github_configuration: GitHubConfiguration,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't be changed as it's the model name.

mocker_logger = mocker.patch("integrations.github.github.logger")

# When
client = APIClient()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use api_client fixture

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


# Then
mocker_logger.error.assert_called_once_with(
"Github Configuration with installation_id 765432 does not exist"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
"Github Configuration with installation_id 765432 does not exist"
"GitHub Configuration with installation_id 765432 does not exist"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@novakzaballa novakzaballa added this pull request to the merge queue May 28, 2024
Merged via the queue into main with commit 7454e4a May 28, 2024
24 checks passed
@novakzaballa novakzaballa deleted the fix/github-repos-unique-constraint-and-delete branch May 28, 2024 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants