public async Task <IActionResult> Create([FromBody] SubscriptionData subscription)
        {
            Data.Models.Channel channel = await _context.Channels.Where(c => c.Name == subscription.ChannelName)
                                          .FirstOrDefaultAsync();

            if (channel == null)
            {
                return(BadRequest(
                           new ApiError(
                               "the request is invalid",
                               new[] { $"The channel '{subscription.ChannelName}' could not be found." })));
            }

            var subscriptionModel = new Data.Models.Subscription(subscription)
            {
                Channel = channel
            };
            await _context.Subscriptions.AddAsync(subscriptionModel);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute(
                       new { action = "GetSubscription", id = subscriptionModel.Id },
                       new Subscription(subscriptionModel)));
        }
Example #2
0
        public async Task <IActionResult> AddBuildToChannel(int channelId, int buildId)
        {
            Data.Models.Channel channel = await _context.Channels.FindAsync(channelId);

            if (channel == null)
            {
                return(NotFound(new ApiError($"The channel with id '{channelId}' was not found.")));
            }

            Data.Models.Build build = await _context.Builds.FindAsync(buildId);

            if (build == null)
            {
                return(NotFound(new ApiError($"The build with id '{buildId}' was not found.")));
            }

            var buildChannel = new Data.Models.BuildChannel {
                Channel = channel, Build = build
            };
            await _context.BuildChannels.AddAsync(buildChannel);

            await _context.SaveChangesAsync();

            return(StatusCode((int)HttpStatusCode.Created));
        }
Example #3
0
        public virtual async Task <IActionResult> Create([FromBody, Required] Goal.GoalRequestJson goalData, [Required] String channelName, [Required] int definitionId)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .FirstOrDefaultAsync(c => c.Name == channelName);

            if (channel == null)
            {
                return(NotFound());
            }
            Data.Models.GoalTime goal = await _context.GoalTime
                                        .FirstOrDefaultAsync(g => g.DefinitionId == definitionId && g.ChannelId == channel.Id);

            if (goal == null)
            {
                goal = new Data.Models.GoalTime
                {
                    DefinitionId = definitionId,
                    Minutes      = goalData.Minutes,
                    ChannelId    = channel.Id
                };
                await _context.GoalTime.AddAsync(goal);
            }
            else
            {
                goal.Minutes = goalData.Minutes;
                _context.GoalTime.Update(goal);
            }
            await _context.SaveChangesAsync();

            return(Ok(new Goal(goal)));
        }
        public override async Task <IActionResult> UpdateSubscription(Guid id, [FromBody] v2018_07_16.Models.SubscriptionUpdate update)
        {
            Data.Models.Subscription subscription = await _context.Subscriptions.Where(sub => sub.Id == id)
                                                    .FirstOrDefaultAsync();

            if (subscription == null)
            {
                return(NotFound());
            }

            var doUpdate = false;

            if (!string.IsNullOrEmpty(update.SourceRepository))
            {
                subscription.SourceRepository = update.SourceRepository;
                doUpdate = true;
            }

            if (update.Policy != null)
            {
                subscription.PolicyObject = update.Policy.ToDb();
                doUpdate = true;
            }

            if (!string.IsNullOrEmpty(update.ChannelName))
            {
                Data.Models.Channel channel = await _context.Channels.Where(c => c.Name == update.ChannelName)
                                              .FirstOrDefaultAsync();

                if (channel == null)
                {
                    return(BadRequest(
                               new ApiError(
                                   "The request is invalid",
                                   new[] { $"The channel '{update.ChannelName}' could not be found." })));
                }

                subscription.Channel = channel;
                doUpdate             = true;
            }

            if (update.Enabled.HasValue)
            {
                subscription.Enabled = update.Enabled.Value;
                doUpdate             = true;
            }

            if (doUpdate)
            {
                _context.Subscriptions.Update(subscription);
                await _context.SaveChangesAsync();
            }


            return(Ok(new Subscription(subscription)));
        }
Example #5
0
        public async Task <IActionResult> CreateChannel([Required] string name, [Required] string classification)
        {
            var channelModel = new Data.Models.Channel {
                Name = name, Classification = classification
            };
            await _context.Channels.AddAsync(channelModel);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute(new { action = "GetChannel", id = channelModel.Id }, new Channel(channelModel)));
        }
