Skip to content

Migrate GitHub OAuth App to GitHub App #11942

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

Closed
wants to merge 111 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
926f95d
Migrate GitHub OAuth App to GitHub App
stsewd Jan 22, 2025
9c5746c
Merge branch 'main' into migrate-to-gh-apps
stsewd Jan 28, 2025
1314a9c
wip
stsewd Jan 30, 2025
22de5d6
Migration and stuff
stsewd Feb 3, 2025
5119e48
Fixes
stsewd Feb 3, 2025
a854c7e
More hooks!
stsewd Feb 4, 2025
7235cd8
Bug fixes and refactor
stsewd Feb 4, 2025
632e2e5
Format
stsewd Feb 4, 2025
b0e2181
More events
stsewd Feb 4, 2025
ba47293
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 4, 2025
91d23aa
More hooks and fixes
stsewd Feb 5, 2025
ddadee1
Implement members hook
stsewd Feb 5, 2025
2c4c0cc
Dead code
stsewd Feb 5, 2025
3c61db6
Delete code
stsewd Feb 5, 2025
88b0021
Poor man implementation
stsewd Feb 6, 2025
2987cf8
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 6, 2025
ad5655e
Stash
stsewd Feb 6, 2025
fa53c1c
Git service: depend on the project instead of users
stsewd Feb 7, 2025
f031b4b
Fix name
stsewd Feb 7, 2025
f7a3917
Fix tests
stsewd Feb 10, 2025
083cfa0
Small updates
stsewd Feb 10, 2025
8e6caa3
Format
stsewd Feb 10, 2025
4671a46
Merge branch 'refactor-services' into migrate-to-gh-apps
stsewd Feb 10, 2025
6d686f3
Format
stsewd Feb 10, 2025
d771092
Refactor
stsewd Feb 10, 2025
0d0511c
Fix import
stsewd Feb 10, 2025
4308bbf
Check for None
stsewd Feb 10, 2025
7065003
refactor
stsewd Feb 10, 2025
b2a8a2b
Fix import
stsewd Feb 10, 2025
b0d9dcc
Full feature!
stsewd Feb 11, 2025
54e7ec6
More refactor
stsewd Feb 11, 2025
4d5a229
More fixes and updates
stsewd Feb 11, 2025
12aab79
Docstrings and updates
stsewd Feb 11, 2025
c2ffe7d
Note on GH user access tokens
stsewd Feb 11, 2025
56b024f
Remove code
stsewd Feb 11, 2025
c09186b
Error handling and logging
stsewd Feb 12, 2025
0b2c686
Validate when trying to connect to an existing account
stsewd Feb 12, 2025
a7f1cb7
Less indentation
stsewd Feb 12, 2025
50637b5
And both apps lived happy forever after.
stsewd Feb 12, 2025
7fc1466
Updates from review
stsewd Feb 12, 2025
27ad8c6
Skip incompatible integrations
stsewd Feb 12, 2025
6ea1655
Format
stsewd Feb 12, 2025
5daf4da
Check for attribute instead
stsewd Feb 13, 2025
8e73ab4
Fix querysets
stsewd Feb 13, 2025
1289298
Git service: attach each service to a allauth provider
stsewd Feb 13, 2025
5319608
Missed this file
stsewd Feb 13, 2025
c9eb355
Use just the hostname for the base_api_url
stsewd Feb 13, 2025
c7b259b
Merge branch 'abstract-allauth-from-services' into migrate-to-gh-apps
stsewd Feb 13, 2025
e031477
Stash
stsewd Feb 14, 2025
bf5014c
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 14, 2025
6165e4c
Format
stsewd Feb 14, 2025
37738df
stash
stsewd Feb 17, 2025
160d41f
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 17, 2025
cd5015c
Basic migration page
stsewd Feb 18, 2025
f67e028
Don't use the ID to get the organization
stsewd Feb 18, 2025
933658c
Docstring
stsewd Feb 18, 2025
346cc34
Fix template
stsewd Feb 18, 2025
cac9567
Cleanup
stsewd Feb 18, 2025
db1950f
More updates
stsewd Feb 18, 2025
75cc864
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 19, 2025
87dad06
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 19, 2025
4b84536
Check for githuapp
stsewd Feb 19, 2025
3899104
Uninstalling the app from a repository, means unlinking it from the p…
stsewd Feb 19, 2025
acfda4c
Format
stsewd Feb 19, 2025
d1e3cb4
Add a sync repositories permissions method
stsewd Feb 20, 2025
de06013
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 24, 2025
b3ad1a2
Add tests for webhook
stsewd Feb 25, 2025
3fb1083
Stash
stsewd Feb 26, 2025
34052b3
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 26, 2025
c5ea8a1
Merge branch 'main' into migrate-to-gh-apps
stsewd Feb 26, 2025
eacf058
Tests
stsewd Feb 27, 2025
33d6151
Merge branch 'main' into migrate-to-gh-apps
stsewd Mar 10, 2025
619858c
Tests
stsewd Mar 10, 2025
80e6c88
More tests
stsewd Mar 10, 2025
926ed6d
Tests for tasks
stsewd Mar 10, 2025
0c338ab
stash
stsewd Mar 11, 2025
616166e
Format to avoid conflicts
stsewd Mar 11, 2025
2c81a2a
Merge branch 'main' into migrate-to-gh-apps
stsewd Mar 11, 2025
b58c3b8
Format
stsewd Mar 11, 2025
d106277
Update migration
stsewd Mar 11, 2025
c2a89d7
Remove suspended installations
stsewd Mar 11, 2025
7fffcea
Delete installation when suspended
stsewd Mar 11, 2025
0adc4b4
Handle authorization event
stsewd Mar 11, 2025
7e3f3f4
Tests
stsewd Mar 12, 2025
5c1ce0b
Merge branch 'main' into migrate-to-gh-apps
stsewd Mar 12, 2025
fa1b70c
Small changes
stsewd Mar 12, 2025
339cf73
Complete migrate view
stsewd Mar 18, 2025
7ff1e96
Create notifications when an error happens during the migration
stsewd Mar 18, 2025
9b14e95
Fixes
stsewd Mar 18, 2025
704ecb4
Check for users with multiple GH accounts
stsewd Mar 19, 2025
19dc240
Better default for accounts linked to multiple GH accounts
stsewd Mar 19, 2025
5a5d532
Rollback changes to avoid merge conflicts
stsewd Mar 20, 2025
26ef7eb
Re-add pygithub
stsewd Mar 20, 2025
34ddb25
Merge branch 'main' into migrate-to-gh-apps
stsewd Mar 20, 2025
33893de
Clean up and docstrings
stsewd Mar 20, 2025
5964ff1
Merge branch 'main' into migrate-to-gh-apps
stsewd Mar 20, 2025
8f23fa4
Docs about local dev
stsewd Mar 24, 2025
192f3ac
Add tests for migration page
stsewd Mar 25, 2025
66fdd95
Merge branch 'main' into migrate-to-gh-apps
stsewd Mar 25, 2025
08f9d2c
Fix merge conflict
stsewd Mar 25, 2025
ce6c2da
Match changes
stsewd Mar 25, 2025
c489137
Decouple migration from current GH account
stsewd Mar 27, 2025
3a88a62
More decoupling
stsewd Mar 27, 2025
928984b
Very WIP docs
stsewd Mar 27, 2025
6227ea3
Update docs
stsewd Mar 31, 2025
c962205
Add old_github_accounts so we can use it as a listing
stsewd Apr 14, 2025
6c5eefb
Add type
stsewd Apr 14, 2025
60a5228
Merge branch 'main' into migrate-to-gh-apps
stsewd Apr 15, 2025
f2d5498
Fix merge conflicts
stsewd Apr 15, 2025
34d7ed6
Format
stsewd Apr 15, 2025
225c640
Fix reference
stsewd Apr 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
10 changes: 10 additions & 0 deletions readthedocs/allauth/providers/githubapp/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from allauth.socialaccount.providers.github.provider import GitHubProvider
from readthedocs.allauth.providers.githubapp.views import GitHubAppOAuth2Adapter


