CollabHistoryItemFactory.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Collaboration;
  5. using UnityEngine;
  6. #if UNITY_2019_1_OR_NEWER
  7. using UnityEngine.UIElements;
  8. #else
  9. using UnityEngine.Experimental.UIElements;
  10. using UnityEngine.Experimental.UIElements.StyleEnums;
  11. #endif
  12. namespace UnityEditor.Collaboration
  13. {
  14. internal class CollabHistoryItemFactory : ICollabHistoryItemFactory
  15. {
  16. const int k_MaxChangesPerRevision = 10;
  17. public IEnumerable<RevisionData> GenerateElements(IEnumerable<Revision> revisions, int totalRevisions, int startIndex, string tipRev, string inProgressRevision, bool revisionActionsEnabled, bool buildServiceEnabled, string currentUser)
  18. {
  19. int index = startIndex;
  20. foreach (var rev in revisions)
  21. {
  22. index++;
  23. var current = rev.revisionID == tipRev;
  24. // Calculate build status
  25. BuildState buildState = BuildState.None;
  26. int buildFailures = 0;
  27. if (rev.buildStatuses != null && rev.buildStatuses.Length > 0)
  28. {
  29. bool inProgress = false;
  30. foreach (CloudBuildStatus buildStatus in rev.buildStatuses)
  31. {
  32. if (buildStatus.complete)
  33. {
  34. if (!buildStatus.success)
  35. {
  36. buildFailures++;
  37. }
  38. }
  39. else
  40. {
  41. inProgress = true;
  42. break;
  43. }
  44. }
  45. if (inProgress)
  46. {
  47. buildState = BuildState.InProgress;
  48. }
  49. else if (buildFailures > 0)
  50. {
  51. buildState = BuildState.Failed;
  52. }
  53. else
  54. {
  55. buildState = BuildState.Success;
  56. }
  57. }
  58. else if (current && !buildServiceEnabled)
  59. {
  60. buildState = BuildState.Configure;
  61. }
  62. // Calculate the number of changes performed on files and folders (not meta files)
  63. var paths = new Dictionary<string, ChangeData>();
  64. foreach (ChangeAction change in rev.entries)
  65. {
  66. if (change.path.EndsWith(".meta"))
  67. {
  68. var path = change.path.Substring(0, change.path.Length - 5);
  69. // Actions taken on meta files are secondary to any actions taken on the main file
  70. if (!paths.ContainsKey(path))
  71. paths[path] = new ChangeData() {path = path, action = change.action};
  72. }
  73. else
  74. {
  75. paths[change.path] = new ChangeData() {path = change.path, action = change.action};
  76. }
  77. }
  78. var displayName = (rev.author != currentUser) ? rev.authorName : "You";
  79. var item = new RevisionData
  80. {
  81. id = rev.revisionID,
  82. index = totalRevisions - index + 1,
  83. timeStamp = TimeStampToDateTime(rev.timeStamp),
  84. authorName = displayName,
  85. comment = rev.comment,
  86. obtained = rev.isObtained,
  87. current = current,
  88. inProgress = (rev.revisionID == inProgressRevision),
  89. enabled = revisionActionsEnabled,
  90. buildState = buildState,
  91. buildFailures = buildFailures,
  92. changes = paths.Values.Take(k_MaxChangesPerRevision).ToList(),
  93. changesTotal = paths.Values.Count,
  94. changesTruncated = paths.Values.Count > k_MaxChangesPerRevision,
  95. };
  96. yield return item;
  97. }
  98. }
  99. private static DateTime TimeStampToDateTime(double timeStamp)
  100. {
  101. DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  102. dateTime = dateTime.AddSeconds(timeStamp).ToLocalTime();
  103. return dateTime;
  104. }
  105. }
  106. }