<commit_before>package com.atlassian.jira.plugins.bitbucket.activeobjects;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.activeobjects.external.ActiveObjects;
import com.atlassian.activeobjects.external.ActiveObjectsUpgradeTask;
import com.atlassian.activeobjects.external.ModelVersion;
import com.atlassian.jira.plugins.bitbucket.activeobjects.v1.IssueMapping;
import com.atlassian.jira.plugins.bitbucket.activeobjects.v1.ProjectMapping;
import com.atlassian.jira.plugins.bitbucket.api.Changeset;
import com.atlassian.jira.plugins.bitbucket.api.Encryptor;
import com.atlassian.jira.plugins.bitbucket.api.RepositoryPersister;
import com.atlassian.jira.plugins.bitbucket.api.impl.DefaultRepositoryPersister;
import com.atlassian.jira.plugins.bitbucket.api.impl.DefaultSourceControlRepository;
import com.atlassian.jira.plugins.bitbucket.spi.bitbucket.BitbucketChangesetFactory;
import com.atlassian.jira.plugins.bitbucket.spi.bitbucket.BitbucketCommunicator;
import com.atlassian.jira.plugins.bitbucket.spi.bitbucket.RepositoryUri;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;

/**
 */
public class PropertyMigrator implements ActiveObjectsUpgradeTask
{
    private final Logger logger = LoggerFactory.getLogger(PropertyMigrator.class);

    private final ProjectManager projectManager;
    private final BitbucketProjectSettings settings;
    private final BitbucketCommunicator bitbucket;

	private final Encryptor encryptor;

    public PropertyMigrator(ProjectManager projectManager, BitbucketProjectSettings settings,
                            BitbucketCommunicator bitbucket, Encryptor encryptor)
    {
        this.projectManager = projectManager;
        this.settings = settings;
        this.bitbucket = bitbucket;
		this.encryptor = encryptor;
    }

    public void upgrade(ModelVersion modelVersion, final ActiveObjects activeObjects)
    {
        logger.debug("upgrade [ " + modelVersion + " ]");

        //noinspection unchecked
        activeObjects.migrate(IssueMapping.class, ProjectMapping.class);

        // why DI doesn't work for this?
    	RepositoryPersister repositoryPersister = new DefaultRepositoryPersister(activeObjects);
    	
        List<Project> projects = projectManager.getProjectObjects();
        for (Project project : projects)
        {
            String projectKey = project.getKey();

            List<String> repositories = settings.getRepositories(projectKey);
            for (String repository : repositories)
            {
                try
                {

                    String username = settings.getUsername(projectKey, repository);
                    String password = settings.getPassword(projectKey, repository);

                    RepositoryUri uri = RepositoryUri.parse(repository);
                    logger.debug("migrate repository [ {} ]", uri);
                    ProjectMapping pm = repositoryPersister.addRepository(projectKey, uri, username, password);

                    String decryptedPassword = encryptor.decrypt(pm.getPassword(), pm.getProjectKey(), pm.getRepositoryUri());
                    DefaultSourceControlRepository repo = new DefaultSourceControlRepository(pm.getID(), RepositoryUri.parse(pm.getRepositoryUri()).getRepositoryUrl(),
							pm.getProjectKey(), pm.getUsername(), decryptedPassword);

                    List<String> issueIds = settings.getIssueIds(projectKey, repository);
                    for (String issueId : issueIds)
                    {

                        List<String> commits = settings.getCommits(projectKey, repository, issueId);

                        for (String commit : commits)
                        {
                            URL changesetURL = new URL(commit);
                            String changesetPath = changesetURL.getPath();
                            String node = changesetPath.substring(changesetPath.lastIndexOf("/") + 1);
                            logger.debug("add changeset [ {} ] to [ {} ]", changesetPath, issueId);

							Changeset changeset = BitbucketChangesetFactory.load(bitbucket, repo, node);
							repositoryPersister.addChangeset(issueId, changeset);
                        }
                    }
                }
                catch (MalformedURLException e)
                {
                    logger.error("invalid repository url [ " + repository + " ] was not processed");
                }
            }
        }
        logger.debug("completed property migration");
    }
<commit_msg>BBC-72 removing dependencies to Bitbucket classes<commit_after>package com.atlassian.jira.plugins.bitbucket.activeobjects;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.activeobjects.external.ActiveObjects;
import com.atlassian.activeobjects.external.ActiveObjectsUpgradeTask;
import com.atlassian.activeobjects.external.ModelVersion;
import com.atlassian.jira.plugins.bitbucket.activeobjects.v1.IssueMapping;
import com.atlassian.jira.plugins.bitbucket.activeobjects.v1.ProjectMapping;
import com.atlassian.jira.plugins.bitbucket.api.RepositoryPersister;
import com.atlassian.jira.plugins.bitbucket.api.impl.DefaultRepositoryPersister;
import com.atlassian.jira.plugins.bitbucket.spi.bitbucket.RepositoryUri;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;

/**
 */
public class PropertyMigrator implements ActiveObjectsUpgradeTask
{
    private final Logger logger = LoggerFactory.getLogger(PropertyMigrator.class);

