MT#33006 tracker: WF <-> MT mapper

* mapper_import command to import issues/tasks:
> ./manage.py mapper_import mapper_production.db
> Successfully imported OrderedDict([('new', 22202), ('update', 0), ('delete', 0), ('skip', 0), ('error', 0), ('invalid', 0)])

Change-Id: I3874381119ed60b7eec6bda9f2f193310178dea9
pull/9/head
Victor Seva 3 years ago
parent 16aadc64a1
commit a411dc8927

@ -0,0 +1,40 @@
# Copyright (C) 2022 The Sipwise Team - http://sipwise.com
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from . import models
class TrackerMapperResource(resources.ModelResource):
class Meta:
model = models.TrackerMapper
import_id_fields = ["mantis_id"]
use_bulk = True
skip_unchanged = True
def skip_row(self, instance, original):
try:
mantis_id = int(original.mantis_id)
except ValueError:
return False
return instance.mantis_id == mantis_id
@admin.register(models.TrackerMapper)
class TrackerMapperAdmin(ImportExportModelAdmin):
resource_class = TrackerMapperResource
list_filter = ["mapper_type"]

@ -24,6 +24,12 @@ class Tracker(Enum):
WORKFRONT = "WorkFront"
@unique
class MapperType(Enum):
ISSUE = "Issue"
TASK = "Task"
class TrackerConf(AppConf):
REGEX = {
Tracker.NONE: r"#(\d+)",

Binary file not shown.

@ -0,0 +1,59 @@
# Copyright (C) 2022 The Sipwise Team - http://sipwise.com
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import sqlite3
from itertools import islice
from pathlib import Path
from django.core.management.base import BaseCommand
from django.core.management.base import CommandError
from tablib import Dataset
from tracker.admin import TrackerMapperResource
from tracker.conf import MapperType
class Command(BaseCommand):
help = "import WF to Mantis mapper info"
headers = ("workfront_uuid", "workfront_id", "mantis_id", "mapper_type")
def add_arguments(self, parser):
parser.add_argument("file", type=lambda p: Path(p).absolute())
def get_dataset(self, _type, max_rows=100):
for row in islice(self.cur, max_rows):
self.data.append([row[0], row[1], row[2], _type])
def handle(self, *args, **options):
resource = TrackerMapperResource()
# resource.get_queryset().all().delete()
self.data = Dataset(headers=self.headers)
con = sqlite3.connect(options["file"])
self.cur = con.execute(
"SELECT wf_id, wf_ticket, mt_ticket FROM mapper_issues"
)
self.get_dataset(MapperType.ISSUE, None)
self.cur = con.execute(
"SELECT wf_id, wf_ticket, mt_ticket FROM mapper_tasks"
)
self.get_dataset(MapperType.TASK, None)
result = resource.import_data(
self.data, raise_errors=True, use_transactions=True
)
if result.has_errors():
raise CommandError("error importing data")
self.stdout.write(
self.style.SUCCESS(f"Successfully imported {result.totals}")
)
con.close()

@ -0,0 +1,45 @@
# Generated by Django 3.2.15 on 2022-09-14 18:31
from django.db import migrations
from django.db import models
import tracker.conf
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="TrackerMapper",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"mapper_type",
models.CharField(
choices=[
(tracker.conf.MapperType["ISSUE"], "Issue"),
(tracker.conf.MapperType["TASK"], "Task"),
],
max_length=15,
),
),
("mantis_id", models.CharField(max_length=50, unique=True)),
("workfront_id", models.CharField(max_length=50, unique=True)),
(
"workfront_uuid",
models.CharField(max_length=50, unique=True),
),
],
),
]

@ -17,6 +17,7 @@ import re
from django.db import models
from . import utils
from .conf import MapperType
from .conf import Tracker
from .conf import TrackerConf
@ -74,3 +75,15 @@ class MantisInfo(TrackerInfo):
def set_target_release(self, release):
return utils.mantis_set_release_target(self.mantis_id, release)
class TrackerMapper(models.Model):
mapper_type = models.CharField(
max_length=15, choices=[(tag, tag.value) for tag in MapperType]
)
mantis_id = models.CharField(max_length=50, null=False, unique=True)
workfront_id = models.CharField(max_length=50, null=False, unique=True)
workfront_uuid = models.CharField(max_length=50, null=False, unique=True)
def __str__(self):
return f"{self.mapper_type}:TT#{self.workfront_id}:MT#{self.mantis_id}"

@ -0,0 +1,40 @@
# Copyright (C) 2020 The Sipwise Team - http://sipwise.com
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
from io import StringIO
from django.conf import settings
from django.core.management import call_command
from django.test import TestCase
from tracker.conf import MapperType
from tracker.models import TrackerMapper
FIXTURES_PATH = settings.BASE_DIR.joinpath("tracker", "fixtures")
DB_FILE = FIXTURES_PATH.joinpath("mapper.db")
class createFakeBuildReleaseTest(TestCase):
fixtures = ["test_models"]
def test_ok(self):
out = StringIO()
call_command("mapper_import", DB_FILE, stdout=out)
self.assertIn("('new', 20)", out.getvalue())
self.assertIn("('error', 0)", out.getvalue())
qs = TrackerMapper.objects
val = qs.filter(mapper_type=MapperType.ISSUE).count()
self.assertEqual(val, 10)
val = qs.filter(mapper_type=MapperType.TASK).count()
self.assertEqual(val, 10)
Loading…
Cancel
Save