|
| 1 | +using GitHub.Primitives; |
| 2 | +using NullGuard; |
| 3 | +using System; |
| 4 | +using System.Globalization; |
| 5 | + |
| 6 | +namespace GitHub.Models |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A repository read from the GitHub API. |
| 10 | + /// </summary> |
| 11 | + public class RemoteRepositoryModel : RepositoryModelBase, IRemoteRepositoryModel, |
| 12 | + IEquatable<RemoteRepositoryModel>, IComparable<RemoteRepositoryModel> |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Initializes a new instance of the <see cref="RemoteRepositoryModel"/> class. |
| 16 | + /// </summary> |
| 17 | + /// <param name="id">The API ID of the repository.</param> |
| 18 | + /// <param name="name">The repository name.</param> |
| 19 | + /// <param name="cloneUrl">The repository's clone URL.</param> |
| 20 | + /// <param name="isPrivate">Whether the repository is private.</param> |
| 21 | + /// <param name="isFork">Whether the repository is a fork.</param> |
| 22 | + /// <param name="ownerAccount">The repository owner account.</param> |
| 23 | + public RemoteRepositoryModel(long id, string name, UriString cloneUrl, bool isPrivate, bool isFork, IAccount ownerAccount) |
| 24 | + : base(name, cloneUrl) |
| 25 | + { |
| 26 | + Id = id; |
| 27 | + OwnerAccount = ownerAccount; |
| 28 | + IsFork = isFork; |
| 29 | + SetIcon(isPrivate, isFork); |
| 30 | + // this is an assumption, we'd have to load the repo information from octokit to know for sure |
| 31 | + // probably not worth it for this ctor |
| 32 | + DefaultBranch = new BranchModel("master", this); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Initializes a new instance of the <see cref="RemoteRepositoryModel"/> class. |
| 37 | + /// </summary> |
| 38 | + /// <param name="repository">The source octokit repository.</param> |
| 39 | + public RemoteRepositoryModel(Octokit.Repository repository) |
| 40 | + : base(repository.Name, repository.CloneUrl) |
| 41 | + { |
| 42 | + Id = repository.Id; |
| 43 | + IsFork = repository.Fork; |
| 44 | + SetIcon(repository.Private, IsFork); |
| 45 | + OwnerAccount = new Account(repository.Owner); |
| 46 | + DefaultBranch = new BranchModel(repository.DefaultBranch, this); |
| 47 | + Parent = repository.Parent != null ? new RemoteRepositoryModel(repository.Parent) : null; |
| 48 | + if (Parent != null) |
| 49 | + Parent.DefaultBranch.DisplayName = Parent.DefaultBranch.Id; |
| 50 | + } |
| 51 | + |
| 52 | +#region Equality Things |
| 53 | + public void CopyFrom(IRemoteRepositoryModel other) |
| 54 | + { |
| 55 | + if (!Equals(other)) |
| 56 | + throw new ArgumentException("Instance to copy from doesn't match this instance. this:(" + this + ") other:(" + other + ")", nameof(other)); |
| 57 | + Icon = other.Icon; |
| 58 | + } |
| 59 | + |
| 60 | + public override int GetHashCode() |
| 61 | + { |
| 62 | + return Id.GetHashCode(); |
| 63 | + } |
| 64 | + |
| 65 | + public override bool Equals([AllowNull]object obj) |
| 66 | + { |
| 67 | + if (ReferenceEquals(this, obj)) |
| 68 | + return true; |
| 69 | + var other = obj as RemoteRepositoryModel; |
| 70 | + return Equals(other); |
| 71 | + } |
| 72 | + |
| 73 | + public bool Equals([AllowNull]IRemoteRepositoryModel other) |
| 74 | + { |
| 75 | + if (ReferenceEquals(this, other)) |
| 76 | + return true; |
| 77 | + return other != null && Id == other.Id; |
| 78 | + } |
| 79 | + |
| 80 | + public bool Equals([AllowNull]RemoteRepositoryModel other) |
| 81 | + { |
| 82 | + if (ReferenceEquals(this, other)) |
| 83 | + return true; |
| 84 | + return other != null && Id == other.Id; |
| 85 | + } |
| 86 | + |
| 87 | + public int CompareTo([AllowNull]IRemoteRepositoryModel other) |
| 88 | + { |
| 89 | + return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1; |
| 90 | + } |
| 91 | + |
| 92 | + public int CompareTo([AllowNull]RemoteRepositoryModel other) |
| 93 | + { |
| 94 | + return other != null ? UpdatedAt.CompareTo(other.UpdatedAt) : 1; |
| 95 | + } |
| 96 | + |
| 97 | + public static bool operator >([AllowNull]RemoteRepositoryModel lhs, [AllowNull]RemoteRepositoryModel rhs) |
| 98 | + { |
| 99 | + if (ReferenceEquals(lhs, rhs)) |
| 100 | + return false; |
| 101 | + return lhs?.CompareTo(rhs) > 0; |
| 102 | + } |
| 103 | + |
| 104 | + public static bool operator <([AllowNull]RemoteRepositoryModel lhs, [AllowNull]RemoteRepositoryModel rhs) |
| 105 | + { |
| 106 | + if (ReferenceEquals(lhs, rhs)) |
| 107 | + return false; |
| 108 | + return (object)lhs == null || lhs.CompareTo(rhs) < 0; |
| 109 | + } |
| 110 | + |
| 111 | + public static bool operator ==([AllowNull]RemoteRepositoryModel lhs, [AllowNull]RemoteRepositoryModel rhs) |
| 112 | + { |
| 113 | + return ReferenceEquals(lhs, rhs); |
| 114 | + } |
| 115 | + |
| 116 | + public static bool operator !=([AllowNull]RemoteRepositoryModel lhs, [AllowNull]RemoteRepositoryModel rhs) |
| 117 | + { |
| 118 | + return !(lhs == rhs); |
| 119 | + } |
| 120 | +#endregion |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Gets the account that is the ower of the repository. |
| 124 | + /// </summary> |
| 125 | + public IAccount OwnerAccount { get; } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Gets the repository's API ID. |
| 129 | + /// </summary> |
| 130 | + public long Id { get; } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Gets the date and time at which the repository was created. |
| 134 | + /// </summary> |
| 135 | + public DateTimeOffset CreatedAt { get; set; } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Gets the repository's last update date and time. |
| 139 | + /// </summary> |
| 140 | + public DateTimeOffset UpdatedAt { get; set; } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Gets a value indicating whether the repository is a fork. |
| 144 | + /// </summary> |
| 145 | + public bool IsFork { get; } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Gets the repository from which this repository was forked, if any. |
| 149 | + /// </summary> |
| 150 | + [AllowNull] public IRemoteRepositoryModel Parent { [return: AllowNull] get; } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Gets the default branch for the repository. |
| 154 | + /// </summary> |
| 155 | + public IBranch DefaultBranch { get; } |
| 156 | + |
| 157 | + internal string DebuggerDisplay |
| 158 | + { |
| 159 | + get |
| 160 | + { |
| 161 | + return String.Format(CultureInfo.InvariantCulture, |
| 162 | + "{4}\tId: {0} Name: {1} CloneUrl: {2} Account: {3}", Id, Name, CloneUrl, Owner, GetHashCode()); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments