Skip to content

Commit

Permalink
Release 0.2.4
Browse files Browse the repository at this point in the history
Changelog:
* Fix logo in the sidebar;
* Fix bug with non staff users;
* Fix import inventory from project page;
* Fix sync of GIT projects with tags.


See merge request polemarch/ce!106
  • Loading branch information
akhmadullin committed Nov 30, 2018
2 parents 2a74c73 + 302d0be commit 642e569
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 165 deletions.
4 changes: 3 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ stages:
- issue_building
only:
changes:
- polemarch/**/*.{py,pyx}
- polemarch/main/**/*
- polemarch/api/**/*
- tests.py
- setup.{py,cfg}
- MANIFEST.in
Expand All @@ -43,6 +44,7 @@ stages:
- Makefile
- tox.ini
- .coveragerc
- .gitlab-ci.yml
retry: 2

.pack_tamplate: &packing-test
Expand Down
2 changes: 1 addition & 1 deletion deb.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Vcs-Browser: https://gitlab.com/vstconsulting/polemarch.git

Package: $(NAME)
Architecture: amd64
Depends: $${shlibs:Depends}, $${misc:Depends}, python3-virtualenv, libffi6, libssl-dev, sshpass, libpython3.5, git, libyaml-dev, libkrb5-dev, libssl-dev, libsasl2-dev, libldap2-dev
Depends: $${shlibs:Depends}, $${misc:Depends}, python3-virtualenv, libffi6, libssl-dev, sshpass, libpython3.5, git, libyaml-dev, libkrb5-dev, libssl-dev, libsasl2-dev, libldap2-dev, mime-support
Description: $(SUMMARY)
$(DESCRIPTION)
endef
Expand Down
4 changes: 2 additions & 2 deletions doc/polemarch-sphinx-theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
{% block logo %}
<!-- Logo -->
<a href="index.html" id="root_link" class="brand-link logo app-logo">
<img src="/static/img/logo/logo.png" alt="{{ project_gui_name }}" class="brand-image img-circle elevation-3" style="opacity: .8; float: inherit">
<img src="/static/img/logo/horizontal.svg" class="brand-image text-logo" style="opacity: .8">
<img src="{{ pathto('_static/img/logo/logo.png', 1) }}" alt="{{ project_gui_name }}" class="brand-image img-circle elevation-3" style="opacity: .8; float: inherit">
<img src="{{ pathto('_static/img/logo/horizontal.svg', 1) }}" class="brand-image text-logo" style="opacity: .8">
</a>
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion polemarch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"VST_ROOT_URLCONF": os.getenv("VST_ROOT_URLCONF", 'vstutils.urls'),
}

__version__ = "0.2.3"
__version__ = "0.2.4"

prepare_environment(**default_settings)
13 changes: 10 additions & 3 deletions polemarch/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import OrderedDict
from django.http import HttpResponse
from django.utils.decorators import method_decorator
from rest_framework import exceptions as excepts, status
from rest_framework import exceptions as excepts, status, permissions
from rest_framework.authtoken import views as token_views
from drf_yasg.utils import swagger_auto_schema
from vstutils.api.permissions import StaffPermission
Expand Down Expand Up @@ -209,7 +209,8 @@ def copy_instance(self, instance):

