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

[feat] Add AI features enabled resolvers #808

Merged
merged 21 commits into from
Sep 16, 2024

Conversation

rohitvinnakota-codecov
Copy link
Contributor

@rohitvinnakota-codecov rohitvinnakota-codecov commented Sep 9, 2024

We want to enable a new AI features tab on the UI, which exists at the org level. A Codecov user installs a separate AI features Github app on their org(on 1 or more repos).

Assuming webhooks are forwarded from the GH app, we should receive installation events in the api service.

The GithubAppInstallation model flow will be leveraged to create a new entry that references the app installation. This entry will have a reference to the owner that installed the app, and the app id of the AI features app. Note that ALL our app id entires in this table are the same(i.e. that of Codecov).

UI design here for folks who are interested: https://www.figma.com/design/hwKUPFyNRwGo5GGeWniHmb/GH-1661?node-id=2206-4584&t=vrlfUg43sgTLKuYv-4

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

@codecov-staging
Copy link

codecov-staging bot commented Sep 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codecov-qa
Copy link

codecov-qa bot commented Sep 9, 2024

❌ 2 Tests Failed:

Tests completed Failed Passed Skipped
2276 2 2274 6
View the top 2 failed tests by shortest run time
graphql_api.tests.test_owner.TestOwnerType test_ai_features_enabled
Stack Traces | 0.377s run time
self = <graphql_api.tests.test_owner.TestOwnerType testMethod=test_ai_features_enabled>
get_config_mock = <MagicMock name='get_config' id='139631388427136'>

    @patch("services.self_hosted.get_config")
    def test_ai_features_enabled(self, get_config_mock):
        current_org = OwnerFactory(
            username="random-plan-user",
            service="github",
        )
    
        get_config_mock.return_value = [
            {"service": "github", "ai_features_app_id": 12345},
        ]
    
        ai_app_installation = GithubAppInstallation(
            name="ai-features",
            owner=current_org,
            repository_service_ids=None,
            installation_id=12345,
        )
    
        ai_app_installation.save()
    
        query = """{
            owner(username: "%s") {
                isAiFeaturesEnabled
            }
        }
    
        """ % (current_org.username)
    
>       data = self.gql_request(query, owner=current_org)

graphql_api/tests/test_owner.py:880: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <graphql_api.tests.test_owner.TestOwnerType testMethod=test_ai_features_enabled>
query = '{\n            owner(username: "random-plan-user") {\n                isAiFeaturesEnabled\n            }\n        }\n\n        '
provider = 'gh', owner = <Owner: Owner<github/random-plan-user>>
variables = None, with_errors = False, okta_signed_in_accounts = []
impersonate_owner = False

    def gql_request(
        self,
        query,
        provider="gh",
        owner=None,
        variables=None,
        with_errors=False,
        okta_signed_in_accounts=[],
        impersonate_owner=False,
    ):
        url = f"/graphql/{provider}"
    
        if owner:
            self.client = Client()
    
            if impersonate_owner:
                staff_owner = OwnerFactory(
                    name="staff_user", service="github", user=UserFactory(is_staff=True)
                )
                self.client.cookies = SimpleCookie({"staff_user": owner.pk})
                self.client.force_login_owner(staff_owner)
            else:
                self.client.force_login_owner(owner)
    
            if okta_signed_in_accounts:
                session = self.client.session
                session[OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY] = okta_signed_in_accounts
                session.save()
    
        response = self.client.post(
            url,
            {"query": query, "variables": variables or {}},
            content_type="application/json",
        )
>       return response.json() if with_errors else response.json()["data"]
E       KeyError: 'data'

graphql_api/tests/helper.py:44: KeyError
graphql_api.tests.test_owner.TestOwnerType test_fetch_repos_ai_features_enabled
Stack Traces | 0.426s run time
self = <graphql_api.tests.test_owner.TestOwnerType testMethod=test_fetch_repos_ai_features_enabled>
get_config_mock = <MagicMock name='get_config' id='139631385181984'>

    @patch("services.self_hosted.get_config")
    def test_fetch_repos_ai_features_enabled(self, get_config_mock):
        current_org = OwnerFactory(
            username="random-plan-user",
            service="github",
        )
    
        get_config_mock.return_value = [
            {"service": "github", "ai_features_app_id": 12345},
        ]
    
        ai_app_installation = GithubAppInstallation(
            name="ai-features",
            owner=current_org,
            repository_service_ids=["repo-1"],
            installation_id=12345,
        )
    
        ai_app_installation.save()
    
        query = query_repositories % (
            self.owner.username,
            "(filters: { isAiFeaturesEnabled: true })",
            "",
        )
        data = self.gql_request(query, owner=current_org)
>       repos = paginate_connection(data["owner"]["repositories"])
E       TypeError: 'NoneType' object is not subscriptable

graphql_api/tests/test_owner.py:909: TypeError

To view individual test run time comparison to the main branch, go to the Test Analytics Dashboard

Copy link

codecov-public-qa bot commented Sep 9, 2024

Test Failures Detected: Due to failing tests, we cannot provide coverage reports at this time.

❌ Failed Test Results:

Completed 2282 tests with 2 failed, 2274 passed and 6 skipped.

View the full list of failed tests

