PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/activeobjects/v2/To_06_GithubRepositories.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 130 lines | 105 code | 20 blank | 5 comment | 7 complexity | e2d84cfbb6cf5468e589cbc03fe2b969 MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.activeobjects.v2;
  2. import java.net.MalformedURLException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import com.atlassian.activeobjects.external.ActiveObjects;
  10. import com.atlassian.activeobjects.external.ActiveObjectsUpgradeTask;
  11. import com.atlassian.activeobjects.external.ModelVersion;
  12. import com.atlassian.jira.project.Project;
  13. import com.atlassian.jira.project.ProjectManager;
  14. import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
  15. import com.google.common.collect.Maps;
  16. /**
  17. * Data migration from jira-github-connector plugin to jira-bitbucket-connector plugin
  18. */
  19. @SuppressWarnings("unchecked")
  20. public class To_06_GithubRepositories implements ActiveObjectsUpgradeTask
  21. {
  22. private final Logger log = LoggerFactory.getLogger(To_06_GithubRepositories.class);
  23. private final PluginSettingsFactory pluginSettingsFactory;
  24. private final ProjectManager projectManager;
  25. public To_06_GithubRepositories(final ProjectManager projectManager, final PluginSettingsFactory pluginSettingsFactory)
  26. {
  27. this.projectManager = projectManager;
  28. this.pluginSettingsFactory = pluginSettingsFactory;
  29. }
  30. private List<String> getRepositories(String projectKey)
  31. {
  32. List<String> repoUrls = (List<String>) pluginSettingsFactory.createSettingsForKey(projectKey).get("githubRepositoryURLArray");
  33. return repoUrls != null ? repoUrls : Collections.<String> emptyList();
  34. }
  35. @Override
  36. public void upgrade(ModelVersion currentVersion, ActiveObjects activeObjects)
  37. {
  38. log.debug("upgrade [ " + getModelVersion() + " ]");
  39. activeObjects.migrate(ProjectMapping.class, IssueMapping.class);
  40. List<Project> projects = projectManager.getProjectObjects();
  41. for (Project project : projects)
  42. {
  43. String projectKey = project.getKey();
  44. log.debug(" === migrating repositories for project [{}] === ", projectKey);
  45. List<String> repositoriesUrls = getRepositories(projectKey);
  46. for (String repositoryUrl : repositoriesUrls)
  47. {
  48. log.debug("migrating repository [{}]", repositoryUrl);
  49. ProjectMapping pm = migrateRepository(activeObjects, projectKey, repositoryUrl);
  50. migrateIssueMappings(activeObjects, projectKey, repositoryUrl, pm);
  51. }
  52. }
  53. }
  54. private void migrateIssueMappings(ActiveObjects activeObjects, String projectKey, String repositoryUrl, ProjectMapping pm)
  55. {
  56. ArrayList<String> issueIds = (ArrayList<String>)pluginSettingsFactory.createSettingsForKey(projectKey).get("githubIssueIDs" + repositoryUrl);
  57. if (issueIds != null)
  58. {
  59. for (String issueId : issueIds)
  60. {
  61. ArrayList<String> commitArray = (ArrayList<String>) pluginSettingsFactory.createSettingsForKey(projectKey).get("githubIssueCommitArray" + issueId);
  62. for (String commit : commitArray)
  63. {
  64. String node = extractNode(commit);
  65. log.debug("migrating commit mapping node:[{}], issueId:[{}]", node, issueId);
  66. final Map<String, Object> map = Maps.newHashMap();
  67. map.put("REPOSITORY_ID", pm.getID());
  68. map.put("NODE", node);
  69. map.put("ISSUE_ID", issueId);
  70. activeObjects.create(IssueMapping.class, map);
  71. }
  72. }
  73. }
  74. }
  75. private ProjectMapping migrateRepository(ActiveObjects activeObjects, String projectKey, String repositoryUrl)
  76. {
  77. String fixedUrl = fixUrl(repositoryUrl);
  78. String access_token = (String) pluginSettingsFactory.createSettingsForKey(projectKey).get("githubRepositoryAccessToken" + repositoryUrl);
  79. final Map<String, Object> map = Maps.newHashMap();
  80. map.put("REPOSITORY_URL", fixedUrl);
  81. map.put("PROJECT_KEY", projectKey);
  82. map.put("REPOSITORY_TYPE", "github");
  83. map.put("ACCESS_TOKEN", access_token);
  84. return activeObjects.create(ProjectMapping.class, map);
  85. }
  86. private String extractNode(String commitUrl)
  87. {
  88. // "https://github.com/api/v2/json/commits/show/" + path[3] + "/" + path[4] +"/" + commitId + "?branch=" + branch
  89. return commitUrl.replaceAll("(.*/|\\?.*)", "");
  90. }
  91. public static void main(String[] args) throws MalformedURLException
  92. {
  93. String url = "https://github.com/api/v2/json/commits/show/owner/repo/jh783263h23kh?branch=mybranch";
  94. String s1 = url.replaceAll("(.*/|\\?.*)", "");
  95. System.out.println(s1);
  96. String url1 = "https://github.com/user/repo/master/blah/asfsadfasd/asdfasdfsad/fasd/";
  97. url1 = url1.replaceAll("(https://.*?/.*?/.*?)/.*", "$1");
  98. System.out.println(url1);
  99. }
  100. private String fixUrl(String repository)
  101. {
  102. // Converts "https://github.com/user/repo/master" to "https://github.com/user/repo";
  103. return repository.replaceAll("(https://.*?/.*?/.*?)/.*", "$1");
  104. }
  105. @Override
  106. public ModelVersion getModelVersion()
  107. {
  108. return ModelVersion.valueOf("6");
  109. }
  110. }