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: Sync plan_activated_users during sync_teams #902

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

suejung-sentry
Copy link
Contributor

@suejung-sentry suejung-sentry commented Nov 18, 2024

sync_teams runs to take a user (e.g., suejung-sentry) and update its organizations.
When it does this, it should also remove from that organization (e.g., codecov)'s plan_activated_users, so the 2 places remain consistent.
That is, owners table row for owner suejung-sentry has owners.organizations=['codecov','other-org','other-org-2'] and owners table row for owner codecov has owners.plan_activated_users=['suejung-sentry','other-user','other-user-2'] and we need both those updated.

Special handling for "outside collaborators" - There may be "outside collaborators" that are not members of the org but may still be activated seats in codecov, so does keeping the 2 lists consistent make sense? Per here, I think we don't expressly support this feature and can employ a manual workaround in the infrequent cases a particular customer requires it.

sync_teams job is triggered from here

Here's another place it can be triggered by https://github.com/codecov/codecov-api/blob/354b64076170dd4115a4c83fbda63ba4478a23d7/services/refresh.py#L38
https://github.com/codecov/codecov-api/blob/354b64076170dd4115a4c83fbda63ba4478a23d7/graphql_api/types/mutation/sync_with_git_provider/sync_with_git_provider.py#L12 (graphql api resolve_sync_with_git_provider)
where it's called in gazebo
https://github.com/codecov/gazebo/blob/26d07c7dd517ae4a45751fa5b2d45785a264f98f/src/services/user/useResyncUser.ts#L33
Can't find your repo resync button (https://github.com/codecov/gazebo/blob/26d07c7dd517ae4a45751fa5b2d45785a264f98f/src/shared/ListRepo/OrgControlTable/RepoOrgNotFound/RepoOrgNotFound.tsx#L61)

Closes https://github.com/codecov/internal-issues/issues/1085

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@5e92d54). Learn more about missing BASE report.
Report is 7 commits behind head on main.

Current head 8774da8 differs from pull request most recent head a001d68

Please upload reports for the commit a001d68 to get more accurate results.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #902   +/-   ##
=======================================
  Coverage        ?   98.06%           
=======================================
  Files           ?      444           
  Lines           ?    35474           
  Branches        ?        0           
=======================================
  Hits            ?    34786           
  Misses          ?      688           
  Partials        ?        0           
Flag Coverage Δ
integration 41.93% <ø> (?)
unit 90.87% <ø> (?)

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.

⚠️ Impact Analysis from Codecov is deprecated and will be sunset on Jan 31 2025. See more

@codecov-notifications
Copy link

codecov-notifications bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tasks/sync_teams.py 60.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link

✅ All tests successful. No failed tests were found.

📣 Thoughts on this report? Let Codecov know! | Powered by Codecov

@codecov-qa
Copy link

codecov-qa bot commented Jan 29, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
1793 1 1792 9
View the top 1 failed tests by shortest run time
tasks/tests/unit/test_sync_teams_task.py::TestSyncTeamsTaskUnit::test_team_removed
Stack Traces | 0.048s run time
self = <worker.tasks.tests.unit.test_sync_teams_task.TestSyncTeamsTaskUnit object at 0x7f18e8d64180>
mocker = <pytest_mock.plugin.MockFixture object at 0x7f18d39b2f90>
mock_configuration = <shared.config.ConfigHelper object at 0x7f18d1f5bd70>
dbsession = <sqlalchemy.orm.session.Session object at 0x7f18d25dee40>
codecov_vcr = <vcr.cassette.Cassette object at 0x7f18d240b4d0>

    def test_team_removed(self, mocker, mock_configuration, dbsession, codecov_vcr):
        token = "testv2ztxs03zwys22v36ama292esl13swroe6dj"
        prev_team = OwnerFactory.create(service="github", username="Evil_Corp")
        dbsession.add(prev_team)
        user = OwnerFactory.create(
            organizations=[prev_team.ownerid],
            service="github",
            unencrypted_oauth_token=token,
        )
        prev_team.plan_activated_users = [user.ownerid]
        dbsession.add(user)
        dbsession.flush()
>       SyncTeamsTask().run_impl(dbsession, user.ownerid, using_integration=False)