Example #6
0
        public async Task <IActionResult> GetChannel(int id)
        {
            Data.Models.Channel channel = await _context.Channels.Where(c => c.Id == id).FirstOrDefaultAsync();

            if (channel == null)
            {
                return(NotFound());
            }

            return(Ok(new Channel(channel)));
        }
Example #7
0
        public Channel([NotNull] Data.Models.Channel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Id             = other.Id;
            Name           = other.Name;
            Classification = other.Classification;
        }
Example #8
0
        public async Task <Channel> GetChannelAsync(int channelId)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .Where(c => c.Id == channelId).FirstOrDefaultAsync();

            if (channel != null)
            {
                return(ToClientModelChannel(channel));
            }

            return(null);
        }
Example #9
0
        public async Task <Channel> GetChannelAsync(int channelId)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .Include(ch => ch.ChannelReleasePipelines)
                                          .ThenInclude(crp => crp.ReleasePipeline)
                                          .Where(c => c.Id == channelId).FirstOrDefaultAsync();

            if (channel != null)
            {
                return(ToClientModelChannel(channel));
            }

            return(null);
        }
Example #10
0
        public async Task <IActionResult> DeleteChannel(int id)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .FirstOrDefaultAsync(c => c.Id == id);

            if (channel == null)
            {
                return(NotFound());
            }

            _context.Channels.Remove(channel);

            await _context.SaveChangesAsync();

            return(Ok(new Channel(channel)));
        }
