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

#492 feat: renaming label Name using RenameFrom in Configuration #493

Closed
Closed
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
9 changes: 5 additions & 4 deletions docs/input/docs/commands/label.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ default labels, so that you can have some consistency across your various
projects. While GitHub, and other Source Control systems provide a set of
default labels, they are not always exactly what you want. This command will
remove all the current labels configured within a repository, and create a new
set of them.
set of them. However, if RenameFrom is specified in the label configuration, the
label can be renamed instead of being deleted and recreated.

:::{.alert .alert-info}
**NOTE:**

The available list of labels that are created is currently hard-coded into
GitReleaseManager, it is not possible to configure them. This will come in a
later version.
The available list of labels that are created by default is currently hard-coded into
GitReleaseManager, it is possible to configure them by overriding the list. See
[Default Configuration](../configuration/default-configuration.md) for more details.
:::

## **Required Parameters**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public async Task Should_Execute_Command()
RepositoryName = "repository",
};

_vcsService.CreateLabelsAsync(options.RepositoryOwner, options.RepositoryName)
_vcsService.CreateOrUpdateLabelsAsync(options.RepositoryOwner, options.RepositoryName)
.Returns(Task.CompletedTask);

var result = await _command.Execute(options).ConfigureAwait(false);
result.ShouldBe(0);

await _vcsService.Received(1).CreateLabelsAsync(options.RepositoryOwner, options.RepositoryName).ConfigureAwait(false);
await _vcsService.Received(1).CreateOrUpdateLabelsAsync(options.RepositoryOwner, options.RepositoryName).ConfigureAwait(false);
_logger.Received(1).Information(Arg.Any<string>());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitReleaseManager.Core.Tests/VcsServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task Should_Create_Labels()
_vcsProvider.CreateLabelAsync(OWNER, REPOSITORY, Arg.Any<Label>())
.Returns(Task.CompletedTask);

await _vcsService.CreateLabelsAsync(OWNER, REPOSITORY).ConfigureAwait(false);
await _vcsService.CreateOrUpdateLabelsAsync(OWNER, REPOSITORY).ConfigureAwait(false);

await _vcsProvider.Received(1).GetLabelsAsync(OWNER, REPOSITORY).ConfigureAwait(false);
await _vcsProvider.Received(labels.Count).DeleteLabelAsync(OWNER, REPOSITORY, Arg.Any<string>()).ConfigureAwait(false);
Expand All @@ -227,7 +227,7 @@ public async Task Should_Log_An_Warning_When_Labels_Not_Configured()
{
_configuration.Labels.Clear();

await _vcsService.CreateLabelsAsync(OWNER, REPOSITORY).ConfigureAwait(false);
await _vcsService.CreateOrUpdateLabelsAsync(OWNER, REPOSITORY).ConfigureAwait(false);

_logger.Received(1).Warning(Arg.Any<string>());
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitReleaseManager.Core/Commands/LabelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public LabelCommand(IVcsService vcsService, ILogger logger)
public async Task<int> Execute(LabelSubOptions options)
{
_logger.Information("Creating standard labels");
await _vcsService.CreateLabelsAsync(options.RepositoryOwner, options.RepositoryName).ConfigureAwait(false);
await _vcsService.CreateOrUpdateLabelsAsync(options.RepositoryOwner, options.RepositoryName).ConfigureAwait(false);

return 0;
}
Expand Down
11 changes: 5 additions & 6 deletions src/GitReleaseManager.Core/Configuration/LabelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ namespace GitReleaseManager.Core.Configuration
{
public class LabelConfig
{
[YamlMember(Alias = "name")]
public string Name { get; set; }
[YamlMember(Alias = "name")] public string Name { get; set; }

[YamlMember(Alias = "description")]
public string Description { get; set; }
[YamlMember(Alias = "description")] public string Description { get; set; }

[YamlMember(Alias = "color")]
public string Color { get; set; }
[YamlMember(Alias = "color")] public string Color { get; set; }

[YamlMember(Alias = "renameFrom")] public string RenameFrom { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/GitReleaseManager.Core/IVcsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public interface IVcsService

Task PublishReleaseAsync(string owner, string repository, string tagName);

Task CreateLabelsAsync(string owner, string repository);
Task CreateOrUpdateLabelsAsync(string owner, string repository);
}
}
10 changes: 10 additions & 0 deletions src/GitReleaseManager.Core/Provider/GitHubProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ public Task CreateLabelAsync(string owner, string repository, Label label)
});
}

