/// <summary> /// Implements the 'subscription-status' operation /// </summary> /// <param name="options"></param> public override async Task <int> ExecuteAsync() { if ((_options.Enable && _options.Disable) || (!_options.Enable && !_options.Disable)) { Console.WriteLine("Please specify either --enable or --disable"); return(Constants.ErrorCode); } IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); // First, try to get the subscription. If it doesn't exist the call will throw and the exception will be // caught by `RunOperation` Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); string statusMessage = "enable"; if (!_options.Enable) { statusMessage = "disable"; } if (subscription.Enabled == _options.Enable) { Console.WriteLine($"Status of subscription '{_options.Id}' is already {statusMessage}d, exiting..."); return(Constants.SuccessCode); } try { SubscriptionUpdate subscriptionToUpdate = new SubscriptionUpdate { ChannelName = subscription.Channel.Name, SourceRepository = subscription.SourceRepository, Enabled = _options.Enable, Policy = subscription.Policy }; subscriptionToUpdate.Policy.Batchable = subscription.Policy.Batchable; subscriptionToUpdate.Policy.UpdateFrequency = subscription.Policy.UpdateFrequency; subscriptionToUpdate.Policy.MergePolicies = subscription.Policy.MergePolicies; var updatedSubscription = await remote.UpdateSubscriptionAsync( _options.Id, subscriptionToUpdate); Console.WriteLine($"Successfully {statusMessage}d subscription with id '{updatedSubscription.Id}'."); return(Constants.SuccessCode); } catch (Exception e) { Logger.LogError(e, $"Failed to {statusMessage} subscription."); return(Constants.ErrorCode); } }
/// <summary> /// Implements the 'update-subscription' operation /// </summary> /// <param name="options"></param> public override async Task <int> ExecuteAsync() { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); // First, try to get the subscription. If it doesn't exist the call will throw and the exception will be // caught by `RunOperation` Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); var suggestedRepos = remote.GetSubscriptionsAsync(); var suggestedChannels = remote.GetChannelsAsync(); UpdateSubscriptionPopUp updateSubscriptionPopUp = new UpdateSubscriptionPopUp( "update-subscription/update-subscription-todo", Logger, subscription, (await suggestedChannels).Select(suggestedChannel => suggestedChannel.Name), (await suggestedRepos).SelectMany(subs => new List <string> { subscription.SourceRepository, subscription.TargetRepository }).ToHashSet(), Constants.AvailableFrequencies, Constants.AvailableMergePolicyYamlHelp); UxManager uxManager = new UxManager(Logger); int exitCode = uxManager.PopUp(updateSubscriptionPopUp); if (exitCode != Constants.SuccessCode) { return(exitCode); } string channel = updateSubscriptionPopUp.Channel; string sourceRepository = updateSubscriptionPopUp.SourceRepository; string updateFrequency = updateSubscriptionPopUp.UpdateFrequency; bool batchable = updateSubscriptionPopUp.Batchable; bool enabled = updateSubscriptionPopUp.Enabled; List <MergePolicy> mergePolicies = updateSubscriptionPopUp.MergePolicies; try { SubscriptionUpdate subscriptionToUpdate = new SubscriptionUpdate { ChannelName = channel ?? subscription.Channel.Name, SourceRepository = sourceRepository ?? subscription.SourceRepository, Enabled = enabled, Policy = subscription.Policy, }; subscriptionToUpdate.Policy.Batchable = batchable; subscriptionToUpdate.Policy.UpdateFrequency = Enum.Parse <UpdateFrequency>(updateFrequency); subscriptionToUpdate.Policy.MergePolicies = mergePolicies?.ToImmutableList(); var updatedSubscription = await remote.UpdateSubscriptionAsync( _options.Id, subscriptionToUpdate); Console.WriteLine($"Successfully updated subscription with id '{updatedSubscription.Id}'."); return(Constants.SuccessCode); } catch (RestApiException e) when(e.Response.StatusCode == System.Net.HttpStatusCode.BadRequest) { // Could have been some kind of validation error (e.g. channel doesn't exist) Logger.LogError($"Failed to update subscription: {e.Response.Content}"); return(Constants.ErrorCode); } catch (Exception e) { Logger.LogError(e, $"Failed to update subscription."); return(Constants.ErrorCode); } }
/// <summary> /// Implements the 'subscription-status' operation /// </summary> /// <param name="options"></param> public override async Task <int> ExecuteAsync() { if ((_options.Enable && _options.Disable) || (!_options.Enable && !_options.Disable)) { Console.WriteLine("Please specify either --enable or --disable"); return(Constants.ErrorCode); } string presentTenseStatusMessage = _options.Enable ? "enable" : "disable"; string pastTenseStatusMessage = _options.Enable ? "enabled" : "disabled"; string actionStatusMessage = _options.Enable ? "Enabling" : "Disabling"; try { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); bool noConfirm = _options.NoConfirmation; List <Subscription> subscriptionsToEnableDisable = new List <Subscription>(); if (!string.IsNullOrEmpty(_options.Id)) { // Look up subscription so we can print it later. try { Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); subscriptionsToEnableDisable.Add(subscription); } catch (RestApiException e) when(e.Response.Status == (int)HttpStatusCode.NotFound) { Console.WriteLine($"Subscription with id '{_options.Id}' was not found."); return(Constants.ErrorCode); } } else { if (!_options.HasAnyFilters()) { Console.WriteLine($"Please specify one or more filters to select which subscriptions should be {pastTenseStatusMessage} (see help)."); return(Constants.ErrorCode); } IEnumerable <Subscription> subscriptions = await _options.FilterSubscriptions(remote); if (!subscriptions.Any()) { Console.WriteLine("No subscriptions found matching the specified criteria."); return(Constants.ErrorCode); } subscriptionsToEnableDisable.AddRange(subscriptions); } // Filter away subscriptions that already have the desired state: subscriptionsToEnableDisable = subscriptionsToEnableDisable.Where(s => s.Enabled != _options.Enable).ToList(); if (!subscriptionsToEnableDisable.Any()) { Console.WriteLine($"All subscriptions are already {pastTenseStatusMessage}."); return(Constants.SuccessCode); } if (!noConfirm) { // Print out the list of subscriptions about to be enabled/disabled. Console.WriteLine($"Will {presentTenseStatusMessage} the following {subscriptionsToEnableDisable.Count} subscriptions..."); foreach (var subscription in subscriptionsToEnableDisable) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } if (!UxHelpers.PromptForYesNo("Continue?")) { Console.WriteLine($"No subscriptions {pastTenseStatusMessage}, exiting."); return(Constants.ErrorCode); } } Console.Write($"{actionStatusMessage} {subscriptionsToEnableDisable.Count} subscriptions...{(noConfirm ? Environment.NewLine : "")}"); foreach (var subscription in subscriptionsToEnableDisable) { // If noConfirm was passed, print out the subscriptions as we go if (noConfirm) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } SubscriptionUpdate subscriptionToUpdate = new SubscriptionUpdate { ChannelName = subscription.Channel.Name, SourceRepository = subscription.SourceRepository, Enabled = _options.Enable, Policy = subscription.Policy }; subscriptionToUpdate.Policy.Batchable = subscription.Policy.Batchable; subscriptionToUpdate.Policy.UpdateFrequency = subscription.Policy.UpdateFrequency; subscriptionToUpdate.Policy.MergePolicies = subscription.Policy.MergePolicies; var updatedSubscription = await remote.UpdateSubscriptionAsync( subscription.Id.ToString(), subscriptionToUpdate); } Console.WriteLine("done"); return(Constants.SuccessCode); } catch (AuthenticationException e) { Console.WriteLine(e.Message); return(Constants.ErrorCode); } catch (Exception e) { Logger.LogError(e, $"Unexpected error while {actionStatusMessage.ToLower()} subscriptions."); return(Constants.ErrorCode); } }
/// <summary> /// Triggers subscriptions /// </summary> /// <returns></returns> public override async Task <int> ExecuteAsync() { try { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); bool noConfirm = _options.NoConfirmation; List <Subscription> subscriptionsToTrigger = new List <Subscription>(); if (!string.IsNullOrEmpty(_options.Id)) { // Look up subscription so we can print it later. try { Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); subscriptionsToTrigger.Add(subscription); } catch (RestApiException e) when(e.Response.StatusCode == HttpStatusCode.NotFound) { Console.WriteLine($"Subscription with id '{_options.Id}' was not found."); return(Constants.ErrorCode); } } else { if (string.IsNullOrEmpty(_options.TargetRepository) && string.IsNullOrEmpty(_options.TargetBranch) && string.IsNullOrEmpty(_options.SourceRepository) && string.IsNullOrEmpty(_options.Channel)) { Console.WriteLine($"Please specify one or more filters to select which subscriptions should be triggered (see help)."); return(Constants.ErrorCode); } IEnumerable <Subscription> subscriptions = (await remote.GetSubscriptionsAsync()).Where(subscription => { return(_options.SubcriptionFilter(subscription)); }); if (subscriptions.Count() == 0) { Console.WriteLine("No subscriptions found matching the specified criteria."); return(Constants.ErrorCode); } subscriptionsToTrigger.AddRange(subscriptions); } if (!noConfirm) { // Print out the list of subscriptions about to be triggered. Console.WriteLine($"Will trigger the following {subscriptionsToTrigger.Count} subscriptions..."); foreach (var subscription in subscriptionsToTrigger) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } char keyChar; do { Console.Write("Continue? (y/n) "); ConsoleKeyInfo keyInfo = Console.ReadKey(); keyChar = char.ToUpperInvariant(keyInfo.KeyChar); Console.WriteLine(); }while (keyChar != 'Y' && keyChar != 'N'); if (keyChar == 'N') { Console.WriteLine($"No subscriptions triggered, exiting."); return(Constants.ErrorCode); } } Console.Write($"Triggering {subscriptionsToTrigger.Count} subscriptions...{(noConfirm ? Environment.NewLine : "")}"); foreach (var subscription in subscriptionsToTrigger) { // If noConfirm was passed, print out the subscriptions as we go if (noConfirm) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } await remote.TriggerSubscriptionAsync(subscription.Id.ToString()); } Console.WriteLine($"done"); return(Constants.SuccessCode); } catch (Exception e) { Logger.LogError(e, "Unexpected error while triggering subscriptions."); return(Constants.ErrorCode); } }
/// <summary> /// Triggers subscriptions /// </summary> /// <returns></returns> public override async Task <int> ExecuteAsync() { try { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); bool noConfirm = _options.NoConfirmation; List <Subscription> subscriptionsToTrigger = new List <Subscription>(); if (!string.IsNullOrEmpty(_options.Id)) { // Look up subscription so we can print it later. try { Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); subscriptionsToTrigger.Add(subscription); } catch (RestApiException e) when(e.Response.Status == (int)HttpStatusCode.NotFound) { Console.WriteLine($"Subscription with id '{_options.Id}' was not found."); return(Constants.ErrorCode); } } else { if (!_options.HasAnyFilters()) { Console.WriteLine($"Please specify one or more filters to select which subscriptions should be triggered (see help)."); return(Constants.ErrorCode); } IEnumerable <Subscription> subscriptions = await _options.FilterSubscriptions(remote); if (!subscriptions.Any()) { Console.WriteLine("No subscriptions found matching the specified criteria."); return(Constants.ErrorCode); } subscriptionsToTrigger.AddRange(subscriptions); } if (!noConfirm) { // Print out the list of subscriptions about to be triggered. Console.WriteLine($"Will trigger the following {subscriptionsToTrigger.Count} subscriptions..."); foreach (var subscription in subscriptionsToTrigger) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } if (!UxHelpers.PromptForYesNo("Continue?")) { Console.WriteLine($"No subscriptions triggered, exiting."); return(Constants.ErrorCode); } } Console.Write($"Triggering {subscriptionsToTrigger.Count} subscriptions...{(noConfirm ? Environment.NewLine : "")}"); foreach (var subscription in subscriptionsToTrigger) { // If noConfirm was passed, print out the subscriptions as we go if (noConfirm) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } await remote.TriggerSubscriptionAsync(subscription.Id.ToString()); } Console.WriteLine("done"); return(Constants.SuccessCode); } catch (Exception e) { Logger.LogError(e, "Unexpected error while triggering subscriptions."); return(Constants.ErrorCode); } }
/// <summary> /// Triggers subscriptions /// </summary> /// <returns></returns> public override async Task <int> ExecuteAsync() { try { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); bool noConfirm = _options.NoConfirmation; List <Subscription> subscriptionsToTrigger = new List <Subscription>(); if (!string.IsNullOrEmpty(_options.Id)) { // Look up subscription so we can print it later. try { Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); subscriptionsToTrigger.Add(subscription); } catch (RestApiException e) when(e.Response.Status == (int)HttpStatusCode.NotFound) { Console.WriteLine($"Subscription with id '{_options.Id}' was not found."); return(Constants.ErrorCode); } } else { if (!_options.HasAnyFilters()) { Console.WriteLine($"Please specify one or more filters to select which subscriptions should be triggered (see help)."); return(Constants.ErrorCode); } IEnumerable <Subscription> subscriptions = await _options.FilterSubscriptions(remote); if (!subscriptions.Any()) { Console.WriteLine("No subscriptions found matching the specified criteria."); return(Constants.ErrorCode); } subscriptionsToTrigger.AddRange(subscriptions); } if (_options.Build != 0) { var specificBuild = await remote.GetBuildAsync(_options.Build); if (specificBuild == null) { Console.WriteLine($"No build found in the BAR with id '{_options.Build}'"); return(Constants.ErrorCode); } // If the user specified repo and a build number, error out if anything doesn't match. if (!_options.SubscriptionParameterMatches(_options.SourceRepository, specificBuild.GitHubRepository)) { Console.WriteLine($"Build #{_options.Build} was made with repo {specificBuild.GitHubRepository} and does not match provided value ({_options.SourceRepository})"); return(Constants.ErrorCode); } Console.WriteLine($"Subscription updates will use Build # {_options.Build} instead of latest available"); } // Filter away subscriptions that are disabled List <Subscription> disabledSubscriptions = subscriptionsToTrigger.Where(s => !s.Enabled).ToList(); subscriptionsToTrigger = subscriptionsToTrigger.Where(s => s.Enabled).ToList(); if (disabledSubscriptions.Any()) { Console.WriteLine($"The following {disabledSubscriptions.Count} subscription(s) are disabled and will not be triggered"); foreach (var subscription in disabledSubscriptions) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } } if (!subscriptionsToTrigger.Any()) { Console.WriteLine("No enabled subscriptions found matching the specified criteria."); return(Constants.ErrorCode); } if (!noConfirm) { // Print out the list of subscriptions about to be triggered. Console.WriteLine($"Will trigger the following {subscriptionsToTrigger.Count} subscriptions..."); foreach (var subscription in subscriptionsToTrigger) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } if (!UxHelpers.PromptForYesNo("Continue?")) { Console.WriteLine($"No subscriptions triggered, exiting."); return(Constants.ErrorCode); } } Console.Write($"Triggering {subscriptionsToTrigger.Count} subscriptions...{(noConfirm ? Environment.NewLine : "")}"); foreach (var subscription in subscriptionsToTrigger) { // If noConfirm was passed, print out the subscriptions as we go if (noConfirm) { Console.WriteLine($" {UxHelpers.GetSubscriptionDescription(subscription)}"); } if (_options.Build > 0) { await remote.TriggerSubscriptionAsync(subscription.Id.ToString(), _options.Build); } else { await remote.TriggerSubscriptionAsync(subscription.Id.ToString()); } } Console.WriteLine("done"); return(Constants.SuccessCode); } catch (AuthenticationException e) { Console.WriteLine(e.Message); return(Constants.ErrorCode); } catch (Exception e) { Logger.LogError(e, "Unexpected error while triggering subscriptions."); return(Constants.ErrorCode); } }
/// <summary> /// Implements the 'update-subscription' operation /// </summary> /// <param name="options"></param> public override async Task <int> ExecuteAsync() { IRemote remote = RemoteFactory.GetBarOnlyRemote(_options, Logger); // First, try to get the subscription. If it doesn't exist the call will throw and the exception will be // caught by `RunOperation` Subscription subscription = await remote.GetSubscriptionAsync(_options.Id); var suggestedRepos = remote.GetSubscriptionsAsync(); var suggestedChannels = remote.GetChannelsAsync(); UpdateSubscriptionPopUp updateSubscriptionPopUp = new UpdateSubscriptionPopUp( "update-subscription/update-subscription-todo", Logger, subscription, (await suggestedChannels).Select(suggestedChannel => suggestedChannel.Name), (await suggestedRepos).SelectMany(subs => new List <string> { subscription.SourceRepository, subscription.TargetRepository }).ToHashSet(), Constants.AvailableFrequencies, Constants.AvailableMergePolicyYamlHelp); UxManager uxManager = new UxManager(_options.GitLocation, Logger); int exitCode = uxManager.PopUp(updateSubscriptionPopUp); if (exitCode != Constants.SuccessCode) { return(exitCode); } string channel = updateSubscriptionPopUp.Channel; string sourceRepository = updateSubscriptionPopUp.SourceRepository; string updateFrequency = updateSubscriptionPopUp.UpdateFrequency; bool batchable = updateSubscriptionPopUp.Batchable; bool enabled = updateSubscriptionPopUp.Enabled; List <MergePolicy> mergePolicies = updateSubscriptionPopUp.MergePolicies; try { SubscriptionUpdate subscriptionToUpdate = new SubscriptionUpdate { ChannelName = channel ?? subscription.Channel.Name, SourceRepository = sourceRepository ?? subscription.SourceRepository, Enabled = enabled, Policy = subscription.Policy, }; subscriptionToUpdate.Policy.Batchable = batchable; subscriptionToUpdate.Policy.UpdateFrequency = Enum.Parse <UpdateFrequency>(updateFrequency); subscriptionToUpdate.Policy.MergePolicies = mergePolicies?.ToImmutableList(); var updatedSubscription = await remote.UpdateSubscriptionAsync( _options.Id, subscriptionToUpdate); Console.WriteLine($"Successfully updated subscription with id '{updatedSubscription.Id}'."); // Determine whether the subscription should be triggered. if (!_options.NoTriggerOnUpdate) { bool triggerAutomatically = _options.TriggerOnUpdate; // Determine whether we should prompt if the user hasn't explicitly // said one way or another. We shouldn't prompt if nothing changes or // if non-interesting options have changed if (!triggerAutomatically && ((subscriptionToUpdate.ChannelName != subscription.Channel.Name) || (subscriptionToUpdate.SourceRepository != subscription.SourceRepository) || (subscriptionToUpdate.Enabled.Value && !subscription.Enabled) || (subscriptionToUpdate.Policy.UpdateFrequency != UpdateFrequency.None && subscriptionToUpdate.Policy.UpdateFrequency != subscription.Policy.UpdateFrequency))) { triggerAutomatically = UxHelpers.PromptForYesNo("Trigger this subscription immediately?"); } if (triggerAutomatically) { await remote.TriggerSubscriptionAsync(updatedSubscription.Id.ToString()); Console.WriteLine($"Subscription '{updatedSubscription.Id}' triggered."); } } return(Constants.SuccessCode); } catch (AuthenticationException e) { Console.WriteLine(e.Message); return(Constants.ErrorCode); } catch (RestApiException e) when(e.Response.Status == (int)System.Net.HttpStatusCode.BadRequest) { // Could have been some kind of validation error (e.g. channel doesn't exist) Logger.LogError($"Failed to update subscription: {e.Response.Content}"); return(Constants.ErrorCode); } catch (Exception e) { Logger.LogError(e, $"Failed to update subscription."); return(Constants.ErrorCode); } }