Example #11
0
        public Channel([NotNull] Data.Models.Channel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Id               = other.Id;
            Name             = other.Name;
            Classification   = other.Classification;
            ReleasePipelines = other?.ChannelReleasePipelines
                               ?.Select(crp => crp.ReleasePipeline)
                               .Where(rp => rp != null)
                               .Select(rp => new ReleasePipeline(rp))
                               .ToList();
        }
        public async Task <IActionResult> Create([FromBody, Required] SubscriptionData subscription)
        {
            Data.Models.Channel channel = await _context.Channels.Where(c => c.Name == subscription.ChannelName)
                                          .FirstOrDefaultAsync();

            if (channel == null)
            {
                return(BadRequest(
                           new ApiError(
                               "the request is invalid",
                               new[] { $"The channel '{subscription.ChannelName}' could not be found." })));
            }

            Data.Models.Repository repo = await _context.Repositories.FindAsync(subscription.TargetRepository);

            if (subscription.TargetRepository.Contains("github.com"))
            {
                // If we have no repository information or an invalid installation id
                // then we will fail when trying to update things, so we fail early.
                if (repo == null || repo.InstallationId <= 0)
                {
                    return(BadRequest(
                               new ApiError(
                                   "the request is invalid",
                                   new[]
                    {
                        $"The repository '{subscription.TargetRepository}' does not have an associated github installation. " +
                        "The Maestro github application must be installed by the repository's owner and given access to the repository."
                    })));
                }
            }
            // In the case of a dev.azure.com repository, we don't have an app installation,
            // but we should add an entry in the repositories table, as this is required when
            // adding a new subscription policy.
            // NOTE:
            // There is a good chance here that we will need to also handle <account>.visualstudio.com
            // but leaving it out for now as it would be preferred to use the new format
            else if (subscription.TargetRepository.Contains("dev.azure.com"))
            {
                if (repo == null)
                {
                    _context.Repositories.Add(
                        new Data.Models.Repository
                    {
                        RepositoryName = subscription.TargetRepository,
                        InstallationId = default
                    });
Example #13
0
        public override async Task <IActionResult> GetGoalTimes([Required] int definitionId, [Required] string channelName)
        {
            Data.Models.Channel channel = await _context.Channels
                                          .FirstOrDefaultAsync(c => c.Name == channelName);

            if (channel == null)
            {
                return(NotFound());
            }
            Data.Models.GoalTime goal = await _context.GoalTime
                                        .FirstOrDefaultAsync(g => g.DefinitionId == definitionId && g.ChannelId == channel.Id);

            if (goal == null)
            {
                return(NotFound());
            }
            return(Ok(new Goal(goal)));
        }
Example #14
0
        public async Task <IActionResult> Create([FromBody] SubscriptionData subscription)
        {
            Data.Models.Channel channel = await _context.Channels.Where(c => c.Name == subscription.ChannelName)
                                          .FirstOrDefaultAsync();

            if (channel == null)
            {
                return(BadRequest(
                           new ApiError(
                               "the request is invalid",
                               new[] { $"The channel '{subscription.ChannelName}' could not be found." })));
            }

            if (subscription.TargetRepository.Contains("github.com"))
            {
                var repoInstallation = await _context.RepoInstallations.FindAsync(subscription.TargetRepository);

                if (repoInstallation == null)
                {
                    return(BadRequest(
                               new ApiError(
                                   "the request is invalid",
                                   new[]
                    {
                        $"The repository '{subscription.TargetRepository}' does not have an associated github installation. " +
                        "The Maestro github application must be installed by the repository's owner and given access to the repository."
                    })));
                }
            }

            var subscriptionModel = subscription.ToDb();

            subscriptionModel.Channel = channel;
            await _context.Subscriptions.AddAsync(subscriptionModel);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute(
                       new { action = "GetSubscription", id = subscriptionModel.Id },
                       new Subscription(subscriptionModel)));
        }
        public async Task <IActionResult> Create([FromBody] DefaultChannel.PostData data)
        {
            int channelId = data.ChannelId;

            Data.Models.Channel channel = await _context.Channels.FindAsync(channelId);

            if (channel == null)
            {
                return(NotFound(new ApiError($"The channel with id '{channelId}' was not found.")));
            }

            var defaultChannel =
                new Data.Models.DefaultChannel
            {
                Channel    = channel,
                Repository = data.Repository,
                Branch     = data.Branch,
            };
            await _context.DefaultChannels.AddAsync(defaultChannel);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute(new { action = "Get", id = defaultChannel.Id }, new DefaultChannel(defaultChannel)));
        }
        public async Task <IActionResult> UpdateSubscription(Guid id, [FromBody] SubscriptionUpdate update)
        {
            Data.Models.Subscription subscription = await _context.Subscriptions.Where(sub => sub.Id == id).Include(sub => sub.Channel)
                                                    .FirstOrDefaultAsync();

            if (subscription == null)
            {
                return(NotFound());
            }

            var doUpdate = false;

            if (!string.IsNullOrEmpty(update.SourceRepository))
            {
                subscription.SourceRepository = update.SourceRepository;
                doUpdate = true;
            }

            if (update.Policy != null)
            {
                subscription.PolicyObject = update.Policy.ToDb();
                doUpdate = true;
            }

            if (update.PullRequestFailureNotificationTags != null)
            {
                if (!await AllNotificationTagsValid(update.PullRequestFailureNotificationTags))
                {
                    return(BadRequest(new ApiError("Invalid value(s) provided in Pull Request Failure Notification Tags; is everyone listed publicly a member of the Microsoft github org?")));
                }

                subscription.PullRequestFailureNotificationTags = update.PullRequestFailureNotificationTags;
                doUpdate = true;
            }

            if (!string.IsNullOrEmpty(update.ChannelName))
            {
                Data.Models.Channel channel = await _context.Channels.Where(c => c.Name == update.ChannelName)
                                              .FirstOrDefaultAsync();

                if (channel == null)
                {
                    return(BadRequest(
                               new ApiError(
                                   "The request is invalid",
                                   new[] { $"The channel '{update.ChannelName}' could not be found." })));
                }

                subscription.Channel = channel;
                doUpdate             = true;
            }

            if (update.Enabled.HasValue)
            {
                subscription.Enabled = update.Enabled.Value;
                doUpdate             = true;
            }

            if (doUpdate)
            {
                Data.Models.Subscription equivalentSubscription = await FindEquivalentSubscription(subscription);

                if (equivalentSubscription != null)
                {
                    return(Conflict(
                               new ApiError(
                                   "the request is invalid",
                                   new[]
                    {
                        $"The subscription '{equivalentSubscription.Id}' already performs the same update."
                    })));
                }

                _context.Subscriptions.Update(subscription);
                await _context.SaveChangesAsync();
            }


            return(Ok(new Subscription(subscription)));
        }