Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialise Labels/Assignee collections when referenced #2993

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Label="Strong name signing">
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)\key.snk</AssemblyOriginatorKeyFile>
Expand Down
32 changes: 32 additions & 0 deletions Octokit.Tests/Models/IssueUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Linq;
using Xunit;

namespace Octokit.Tests.Models
{
public class IssueUpdateTests
{
[Fact]
public void Can_Initialise_With_Label()
{
var issueUpdate = new IssueUpdate
{
Labels = { "Foo" }
};

Assert.Single(issueUpdate.Labels);
Assert.Equal("Foo", issueUpdate.Labels.First());
}

[Fact]
public void Can_Initialise_With_Assignee()
{
var issueUpdate = new IssueUpdate
{
Assignees = { "Foo" }
};

Assert.Single(issueUpdate.Assignees);
Assert.Equal("Foo", issueUpdate.Assignees.First());
}
}
}
70 changes: 11 additions & 59 deletions Octokit/Models/Request/IssueUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Octokit.Internal;
Expand All @@ -12,6 +11,9 @@ namespace Octokit
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class IssueUpdate
{
private ICollection<string> _labels;
private ICollection<string> _assignees;

/// <summary>
/// Title of the issue (required)
/// </summary>
Expand All @@ -28,7 +30,7 @@ public class IssueUpdate
/// <remarks>
/// Only users with push access can set the multiple assignees for new issues. The assignees are silently dropped otherwise.
/// </remarks>
public ICollection<string> Assignees { get; private set; }
public ICollection<string> Assignees => _assignees ??= new List<string>();

/// <summary>
/// Milestone to associate this issue with.
Expand All @@ -46,7 +48,7 @@ public class IssueUpdate
/// <remarks>
/// Only users with push access can set labels for new issues. Labels are silently dropped otherwise.
/// </remarks>
public ICollection<string> Labels { get; private set; }
public ICollection<string> Labels => _labels ??= new List<string>();

/// <summary>
/// Whether the issue is open or closed.
Expand All @@ -58,26 +60,14 @@ public class IssueUpdate
/// </summary>
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);

/// <summary>
/// Adds the specified assignees to the issue.
/// </summary>
/// <param name="name">The login of the assignee.</param>
public void AddAssignee(string name)
{
// lazily create the assignees array
if (Assignees == null)
{
Assignees = new List<string>();
}

Assignees.Add(name);
}

Expand All @@ -86,15 +76,7 @@ public void AddAssignee(string name)
/// </summary>
public void ClearAssignees()
{
// lazily create the assignees array
if (Assignees == null)
{
Assignees = new List<string>();
}
else
{
Assignees.Clear();
}
Assignees.Clear();
}

/// <summary>
Expand All @@ -103,15 +85,7 @@ public void ClearAssignees()
/// <param name="name">The login of the assignee to remove</param>
public void RemoveAssignee(string name)
{
// lazily create the assignees array
if (Assignees == null)
{
Assignees = new List<string>();
}
else
{
Assignees.Remove(name);
}
Assignees.Remove(name);
}

/// <summary>
Expand All @@ -120,12 +94,6 @@ public void RemoveAssignee(string name)
/// <param name="name">The name of the label.</param>
public void AddLabel(string name)
{
// lazily create the label array
if (Labels == null)
{
Labels = new List<string>();
}

Labels.Add(name);
}

Expand All @@ -134,15 +102,7 @@ public void AddLabel(string name)
/// </summary>
public void ClearLabels()
{
// lazily create the label array
if (Labels == null)
{
Labels = new List<string>();
}
else
{
Labels.Clear();
}
Labels.Clear();
}

/// <summary>
Expand All @@ -151,15 +111,7 @@ public void ClearLabels()
/// <param name="name">The name of the label to remove</param>
public void RemoveLabel(string name)
{
// lazily create the label array
if (Labels == null)
{
Labels = new List<string>();
}
else
{
Labels.Remove(name);
}
Labels.Remove(name);
}
}
}
Loading