@deco.action(
["post", "delete", "get"], url_path="settings",
detail=yes, serializer_class=sers.UserSettingsSerializer
detail=yes, serializer_class=sers.UserSettingsSerializer,
permission_classes=(permissions.IsAuthenticated,)
)
def user_settings(self, request, *args, **kwargs):
'''
Expand All @@ -222,7 +223,7 @@ def user_settings(self, request, *args, **kwargs):
obj.settings.save()
return base.Response(obj.settings.data, status.HTTP_200_OK).resp

@deco.action(["post"], detail=yes)
@deco.action(["post"], detail=yes, permission_classes=(permissions.IsAuthenticated,))
def change_password(self, request, *args, **kwargs):
serializer = self.get_serializer(self.get_object(), data=request.data)
serializer.is_valid(raise_exception=True)
Expand Down Expand Up @@ -447,6 +448,12 @@ def import_inventory(self, request, **kwargs):
serializer = sers.InventoryImportSerializer(data=request.data)
serializer.is_valid(True)
serializer.save()
if hasattr(self, 'nested_manager'):
data = {self.lookup_field: serializer.data['inventory_id']}
self._data_create(
self.prepare_request_data(data, False),
self.lookup_field
)
return base.Response(serializer.data, status.HTTP_201_CREATED).resp


Expand Down
13 changes: 11 additions & 2 deletions polemarch/main/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ def _make_ext(self, file_name):
self.ext = os.path.splitext(file_name)[1]
self.file_name = self.path + '/' + file_name

def _read(self, file):
data = file.read()
with raise_context():
data = data.decode('utf-8')
return data

def _make_rst(self, file):
return rst_gen(file.read(), writer_name='html')['html_body']
return rst_gen(self._read(file), writer_name='html')['html_body']

def _make_md(self, file):
return Markdown(extras=['tables', 'header-ids']).convert(file.read())
return Markdown(extras=['tables', 'header-ids']).convert(self._read(file))

def set_readme(self):
if not os.path.exists(self.path):
Expand Down Expand Up @@ -280,17 +286,20 @@ def clone(self, *args, **kwargs):
return self.repo_class.clone()

@property
@raise_context()
def revision(self):
return self.repo_class.revision()

@property
@raise_context()
def branch(self):
return self.repo_class.get_branch_name()

@property
def module(self):
return Module.objects.filter(Q(project=self) | Q(project=None))

@raise_context()
def __get_readme(self):
if not hasattr(self, 'readme'):
self.readme = self.ReadMe(self)
Expand Down
45 changes: 32 additions & 13 deletions polemarch/main/repo/vcs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# pylint: disable=expression-not-assigned,abstract-method,import-error
from __future__ import unicode_literals
import warnings
from vstutils.utils import tmp_file_context, raise_context
import git
try:
import git
except: # nocv
warnings.warn("Git is not installed or have problems.")
from ._base import _Base, os


Expand Down Expand Up @@ -38,42 +42,54 @@ def __init__(self, *args, **kwargs):
self._fetch_map = {
1 << x: self._fetch_statuses[x] for x in range(8)
}
self.target_branch = self.proj.vars.get('repo_branch', None)

def get_repo(self):
return git.Repo(self.path)

def vsc_clone(self, *args, **kwargs):
return git.Repo.clone_from(*args, **kwargs)

def vcs_update(self, repo, env):
@raise_context()
def _fetch_from_remote(self, repo, env):
with repo.git.custom_environment(**env):
kwargs = self.options.get("FETCH_KWARGS", dict())
fetch_result = repo.remotes.origin.pull(**kwargs)
return repo.remotes.origin.fetch(**kwargs)

def vcs_update(self, repo, env):
fetch_result = self._fetch_from_remote(repo, env)
return fetch_result

def get_branch_name(self):
# pylint: disable=broad-except
reponame = "waiting..."
with raise_context():
reponame = self.get_repo().active_branch.name
repo = self.get_repo()
repo_branch = self.target_branch or 'ERROR'
if repo.head.is_detached and repo_branch.replace('tags/', '') in repo.tags:
return repo_branch
reponame = repo.active_branch.name
return reponame

def make_clone(self, env):
kw = dict(**self.options.get("CLONE_KWARGS", dict()))
branch = self.proj.vars.get('repo_branch', None)
if branch:
kw['branch'] = branch
if self.target_branch:
kw['branch'] = self.target_branch.replace('tags/', '')
repo = self.vsc_clone(self.proj.repository, self.path, env=env, **kw)
self.proj.variables.update_or_create(
key='repo_branch', defaults=dict(value=repo.active_branch.name)
)
with raise_context():
self.proj.variables.update_or_create(
key='repo_branch', defaults=dict(value=repo.active_branch.name)
)
return repo, None

def _get_or_create_repo(self, env):
try:
repo = self.get_repo()
branch = self.proj.vars.get('repo_branch', None)
if branch and repo.active_branch.name != branch:
branch = self.target_branch
with raise_context():
repo.git.checkout(branch)
is_not_detached = not repo.head.is_detached
if branch and is_not_detached and repo.active_branch.name != branch:
self.delete()
raise git.NoSuchPathError
except git.NoSuchPathError:
Expand All @@ -82,7 +98,10 @@ def _get_or_create_repo(self, env):

def make_update(self, env):
repo = self._get_or_create_repo(env)
return repo, self.vcs_update(repo, env)
resutls = repo, self.vcs_update(repo, env)
with raise_context():
repo.git.checkout(self.target_branch)
return resutls

def get_revision(self, *args, **kwargs):
# pylint: disable=unused-argument
Expand Down
95 changes: 92 additions & 3 deletions polemarch/main/tests/executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from yaml import dump
from ._base import BaseTestCase, os
from ..tasks import ScheduledTask
from ..unittests.ansible import inventory_data, valid_inventory


test_ansible_cfg = '''
Expand Down Expand Up @@ -575,15 +576,36 @@ def wip_git(self, project_data):
self.assertEqual(project_data['revision'], self.revisions[-1])
self.assertEqual(project_data['branch'], 'master')
# Update branch
new_branch_var = dict(key='repo_branch', value='new_branch')
new_branch_var = dict(key='repo_branch', value='invalid_branch')
self.make_bulk([
self.get_mod_bulk('project', project_data['id'], new_branch_var)
])
project_data = self.sync_project(project_data['id'])
self.assertEqual(project_data['status'], 'ERROR')
self.assertEqual(project_data['branch'], 'waiting...')

