No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrateCollabProject.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using Codice.Client.Common.Threading;
  5. using Codice.CM.Common;
  6. using Codice.CM.WorkspaceServer;
  7. using Codice.LogWrapper;
  8. using Unity.PlasticSCM.Editor.AssetUtils;
  9. using Unity.PlasticSCM.Editor.Hub;
  10. using Unity.PlasticSCM.Editor.WebApi;
  11. namespace Unity.PlasticSCM.Editor.CollabMigration
  12. {
  13. public static class MigrateCollabProject
  14. {
  15. internal static void Initialize()
  16. {
  17. if (SessionState.GetInt(
  18. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  19. MIGRATED_NOT_CALCULATED) == MIGRATED_NOTHING_TO_DO)
  20. return;
  21. EditorApplication.update += RunOnceWhenAccessTokenAndProjectIdAreInitialized;
  22. }
  23. internal static void RunOnceWhenAccessTokenAndProjectIdAreInitialized()
  24. {
  25. if (string.IsNullOrEmpty(CloudProjectSettings.accessToken))
  26. return;
  27. if (!CloudProjectId.HasValue())
  28. return;
  29. if (!SessionState.GetBool(
  30. ProcessCommand.IS_PROCESS_COMMAND_ALREADY_EXECUTED_KEY, false))
  31. return;
  32. EditorApplication.update -= RunOnceWhenAccessTokenAndProjectIdAreInitialized;
  33. string projectPath = ProjectPath.FromApplicationDataPath(
  34. ApplicationDataPath.Get());
  35. string projectGuid = CloudProjectId.GetValue();
  36. if (!ShouldProjectBeMigrated(projectPath, projectGuid))
  37. {
  38. SessionState.SetInt(
  39. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  40. MIGRATED_NOTHING_TO_DO);
  41. return;
  42. }
  43. Execute(
  44. CloudProjectSettings.accessToken,
  45. projectPath,
  46. projectGuid);
  47. }
  48. static bool ShouldProjectBeMigrated(
  49. string projectPath,
  50. string projectGuid)
  51. {
  52. if (SessionState.GetBool(
  53. ProcessCommand.IS_PLASTIC_COMMAND_KEY, false))
  54. {
  55. return false;
  56. }
  57. string collabPath = GetCollabSnapshotFile(
  58. projectPath, projectGuid);
  59. if (!File.Exists(collabPath))
  60. {
  61. return false;
  62. }
  63. if (FindWorkspace.HasWorkspace(ApplicationDataPath.Get()))
  64. {
  65. return false;
  66. }
  67. return true;
  68. }
  69. static void Execute(
  70. string unityAccessToken,
  71. string projectPath,
  72. string projectGuid)
  73. {
  74. string headCommitSha = GetCollabHeadCommitSha(projectPath, projectGuid);
  75. if (string.IsNullOrEmpty(headCommitSha))
  76. return;
  77. PlasticApp.InitializeIfNeeded();
  78. LaunchMigrationIfProjectIsArchivedAndMigrated(
  79. unityAccessToken,
  80. projectPath,
  81. projectGuid,
  82. headCommitSha);
  83. }
  84. internal static void DeletePlasticDirectoryIfExists(string projectPath)
  85. {
  86. WorkspaceInfo wkInfo = new WorkspaceInfo("wk", projectPath);
  87. string plasticDirectory = WorkspaceConfigFile.GetPlasticWkConfigPath(wkInfo);
  88. if (!Directory.Exists(plasticDirectory))
  89. return;
  90. Directory.Delete(plasticDirectory, true);
  91. }
  92. static void LaunchMigrationIfProjectIsArchivedAndMigrated(
  93. string unityAccessToken,
  94. string projectPath,
  95. string projectGuid,
  96. string headCommitSha)
  97. {
  98. IsCollabProjectMigratedResponse isMigratedResponse = null;
  99. ChangesetFromCollabCommitResponse changesetResponse = null;
  100. IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
  101. waiter.Execute(
  102. /*threadOperationDelegate*/ delegate
  103. {
  104. isMigratedResponse = WebRestApiClient.PlasticScm.
  105. IsCollabProjectMigrated(unityAccessToken, projectGuid);
  106. if (isMigratedResponse.Error != null)
  107. return;
  108. if (!isMigratedResponse.IsMigrated)
  109. return;
  110. OrganizationCredentials credentials = new OrganizationCredentials();
  111. credentials.User = isMigratedResponse.Credentials.Email;
  112. credentials.Password = isMigratedResponse.Credentials.Token;
  113. string webLoginAccessToken = WebRestApiClient.CloudServer.WebLogin(
  114. isMigratedResponse.WebServerUri,
  115. isMigratedResponse.PlasticCloudOrganizationName,
  116. credentials);
  117. changesetResponse = WebRestApiClient.CloudServer.
  118. GetChangesetFromCollabCommit(
  119. isMigratedResponse.WebServerUri,
  120. isMigratedResponse.PlasticCloudOrganizationName,
  121. webLoginAccessToken, projectGuid, headCommitSha);
  122. },
  123. /*afterOperationDelegate*/ delegate
  124. {
  125. if (waiter.Exception != null)
  126. {
  127. ExceptionsHandler.LogException(
  128. "IsCollabProjectArchivedAndMigrated",
  129. waiter.Exception);
  130. return;
  131. }
  132. if (isMigratedResponse.Error != null)
  133. {
  134. mLog.ErrorFormat(
  135. "Unable to get IsCollabProjectMigratedResponse: {0} [code {1}]",
  136. isMigratedResponse.Error.Message,
  137. isMigratedResponse.Error.ErrorCode);
  138. return;
  139. }
  140. if (!isMigratedResponse.IsMigrated)
  141. {
  142. SessionState.SetInt(
  143. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  144. MIGRATED_NOTHING_TO_DO);
  145. return;
  146. }
  147. if (changesetResponse.Error != null)
  148. {
  149. mLog.ErrorFormat(
  150. "Unable to get ChangesetFromCollabCommitResponse: {0} [code {1}]",
  151. changesetResponse.Error.Message,
  152. changesetResponse.Error.ErrorCode);
  153. return;
  154. }
  155. DeletePlasticDirectoryIfExists(projectPath);
  156. MigrationDialog.Show(
  157. null,
  158. unityAccessToken,
  159. projectPath,
  160. isMigratedResponse.Credentials.Email,
  161. isMigratedResponse.PlasticCloudOrganizationName,
  162. new RepId(
  163. changesetResponse.RepId,
  164. changesetResponse.RepModuleId),
  165. changesetResponse.ChangesetId,
  166. changesetResponse.BranchId,
  167. AfterWorkspaceMigrated);
  168. });
  169. }
  170. static void AfterWorkspaceMigrated()
  171. {
  172. SessionState.SetInt(
  173. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  174. MIGRATED_NOTHING_TO_DO);
  175. CollabPlugin.Disable();
  176. mLog.DebugFormat(
  177. "Disabled Collab Plugin after the migration for Project: {0}",
  178. ProjectPath.FromApplicationDataPath(ApplicationDataPath.Get()));
  179. }
  180. static string GetCollabHeadCommitSha(
  181. string projectPath,
  182. string projectGuid)
  183. {
  184. string collabPath = GetCollabSnapshotFile(
  185. projectPath, projectGuid);
  186. if (!File.Exists(collabPath))
  187. return null;
  188. string text = File.ReadAllText(collabPath);
  189. string[] chunks = text.Split(
  190. new string[] { "currRevisionID" },
  191. StringSplitOptions.None);
  192. string current = chunks[1].Substring(3, 40);
  193. if (!current.Contains("none"))
  194. return current;
  195. chunks = text.Split(
  196. new string[] { "headRevisionID" },
  197. StringSplitOptions.None);
  198. return chunks[1].Substring(3, 40);
  199. }
  200. static string GetCollabSnapshotFile(
  201. string projectPath,
  202. string projectGuid)
  203. {
  204. return projectPath + "/Library/Collab/CollabSnapshot_" + projectGuid + ".txt";
  205. }
  206. const string IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY =
  207. "PlasticSCM.MigrateCollabProject.IsAlreadyCalculated";
  208. const int MIGRATED_NOT_CALCULATED = 0;
  209. const int MIGRATED_NOTHING_TO_DO = 1;
  210. static readonly ILog mLog = PlasticApp.GetLogger("MigrateCollabProject");
  211. }
  212. }