Skip to content
Open
112 changes: 56 additions & 56 deletions app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,72 @@


# admin.site.register(models.GithubAppAuth)
admin.site.register(models.GithubUser)
# admin.site.register(models.GithubUser)


class GithubRepoMapAdmin(admin.TabularInline):
model = models.GithubRepoMap
# class GithubRepoMapAdmin(admin.TabularInline):
# model = models.GithubRepoMap

def formfield_for_foreignkey(self, db_field, request, **kwargs):
print(db_field.name)
# print(request)
# print(kwargs)
if db_field.name in ["code_repo", "documentation_repo"]:
github_app_installation_id = request.resolver_match.kwargs.get("object_id")
kwargs["queryset"] = models.GithubRepository.objects.filter(
owner_id=github_app_installation_id
)
return super(GithubRepoMapAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs
)
# def formfield_for_foreignkey(self, db_field, request, **kwargs):
# print(db_field.name)
# # print(request)
# # print(kwargs)
# if db_field.name in ["code_repo", "documentation_repo"]:
# github_app_installation_id = request.resolver_match.kwargs.get("object_id")
# kwargs["queryset"] = models.GithubRepository.objects.filter(
# owner_id=github_app_installation_id
# )
# return super(GithubRepoMapAdmin, self).formfield_for_foreignkey(
# db_field, request, **kwargs
# )

# def get_queryset(self, request):
# qs = super(GithubRepoMapAdmin, self).get_queryset(request)
# return qs.filter(=request.user)
# # def get_queryset(self, request):
# # qs = super(GithubRepoMapAdmin, self).get_queryset(request)
# # return qs.filter(=request.user)


class GithubAppInstallationForm(forms.ModelForm):
class Meta:
model = models.GithubAppInstallation
exclude = ["account_id"]
# class GithubAppInstallationForm(forms.ModelForm):
# class Meta:
# model = models.GithubAppInstallation
# exclude = ["account_id"]


class GithubAppInstallationAdmin(admin.ModelAdmin):
list_display = (
"account_name",
"created_at",
)
readonly_fields = (
"account_name",
"installation_id",
"account_type",
"state",
"creator",
)
# class GithubAppInstallationAdmin(admin.ModelAdmin):
# list_display = (
# "account_name",
# "created_at",
# )
# readonly_fields = (
# "account_name",
# "installation_id",
# "account_type",
# "state",
# "creator",
# )

form = GithubAppInstallationForm
inlines = [
GithubRepoMapAdmin,
]
# form = GithubAppInstallationForm
# inlines = [
# GithubRepoMapAdmin,
# ]

def get_queryset(self, request):
qs = super(GithubAppInstallationAdmin, self).get_queryset(request)
if request.user.is_superuser:
return qs
else:
return qs.filter(creator=request.user.github_user)
# def get_queryset(self, request):
# qs = super(GithubAppInstallationAdmin, self).get_queryset(request)
# if request.user.is_superuser:
# return qs
# else:
# return qs.filter(creator=request.user.github_user)

def get_form(self, request, obj=None, **kwargs):
form = super(GithubAppInstallationAdmin, self).get_form(request, obj, **kwargs)
# form.base_fields['theme'].queryset = Theme.objects.filter(name__iexact='company')
return form
# def get_form(self, request, obj=None, **kwargs):
# form = super(GithubAppInstallationAdmin, self).get_form(request, obj, **kwargs)
# # form.base_fields['theme'].queryset = Theme.objects.filter(name__iexact='company')
# return form


admin.site.register(models.GithubAppInstallation, GithubAppInstallationAdmin)
admin.site.register(models.GithubUserAccessToken)
admin.site.register(models.GithubUserRefreshToken)
admin.site.register(models.GithubInstallationToken)
admin.site.register(models.GithubPullRequest)
admin.site.register(models.MonitoredPullRequest)
admin.site.register(models.GithubCheckRun)
# admin.site.register(models.GithubRepoMap)
# admin.site.register(models.GithubAppInstallation, GithubAppInstallationAdmin)
# admin.site.register(models.GithubUserAccessToken)
# admin.site.register(models.GithubUserRefreshToken)
# admin.site.register(models.GithubInstallationToken)
# admin.site.register(models.GithubPullRequest)
# admin.site.register(models.MonitoredPullRequest)
# admin.site.register(models.GithubCheckRun)
# # admin.site.register(models.GithubRepoMap)
3 changes: 1 addition & 2 deletions app/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ class AppConfig(AppConfig):
name = "app"

def ready(self):
# import app.jobs # noqa
pass
import app.signals # noqa
50 changes: 50 additions & 0 deletions app/github_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging
import django

import django_rq
import github
from django.conf import settings
from django_rq import job

from app import models as app_models
from allauth.socialaccount import models as social_models

from app import lib as app_lib

logger = logging.getLogger(__name__)


@job
def sync_repositories_for_installation(social_account: social_models.SocialAccount):
github_instance = github.Github(social_account.socialtoken_set.last())
existing_mapping = list(
app_models.UserRepoAccess.objects.filter(
social_account=social_account
).values_list("id", flat=True)
)
# for project in gitlab_instance.projects.list(all=True, membership=True):
# (repo_instance, _) = app_models.Repository.objects.update_or_create(
# repo_id=project.id,
# app=social_models.SocialApp.objects.get(provider="gitlab"),
# defaults={
# "repo_name": project.name.replace(" ", ""),
# "repo_full_name": project.path_with_namespace,
# },
# )
# project_perms = project._attrs["permissions"]
# user_access_level = max(
# (project_perms.get("project_access") or {}).get("access_level", 0),
# (project_perms.get("group_access") or {}).get("access_level", 0),
# )

# (instance, is_new) = app_models.UserRepoAccess.objects.update_or_create(
# repository=repo_instance,
# social_account=social_account,
# defaults={"access": user_access_level},
# )
if True:
if is_new is False:
existing_mapping.pop(existing_mapping.index(instance.id))
app_models.UserRepoAccess.objects.filter(id__in=existing_mapping).update(
access=app_models.UserRepoAccess.AccessLevel.NO_ACCESS
)
Loading