add command to update redmine issue status

This commit is contained in:
Frédéric Péters 2018-12-26 10:04:49 +01:00
parent ba2a43ab0d
commit 546d0cd515
5 changed files with 98 additions and 37 deletions

View File

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.management.base import BaseCommand
import redmine
from redmine import Redmine
from scrutiny.projects.models import Module
from scrutiny.projects.utils import get_issue_deployment_status
class Command(BaseCommand):
def handle(self, verbosity, *args, **options):
redmine_server = Redmine(settings.REDMINE_URL, key=settings.REDMINE_API_KEY)
RESOLVED_ID = None
DEPLOYED_ID = None
for issue_status in redmine_server.issue_status.all():
if issue_status.name == u'Résolu (à déployer)':
RESOLVED_ID = issue_status.id
elif issue_status.name == u'Solution déployée':
DEPLOYED_ID = issue_status.id
for module in Module.objects.all():
if not module.repository_url:
continue
try:
issues = list(redmine_server.issue.filter(project_id=module.redmine_project, status_id=RESOLVED_ID))
except redmine.exceptions.ResourceNotFoundError:
if verbosity > 1:
print('unknown redmine module:', module.name)
continue
for issue in issues:
deployment_status = get_issue_deployment_status(issue.id)
if not deployment_status.get('platforms'):
if verbosity > 1:
print(' unknown status for https://dev.entrouvert.org/issues/%s' % issue.id)
continue
platforms_ok = [x.rsplit(' / ', 1)[0] for x in deployment_status['platforms'].keys()
if deployment_status['platforms'][x]['status'] == 'ok']
if settings.REDMINE_REFERENCE_PLATFORM in platforms_ok:
if verbosity > 1:
print('%s (%s/%s) https://dev.entrouvert.org/issues/%s' % (project, i+1, len(issues), issue.id))
issue.status_id = DEPLOYED_ID
#issue.save()

View File

@ -84,6 +84,14 @@ class Module(models.Model):
def __unicode__(self):
return self.name
@property
def redmine_project(self):
mapping = {
'authentic2': 'authentic',
'python-django-mellon': 'django-mellon',
}
return mapping.get(self.name) or self.name
def get_installed_version(self, platform, service):
try:
installed_service = InstalledService.objects.get(

View File

@ -87,3 +87,42 @@ class CommitAndIssues(object):
def add_issue(self, issue_id):
self.issues.append(Issue(issue_id))
def get_issue_deployment_status(issue_id):
from .models import Module, InstalledService
data = {}
for module in Module.objects.all():
try:
git_log = module.get_git_log()
except OSError:
continue
git_log_hashes = [x[0][:7] for x in git_log]
for line in git_log:
if not unicode(line[1], 'utf-8', 'ignore').endswith('#%s)' % issue_id):
continue
commit_hash = line[0][:7]
fix_index = git_log_hashes.index(commit_hash)
data = {'platforms': {}}
for service in InstalledService.objects.all():
installed_version = module.get_installed_version(service.platform, service.service)
if not installed_version or not installed_version.version:
continue
version_number = installed_version.version.version
if not version_number:
continue
service_name = '%s / %s / %s' % (
installed_version.service.platform.project.title,
installed_version.service.platform.title,
installed_version.service.service.title)
version_hash = module.get_version_hash(version_number)
if not version_hash in git_log_hashes:
continue
version_index = git_log_hashes.index(version_hash)
data['platforms'][service_name] = {
'version': version_number,
'status': 'ok' if version_index <= fix_index else 'nok'
}
if data:
break
return data

View File

@ -9,7 +9,7 @@ from django.views.generic.base import TemplateView
from django.views.generic.detail import DetailView
from .models import Project, InstalledService, Module, InstalledVersion
from .utils import CommitAndIssues, decorate_commit_line
from .utils import CommitAndIssues, decorate_commit_line, get_issue_deployment_status
class ProjectDetailView(DetailView):
model = Project
@ -267,41 +267,8 @@ class IssuesSnippet(TemplateView):
def api_issues_json(request, *args, **kwargs):
response = HttpResponse(content_type='application/json')
data = {}
for module in Module.objects.all():
try:
git_log = module.get_git_log()
except OSError:
continue
git_log_hashes = [x[0][:7] for x in git_log]
for line in git_log:
if not unicode(line[1], 'utf-8', 'ignore').endswith('#%s)' % kwargs.get('issue')):
continue
commit_hash = line[0][:7]
fix_index = git_log_hashes.index(commit_hash)
data = {'platforms': {}}
for service in InstalledService.objects.all():
installed_version = module.get_installed_version(service.platform, service.service)
if not installed_version or not installed_version.version:
continue
version_number = installed_version.version.version
if not version_number:
continue
service_name = '%s / %s / %s' % (
installed_version.service.platform.project.title,
installed_version.service.platform.title,
installed_version.service.service.title)
version_hash = module.get_version_hash(version_number)
if not version_hash in git_log_hashes:
continue
version_index = git_log_hashes.index(version_hash)
data['platforms'][service_name] = {
'version': version_number,
'status': 'ok' if version_index <= fix_index else 'nok'
}
if data:
break
issue_id = kwargs.get('issue')
data = get_issue_deployment_status(issue_id)
json_str = json.dumps(data, indent=2)
for variable in ('jsonpCallback', 'callback'):
if variable in request.GET:

View File

@ -92,7 +92,7 @@ TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'scrutiny', 'templates'),
)
REDMINE_REFERENCE_PLATFORM = 'SaaS2 / Test'
REDMINE_URL = 'https://dev.entrouvert.org'
REDMINE_API_KEY = None