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

Implement improved labels API #1802

Merged
merged 10 commits into from
May 17, 2018
6 changes: 4 additions & 2 deletions Octokit.Tests.Integration/Clients/IssuesLabelsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IssuesLabelsClientTests()
public async Task CanListIssueLabelsForAnIssue()
{
var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue" };
var newLabel = new NewLabel("test label", "FFFFFF");
var newLabel = new NewLabel("test label", "FFFFFF", "Test label description.");

var label = await _issuesLabelsClient.Create(_context.RepositoryOwner, _context.RepositoryName, newLabel);
var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue);
Expand All @@ -46,13 +46,14 @@ public async Task CanListIssueLabelsForAnIssue()

Assert.Equal(1, issueLabelsInfo.Count);
Assert.Equal(newLabel.Color, issueLabelsInfo[0].Color);
Assert.Equal(newLabel.Description, issueLabelsInfo[0].Description);
}

[IntegrationTest]
public async Task CanListIssueLabelsForAnIssueWithRepositoryId()
{
var newIssue = new NewIssue("A test issue") { Body = "A new unassigned issue" };
var newLabel = new NewLabel("test label", "FFFFFF");
var newLabel = new NewLabel("test label", "FFFFFF", "Test label description.");

var label = await _issuesLabelsClient.Create(_context.Repository.Id, newLabel);
var issue = await _issuesClient.Create(_context.RepositoryOwner, _context.RepositoryName, newIssue);
Expand All @@ -69,6 +70,7 @@ public async Task CanListIssueLabelsForAnIssueWithRepositoryId()

Assert.Equal(1, issueLabelsInfo.Count);
Assert.Equal(newLabel.Color, issueLabelsInfo[0].Color);
Assert.Equal(newLabel.Description, issueLabelsInfo[0].Description);
}

[IntegrationTest]
Expand Down
8 changes: 4 additions & 4 deletions Octokit.Tests/Clients/IssuesLabelsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public void CreatesCorrectUrl()

client.Create("fake", "repo", newLabel);

connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/labels"), newLabel);
connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/labels"), newLabel, "application/vnd.github.symmetra-preview+json");
}

[Fact]
Expand All @@ -533,7 +533,7 @@ public void CreatesCorrectUrlWithRepositoryId()

client.Create(1, newLabel);

connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/labels"), newLabel);
connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/labels"), newLabel, "application/vnd.github.symmetra-preview+json");
}

[Fact]
Expand Down Expand Up @@ -565,7 +565,7 @@ public void UpdatesCorrectUrl()

client.Update("fake", "repo", "labelName", labelUpdate);

connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/labels/labelName"), labelUpdate);
connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repos/fake/repo/labels/labelName"), labelUpdate, "application/vnd.github.symmetra-preview+json");
}

[Fact]
Expand All @@ -578,7 +578,7 @@ public void UpdatesCorrectUrlWithRepositoryId()

client.Update(1, "labelName", labelUpdate);

connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/labels/labelName"), labelUpdate);
connection.Received().Post<Label>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/labels/labelName"), labelUpdate, "application/vnd.github.symmetra-preview+json");
}

[Fact]
Expand Down
8 changes: 4 additions & 4 deletions Octokit/Clients/IssuesLabelsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public Task<Label> Create(string owner, string name, NewLabel newLabel)
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNull(newLabel, nameof(newLabel));

return ApiConnection.Post<Label>(ApiUrls.Labels(owner, name), newLabel);
return ApiConnection.Post<Label>(ApiUrls.Labels(owner, name), newLabel, AcceptHeaders.LabelsApiPreview);
}

/// <summary>
Expand All @@ -303,7 +303,7 @@ public Task<Label> Create(long repositoryId, NewLabel newLabel)
{
Ensure.ArgumentNotNull(newLabel, nameof(newLabel));

return ApiConnection.Post<Label>(ApiUrls.Labels(repositoryId), newLabel);
return ApiConnection.Post<Label>(ApiUrls.Labels(repositoryId), newLabel, AcceptHeaders.LabelsApiPreview);
}

