def perform_migrate_issues(args):
    closed_states = []
    if (args.closed_states):
        closed_states = args.closed_states.split(',')

    custom_fields = []
    if (args.custom_fields):
        custom_fields = args.custom_fields.split(',')

    if (args.user_dict is not None):
        load_user_dict(args.user_dict)

    redmine = RedmineClient(args.redmine_key, args.no_verify)
    gitlab = GitlabClient(args.gitlab_key, args.no_verify)

    redmine_project = RedmineProject(args.redmine_project_url, redmine)
    gitlab_project = GitlabProject(args.gitlab_project_url, gitlab)

    gitlab_instance = gitlab_project.get_instance()

    if (args.project_members_only):
        gitlab_users_index = gitlab_project.get_members_index()
    else:
        gitlab_users_index = gitlab_instance.get_users_index()

    redmine_users_index = redmine_project.get_users_index()
    milestones_index = gitlab_project.get_milestones_index()
    textile_converter = TextileConverter()

    log.debug('GitLab milestones are: {}'.format(', '.join(milestones_index) +
                                                 ' '))

    # get issues
    log.info('Getting redmine issues')
    issues = redmine_project.get_all_issues()
    if args.initial_id:
        issues = [
            issue for issue in issues if int(args.initial_id) <= issue['id']
        ]

    # convert issues
    log.info('Converting issues')
    issues_data = (convert_issue(args.redmine_key, i, redmine_users_index,
                                 gitlab_users_index, milestones_index,
                                 closed_states, custom_fields,
                                 textile_converter, args.keep_id
                                 or args.keep_title, args.sudo)
                   for i in issues)

    # create issues
    log.info('Creating gitlab issues')
    last_iid = int(args.initial_id or 1) - 1
    for data, meta, redmine_id in issues_data:
        if args.check:
            milestone_id = data.get('milestone_id', None)
            if milestone_id:
                try:
                    gitlab_project.get_milestone_by_id(milestone_id)
                except ValueError:
                    raise CommandError(
                        "issue \"{}\" points to unknown milestone_id \"{}\". "
                        "Check that you already migrated roadmaps".format(
                            data['title'], milestone_id))

            log.info('Would create issue "{}" and {} notes.'.format(
                data['title'], len(meta['notes'])))

        else:
            if args.keep_id:
                try:
                    fake_meta = {
                        'uploads': [],
                        'notes': [],
                        'must_close': False
                    }
                    if args.sudo:
                        fake_meta['sudo_user'] = meta['sudo_user']
                    while redmine_id > last_iid + 1:
                        created = gitlab_project.create_issue(
                            {'title': 'fake'}, fake_meta)
                        last_iid = created['iid']
                        gitlab_project.delete_issue(created['iid'])
                        log.info('#{iid} {title}'.format(**created))
                except:
                    log.info('create issue "{}" failed'.format('fake'))
                    raise

            try:
                created = gitlab_project.create_issue(data, meta)
                last_iid = created['iid']
                log.info('#{iid} {title}'.format(**created))
            except:
                log.info('create issue "{}" failed'.format(data['title']))
                raise
Beispiel #2
0
def perform_migrate_roadmap(args):
    redmine = RedmineClient(args.redmine_key, args.no_verify)
    gitlab = GitlabClient(args.gitlab_key, args.no_verify)

    redmine_project = RedmineProject(args.redmine_project_url, redmine)
    gitlab_project = GitlabProject(args.gitlab_project_url, gitlab)

    # auto create milestones
    milestones_data = []
    if args.auto_milestones:
        match = re.match("^(monthly|yearly):(\d{4}(-[01]\d)?)$",
                         args.auto_milestones)
        if not match:
            log.error(
                "--auto-milestone must be formed as (monthly|yearly):<start_date>"
            )
            exit(1)

        milestones_type = match.group(1)
        milestones_date = list(map(lambda x: int(x),
                                   match.group(2).split("-")))

        if milestones_type == "monthly" and len(milestones_date) == 1:
            milestones_date.append(1)

        log.info("Creating %s milestones, from %s" %
                 (milestones_type, "-".join(
                     map(lambda x: str(x).zfill(2), milestones_date))))

        day = date(milestones_date[0], milestones_date[1], 1)
        today = date.today()

        while day <= today:
            month_days = monthrange(day.year, day.month)[1]

            milestone = {
                "title": day.strftime("%Y-%m"),
                "description": "\n\n*(auto-generated milestone)*",
                "start_date": day.strftime("%Y-%m-%d"),
                "due_date": day.replace(day=month_days).strftime("%Y-%m-%d"),
            }

            must_close = True

            if day > today - timedelta(days=4 * 31):  # keep last 4 months open
                must_close = False

            meta = {
                "must_close": must_close,
            }
            milestones_data.append((milestone, meta))

            # next month
            day = day + timedelta(days=month_days)

    checks = [
        #(check_no_milestone, 'Gitlab project has no pre-existing milestone'),
        (check_origin_milestone, 'Redmine project contains versions'),
    ]
    for i in checks:
        check(*i,
              redmine_project=redmine_project,
              gitlab_project=gitlab_project)

    versions = redmine_project.get_versions()
    versions_data = [convert_version(i) for i in versions]

    if milestones_data:
        versions_titles = []
        for data, meta in versions_data:
            versions_titles.append(data['title'])

        for data, meta in milestones_data:
            if data['title'] in versions_titles:
                continue

            versions_data.append((data, meta))

    for data, meta in versions_data:
        if args.check:
            log.info("Would create version {}".format(data))
        else:
            created = gitlab_project.create_milestone(data, meta)
            log.info("Version {}".format(created['title']))