Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public async Task<IActionResult> Get(int id)

[HttpDelete("{id}")]
[ValidateModelState]
[SwaggerResponse((int)HttpStatusCode.Accepted)]

This comment was marked as spam.

This comment was marked as spam.

public async Task<IActionResult> Delete(int id)
{
Data.Models.DefaultChannel defaultChannel = await _context.DefaultChannels.FindAsync(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.DotNet.Darc.Helpers;
using Microsoft.DotNet.Darc.Options;
using Microsoft.DotNet.DarcLib;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Microsoft.DotNet.Darc.Operations
{
internal class AddDefaultChannelOperation : Operation
{
AddDefaultChannelCommandLineOptions _options;
public AddDefaultChannelOperation(AddDefaultChannelCommandLineOptions options)
: base(options)
{
_options = options;
}

public override async Task<int> ExecuteAsync()
{
try
{
DarcSettings darcSettings = LocalCommands.GetSettings(_options, Logger);
// No need to set up a git type or PAT here.
Remote remote = new Remote(darcSettings, Logger);

await remote.AddDefaultChannelAsync(_options.Repository, _options.Branch, _options.Channel);

return Constants.SuccessCode;
}
catch (Exception e)
{
Logger.LogError(e, "Error: Failed to add a new default channel association.");
return Constants.ErrorCode;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.DotNet.Darc.Helpers;
using Microsoft.DotNet.Darc.Options;
using Microsoft.DotNet.DarcLib;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Microsoft.DotNet.Darc.Operations
{
internal class DeleteDefaultChannelOperation : Operation
{
DeleteDefaultChannelCommandLineOptions _options;
public DeleteDefaultChannelOperation(DeleteDefaultChannelCommandLineOptions options)
: base(options)
{
_options = options;
}

public override async Task<int> ExecuteAsync()
{
try
{
DarcSettings darcSettings = LocalCommands.GetSettings(_options, Logger);
// No need to set up a git type or PAT here.
Remote remote = new Remote(darcSettings, Logger);

await remote.DeleteDefaultChannelAsync(_options.Repository, _options.Branch, _options.Channel);

return Constants.SuccessCode;
}
catch (Exception e)
{
Logger.LogError(e, "Error: Failed remove the default channel association.");
return Constants.ErrorCode;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommandLine;

namespace Microsoft.DotNet.Darc.Options
{
[Verb("add-default-channel", HelpText = "Add a channel that a build of a branch+repository is automatically applied to.")]
internal class AddDefaultChannelCommandLineOptions : CommandLineOptions
{
[Option("channel", Required = true, HelpText = "Name of channel that a build of 'branch' and 'repo' should be applied to.")]
public string Channel { get; set; }

[Option("branch", Required = true, HelpText = "Build of 'repo' on this branch will be automatically applied to 'channel'")]
public string Branch { get; set; }

[Option("repo", Required = true, HelpText = "Build of this repo repo on 'branch' will be automatically applied to 'channel'")]
public string Repository { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommandLine;

namespace Microsoft.DotNet.Darc.Options
{
[Verb("delete-default-channel", HelpText = "Remove a default channel association.")]
internal class DeleteDefaultChannelCommandLineOptions : CommandLineOptions
{
[Option("channel", HelpText = "Name of channel that builds of 'repository' and 'branch' should not apply to.")]
public string Channel { get; set; }

[Option("branch", Required = true, HelpText = "Repository that should have its default association removed.")]
public string Branch { get; set; }

[Option("repo", Required = true, HelpText = "Branch that should have its default association removed.")]
public string Repository { get; set; }
}
}
6 changes: 5 additions & 1 deletion src/Microsoft.DotNet.Darc/src/Darc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ static int Main(string[] args)
GetSubscriptionsCommandLineOptions,
AddSubscriptionCommandLineOptions,
DeleteSubscriptionCommandLineOptions,
GetDefaultChannelCommandLineOptions>(args)
GetDefaultChannelCommandLineOptions,
AddDefaultChannelCommandLineOptions,
DeleteDefaultChannelCommandLineOptions>(args)
.MapResult(
(AuthenticateCommandLineOptions opts) => { return RunOperation(new AuthenticateOperation(opts)); },
(GetDependenciesCommandLineOptions opts) => { return RunOperation(new GetDependenciesOperation(opts)); },
Expand All @@ -30,6 +32,8 @@ static int Main(string[] args)
(AddSubscriptionCommandLineOptions opts) => { return RunOperation(new AddSubscriptionOperation(opts)); },
(DeleteSubscriptionCommandLineOptions opts) => { return RunOperation(new DeleteSubscriptionOperation(opts)); },
(GetDefaultChannelCommandLineOptions opts) => { return RunOperation(new GetDefaultChannelsOperation(opts)); },
(AddDefaultChannelCommandLineOptions opts) => { return RunOperation(new AddDefaultChannelOperation(opts)); },
(DeleteDefaultChannelCommandLineOptions opts) => { return RunOperation(new DeleteDefaultChannelOperation(opts)); },
(errs => 1));
}

Expand Down
60 changes: 57 additions & 3 deletions src/Microsoft.DotNet.Darc/src/DarcLib/Actions/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,66 @@ public Remote(DarcSettings settings, ILogger logger)
/// </summary>
/// <param name="repository">Optionally filter by repository</param>
/// <param name="branch">Optionally filter by branch</param>
/// <param name="channelId">Optionally filter by channel ID</param>
/// <param name="channel">Optionally filter by channel</param>
/// <returns>Collection of default channels.</returns>
public async Task<IEnumerable<DefaultChannel>> GetDefaultChannelsAsync(string repository = null, string branch = null, int? channelId = null)
public async Task<IEnumerable<DefaultChannel>> GetDefaultChannelsAsync(string repository = null, string branch = null, string channel = null)
{
CheckForValidBarClient();
return await _barClient.DefaultChannels.ListAsync(repository, branch, channelId);
var channels = await _barClient.DefaultChannels.ListAsync(repository: repository, branch: branch);
if (!string.IsNullOrEmpty(channel))
{
return channels.Where(c => c.Channel.Name.Equals(channel, StringComparison.OrdinalIgnoreCase));
}
// Filter away based on channel info.
return channels;
}

/// <summary>
/// Adds a default channel association.
/// </summary>
/// <param name="repository">Repository receiving the default association</param>
/// <param name="branch">Branch receiving the default association</param>
/// <param name="channel">Name of channel that builds of 'repository' on 'branch' should automatically be applied to.</param>
/// <returns>Async task.</returns>
public async Task AddDefaultChannelAsync(string repository, string branch, string channel)
{
CheckForValidBarClient();
// Look up channel to translate to channel id.
var foundChannel = (await _barClient.Channels.GetAsync()).Where(c => c.Name.Equals(channel, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
if (foundChannel == null)
{
throw new ArgumentException($"Channel {channel} is not a valid channel.");
}

PostData defaultChannelsData = new PostData {
Branch = branch,
Repository = repository,
ChannelId = foundChannel.Id.Value
};

await _barClient.DefaultChannels.CreateAsync(defaultChannelsData);
return;
}

/// <summary>
/// Removes a default channel based on the specified criteria
/// </summary>
/// <param name="repository">Repository having a default association</param>
/// <param name="branch">Branch having a default association</param>
/// <param name="channel">Name of channel that builds of 'repository' on 'branch' are being applied to.</param>
/// <returns>Async task</returns>
public async Task DeleteDefaultChannelAsync(string repository, string branch, string channel)
{
CheckForValidBarClient();

var existingDefaultChannel = (await GetDefaultChannelsAsync(repository, branch, channel)).SingleOrDefault();

if (existingDefaultChannel != null)
{
// Find the existing default channel. If none found then nothing to do.
await _barClient.DefaultChannels.DeleteAsync(existingDefaultChannel.Id.Value);
return;
}
}

/// <summary>
Expand Down
27 changes: 26 additions & 1 deletion src/Microsoft.DotNet.Darc/src/DarcLib/IRemote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,32 @@ namespace Microsoft.DotNet.DarcLib
{
public interface IRemote
{
Task<IEnumerable<DefaultChannel>> GetDefaultChannelsAsync(string repository = null, string branch = null, int? channelId = null);
/// <summary>
/// Retrieve a set of default channel associations based on the provided filters.
/// </summary>
/// <param name="repository">Repository name</param>
/// <param name="branch">Name of branch</param>
/// <param name="channel">Channel name.</param>
/// <returns>List of default channel associations. Channel is matched based on case insensitivity.</returns>
Task<IEnumerable<DefaultChannel>> GetDefaultChannelsAsync(string repository = null, string branch = null, string channel = null);

/// <summary>
/// Adds a default channel association.
/// </summary>
/// <param name="repository">Repository receiving the default association</param>
/// <param name="branch">Branch receiving the default association</param>
/// <param name="channel">Name of channel that builds of 'repository' on 'branch' should automatically be applied to.</param>
/// <returns>Async task.</returns>
Task AddDefaultChannelAsync(string repository, string branch, string channel);

/// <summary>
/// Removes a default channel based on the specified criteria
/// </summary>
/// <param name="repository">Repository having a default association</param>
/// <param name="branch">Branch having a default association</param>
/// <param name="channel">Name of channel that builds of 'repository' on 'branch' are being applied to.</param>
/// <returns>Async task</returns>
Task DeleteDefaultChannelAsync(string repository, string branch, string channel);

Task<Channel> CreateChannelAsync(string name, string classification);

Expand Down