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

Commit

Permalink
feat: fetch gitlab envs 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gjuro87 committed Mar 19, 2021
1 parent 6f9ec70 commit f47db5d
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 74 deletions.
1 change: 0 additions & 1 deletion zoo/base/assets/js/components/RepoInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions zoo/repos/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.forms import widgets

from ..services.constants import EnviromentType
from .models import Repository


Expand All @@ -19,4 +20,5 @@ def get_context(self, name, value, attrs):
)

context["widget"]["value"] = value
context["env_type_gitlab"] = EnviromentType.GITLAB.value
return context
13 changes: 13 additions & 0 deletions zoo/repos/migrations/0008_merge_20210316_2131.py
Original file line number Diff line number Diff line change
@@ -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 = []
17 changes: 17 additions & 0 deletions zoo/repos/migrations/0009_remove_repositoryenvironment_type.py
Original file line number Diff line number Diff line change
@@ -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",
),
]
5 changes: 0 additions & 5 deletions zoo/repos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
28 changes: 23 additions & 5 deletions zoo/repos/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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,
)
1 change: 1 addition & 0 deletions zoo/repos/templates/repos/fields/repo_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}'
}
Expand Down
15 changes: 9 additions & 6 deletions zoo/repos/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
105 changes: 66 additions & 39 deletions zoo/services/assets/js/gitlab_envs.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,76 @@
const gitlabEnvs = {
_fillData: (imageUrl, data) => {
const name = "name" in data ? data.name : ""
const dashboardUrl = "dashboardUrl" in data ? data.dashboardUrl : ""

return `<div class="ui secondary segment environment">
<div class="ui top left attached label paddingless gitlab-img">
<img src="${imageUrl}" alt="gitlab">
</div>
<div class="two fields">
<div class="field name">
<label>Name</label>
<input type="text" value="${name}" readonly>
</div>
<div class="field dashboard-url">
<label>Dashboard URL</label>
<input type="text" value="${dashboardUrl}" readonly>
</div>
</div>
<div class="field logs-url">
<label>Logs URL</label>
<input type="text" value="" readonly>
</div>
<div class="field service-urls">
<label>Service URLs</label>
<input type="text" value="" readonly>
</div>
<div class="field open_api_url">
<label>OpenAPI URL</label>
<input type="text" value="" readonly>
</div>
</div>`
_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" );
})
Expand Down
7 changes: 0 additions & 7 deletions zoo/services/assets/js/service_form.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
1 change: 1 addition & 0 deletions zoo/services/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Meta:
"logs_url",
"service_urls",
"open_api_url",
"type",
]
labels = {
"dashboard_url": "Dashboard URL",
Expand Down
23 changes: 23 additions & 0 deletions zoo/services/migrations/0027_auto_20210316_2131.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
1 change: 0 additions & 1 deletion zoo/services/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ class Meta:
choices=((item.value, item.value) for item in EnviromentType),
null=True,
max_length=100,
default=EnviromentType.ZOO.value,
)


Expand Down
33 changes: 23 additions & 10 deletions zoo/services/templates/services/service_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
{% endblock %}

{% block disclaimer %}
{% singleton 'instance.Hints' as hints %}
{% if hints.service_form_explanation %}
<p class="ui secondary yellow segment">{{ hints.service_form_explanation|safe }}</p>
{% endif %}
{% singleton 'instance.Hints' as hints %}
{% if hints.service_form_explanation %}
<p class="ui secondary yellow segment">{{ hints.service_form_explanation|safe }}</p>
{% endif %}
{% endblock %}

{% block content %}
Expand All @@ -37,12 +37,13 @@ <h3 class="ui marginless header">Environments</h3>
</div>
</div>
</div>
<div id="gitlab-envs" style="margin-top: 1rem;">
</div>
{{ envs_formset.management_form }}
{% for form in envs_formset %}
<div class="ui secondary segment environment" {% if not form.has_changed and forloop.counter > 1 %}style="display: none"{% endif %}>
{% if forloop.counter > 1 %}
<div class="ui top left attached label paddingless gitlab-img" {% if form.type != env_type_gitlab %} style="display: none"{% endif %}>
<img src="{% static 'img/gitlab-icon-rgb.svg' %}" alt="gitlab">
</div>
{% if forloop.counter > 1 and form.type != env_type_gitlab %}
<div class="ui top right attached label paddingless">
<button class="ui remove-environment icon button marginless" type="button"
data-checkbox="{{ form.DELETE.auto_id }}">
Expand Down Expand Up @@ -70,10 +71,15 @@ <h3 class="ui marginless header">Environments</h3>
{% endfor %}
</div>
{{ form.DELETE.as_hidden }}
<input type="hidden" name="{{ form.type.html_name }}" id="{{ form.type.id_for_label }}" {% if form.type.value %} value="{{ form.type.value }}" {% elif forloop.counter == 0 %}value="{{ env_type_zoo }}"{% endif %}>
<div class="two fields">
<div class="field name">
<label>{{ form.name.label }}</label>
{{ form.name }}
{% if form.type == env_type_gitlab %}
<input type="text" value="{{ form.name }}" name="{{ form.name.html_name }}" id="{{ form.name.id_for_label }}" readonly>
{% else %}
{{ form.name }}
{% endif %}
</div>
<div class="field dashboard-url">
<label>{{ form.dashboard_url.label }}</label>
Expand All @@ -86,18 +92,25 @@ <h3 class="ui marginless header">Environments</h3>
</div>
<div class="field service-urls">
<label>{{ form.service_urls.label }}</label>
{{ form.service_urls }}
{% if form.type == env_type_gitlab %}
<input type="text" value="{{ form.service_urls }}" name="{{ form.service_urls.html_name }}" id="{{ form.service_urls.id_for_label }}" readonly>
{% else %}
{{ form.service_urls }}
{% endif %}
</div>
<div class="field open_api_url">
<label>{{ form.open_api_url.label }}</label>
{{ form.open_api_url }}
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}

{% block scripts %}
{{ block.super }}
<script>
const envTypeZoo = '{{ env_type_zoo }}';
</script>
<script src="{% static 'service_form.js' %}"></script>
{% endblock %}
Loading

0 comments on commit f47db5d

Please sign in to comment.