Skip to content

Commit 2fb357e

Browse files
author
Shobhit
committed
Github oauth user
1 parent 45809ad commit 2fb357e

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 4.0.3 on 2022-04-12 06:44
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('app', '0012_alter_githubpullrequest_pr_closed_at_and_more'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='monitoredpullrequest',
16+
name='pull_request_status',
17+
field=models.CharField(choices=[('NOT_CONNECTED', 'Not Connected'), ('APPROVAL_PENDING', 'Approval Pending'), ('STALE_CODE', 'Stale Code'), ('STALE_APPROVAL', 'Stale Approval'), ('APPROVED', 'Approved'), ('MANUALLY_APPROVED', 'Manual Approval')], default='NOT_CONNECTED', max_length=20),
18+
),
19+
migrations.CreateModel(
20+
name='GithubAppUser',
21+
fields=[
22+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
23+
('github_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.githubuser')),
24+
('installation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.githubappinstallation')),
25+
],
26+
options={
27+
'unique_together': {('installation', 'github_user')},
28+
},
29+
),
30+
]

app/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ def update_token(self):
161161
)[1]
162162

163163

164+
class GithubAppUser(models.Model):
165+
installation = models.ForeignKey(GithubAppInstallation, on_delete=models.CASCADE)
166+
github_user = models.ForeignKey(GithubUser, on_delete=models.CASCADE)
167+
168+
class Meta:
169+
unique_together = ("installation", "github_user")
170+
171+
164172
class GithubInstallationToken(Token):
165173
github_app_installation = models.ForeignKey(
166174
GithubAppInstallation, on_delete=models.CASCADE, related_name="tokens"

app/signals.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ def synchronize_github_check(sender, instance, **kwargs):
134134
},
135135
}
136136
)
137-
elif (
138-
instance.ref_pull_request.pull_request_status
139-
== app_models.MonitoredPullRequest.PullRequestStatus.APPROVED
140-
):
137+
elif instance.ref_pull_request.pull_request_status in [
138+
app_models.MonitoredPullRequest.PullRequestStatus.APPROVED,
139+
app_models.MonitoredPullRequest.PullRequestStatus.MANUAL_APPROVAL,
140+
]:
141141
data.update(
142142
{
143143
"conclusion": "success",

app/views.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from django.conf import settings
1313
from django.contrib.auth import get_user_model, login
1414
from django.db import transaction
15-
from django.http import HttpResponseRedirect, JsonResponse
15+
from django.http import Http404, HttpResponseRedirect, JsonResponse
1616
from django.shortcuts import render
1717
from django.utils import timezone
1818
from django.utils.decorators import method_decorator
@@ -23,6 +23,7 @@
2323
from django.views.generic import FormView, TemplateView
2424
from django.contrib.auth import views as auth_views
2525
from requests.models import PreparedRequest
26+
from django.shortcuts import get_object_or_404
2627

2728
from . import models as app_models
2829
from . import forms as app_forms
@@ -144,6 +145,18 @@ def get(self, request, *args, **kwargs):
144145
)
145146
# installation_instance.save()
146147
installation_instance.update_token()
148+
for installation in user_instance.get_installations():
149+
if installation.app_id == settings.GITHUB_CREDS["app_id"]:
150+
installation_instance = (
151+
app_models.GithubAppInstallation.objects.get(
152+
account_id=installation.target_id,
153+
installation_id=installation.id,
154+
)
155+
)
156+
app_models.GithubAppUser.objects.update_or_create(
157+
github_user=github_user,
158+
installation=installation_instance,
159+
)
147160
else:
148161
logger.error(resp.text)
149162
return HttpResponseRedirect("/admin/")
@@ -257,6 +270,21 @@ def get(self, request):
257270
class AllPRView(TemplateView):
258271
template_name = "index.html"
259272

273+
def get(self, request, *args: Any, **kwargs: Any):
274+
github_user = request.user.github_user
275+
context = self.get_context_data(**kwargs)
276+
277+
if github_user is None:
278+
return Http404("User not found")
279+
# elif app_models.GithubAppUser.objects.filter():
280+
# pass
281+
get_object_or_404(
282+
app_models.GithubAppUser,
283+
github_user=github_user,
284+
installation=context["repo_mapping"].integration,
285+
)
286+
return self.render_to_response(context)
287+
260288
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
261289
context = super().get_context_data(**kwargs)
262290
matches = self.request.resolver_match.kwargs

0 commit comments

Comments
 (0)