new_branch_var = dict(key='repo_branch', value='master')
self.make_bulk([
self.get_mod_bulk('project', project_data['id'], new_branch_var)
])
project_data = self.sync_project(project_data['id'])
self.assertEqual(project_data['status'], 'OK')
self.assertEqual(project_data['branch'], 'master')

new_branch_var = dict(key='repo_branch', value='tags/new_tag')
self.make_bulk([
self.get_mod_bulk('project', project_data['id'], new_branch_var)
])
project_data = self.sync_project(project_data['id'])
# Check updated brunch and revision
self.assertEqual(project_data['revision'], self.revisions[0])
self.assertEqual(project_data['branch'], 'new_branch')
self.assertEqual(project_data['branch'], 'tags/new_tag')
self.assertEqual(project_data['revision'], self.revisions[1])
# Return old branch
new_branch_var['value'] = 'new_branch'
self.make_bulk([
self.get_mod_bulk('project', project_data['id'], new_branch_var)
])
project_data = self.sync_project(project_data['id'])
new_branch_var['value'] = 'master'
results = self.make_bulk([
self.get_mod_bulk('project', project_data['id'], new_branch_var),
Expand Down Expand Up @@ -1054,6 +1076,7 @@ def test_project_git(self):
repo.index.add(["other.yml", ".polemarch.yaml"])
repo.index.commit("no message 2")
second_revision = repo.head.object.hexsha
repo.create_tag('new_tag')

# Test project
self.revisions = [first_revision, second_revision]
Expand Down Expand Up @@ -1251,3 +1274,69 @@ def test_history_facts(self):
history.kind = "PLAYBOOK"
history.save()
self.get_result("get", url, code=404)

def test_import_inventory(self):
bulk_data = [
dict(data_type=['project'], method='post', data=dict(name='testProj')),
self.get_mod_bulk(
'project', '<0[data][id]>',
{'name': 'test-inventory', 'raw_data': inventory_data},
['inventory', 'import_inventory'],
),
self.get_mod_bulk(
'inventory',
'<1[data][inventory_id]>',
{},
'all_hosts',
method='get'
),
self.get_mod_bulk(
'inventory',
'<1[data][inventory_id]>',
{},
'all_groups',
method='get'
),
self.get_mod_bulk(
'inventory',
'<1[data][inventory_id]>',
{},
'variables',
method='get'
),
self.get_mod_bulk(
'project',
'<0[data][id]>',
{},
['inventory', '<1[data][inventory_id]>'],
method='get'
),
]
results = self.make_bulk(bulk_data, 'put')

self.assertEqual(results[0]['status'], 201)
self.assertEqual(results[1]['status'], 201)
self.assertIn('inventory_id', results[1]['data'])

self.assertEqual(
results[2]['data']['count'],
len(valid_inventory['hosts'])
)
self.assertEqual(
results[3]['data']['count'],
len(valid_inventory['groups'])
)
self.assertEqual(
results[4]['data']['count'],
len(valid_inventory['vars'])
)

self.assertEqual(results[5]['status'], 200)
self.assertEqual(results[5]['data']['name'], 'test-inventory')

for host in results[2]['data']['results']:
self.assertIn(host['name'], valid_inventory['hosts'].keys())
for group in results[3]['data']['results']:
self.assertIn(group['name'], valid_inventory['groups'].keys())
for variable in results[4]['data']['results']:
self.assertIn(variable['key'], valid_inventory['vars'].keys())
Loading

0 comments on commit 642e569

Please sign in to comment.