class GitHubAppProvider(GitHubProvider):
id = "githubapp"
name = "GitHub App"
oauth2_adapter_class = GitHubAppOAuth2Adapter

provider_classes = [GitHubAppProvider]
6 changes: 6 additions & 0 deletions readthedocs/allauth/providers/githubapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns

from readthedocs.allauth.providers.githubapp.provider import GitHubAppProvider


urlpatterns = default_urlpatterns(GitHubAppProvider)
13 changes: 13 additions & 0 deletions readthedocs/allauth/providers/githubapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter

from allauth.socialaccount.providers.oauth2.views import (
OAuth2CallbackView,
OAuth2LoginView,
)

class GitHubAppOAuth2Adapter(GitHubOAuth2Adapter):
provider_id = 'githubapp'


oauth2_login = OAuth2LoginView.adapter_view(GitHubAppOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(GitHubAppOAuth2Adapter)
18 changes: 18 additions & 0 deletions readthedocs/core/adapters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Allauth overrides."""

from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.socialaccount.models import SocialAccount
from allauth.socialaccount.providers.github.provider import GitHubProvider

import structlog
from allauth.account.adapter import DefaultAccountAdapter
from django.utils.encoding import force_str

from readthedocs.allauth.providers.githubapp.provider import GitHubAppProvider
from readthedocs.core.utils import send_email_from_object
from readthedocs.invitations.models import Invitation

Expand Down Expand Up @@ -50,3 +55,16 @@ def save_user(self, request, user, form, commit=True):
invitation.delete()
else:
log.info("Invitation not found", invitation_pk=invitation_pk)


class SocialAccountAdapter(DefaultSocialAccountAdapter):

def pre_social_login(self, request, sociallogin):
provider = sociallogin.account.get_provider()
if provider.id == GitHubAppProvider.id and not sociallogin.is_existing:
social_ccount = SocialAccount.objects.filter(
provider=GitHubProvider.id,
uid=sociallogin.account.uid,
).first()
if social_ccount:
sociallogin.connect(request, social_ccount.user)
20 changes: 13 additions & 7 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

import os
import re
import subprocess
import socket
import subprocess

import structlog

from celery.schedules import crontab
from corsheaders.defaults import default_headers
from django.conf.global_settings import PASSWORD_HASHERS

from readthedocs.builds import constants_docker
from readthedocs.core.logs import shared_processors
from corsheaders.defaults import default_headers
from readthedocs.core.settings import Settings
from readthedocs.builds import constants_docker

from django.conf.global_settings import PASSWORD_HASHERS

try:
import readthedocsext.cdn # noqa
Expand Down Expand Up @@ -73,7 +71,7 @@ def _show_debug_toolbar(request):
# It's a "known issue/bug" and there is no solution as far as we can tell.
"debug_toolbar.panels.sql.SQLPanel",
"debug_toolbar.panels.templates.TemplatesPanel",
]
],
}

@property
Expand Down Expand Up @@ -294,6 +292,7 @@ def INSTALLED_APPS(self): # noqa
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.github",
"readthedocs.allauth.providers.githubapp",
"allauth.socialaccount.providers.gitlab",
"allauth.socialaccount.providers.bitbucket_oauth2",
"allauth.mfa",
Expand Down Expand Up @@ -679,6 +678,7 @@ def DOCKER_LIMITS(self):

# Allauth
ACCOUNT_ADAPTER = "readthedocs.core.adapters.AccountAdapter"
SOCIALACCOUNT_ADAPTER = 'readthedocs.core.adapters.SocialAccountAdapter'
ACCOUNT_EMAIL_REQUIRED = True
# By preventing enumeration, we will always send an email,
# even if the email is not registered, that's hurting
Expand Down Expand Up @@ -709,6 +709,12 @@ def DOCKER_LIMITS(self):
"repo:status",
],
},
"githubapp": {
"APPS": [
{"client_id": "123", "secret": "456", "key": ""},
],
"SCOPE": [],
},
"gitlab": {
"APPS": [
{"client_id": "123", "secret": "456", "key": ""},
Expand Down