Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] SubscriptionData subscription)
        {
            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"))
            {
                // If we have no repository information or an invalid installation id
                // then we will fail when trying to update things, so we fail early.
                Repository repo = await _context.Repositories.FindAsync(subscription.TargetRepository);

                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."
                    })));
                }
            }

            Data.Models.Subscription 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)));
        }
Exemplo n.º 2
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)));
        }
Exemplo n.º 3
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." })));
            }

            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)));
        }