    private final ProjectManager projectManager;
    private final BitbucketProjectSettings settings;


    public PropertyMigrator(ProjectManager projectManager, BitbucketProjectSettings settings)
    {
        this.projectManager = projectManager;
        this.settings = settings;
    }

    public void upgrade(ModelVersion modelVersion, final ActiveObjects activeObjects)
    {
        logger.debug("upgrade [ " + modelVersion + " ]");

        //noinspection unchecked
        activeObjects.migrate(IssueMapping.class, ProjectMapping.class);

        // why DI doesn't work for this?
    	RepositoryPersister repositoryPersister = new DefaultRepositoryPersister(activeObjects);
    	
        List<Project> projects = projectManager.getProjectObjects();
        for (Project project : projects)
        {
            String projectKey = project.getKey();

            List<String> repositories = settings.getRepositories(projectKey);
            for (String repository : repositories)
            {

                String username = settings.getUsername(projectKey, repository);
                String password = settings.getPassword(projectKey, repository);

                String repositoryUrl = RepositoryUri.parse(repository).getRepositoryUrl(); // re-convert the url in case it's in short format "owner/slug";
                logger.debug("migrate repository [ {} ]", repositoryUrl);
                repositoryPersister.addRepository(projectKey, repositoryUrl, username, password);
                // After addding repository we should synchronise now. 
                // Synchronisation can be triggered from RepositoryManager, 
                // but injecting it into this class doesn't work.
                // Until we come with better solutions users with old data 
                // will have to manually trigger Force Synchronisation
/*
                try
                {
                    String decryptedPassword = encryptor.decrypt(pm.getPassword(), pm.getProjectKey(), pm.getRepositoryUri());
                    DefaultSourceControlRepository repo = new DefaultSourceControlRepository(pm.getID(), RepositoryUri.parse(pm.getRepositoryUri()).getRepositoryUrl(),
							pm.getProjectKey(), pm.getUsername(), decryptedPassword);

                    List<String> issueIds = settings.getIssueIds(projectKey, repository);
                    for (String issueId : issueIds)
                    {

                        List<String> commits = settings.getCommits(projectKey, repository, issueId);

                        for (String commit : commits)
                        {
                            URL changesetURL = new URL(commit);
                            String changesetPath = changesetURL.getPath();
                            String node = changesetPath.substring(changesetPath.lastIndexOf("/") + 1);
                            logger.debug("add changeset [ {} ] to [ {} ]", changesetPath, issueId);

							Changeset changeset = BitbucketChangesetFactory.load(bitbucket, repo, node);
							repositoryPersister.addChangeset(issueId, changeset);
                        }
                    }
                }
                catch (MalformedURLException e)
                {
                    logger.error("invalid repository url [ " + repository + " ] was not processed");
                }
*/
            }
        }
        logger.debug("completed property migration");
    }

{'fraction_non_alphanumeric': 0.0856043956043956, 'fraction_numerical': 0.0013186813186813187, 'mean_word_length': 2.6072136345620294, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5715279', 'n_tokens_mistral': 2340, 'n_tokens_neox': 2192, 'n_words': 445}