示例#1
0
        public async Task <IActionResult> Get(int id)
        {
            Data.Models.DefaultChannel defaultChannel = await _context.DefaultChannels.FindAsync(id);

            if (defaultChannel == null)
            {
                return(NotFound());
            }
            return(Ok(new DefaultChannel(defaultChannel)));
        }
示例#2
0
        public async Task <BuildTime> GetBuildTimeAsync(int defaultChannelId, int days)
        {
            Data.Models.DefaultChannel defaultChannel = await _context.DefaultChannels.FindAsync(defaultChannelId);

            if (defaultChannel == null)
            {
                return(new BuildTime
                {
                    DefaultChannelId = 0,
                    OfficialBuildTime = 0,
                    PrBuildTime = 0,
                    GoalTimeInMinutes = 0
                });
            }

            MultiProjectKustoQuery queries = SharedKustoQueries.CreateBuildTimesQueries(defaultChannel.Repository, defaultChannel.Branch, days);

            var results = await Task.WhenAll <IDataReader>(_kustoClientProvider.ExecuteKustoQueryAsync(queries.Internal),
                                                           _kustoClientProvider.ExecuteKustoQueryAsync(queries.Public));

            (int officialBuildId, TimeSpan officialBuildTime) = SharedKustoQueries.ParseBuildTime(results[0]);
            (int prBuildId, TimeSpan prBuildTime)             = SharedKustoQueries.ParseBuildTime(results[1]);

            double officialTime = 0;
            double prTime       = 0;
            int    goalTime     = 0;

            if (officialBuildId != -1)
            {
                officialTime = officialBuildTime.TotalMinutes;

                // Get goal time for definition id
                Data.Models.GoalTime goal = await _context.GoalTime
                                            .FirstOrDefaultAsync(g => g.DefinitionId == officialBuildId && g.ChannelId == defaultChannel.ChannelId);

                if (goal != null)
                {
                    goalTime = goal.Minutes;
                }
            }

            if (prBuildId != -1)
            {
                prTime = prBuildTime.TotalMinutes;
            }

            return(new BuildTime
            {
                DefaultChannelId = defaultChannelId,
                OfficialBuildTime = officialTime,
                PrBuildTime = prTime,
                GoalTimeInMinutes = goalTime
            });
        }
        public DefaultChannel([NotNull] Data.Models.DefaultChannel other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            Id         = other.Id;
            Repository = other.Repository;
            Branch     = other.Branch;
            Channel    = other.Channel == null ? null : new Channel(other.Channel);
        }
示例#4
0
        public async Task <IActionResult> Update(int id, [FromBody] DefaultChannel.DefaultChannelUpdateData update)
        {
            Data.Models.DefaultChannel defaultChannel = await _context.DefaultChannels.FindAsync(id);

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

            bool doUpdate = false;

            if (!string.IsNullOrEmpty(update.Branch))
            {
                defaultChannel.Branch = update.Branch;
                doUpdate = true;
            }

            if (!string.IsNullOrEmpty(update.Repository))
            {
                defaultChannel.Repository = update.Repository;
                doUpdate = true;
            }

            if (update.ChannelId.HasValue)
            {
                int     channelId = update.ChannelId.Value;
                Channel channel   = await _context.Channels.FindAsync(channelId);

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

                defaultChannel.ChannelId = channelId;
                defaultChannel.Channel   = channel;
                doUpdate = true;
            }

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

            if (doUpdate)
            {
                _context.DefaultChannels.Update(defaultChannel);
                await _context.SaveChangesAsync();
            }

            return(Ok(new DefaultChannel(defaultChannel)));
        }
示例#5
0
        public async Task <IActionResult> Delete(int id)
        {
            Data.Models.DefaultChannel defaultChannel = await _context.DefaultChannels.FindAsync(id);

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

            _context.DefaultChannels.Remove(defaultChannel);
            await _context.SaveChangesAsync();

            return(StatusCode((int)HttpStatusCode.Accepted));
        }
示例#6
0
        public async Task <IActionResult> Create([FromBody, Required] DefaultChannel.DefaultChannelCreateData data)
        {
            int     channelId = data.ChannelId;
            Channel channel   = await _context.Channels.FindAsync(channelId);

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

            Data.Models.DefaultChannel defaultChannel;

            // Due to abundant retry logic, we'll return a normal response even if this is creating a duplicate, by simply
            // returning the one that already exists vs. HTTP 409 / 500
            var existingInstance = _context.DefaultChannels
                                   .Where(d => d.Channel == channel &&
                                          d.Repository == data.Repository &&
                                          d.Branch == data.Branch)
                                   .FirstOrDefault();

            if (existingInstance != null)
            {
                defaultChannel = existingInstance;
            }
            else
            {
                defaultChannel = new Data.Models.DefaultChannel
                {
                    Channel    = channel,
                    Repository = data.Repository,
                    Branch     = data.Branch,
                    Enabled    = data.Enabled ?? true
                };
                await _context.DefaultChannels.AddAsync(defaultChannel);

                await _context.SaveChangesAsync();
            }
            return(CreatedAtRoute(
                       new
            {
                action = "Get",
                id = defaultChannel.Id
            },
                       new DefaultChannel(defaultChannel)));
        }
        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)));
        }