/// <summary>
Expand All @@ -323,7 +323,7 @@ public Task<Label> Update(string owner, string name, string labelName, LabelUpda
Ensure.ArgumentNotNullOrEmptyString(labelName, nameof(labelName));
Ensure.ArgumentNotNull(labelUpdate, nameof(labelUpdate));

return ApiConnection.Post<Label>(ApiUrls.Label(owner, name, labelName), labelUpdate);
return ApiConnection.Post<Label>(ApiUrls.Label(owner, name, labelName), labelUpdate, AcceptHeaders.LabelsApiPreview);
}

/// <summary>
Expand All @@ -340,7 +340,7 @@ public Task<Label> Update(long repositoryId, string labelName, LabelUpdate label
Ensure.ArgumentNotNullOrEmptyString(labelName, nameof(labelName));
Ensure.ArgumentNotNull(labelUpdate, nameof(labelUpdate));

return ApiConnection.Post<Label>(ApiUrls.Label(repositoryId, labelName), labelUpdate);
return ApiConnection.Post<Label>(ApiUrls.Label(repositoryId, labelName), labelUpdate, AcceptHeaders.LabelsApiPreview);
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions Octokit/Helpers/AcceptHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static class AcceptHeaders

public const string PreReceiveEnvironmentsPreview = "application/vnd.github.eye-scream-preview+json";

public const string LabelsApiPreview = "application/vnd.github.symmetra-preview+json";

/// <summary>
/// Combines multiple preview headers. GitHub API supports Accept header with multiple
/// values separated by comma.
Expand Down
9 changes: 8 additions & 1 deletion Octokit/Models/Request/LabelUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class LabelUpdate
/// </summary>
/// <param name="name">The name of the label.</param>
/// <param name="color">The color of the label.</param>
public LabelUpdate(string name, string color)
/// <param name="description">The description of the label.</param>
public LabelUpdate(string name, string color, string description = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the rest of the library, the request model constructor should only take required parameters. Optional parameters can be set via object initializer pattern

{
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(color, nameof(color));

Name = name;
Color = color;
Description = description;
}

/// <summary>
Expand All @@ -49,6 +51,11 @@ public string Color
}
}

/// <summary>
/// A short description of the label (optional).
/// </summary>
public string Description { get; set; }

internal string DebuggerDisplay
{
get
Expand Down
16 changes: 15 additions & 1 deletion Octokit/Models/Request/NewLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,32 @@ public class NewLabel
/// </summary>
/// <param name="name">The name of the label.</param>
/// <param name="color">The color of the label.</param>
public NewLabel(string name, string color)
/// <param name="description">The description of the label.</param>
public NewLabel(string name, string color, string description = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional field shouldn't be in the ctor

{
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(color, nameof(color));

Name = name;
Color = color;
Description = description;
}

/// <summary>
/// Name of the label (required).
/// </summary>
/// <remarks>
/// Emoji can be added to label names, using either native emoji or colon-style markup. For example,
/// typing :strawberry: will render the emoji for strawberry. For a full list of available emoji and codes, see http://emoji-cheat-sheet.com/.
/// </remarks>
public string Name { get; set; }

/// <summary>
/// Color of the label (required).
/// </summary>
/// <remarks>
/// The hexadecimal color code for the label, without the leading #.
/// </remarks>
public string Color
{
get { return _color; }
Expand All @@ -49,6 +58,11 @@ public string Color
}
}

/// <summary>
/// A short description of the label (optional).
/// </summary>
public string Description { get; set; }

internal string DebuggerDisplay
{
get
Expand Down
14 changes: 13 additions & 1 deletion Octokit/Models/Response/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public class Label
{
public Label() { }

public Label(string url, string name, string color)
public Label(string url, string name, string color, string description, bool @default)
{
Url = url;
Name = name;
Color = color;
Description = description;
IsDefault = @default;
}

/// <summary>
Expand All @@ -30,6 +32,16 @@ public Label(string url, string name, string color)
/// </summary>
public string Color { get; protected set; }

/// <summary>
/// Description of the label
/// </summary>
public string Description { get; protected set; }

/// <summary>
/// Is default label
/// </summary>
public bool IsDefault { get; protected set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will need to be named Default to match the json response field


internal string DebuggerDisplay
{
get { return string.Format(CultureInfo.InvariantCulture, "Name: {0} Color: {1}", Name, Color); }
Expand Down