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 `
{{ 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 @@