Skip to content

New UI import and basic UI integration #9

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

Merged
merged 10 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
release: python manage.py migrate
web: gunicorn cdoc.wsgi
17 changes: 9 additions & 8 deletions app/migrations/0002_auto_20220329_1319.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@


def update_permissions(schema, group):
call_command('update_permissions')
call_command("update_permissions")


def apply_migration(apps, schema_editor):
# Add, change, delete, view
Group = apps.get_model('auth', 'Group')
Permission = apps.get_model('auth', 'Permission')
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
required_permissions = [
"view_githubappinstallation",
"change_githubappinstallation",
Expand All @@ -39,12 +39,13 @@ def revert_migration(*args, **kwargs):
class Migration(migrations.Migration):

dependencies = [
('app', '0001_initial'),
('auth', "0012_alter_user_first_name_max_length"),
("app", "0001_initial"),
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [
migrations.RunPython(update_permissions,
reverse_code=migrations.RunPython.noop),
migrations.RunPython(apply_migration, revert_migration)
migrations.RunPython(
update_permissions, reverse_code=migrations.RunPython.noop
),
migrations.RunPython(apply_migration, revert_migration),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.0.3 on 2022-04-10 11:13

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0011_githubpullrequest_pr_owner_username'),
]

operations = [
migrations.AlterField(
model_name='githubpullrequest',
name='pr_closed_at',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AlterField(
model_name='githubpullrequest',
name='pr_head_modified_on',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AlterField(
model_name='githubpullrequest',
name='pr_merged_at',
field=models.DateTimeField(blank=True, null=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Generated by Django 4.0.3 on 2022-04-12 06:44

from django.db import migrations, models
import django.db.models.deletion


def migrate_admin_users(apps, schema_editor):
# Add, change, delete, view
GithubAppUser = apps.get_model("app", "GithubAppUser")
GithubAppInstallation = apps.get_model("app", "GithubAppInstallation")
for gai_instance in GithubAppInstallation.objects.all():
GithubAppUser.objects.update_or_create(
github_user=gai_instance.creator, installation=gai_instance
)


class Migration(migrations.Migration):

dependencies = [
("app", "0012_alter_githubpullrequest_pr_closed_at_and_more"),
]

operations = [
migrations.AlterField(
model_name="monitoredpullrequest",
name="pull_request_status",
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,
),
),
migrations.CreateModel(
name="GithubAppUser",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"github_user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="app.githubuser"
),
),
(
"installation",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="app.githubappinstallation",
),
),
],
options={
"unique_together": {("installation", "github_user")},
},
),
migrations.RunPython(migrate_admin_users, migrations.RunPython.noop),
]
61 changes: 61 additions & 0 deletions app/migrations/0014_monitoredpullrequest_integration_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 4.0.3 on 2022-04-12 07:15

from django.db import migrations, models
import django.db.models.deletion


def migrate_pr_integrations(apps, schema_editor):
# Add, change, delete, view
# Group = apps.get_model("auth", "Group")
MonitoredPullRequest = apps.get_model("app", "MonitoredPullRequest")
# GithubAppInstallation = apps.get_model("app", "GithubAppInstallation")
GithubRepoMap = apps.get_model("app", "GithubRepoMap")
for monitored_pr_instance in MonitoredPullRequest.objects.all():
code_pull_request = monitored_pr_instance.code_pull_request
# documentation_pull_request = monitored_pr_instance.documentation_pull_request
grm = GithubRepoMap.objects.filter(
code_repo=code_pull_request.repository,
# documentation_repo=documentation_pull_request.repository,
).last()
if grm is None:
assert False, "Exception: grm is None"
monitored_pr_instance.integration = grm.integration
monitored_pr_instance.save()


class Migration(migrations.Migration):

dependencies = [
("app", "0013_alter_monitoredpullrequest_pull_request_status_and_more"),
]

operations = [
migrations.AddField(
model_name="monitoredpullrequest",
name="integration",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="app.githubappinstallation",
),
preserve_default=False,
),
migrations.AlterUniqueTogether(
name="githubrepomap",
unique_together={("integration", "code_repo", "documentation_repo")},
),
migrations.AlterUniqueTogether(
name="monitoredpullrequest",
unique_together={("code_pull_request", "documentation_pull_request")},
),
migrations.RunPython(migrate_pr_integrations, migrations.RunPython.noop),
migrations.AlterField(
model_name="monitoredpullrequest",
name="integration",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="app.githubappinstallation",
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 4.0.3 on 2022-04-12 08:37

import datetime
from django.db import migrations, models
import django.utils.timezone
from django.utils.timezone import utc
import uuid


class Migration(migrations.Migration):

dependencies = [
('app', '0014_monitoredpullrequest_integration_and_more'),
]

operations = [
migrations.CreateModel(
name='GithubLoginState',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('state', models.UUIDField(default=uuid.uuid4)),
('redirect_url', models.URLField()),
('created_at', models.DateTimeField(auto_now_add=True)),
],
),
migrations.AddField(
model_name='githubappuser',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='githubrepomap',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='githubrepository',
name='created_at',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
]
44 changes: 42 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ def update_token(self):
)[1]


class GithubAppUser(models.Model):
installation = models.ForeignKey(GithubAppInstallation, on_delete=models.CASCADE)
github_user = models.ForeignKey(GithubUser, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
unique_together = ("installation", "github_user")

def __str__(self) -> str:
return f"{self.github_user.account_name}[{self.installation.installation_id}]"
return super().__str__()


class GithubInstallationToken(Token):
github_app_installation = models.ForeignKey(
GithubAppInstallation, on_delete=models.CASCADE, related_name="tokens"
Expand All @@ -172,6 +185,7 @@ class GithubRepository(models.Model):
repo_name = models.CharField(max_length=150)
repo_full_name = models.CharField(max_length=200, unique=True)
owner = models.ForeignKey(GithubAppInstallation, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self) -> str:
return self.repo_full_name
Expand All @@ -195,6 +209,10 @@ class IntegrationType(models.TextChoices):
GithubRepository, on_delete=models.PROTECT, related_name="documentation_repos"
)
integration_type = models.CharField(max_length=20, choices=IntegrationType.choices)
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
unique_together = ("integration", "code_repo", "documentation_repo")


class GithubPullRequest(models.Model):
Expand Down Expand Up @@ -239,6 +257,7 @@ class PullRequestStatus(models.TextChoices):
STALE_CODE = "STALE_CODE", "Stale Code"
STALE_APPROVAL = "STALE_APPROVAL", "Stale Approval"
APPROVED = "APPROVED", "Approved"
MANUAL_APPROVAL = "MANUALLY_APPROVED", "Manual Approval"

code_pull_request = models.OneToOneField(
GithubPullRequest,
Expand All @@ -257,6 +276,10 @@ class PullRequestStatus(models.TextChoices):
choices=PullRequestStatus.choices,
default=PullRequestStatus.NOT_CONNECTED,
)
integration = models.ForeignKey(GithubAppInstallation, on_delete=models.CASCADE)

class Meta:
unique_together = ("code_pull_request", "documentation_pull_request")

def __str__(self) -> str:
return f"{self.code_pull_request.repository.repo_full_name}[{self.code_pull_request.pr_number}]"
Expand All @@ -270,15 +293,18 @@ def save(self, *args, **kwargs):
self.pull_request_status = (
MonitoredPullRequest.PullRequestStatus.APPROVAL_PENDING
)
elif self.documentation_pull_request is None:
elif (
self.documentation_pull_request is None
and self.pull_request_status
!= MonitoredPullRequest.PullRequestStatus.MANUAL_APPROVAL
):
self.pull_request_status = (
MonitoredPullRequest.PullRequestStatus.NOT_CONNECTED
)
super().save(*args, **kwargs)

def get_display_name(self):
return f"{self.code_pull_request.repository.repo_full_name}/#{self.code_pull_request.pr_number}: {self.code_pull_request.pr_title}"
pass


class GithubCheckRun(models.Model):
Expand Down Expand Up @@ -326,3 +352,17 @@ def save(self, *args, **kwargs):
)
self.run_id = check_run_instance.id
super().save(*args, **kwargs)


class GithubLoginState(models.Model):
"""
This model is used to store the state of the login process. Along with it,
it also stores the redirect URL for the state.
"""

state = models.UUIDField(default=uuid.uuid4)
redirect_url = models.URLField()
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self) -> str:
return self.state.__str__()
10 changes: 5 additions & 5 deletions app/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def synchronize_github_check(sender, instance, **kwargs):
"external_id": instance.unique_id.__str__(),
"status": "completed",
"conclusion": "action_required",
"details_url": f"{settings.WEBSITE_HOST}/{github_repo.full_name}/pull/{instance.ref_pull_request.code_pull_request.pr_number}",
"details_url": f"{settings.WEBSITE_HOST}/{github_repo.full_name}/pulls/",
}
if (
instance.ref_pull_request.pull_request_status
Expand All @@ -134,10 +134,10 @@ def synchronize_github_check(sender, instance, **kwargs):
},
}
)
elif (
instance.ref_pull_request.pull_request_status
== app_models.MonitoredPullRequest.PullRequestStatus.APPROVED
):
elif instance.ref_pull_request.pull_request_status in [
app_models.MonitoredPullRequest.PullRequestStatus.APPROVED,
app_models.MonitoredPullRequest.PullRequestStatus.MANUAL_APPROVAL,
]:
data.update(
{
"conclusion": "success",
Expand Down
3 changes: 3 additions & 0 deletions app/templates/all_installations.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for installation in all_installations %}
<a href="/{{installation.account_name}}/repos/">{{installation.account_name}}</a>
{% endfor %}
3 changes: 3 additions & 0 deletions app/templates/all_repos.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for repo in all_repos %}
<a href="/{{repo.repo_full_name}}/pulls">{{repo.repo_full_name}}</a>
{% endfor %}
Loading