From fc283af1864abee9d93a2c7915b8d34584783508 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Sat, 25 Jan 2025 14:20:09 +0000 Subject: [PATCH 1/4] Initialise collection when accessed --- Directory.Build.props | 3 ++ Octokit/Models/Request/IssueUpdate.cs | 70 +++++---------------------- 2 files changed, 14 insertions(+), 59 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index c7568165b2..a5c3dfa99f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,4 +1,7 @@ + + latest + true $(MSBuildThisFileDirectory)\key.snk diff --git a/Octokit/Models/Request/IssueUpdate.cs b/Octokit/Models/Request/IssueUpdate.cs index 34a4821244..c023439f78 100644 --- a/Octokit/Models/Request/IssueUpdate.cs +++ b/Octokit/Models/Request/IssueUpdate.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using Octokit.Internal; @@ -12,6 +11,9 @@ namespace Octokit [DebuggerDisplay("{DebuggerDisplay,nq}")] public class IssueUpdate { + private ICollection _labels; + private ICollection _assignees; + /// /// Title of the issue (required) /// @@ -28,7 +30,7 @@ public class IssueUpdate /// /// Only users with push access can set the multiple assignees for new issues. The assignees are silently dropped otherwise. /// - public ICollection Assignees { get; private set; } + public ICollection Assignees => _assignees ??= []; /// /// Milestone to associate this issue with. @@ -46,7 +48,7 @@ public class IssueUpdate /// /// Only users with push access can set labels for new issues. Labels are silently dropped otherwise. /// - public ICollection Labels { get; private set; } + public ICollection Labels => _labels ??= []; /// /// Whether the issue is open or closed. @@ -58,13 +60,7 @@ public class IssueUpdate /// public ItemStateReason? StateReason { get; set; } - internal string DebuggerDisplay - { - get - { - return string.Format(CultureInfo.InvariantCulture, "Title: {0}", Title); - } - } + internal string DebuggerDisplay => string.Format(CultureInfo.InvariantCulture, "Title: {0}", Title); /// /// Adds the specified assignees to the issue. @@ -72,12 +68,6 @@ internal string DebuggerDisplay /// The login of the assignee. public void AddAssignee(string name) { - // lazily create the assignees array - if (Assignees == null) - { - Assignees = new List(); - } - Assignees.Add(name); } @@ -86,15 +76,7 @@ public void AddAssignee(string name) /// public void ClearAssignees() { - // lazily create the assignees array - if (Assignees == null) - { - Assignees = new List(); - } - else - { - Assignees.Clear(); - } + Assignees.Clear(); } /// @@ -103,15 +85,7 @@ public void ClearAssignees() /// The login of the assignee to remove public void RemoveAssignee(string name) { - // lazily create the assignees array - if (Assignees == null) - { - Assignees = new List(); - } - else - { - Assignees.Remove(name); - } + Assignees.Remove(name); } /// @@ -120,12 +94,6 @@ public void RemoveAssignee(string name) /// The name of the label. public void AddLabel(string name) { - // lazily create the label array - if (Labels == null) - { - Labels = new List(); - } - Labels.Add(name); } @@ -134,15 +102,7 @@ public void AddLabel(string name) /// public void ClearLabels() { - // lazily create the label array - if (Labels == null) - { - Labels = new List(); - } - else - { - Labels.Clear(); - } + Labels.Clear(); } /// @@ -151,15 +111,7 @@ public void ClearLabels() /// The name of the label to remove public void RemoveLabel(string name) { - // lazily create the label array - if (Labels == null) - { - Labels = new List(); - } - else - { - Labels.Remove(name); - } + Labels.Remove(name); } } } From 3f7a2e14031cce14cc4d1ecfb6823b2014c2c2ea Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Sat, 25 Jan 2025 14:26:24 +0000 Subject: [PATCH 2/4] IssueUpdateTests --- Octokit.Tests/Models/IssueUpdateTests.cs | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Octokit.Tests/Models/IssueUpdateTests.cs diff --git a/Octokit.Tests/Models/IssueUpdateTests.cs b/Octokit.Tests/Models/IssueUpdateTests.cs new file mode 100644 index 0000000000..5cd8c788d5 --- /dev/null +++ b/Octokit.Tests/Models/IssueUpdateTests.cs @@ -0,0 +1,25 @@ +using Xunit; + +namespace Octokit.Tests.Models +{ + public class IssueUpdateTests + { + [Fact] + public void Can_Initialise_With_Label() + { + _ = new IssueUpdate + { + Labels = { "Foo" } + }; + } + + [Fact] + public void Can_Initialise_With_Assignee() + { + _ = new IssueUpdate + { + Assignees = { "Foo" } + }; + } + } +} From 2ec5aa6d56f8bef883f8db1171de9eb23ad52b1c Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Sat, 25 Jan 2025 14:29:38 +0000 Subject: [PATCH 3/4] Can't use collection initialisers due to build error --- Octokit/Models/Request/IssueUpdate.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Octokit/Models/Request/IssueUpdate.cs b/Octokit/Models/Request/IssueUpdate.cs index c023439f78..bdf145a613 100644 --- a/Octokit/Models/Request/IssueUpdate.cs +++ b/Octokit/Models/Request/IssueUpdate.cs @@ -30,7 +30,7 @@ public class IssueUpdate /// /// Only users with push access can set the multiple assignees for new issues. The assignees are silently dropped otherwise. /// - public ICollection Assignees => _assignees ??= []; + public ICollection Assignees => _assignees ??= new List(); /// /// Milestone to associate this issue with. @@ -48,7 +48,7 @@ public class IssueUpdate /// /// Only users with push access can set labels for new issues. Labels are silently dropped otherwise. /// - public ICollection Labels => _labels ??= []; + public ICollection Labels => _labels ??= new List(); /// /// Whether the issue is open or closed. From ab0d9490f37d065f5fb52e94c59f811be0b54787 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Sat, 25 Jan 2025 14:41:10 +0000 Subject: [PATCH 4/4] Asserts --- Octokit.Tests/Models/IssueUpdateTests.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Octokit.Tests/Models/IssueUpdateTests.cs b/Octokit.Tests/Models/IssueUpdateTests.cs index 5cd8c788d5..7e2ad545ec 100644 --- a/Octokit.Tests/Models/IssueUpdateTests.cs +++ b/Octokit.Tests/Models/IssueUpdateTests.cs @@ -1,4 +1,5 @@ -using Xunit; +using System.Linq; +using Xunit; namespace Octokit.Tests.Models { @@ -7,19 +8,25 @@ public class IssueUpdateTests [Fact] public void Can_Initialise_With_Label() { - _ = new IssueUpdate + var issueUpdate = new IssueUpdate { Labels = { "Foo" } }; + + Assert.Single(issueUpdate.Labels); + Assert.Equal("Foo", issueUpdate.Labels.First()); } [Fact] public void Can_Initialise_With_Assignee() { - _ = new IssueUpdate + var issueUpdate = new IssueUpdate { Assignees = { "Foo" } }; + + Assert.Single(issueUpdate.Assignees); + Assert.Equal("Foo", issueUpdate.Assignees.First()); } } }