diff --git a/zoo/base/assets/js/components/RepoInput.vue b/zoo/base/assets/js/components/RepoInput.vue index 56cc998b..75d7ad21 100644 --- a/zoo/base/assets/js/components/RepoInput.vue +++ b/zoo/base/assets/js/components/RepoInput.vue @@ -153,7 +153,6 @@ export default { if (this.repoInputInfo.initialValue) { this.$store.commit("selectSuggestion", parseInt(this.repoInputInfo.initialValue)) this.$store.commit("refreshEnteredText") - loadGitlabEnvs(parseInt(this.repoInputInfo.initialValue)) } }, onBlur () { diff --git a/zoo/repos/forms.py b/zoo/repos/forms.py index 9897372a..17ea0643 100644 --- a/zoo/repos/forms.py +++ b/zoo/repos/forms.py @@ -1,5 +1,6 @@ from django.forms import widgets +from ..services.constants import EnviromentType from .models import Repository @@ -19,4 +20,5 @@ def get_context(self, name, value, attrs): ) context["widget"]["value"] = value + context["env_type_gitlab"] = EnviromentType.GITLAB.value return context diff --git a/zoo/repos/migrations/0008_merge_20210316_2131.py b/zoo/repos/migrations/0008_merge_20210316_2131.py new file mode 100644 index 00000000..22022da4 --- /dev/null +++ b/zoo/repos/migrations/0008_merge_20210316_2131.py @@ -0,0 +1,13 @@ +# Generated by Django 2.2.19 on 2021-03-16 21:31 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("repos", "0007_endpoint"), + ("repos", "0007_repositoryenvironment"), + ] + + operations = [] diff --git a/zoo/repos/migrations/0009_remove_repositoryenvironment_type.py b/zoo/repos/migrations/0009_remove_repositoryenvironment_type.py new file mode 100644 index 00000000..f03c7a0b --- /dev/null +++ b/zoo/repos/migrations/0009_remove_repositoryenvironment_type.py @@ -0,0 +1,17 @@ +# Generated by Django 2.2.19 on 2021-03-16 21:31 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("repos", "0008_merge_20210316_2131"), + ] + + operations = [ + migrations.RemoveField( + model_name="repositoryenvironment", + name="type", + ), + ] diff --git a/zoo/repos/models.py b/zoo/repos/models.py index 0217ab7a..1f667028 100644 --- a/zoo/repos/models.py +++ b/zoo/repos/models.py @@ -102,8 +102,3 @@ class Meta: name = models.CharField(max_length=100) external_url = models.CharField(max_length=100) - type = models.CharField( - choices=((item.value, item.value) for item in Provider), - null=True, - max_length=100, - ) diff --git a/zoo/repos/tasks.py b/zoo/repos/tasks.py index 15b7744d..b77837c2 100644 --- a/zoo/repos/tasks.py +++ b/zoo/repos/tasks.py @@ -12,13 +12,14 @@ from ..auditing import runner from ..auditing.check_discovery import CHECKS as AUDITING_CHECKS from ..repos.models import Endpoint +from ..services.constants import EnviromentType from ..services.models import Environment, Service from .exceptions import MissingFilesError, RepositoryNotFoundError from .github import get_repositories as get_github_repositories from .gitlab import get_project from .gitlab import get_repositories as get_gitlab_repositories -from .utils import download_repository, get_scm_module, openapi_definition from .models import Provider, Repository, RepositoryEnvironment +from .utils import download_repository, get_scm_module, openapi_definition from .zoo_yml import parse, validate log = structlog.get_logger() @@ -215,8 +216,6 @@ def get_zoo_file_content(proj: Dict) -> str: def sync_enviroments_from_gitlab(repo: Repository): - RepositoryEnvironment.objects.filter(repository_id=repo.id).delete() - try: gl_envs = get_project(repo.remote_id).environments.list() except RepositoryNotFoundError: @@ -226,10 +225,29 @@ def sync_enviroments_from_gitlab(repo: Repository): ) return + envs = [] for gl_env in gl_envs: - RepositoryEnvironment.objects.create( + env, _ = RepositoryEnvironment.objects.get_or_create( repository_id=repo.id, name=gl_env.name, external_url=gl_env.external_url, - type=Provider.GITLAB, ) + envs.append(env) + + RepositoryEnvironment.objects.filter(repository_id=repo.id).exclude( + id__in=[env.id for env in envs] + ).delete() + + # update gitlab envs on every service + services = Service.objects.filter(repository_id=repo.id) + for service in services: + Environment.objects.filter( + service_id=service.id, type=EnviromentType.GITLAB.value + ).exclude(name__in=[env.name for env in envs]).delete() + for gl_env in gl_envs: + _, _ = Environment.objects.update_or_create( + service_id=service.id, + name=gl_env.name, + external_url=gl_env.external_url, + type=EnviromentType.GITLAB.value, + ) diff --git a/zoo/repos/templates/repos/fields/repo_input.html b/zoo/repos/templates/repos/fields/repo_input.html index 3458af0c..63db47f1 100644 --- a/zoo/repos/templates/repos/fields/repo_input.html +++ b/zoo/repos/templates/repos/fields/repo_input.html @@ -23,6 +23,7 @@ initialValue: '{{ widget.value|default:"" }}' }; const gitlabEnvsInfo = { + envTypeGitlab: '{{ env_type_gitlab }}', imageUrl: '{% static 'img/gitlab-icon-rgb.svg' %}', envsUrl: '{% url "get_gitlab_envs" %}' } diff --git a/zoo/repos/views.py b/zoo/repos/views.py index 351de3cb..b75957db 100644 --- a/zoo/repos/views.py +++ b/zoo/repos/views.py @@ -3,6 +3,8 @@ from django.views.decorators.http import require_GET from django.views.generic import ListView +from ..services.constants import EnviromentType +from ..services.models import Environment, Service from . import models from .exceptions import RepositoryNotFoundError from .models import Provider, Repository @@ -38,9 +40,10 @@ def get_gitlab_envs(request): except RepositoryNotFoundError: return JsonResponse({"message": "Wrong project_id"}, safe=False) - parsed_envs = [ - {"name": gl_env.name, "dashboardUrl": gl_env.external_url} - for gl_env in repo.repository_environments.all() - ] - - return JsonResponse(parsed_envs, safe=False) + return JsonResponse( + [ + {"name": gl_env.name, "dashboardUrl": gl_env.external_url} + for gl_env in repo.repository_environments.all() + ], + safe=False, + ) diff --git a/zoo/services/assets/js/gitlab_envs.js b/zoo/services/assets/js/gitlab_envs.js index 876240e2..e9973223 100644 --- a/zoo/services/assets/js/gitlab_envs.js +++ b/zoo/services/assets/js/gitlab_envs.js @@ -1,49 +1,76 @@ const gitlabEnvs = { - _fillData: (imageUrl, data) => { - const name = "name" in data ? data.name : "" - const dashboardUrl = "dashboardUrl" in data ? data.dashboardUrl : "" - - return `
-
- gitlab -
-
-
- - -
-
- - -
-
-
- - -
-
- - -
-
- - -
-
` + _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(); }, load: (gitlabEnvsInfo) => { - const gitlabEnvsSelector = $("#gitlab-envs") + //delete previous gitlab envs + gitlabEnvs._resetGilabEnvs(gitlabEnvsInfo.envTypeGitlab) + //fetch gitlab envs $.get(gitlabEnvsInfo.envsUrl + "?project_id=" + gitlabEnvsInfo.repoId, function (response) { - if (response.length == 0) { return console.log('Fetching Gitlab environments completed!') } - - //delete previous gitlab envs - gitlabEnvsSelector.empty() + if (response.length == 0) { return console.log('Fetching Gitlab environments completed! No envs.') } - for (let i = 0; i < response.length; i++) { - gitlabEnvsSelector.append(gitlabEnvs._fillData(gitlabEnvsInfo.imageUrl, response[i])) - } + $.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); + } + }) + console.log('Fetching Gitlab environments completed!') }).fail(function() { console.log( "Error fetching envs" ); }) diff --git a/zoo/services/assets/js/service_form.js b/zoo/services/assets/js/service_form.js index 19221af0..869d512a 100644 --- a/zoo/services/assets/js/service_form.js +++ b/zoo/services/assets/js/service_form.js @@ -1,13 +1,6 @@ import '../style/service_form.less' $(document).ready(function () { - //set default env type to zoo - $(".ui.segment.environment:visible").find('input[name$="type"]').each(function () { - if (!$(this).val()) { - $(this).val(envTypeZoo) - } - }); - $(".field.service-urls").each(function () { var previous = null; var previousEmpty = false; diff --git a/zoo/services/forms.py b/zoo/services/forms.py index d3c1c4e1..8fb959cd 100644 --- a/zoo/services/forms.py +++ b/zoo/services/forms.py @@ -58,6 +58,7 @@ class Meta: "logs_url", "service_urls", "open_api_url", + "type", ] labels = { "dashboard_url": "Dashboard URL", diff --git a/zoo/services/migrations/0027_auto_20210316_2131.py b/zoo/services/migrations/0027_auto_20210316_2131.py new file mode 100644 index 00000000..bc3982f5 --- /dev/null +++ b/zoo/services/migrations/0027_auto_20210316_2131.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.19 on 2021-03-16 21:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("services", "0026_environment_type"), + ] + + operations = [ + migrations.AlterField( + model_name="environment", + name="type", + field=models.CharField( + choices=[("gitlab", "gitlab"), ("zoo", "zoo")], + default="zoo", + max_length=100, + null=True, + ), + ), + ] diff --git a/zoo/services/models.py b/zoo/services/models.py index 9b36fa18..cd27c2a0 100644 --- a/zoo/services/models.py +++ b/zoo/services/models.py @@ -195,7 +195,6 @@ class Meta: choices=((item.value, item.value) for item in EnviromentType), null=True, max_length=100, - default=EnviromentType.ZOO.value, ) diff --git a/zoo/services/templates/services/service_form.html b/zoo/services/templates/services/service_form.html index e81483ea..3da1d314 100644 --- a/zoo/services/templates/services/service_form.html +++ b/zoo/services/templates/services/service_form.html @@ -14,10 +14,10 @@ {% endblock %} {% block disclaimer %} -{% singleton 'instance.Hints' as hints %} -{% if hints.service_form_explanation %} -

{{ hints.service_form_explanation|safe }}

-{% endif %} + {% singleton 'instance.Hints' as hints %} + {% if hints.service_form_explanation %} +

{{ hints.service_form_explanation|safe }}

+ {% endif %} {% endblock %} {% block content %} @@ -37,12 +37,13 @@

Environments

-
-
{{ envs_formset.management_form }} {% for form in envs_formset %}
1 %}style="display: none"{% endif %}> - {% if forloop.counter > 1 %} + + {% if forloop.counter > 1 and form.type != env_type_gitlab %}
{{ form.DELETE.as_hidden }} +
- {{ form.name }} + {% if form.type == env_type_gitlab %} + + {% else %} + {{ form.name }} + {% endif %}
@@ -86,7 +92,11 @@

Environments

- {{ form.service_urls }} + {% if form.type == env_type_gitlab %} + + {% else %} + {{ form.service_urls }} + {% endif %}
@@ -94,10 +104,13 @@

Environments

{% endfor %} -
+ {% endblock %} {% block scripts %} {{ block.super }} + {% endblock %} diff --git a/zoo/services/views.py b/zoo/services/views.py index 5d6796d6..87c826b2 100644 --- a/zoo/services/views.py +++ b/zoo/services/views.py @@ -61,6 +61,8 @@ def get_context_data(self, **kwargs): data["envs_formset"] = forms.ServiceEnvironmentsFormSet(self.request.POST) return data data["envs_formset"] = forms.ServiceEnvironmentsFormSet() + data["env_type_gitlab"] = EnviromentType.GITLAB.value + data["env_type_zoo"] = EnviromentType.ZOO.value return data