.../tests/unit/test_sync_teams_task.py:43: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <@task: app.tasks.sync_teams.SyncTeams of tasks at 0x7f18e540f8c0>
db_session = <sqlalchemy.orm.session.Session object at 0x7f18d25dee40>
ownerid = 202, username = None, kwargs = {'using_integration': False}
owner = Owner<202@service<github>>, service = 'github'
git = <github slug=None ownerid=202 repoid=None>, teams = [], updated_teams = []

    def run_impl(self, db_session, ownerid, *, username=None, **kwargs):
        log.info("Sync teams", extra=dict(ownerid=ownerid, username=username))
        owner = db_session.query(Owner).filter(Owner.ownerid == ownerid).first()
    
        assert owner, "Owner not found"
        service = owner.service
    
        git = get_owner_provider_service(owner, ignore_installation=True)
    
        # get list of teams with username, name, email, id (service_id), etc
        teams = async_to_sync(git.list_teams)()
    
        updated_teams = []
    
        for team in teams:
            team_data = dict(
                username=team["username"],
                name=team["name"],
                email=team.get("email"),
                avatar_url=team.get("avatar_url"),
                parent_service_id=team.get("parent_id"),
            )
            team_ownerid = self.upsert_team(
                db_session, service, str(team["id"]), team_data
            )
            team_data["ownerid"] = team_ownerid
            updated_teams.append(team_data)
    
        team_ids = [team["ownerid"] for team in updated_teams]
    
        removed_orgs = set(owner.organizations or []) - set(team_ids)
        if removed_orgs:
            log.warning(
                "Owner had access to organization that are being removed",
                extra=dict(
                    old_orgs=owner.organizations,
                    new_orgs=team_ids,
                    removed_orgs=sorted(removed_orgs),
                    ownerid=ownerid,
                ),
            )
            for org in removed_orgs:
>               org.plan_activated_users.remove(ownerid)
E               AttributeError: 'NoneType' object has no attribute 'plan_activated_users'

tasks/sync_teams.py:62: AttributeError

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

Copy link

codecov-public-qa bot commented Jan 29, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
1793 1 1792 9
View the top 1 failed tests by shortest run time
tasks/tests/unit/test_sync_teams_task.py::TestSyncTeamsTaskUnit::test_team_removed
Stack Traces | 0.048s run time
self = <worker.tasks.tests.unit.test_sync_teams_task.TestSyncTeamsTaskUnit object at 0x7f18e8d64180>
mocker = <pytest_mock.plugin.MockFixture object at 0x7f18d39b2f90>
mock_configuration = <shared.config.ConfigHelper object at 0x7f18d1f5bd70>
dbsession = <sqlalchemy.orm.session.Session object at 0x7f18d25dee40>
codecov_vcr = <vcr.cassette.Cassette object at 0x7f18d240b4d0>

    def test_team_removed(self, mocker, mock_configuration, dbsession, codecov_vcr):
        token = "testv2ztxs03zwys22v36ama292esl13swroe6dj"
        prev_team = OwnerFactory.create(service="github", username="Evil_Corp")
        dbsession.add(prev_team)
        user = OwnerFactory.create(
            organizations=[prev_team.ownerid],
            service="github",
            unencrypted_oauth_token=token,
        )
        prev_team.plan_activated_users = [user.ownerid]
        dbsession.add(user)
        dbsession.flush()
>       SyncTeamsTask().run_impl(dbsession, user.ownerid, using_integration=False)

.../tests/unit/test_sync_teams_task.py:43: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <@task: app.tasks.sync_teams.SyncTeams of tasks at 0x7f18e540f8c0>
db_session = <sqlalchemy.orm.session.Session object at 0x7f18d25dee40>
ownerid = 202, username = None, kwargs = {'using_integration': False}
owner = Owner<202@service<github>>, service = 'github'
git = <github slug=None ownerid=202 repoid=None>, teams = [], updated_teams = []

    def run_impl(self, db_session, ownerid, *, username=None, **kwargs):
        log.info("Sync teams", extra=dict(ownerid=ownerid, username=username))
        owner = db_session.query(Owner).filter(Owner.ownerid == ownerid).first()
    
        assert owner, "Owner not found"
        service = owner.service
    
        git = get_owner_provider_service(owner, ignore_installation=True)
    
        # get list of teams with username, name, email, id (service_id), etc
        teams = async_to_sync(git.list_teams)()
    
        updated_teams = []
    
        for team in teams:
            team_data = dict(
                username=team["username"],
                name=team["name"],
                email=team.get("email"),
                avatar_url=team.get("avatar_url"),
                parent_service_id=team.get("parent_id"),
            )
            team_ownerid = self.upsert_team(
                db_session, service, str(team["id"]), team_data
            )
            team_data["ownerid"] = team_ownerid
            updated_teams.append(team_data)
    
        team_ids = [team["ownerid"] for team in updated_teams]
    
        removed_orgs = set(owner.organizations or []) - set(team_ids)
        if removed_orgs:
            log.warning(
                "Owner had access to organization that are being removed",
                extra=dict(
                    old_orgs=owner.organizations,
                    new_orgs=team_ids,
                    removed_orgs=sorted(removed_orgs),
                    ownerid=ownerid,
                ),
            )
            for org in removed_orgs:
>               org.plan_activated_users.remove(ownerid)
E               AttributeError: 'NoneType' object has no attribute 'plan_activated_users'

tasks/sync_teams.py:62: AttributeError

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

@suejung-sentry suejung-sentry marked this pull request as ready for review January 30, 2025 00:02
@suejung-sentry
Copy link
Contributor Author

suejung-sentry commented Jan 30, 2025

Actually it looks like this logic is handled upstream by the place this job is called
https://github.com/codecov/codecov-api/blob/ae83c2fa69b4d572b0096c54803d8068a8f13ebe/webhook_handlers/views/github.py#L611C13-L616C21

EDIT - see pr description - this is only 1 of many places this can be called

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.

1 participant