pytest

  • Class name: graphql_api.tests.test_owner.TestOwnerType
    Test name: test_ai_features_enabled

    self = <graphql_api.tests.test_owner.TestOwnerType testMethod=test_ai_features_enabled>
    get_config_mock = <MagicMock name='get_config' id='139631388427136'>

    @patch("services.self_hosted.get_config")
    def test_ai_features_enabled(self, get_config_mock):
    current_org = OwnerFactory(
    username="random-plan-user",
    service="github",
    )

    get_config_mock.return_value = [
    {"service": "github", "ai_features_app_id": 12345},
    ]

    ai_app_installation = GithubAppInstallation(
    name="ai-features",
    owner=current_org,
    repository_service_ids=None,
    installation_id=12345,
    )

    ai_app_installation.save()

    query = """{
    owner(username: "%s") {
    isAiFeaturesEnabled
    }
    }

    """ % (current_org.username)

    > data = self.gql_request(query, owner=current_org)

    graphql_api/tests/test_owner.py:880:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    self = <graphql_api.tests.test_owner.TestOwnerType testMethod=test_ai_features_enabled>
    query = '{\n owner(username: "random-plan-user") {\n isAiFeaturesEnabled\n }\n }\n\n '
    provider = 'gh', owner = <Owner: Owner<github/random-plan-user>>
    variables = None, with_errors = False, okta_signed_in_accounts = []
    impersonate_owner = False

    def gql_request(
    self,
    query,
    provider="gh",
    owner=None,
    variables=None,
    with_errors=False,
    okta_signed_in_accounts=[],
    impersonate_owner=False,
    ):
    url = f"/graphql/{provider}"

    if owner:
    self.client = Client()

    if impersonate_owner:
    staff_owner = OwnerFactory(
    name="staff_user", service="github", user=UserFactory(is_staff=True)
    )
    self.client.cookies = SimpleCookie({"staff_user": owner.pk})
    self.client.force_login_owner(staff_owner)
    else:
    self.client.force_login_owner(owner)

    if okta_signed_in_accounts:
    session = self.client.session
    session[OKTA_SIGNED_IN_ACCOUNTS_SESSION_KEY] = okta_signed_in_accounts
    session.save()

    response = self.client.post(
    url,
    {"query": query, "variables": variables or {}},
    content_type="application/json",
    )
    > return response.json() if with_errors else response.json()["data"]
    E KeyError: 'data'

    graphql_api/tests/helper.py:44: KeyError
  • Class name: graphql_api.tests.test_owner.TestOwnerType
    Test name: test_fetch_repos_ai_features_enabled

    self = <graphql_api.tests.test_owner.TestOwnerType testMethod=test_fetch_repos_ai_features_enabled>
    get_config_mock = <MagicMock name='get_config' id='139631385181984'>

    @patch("services.self_hosted.get_config")
    def test_fetch_repos_ai_features_enabled(self, get_config_mock):
    current_org = OwnerFactory(
    username="random-plan-user",
    service="github",
    )

    get_config_mock.return_value = [
    {"service": "github", "ai_features_app_id": 12345},
    ]

    ai_app_installation = GithubAppInstallation(
    name="ai-features",
    owner=current_org,
    repository_service_ids=["repo-1"],
    installation_id=12345,
    )

    ai_app_installation.save()

    query = query_repositories % (
    self.owner.username,
    "(filters: { isAiFeaturesEnabled: true })",
    "",
    )
    data = self.gql_request(query, owner=current_org)
    > repos = paginate_connection(data["owner"]["repositories"])
    E TypeError: 'NoneType' object is not subscriptable

    graphql_api/tests/test_owner.py:909: TypeError

Copy link

codecov bot commented Sep 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.23%. Comparing base (52b7d30) to head (a546605).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##               main       #808   +/-   ##
===========================================
  Coverage   96.23000   96.23000           
===========================================
  Files           812        812           
  Lines         18547      18566   +19     
===========================================
+ Hits          17848      17867   +19     
  Misses          699        699           
Flag Coverage Δ
unit 92.46% <100.00%> (+<0.01%) ⬆️
unit-latest-uploader 92.46% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@rohitvinnakota-codecov rohitvinnakota-codecov changed the title [draft] Add AI features enabled resolvers and webhook handlers [feat] Add AI features enabled resolvers and webhook handlers Sep 10, 2024
@rohitvinnakota-codecov rohitvinnakota-codecov marked this pull request as ready for review September 10, 2024 16:32
@rohitvinnakota-codecov rohitvinnakota-codecov requested a review from a team as a code owner September 10, 2024 16:32
@rohitvinnakota-codecov
Copy link
Contributor Author

@codecov-ai-reviewer

Comment on lines 339 to 342

@owner_bindable.field("aiFeaturesEnabled")
@require_part_of_org
def resolve_ai_features_enabled(owner: Owner, info) -> bool | None:
Copy link

Choose a reason for hiding this comment

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

The get_config function call should be moved outside of the resolver function to avoid fetching the configuration value on each call. This will improve performance by reducing redundant calls to the configuration service.

Suggested change
@owner_bindable.field("aiFeaturesEnabled")
@require_part_of_org
def resolve_ai_features_enabled(owner: Owner, info) -> bool | None:
ai_features_gh_app_id = get_config("github", "ai_features_app_id")
@owner_bindable.field("aiFeaturesEnabled")
@sync_to_async
@require_part_of_org
def resolve_ai_features_enabled(owner: Owner, info) -> bool:
return GithubAppInstallation.objects.filter(
app_id=ai_features_gh_app_id, owner=owner
).exists()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Out of curiosity, what happens if I leave it as is?

@rohitvinnakota-codecov rohitvinnakota-codecov changed the title [feat] Add AI features enabled resolvers and webhook handlers [feat] Add AI features enabled resolvers Sep 16, 2024
@rohitvinnakota-codecov rohitvinnakota-codecov added this pull request to the merge queue Sep 16, 2024
Merged via the queue into main with commit bb6cc30 Sep 16, 2024
18 of 19 checks passed
@rohitvinnakota-codecov rohitvinnakota-codecov deleted the rvinnakota/add-ai-features-enabled branch September 16, 2024 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants