コード例 #1
0
ファイル: models.py プロジェクト: skygate/tasks-sync
    def save_from_redmine_to_redmine(self, issue, project):
        redmine_main_tasks_manager = Redmine(self.main_task_manager.url,
                                             key=self.main_task_manager.api_key)
        user = redmine_main_tasks_manager.user.get(issue.author.id)
        logger.info("Issue {} is processed".format(issue.id))
        new_issue = Redmine(self.sync_task_manager.url, key=self.sync_task_manager.api_key,
                            impersonate=self.mapping("username", user.login)).issue.new()
        new_issue.project_id = project
        new_issue.subject = issue.subject
        new_issue.tracker_id = self.mapping("tracker_id", issue.tracker.id)
        new_issue.description = issue.description
        new_issue.status_id = self.mapping("status_id", issue.status.id)
        new_issue.priority_id = self.mapping("priority_id", issue.priority.id)
        if hasattr(issue, "assigned_to"):
            new_issue.assigned_to_id = self.mapping("user_id", issue.assigned_to.id)
        if hasattr(issue, "watchers"):
            # It's not working!
            watcher_user_ids = []
            for watcher in issue.watchers:
                watcher_user_ids.append(self.mapping("user_id", watcher.id))
            new_issue.watcher_user_ids = watcher_user_ids
        if hasattr(issue, "start_date"):
            new_issue.start_date = issue.start_date
        if hasattr(issue, "due_date"):
            new_issue.due_date = issue.due_date
        if hasattr(issue, "estimated_hours"):
            new_issue.estimated_hours = issue.estimated_hours
        new_issue.done_ratio = issue.done_ratio
        if issue.attachments:
            upload_list = []
            for attachment in issue.attachments:
                redmine_main_tasks_manager.download(attachment.content_url, savepath='/tmp/',
                                                    filename=attachment.filename)
                upload_list.append({'path': '/tmp/' + attachment.filename, 'filename': attachment.filename,
                                    'description': attachment.description})

            new_issue.uploads = upload_list
        new_issue.save()
        self.mapping_dict_issue_id[str(issue.id)] = new_issue.id
        self.save()
        time_entries = redmine_main_tasks_manager.time_entry.filter(issue_id=issue.id)
        if time_entries:
            for time_entry in time_entries:
                user = redmine_main_tasks_manager.user.get(time_entry.user.id)
                new_time_entry = Redmine(self.sync_task_manager.url, key=self.sync_task_manager.api_key,
                                         impersonate=self.mapping("username", user.login)).time_entry.new()
                new_time_entry.user_id = self.mapping("user_id", time_entry.user.id)

                if issue.id == time_entry.issue.id:
                    # if we have related tasks then time entries are doubled and we need to check it
                    new_time_entry.issue_id = new_issue.id
                    new_time_entry.spent_on = time_entry.spent_on
                    new_time_entry.hours = time_entry.hours
                    new_time_entry.activity_id = time_entry.activity.id
                    new_time_entry.comments = time_entry.comments
                    new_time_entry.save()
        # adding notes to the task

        if issue.journals:
            for arg in issue.journals:
                if arg.notes:
                    user = redmine_main_tasks_manager.user.get(arg.user.id)
                    update_issue = Redmine(self.sync_task_manager.url, key=self.sync_task_manager.api_key,
                                           impersonate=self.mapping("username", user.login)).issue.get(new_issue.id)
                    update_issue.notes = arg.notes
                    update_issue.save()
コード例 #2
0
ファイル: models.py プロジェクト: skygate/tasks-sync
    def update_issue_redmine_redmine(self, issue_id, issue_status, project_id, reverse=False):
        main_tasks_manager = self.main_task_manager if not reverse else self.sync_task_manager
        sync_tasks_manager = self.sync_task_manager if not reverse else self.main_task_manager
        issue = Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key). \
            issue.get(issue_id)
        mapping = self.mapping if not reverse else self.reverse_mapping
        logger.info("Main tasks manager: {}".format(main_tasks_manager.name))
        logger.info("Sync tasks manager: {}".format(sync_tasks_manager.name))
        if issue_status == "edit":
            logger.info("Editing an existing task")
            old_issue_id = self.mapping_dict_issue_id[str(issue_id)] if not reverse else \
                list(self.mapping_dict_issue_id.keys())[list(self.mapping_dict_issue_id.values()).index(str(issue_id))]

            old_issue = Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key).issue.get(old_issue_id)

            updated_old_issue = old_issue.updated_on
            for arg in issue.journals:
                if arg.created_on > updated_old_issue:
                    upload_list = []  # This variable is used when we need to update attachement list
                    user = Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key).user.get(arg.user.id)
                    old_issue = Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key,
                                        impersonate=mapping("username", user.login)).issue.get(old_issue.id)
                    for detail in arg.details:
                        if detail['property'] == "attachment":
                            for attachment in issue.attachments:
                                if detail['new_value'] == attachment.filename:
                                    Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key). \
                                        download(attachment.content_url, savepath='/tmp/', filename=attachment.filename)
                                    upload_list.append(
                                        {'path': '/tmp/' + attachment.filename, 'filename': attachment.filename,
                                         'description': attachment.description})
                            if 'old_value' in detail and 'new_value' not in detail:
                                for attachment in old_issue.attachemnts:
                                    if detail['old_value'] == attachment.filename:
                                        pass
                                        # It is not working because Redmine python api doesn't support it
                                        # Redmine(main_tasks_manager.url, key=sync_tasks_manager.api_key).attachment.delete(attachment.id)

                        elif detail['name'] == "tracker_id":
                            old_issue.tracker_id = mapping("tracker_id", detail['new_value'])
                        elif detail['name'] == "subject":
                            old_issue.subject = detail['new_value']
                        elif detail['name'] == "description":
                            old_issue.description = detail['new_value']
                        elif detail['name'] == "status_id":
                            old_issue.status_id = mapping("status_id", detail['new_value'])
                        elif detail['name'] == "assigned_to_id":
                            old_issue.assigned_to_id = mapping("user_id", detail['new_value'])
                        elif detail['name'] == "estimated_hours":
                            old_issue.estimated_hours = detail['new_value']
                    if len(upload_list):
                        old_issue.uploads = upload_list
                    old_issue.save()
                    if hasattr(arg, "notes"):
                        old_issue.notes = arg.notes
                        old_issue.save()
                        # TODO
                        # adding parent id
                        # addining new task after full sync
                        # tests
            # remove old times entries
            for time_entry in Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key).time_entry.filter(
                    issue_id=old_issue.id):
                Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key).time_entry.delete(time_entry.id)

            # We need to add all times entries again
            time_entries = Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key).time_entry.filter(
                issue_id=issue.id)
            if time_entries:
                logger.info("In the time entries block")
                # import ipdb;ipdb.set_trace()
                for time_entry in time_entries:
                    user = Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key).user.get(time_entry.user.id)
                    new_time_entry = Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key,
                                             impersonate=mapping("username", user.login)).time_entry.new()
                    new_time_entry.user_id = self.mapping("user_id", time_entry.user.id)

                    if issue.id == time_entry.issue.id:
                        # if we have related tasks then time entries are doubled and we need to check it
                        new_time_entry.issue_id = old_issue.id
                        new_time_entry.spent_on = time_entry.spent_on
                        new_time_entry.hours = time_entry.hours
                        new_time_entry.activity_id = time_entry.activity.id
                        new_time_entry.comments = time_entry.comments
                        new_time_entry.save()
            logger.info("Task was edited")
        elif issue_status == "new":
            logger.info("Create a new task")
            user = Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key).user.get(issue.author.id)
            new_issue = Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key,
                                impersonate=mapping("username", user.login)).issue.new()
            logger.info("Project name {}".format(mapping("project_name", project_id)))
            new_issue.project_id = mapping("project_name", project_id)
            new_issue.subject = issue.subject
            new_issue.tracker_id = mapping("tracker_id", issue.tracker.id)
            new_issue.description = issue.description
            new_issue.status_id = mapping("status_id", issue.status.id)
            new_issue.priority_id = mapping("priority_id", issue.priority.id)
            if hasattr(issue, "assigned_to"):
                new_issue.assigned_to_id = mapping("user_id", issue.assigned_to.id)
            if hasattr(issue, "watchers"):
                # It's not working!
                watcher_user_ids = []
                for watcher in issue.watchers:
                    watcher_user_ids.append(mapping("user_id", watcher.id))
                new_issue.watcher_user_ids = watcher_user_ids
            if hasattr(issue, "start_date"):
                new_issue.start_date = issue.start_date
            if hasattr(issue, "due_date"):
                new_issue.due_date = issue.due_date
            if hasattr(issue, "estimated_hours"):
                new_issue.estimated_hours = issue.estimated_hours
            new_issue.done_ratio = issue.done_ratio
            if issue.attachments:
                logger.info("Updated issue has attachments. Creating attachements")
                upload_list = []
                for attachment in issue.attachments:
                    Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key).download(attachment.content_url, savepath='/tmp/', filename=attachment.filename)
                    upload_list.append({'path': '/tmp/' + attachment.filename, 'filename': attachment.filename,
                                        'description': attachment.description})

                new_issue.uploads = upload_list
            new_issue.save()
            if reverse:
                self.mapping_dict_issue_id[str(new_issue.id)] = issue.id
            else:
                self.mapping_dict_issue_id[str(issue.id)] = new_issue.id

            self.save()
            time_entries = Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key).time_entry.filter(issue_id=issue.id)
            if time_entries:
                logger.info("In the time entries block")
                for time_entry in time_entries:
                    user = main_tasks_manager.user.get(time_entry.user.id)
                    new_time_entry = Redmine(sync_tasks_manager.url, key=sync_tasks_manager.api_key,
                                             impersonate=mapping("username", user.login)).time_entry.new()
                    new_time_entry.user_id = mapping("user_id", time_entry.user.id)

                    if issue.id == time_entry.issue.id:
                        # if we have related tasks then time entries are doubled and we need to check it
                        new_time_entry.issue_id = new_issue.id
                        new_time_entry.spent_on = time_entry.spent_on
                        new_time_entry.hours = time_entry.hours
                        new_time_entry.activity_id = time_entry.activity.id
                        new_time_entry.comments = time_entry.comments
                        new_time_entry.save()
            # adding notes to the task

            if issue.journals:
                for arg in issue.journals:
                    if arg.notes:
                        user = Redmine(main_tasks_manager.url, key=main_tasks_manager.api_key).user.get(arg.user.id)
                        update_issue = Redmine(sync_tasks_manager.url, key=self.sync_tasks_manager.api_key,
                                               impersonate=mapping("username", user.login)).issue.get(new_issue.id)
                        update_issue.notes = arg.notes
                        update_issue.save()
            logger.info("New task was created.")