public Task UpdateLabelAsync(string owner, string repository, string originalName, Label label)
{
return ExecuteAsync(async () =>
{
var updatedLabel = _mapper.Map<LabelUpdate>(label);

await _gitHubClient.Issue.Labels.Update(owner, repository, originalName, updatedLabel);
});
}

public Task DeleteLabelAsync(string owner, string repository, string labelName)
{
return ExecuteAsync(async () =>
Expand Down
2 changes: 2 additions & 0 deletions src/GitReleaseManager.Core/Provider/IVcsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IVcsProvider
Task<IEnumerable<IssueComment>> GetIssueCommentsAsync(string owner, string repository, int issueNumber);

Task CreateLabelAsync(string owner, string repository, Label label);

Task UpdateLabelAsync(string owner, string repository, string existingLabelName,Label label);

Task DeleteLabelAsync(string owner, string repository, string labelName);

Expand Down
50 changes: 41 additions & 9 deletions src/GitReleaseManager.Core/VcsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,33 +290,65 @@ public async Task PublishReleaseAsync(string owner, string repository, string ta
}
}

public async Task CreateLabelsAsync(string owner, string repository)
public async Task CreateOrUpdateLabelsAsync(string owner, string repository)
{
if (_configuration.Labels.Any())
{
var newLabels = new List<Label>();
var updatedLabels = new List<Tuple<string, Label>>();

_logger.Verbose("Grabbing all existing labels on '{Owner}/{Repository}'", owner, repository);
var existingLabels = await _vcsProvider.GetLabelsAsync(owner, repository).ConfigureAwait(false);

IEnumerable<Label> existingLabelsEnumerable = existingLabels as Label[] ?? existingLabels.ToArray();
foreach (var label in _configuration.Labels)
{
newLabels.Add(new Label
var newLabel = new Label
{
Name = label.Name,
Color = label.Color,
Description = label.Description,
});
}
};

_logger.Verbose("Grabbing all existing labels on '{Owner}/{Repository}'", owner, repository);
var labels = await _vcsProvider.GetLabelsAsync(owner, repository).ConfigureAwait(false);
// Labels that don't exist yet will be created, otherwise they will be updated given RenameFrom matches an existing label
if (!string.IsNullOrEmpty(label.RenameFrom) &&
existingLabelsEnumerable.Any(el => el.Name == label.RenameFrom))
{
updatedLabels.Add(new Tuple<string, Label>(label.RenameFrom, newLabel));
}
else
{
if (!string.IsNullOrEmpty(label.RenameFrom))
{
_logger.Warning(
"Label '{RenameFrom}' does not exist, creating new label '{Name}' instead",
label.RenameFrom, label.Name);
}

newLabels.Add(newLabel);
}
}

// Process labels to delete
var deletedLabels = existingLabelsEnumerable.Where(el => updatedLabels.All(n => n.Item1 != el.Name))
.ToList();
_logger.Verbose("Removing existing labels");
_logger.Debug("{@Labels}", labels);
var deleteLabelsTasks = labels.Select(label => _vcsProvider.DeleteLabelAsync(owner, repository, label.Name));
_logger.Debug("{@Labels}", deletedLabels);
var deleteLabelsTasks =
deletedLabels.Select(label => _vcsProvider.DeleteLabelAsync(owner, repository, label.Name));
await Task.WhenAll(deleteLabelsTasks).ConfigureAwait(false);

// Update labels which have a RenameFrom that can be found in the existing labels
_logger.Verbose("Updating existing labels");
_logger.Debug("{@Labels}", updatedLabels);
var updateLabelsTasks = updatedLabels.Select(label =>
_vcsProvider.UpdateLabelAsync(owner, repository, label.Item1, label.Item2));
await Task.WhenAll(updateLabelsTasks).ConfigureAwait(false);

_logger.Verbose("Creating new standard labels");
_logger.Debug("{@Labels}", newLabels);
var createLabelsTasks = newLabels.Select(label => _vcsProvider.CreateLabelAsync(owner, repository, label));
var createLabelsTasks =
newLabels.Select(label => _vcsProvider.CreateLabelAsync(owner, repository, label));
await Task.WhenAll(createLabelsTasks).ConfigureAwait(false);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/GitReleaseManager.Tests/VcsServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Task PublishReleaseAsync(string owner, string repository, string tagName)
throw new System.NotImplementedException();
}

public Task CreateLabelsAsync(string owner, string repository)
public Task CreateOrUpdateLabelsAsync(string owner, string repository)
{
throw new System.NotImplementedException();
}
Expand Down