Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
refactor: rework of gitlab envs
Browse files Browse the repository at this point in the history
  • Loading branch information
gjuro87 committed May 9, 2021
1 parent cbd123d commit fe77b12
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 164 deletions.
1 change: 1 addition & 0 deletions test/repos/tasks/test_sync_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, id=None, name=None, external_url=None, slug=None):
self.name = self.fake.word() if name is None else name
self.slug = self.fake.word() if slug is None else slug
self.external_url = self.fake.url() if external_url is None else external_url
self.state = "available"


class FakeGitProject:
Expand Down
21 changes: 18 additions & 3 deletions zoo/repos/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@

@shared_task
def sync_repos():

i = 0
for project in itertools.chain(
get_github_repositories(), get_gitlab_repositories()
):
if settings.SYNC_REPOS_SKIP_FORKS and project["is_fork"]:
continue
if settings.SYNC_REPOS_SKIP_PERSONAL and project["is_personal"]:
continue

i += 1
log.info("sync_repos.fetch", repo_number=i, project=project)
try:
repo = Repository.objects.get(
remote_id=project["id"], provider=project["provider"]
Expand All @@ -71,8 +74,11 @@ def sync_repos():
repo.save()

if project["provider"] == "gitlab":
log.info("sync_repos.calling.sync_enviroments_from_gitlab")
sync_enviroments_from_gitlab(repo)

log.info("sync_repos.total", repo_number=i)


@shared_task
def schedule_pulls():
Expand Down Expand Up @@ -219,16 +225,22 @@ def sync_enviroments_from_gitlab(repo: Repository):
gl_envs = get_project_enviroments(repo.remote_id)

if not gl_envs:
log.info("sync_enviroments_from_gitlab.no_envs")
return

envs = []
i = 0
log.info("sync_enviroments_from_gitlab.start")
for gl_env in gl_envs:
if gl_env.state != "available":
continue
env, _ = RepositoryEnvironment.objects.get_or_create(
repository_id=repo.id, name=gl_env.name
)
env.external_url = gl_env.external_url
env.save()
envs.append(env)
i += 1

RepositoryEnvironment.objects.filter(repository_id=repo.id).exclude(
id__in=[env.id for env in envs]
Expand All @@ -241,9 +253,12 @@ def sync_enviroments_from_gitlab(repo: Repository):
service_id=service.id, type=EnviromentType.GITLAB.value
).exclude(name__in=[env.name for env in envs]).delete()
for env in envs:
Environment.objects.update_or_create(
service_env, _ = Environment.objects.get_or_create(
service_id=service.id,
name=env.name,
dashboard_url=env.external_url,
type=EnviromentType.GITLAB.value,
)
service_env.dashboard_url = env.external_url
service_env.save()

log.info("sync_enviroments_from_gitlab.total.synced", envs_synced=i)
2 changes: 0 additions & 2 deletions zoo/repos/templates/repos/fields/repo_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
initialValue: '{{ widget.value|default:"" }}'
};
const gitlabEnvsInfo = {
envTypeGitlab: '{{ env_type_gitlab }}',
imageUrl: '{% static 'img/gitlab-icon.svg' %}',
envsUrl: '{% url "get_gitlab_envs" %}'
}
</script>
3 changes: 2 additions & 1 deletion zoo/repos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def get_gitlab_envs(request):
return JsonResponse(
[
{"name": gl_env.name, "dashboardUrl": gl_env.external_url}
for gl_env in repo.repository_environments.all()
for index, gl_env in enumerate(repo.repository_environments.all())
if index < 10
],
safe=False,
)
81 changes: 18 additions & 63 deletions zoo/services/assets/js/gitlab_envs.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,31 @@
const gitlabEnvs = {
_resetGilabEnvs: (envTypeGitlab) => {
const gitlab_envs = $("input[value='" + envTypeGitlab + "']").closest(".ui.segment");
if (gitlab_envs.length > 0) {
$.each(gitlab_envs, function(index, element){
const uiSegment = $(element)

// set values to default
uiSegment.find(".field.name input").val("");
uiSegment.find('input[name$="type"]').val("");
uiSegment.find('input[name$="dashboard_url"]').val("");
uiSegment.find('input[name$="logs_url"]').val("");
uiSegment.find('input[name$="open_api_url"]').val("");

// remove gitlab icon
uiSegment.find(".gitlab-img").hide()

uiSegment.hide();
uiSegment.find(".field.name input").prop("required", false);

//if all segments are hidden,
const hidden = $(".ui.segment.environment:hidden");
if (hidden.length == 5) {
let firstSegment = hidden.eq(0);
firstSegment.show()
}

if (hidden.length > 0) {
$("button.add-environment").prop("disabled", false);
}

})
}
},
_getFirstAvailableSegment: () => {
const all_envs = $(".ui.segment.environment");
if (all_envs.length == 0) { return false }

for (let i = 0; i < all_envs.length; i++) {
let firstSegment = all_envs.eq(i)
if (firstSegment.find(".field.name input").val() == "") {
return firstSegment
}
}

return false;
},
_fillData: (envTypeGitlab, firstSegment, response) => {
firstSegment.find(":not(.no-reset) input").val("");
firstSegment.find('input[name$="type"]').val(envTypeGitlab);
firstSegment.find(".field.name input").val(response["name"]).prop('readonly', true);
firstSegment.find('input[name$="dashboard_url"]').val(response["dashboardUrl"]).prop('readonly', true);
firstSegment.find(".gitlab-img").show()
firstSegment.show();
_fillData: (response) => {
const envName = response['name'] || ""
const envDashboardUrl = response['dashboardUrl'] || ""

let $env = $('#env_template').clone()
$env.find("input[name='gitlab-name']").val(envName)
$env.find("input[name='gitlab-dashboard-url']").val(envDashboardUrl)
$env.appendTo("#gitlab_envs" )
$env.show()
},
load: (gitlabEnvsInfo) => {
if (Number.isNaN(gitlabEnvsInfo.repoId)) { return }

//delete previous gitlab envs
gitlabEnvs._resetGilabEnvs(gitlabEnvsInfo.envTypeGitlab)

$("#gitlab_envs").empty()
//fetch gitlab envs
$.get(gitlabEnvsInfo.envsUrl + "?project_id=" + gitlabEnvsInfo.repoId, function (response) {
if (response.length == 0) { return console.log('Fetching Gitlab environments completed! No envs.') }
let customEnvsNameInput = $(".ui.segment.environment:visible").find(".field.name input")
if (response.length == 0) {
customEnvsNameInput.prop("required", true);
return console.log('Fetching Gitlab environments completed! No envs.')
}

customEnvsNameInput.prop("required", false);
$.each(response, function(index, element) {
let firstSegment = gitlabEnvs._getFirstAvailableSegment();
if (!firstSegment) { return;}

gitlabEnvs._fillData(gitlabEnvsInfo.envTypeGitlab, firstSegment, element)
if ($(".ui.segment.environment:hidden").length == 0) {
$("button.add-environment").prop("disabled", true);
}
if (index > 10) { return;}
gitlabEnvs._fillData(element)
})
console.log('Fetching Gitlab environments completed!')
}).fail(function() {
Expand Down
6 changes: 6 additions & 0 deletions zoo/services/assets/js/service_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ $('.icon-hint .label').popup({
variation: 'inverted',
position: 'left center'
});

$(".ui.green.button").on('submit', function(e) {
e.preventDefault();
$("#gitlab_envs input").prop('disabled', true)
$(this).submit();
});
22 changes: 20 additions & 2 deletions zoo/services/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ..repos.forms import RepoInput
from ..repos.github import get_namespaces as get_github_namespaces
from ..repos.gitlab import get_namespaces as get_gitlab_namespaces
from ..repos.models import Provider
from . import models


Expand Down Expand Up @@ -58,7 +59,6 @@ class Meta:
"logs_url",
"service_urls",
"open_api_url",
"type",
]
labels = {
"dashboard_url": "Dashboard URL",
Expand Down Expand Up @@ -178,7 +178,25 @@ def save(self, commit=True):
repository.save()
except (KeyError, AttributeError):
pass
return super().save()

service = super().save()
self.create_gitlab_envs(service)
return service

def create_gitlab_envs(self, service):
if not service.repository:
return

if service.repository.provider == Provider.GITHUB:
return

for env in service.repository.repository_environments.all():
models.Environment.objects.update_or_create(
type=models.EnviromentType.GITLAB,
name=env.name,
dashboard_url=env.external_url,
service_id=service.id,
)


ServiceEnvironmentsFormSet = forms.inlineformset_factory(
Expand Down
33 changes: 33 additions & 0 deletions zoo/services/migrations/0027_auto_20210509_1900.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 2.2.19 on 2021-05-09 19:00

from django.db import migrations, models

import zoo.services.constants


class Migration(migrations.Migration):

dependencies = [
("services", "0026_link"),
]

operations = [
migrations.AddField(
model_name="environment",
name="type",
field=models.CharField(
choices=[("gitlab", "gitlab"), ("zoo", "zoo")],
default=zoo.services.constants.EnviromentType("zoo"),
max_length=100,
),
),
migrations.AlterField(
model_name="environment",
name="name",
field=models.CharField(max_length=200),
),
migrations.AlterUniqueTogether(
name="environment",
unique_together={("service", "name", "type")},
),
]
22 changes: 0 additions & 22 deletions zoo/services/migrations/0027_environment_type.py

This file was deleted.

6 changes: 3 additions & 3 deletions zoo/services/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __repr__(self):

class Environment(models.Model):
class Meta:
unique_together = ("service", "name")
unique_together = ("service", "name", "type")
ordering = ["name"]

service = models.ForeignKey(
Expand All @@ -183,7 +183,7 @@ class Meta:
related_name="environments",
related_query_name="environment",
)
name = models.CharField(max_length=100)
name = models.CharField(max_length=200)
service_urls = pg_fields.ArrayField(
models.URLField(max_length=500), null=True, default=list
)
Expand All @@ -193,8 +193,8 @@ class Meta:
open_api_url = models.URLField(max_length=500, null=True, blank=True)
type = models.CharField(
choices=((item.value, item.value) for item in EnviromentType),
null=True,
max_length=100,
default=EnviromentType.ZOO,
)


Expand Down
4 changes: 2 additions & 2 deletions zoo/services/templates/services/service_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
docs
</a>

{% if object.environments_dict|length >= 1 %}
{% if environments_dict|length >= 1 %}
<div class="ui environments item top pointing fade dropdown">
<i class="laptop icon"></i> env:{{ environment.name }}
<div class="large bottom menu transition">
<h4 class="ui header">Environments</h4>
{% for name in object.environments_dict %}
{% for name in environments_dict %}
<a class="item {% if environment.name == name %}active{% endif %}" href="{{ request.path }}?environment={{ name }}">
<i class="laptop icon"></i> {{ name }}
</a>
Expand Down
Loading

0 comments on commit fe77b12

Please sign in to comment.