Example #1
0
        public async Task <IHttpActionResult> Post([FromBody] HttpHookBuild build)
        {
            var knowDefinitionsBuilds = ConfigurationManager.AppSettings["ExpectedBuilds"].Split('|').Where(i => !string.IsNullOrEmpty(i)).ToArray();

            if (knowDefinitionsBuilds.Any() && !knowDefinitionsBuilds.Contains(build.resource.definition.name))
            {
                return(Ok());
            }

            try
            {
                using (var context = new ReportingContext())
                {
                    var request = build.resource.requests.FirstOrDefault();

                    var buildEntity = new BuildEntity
                    {
                        BuildName          = build.resource.buildNumber,
                        Definition         = build.resource.definition.name,
                        Date               = build.resource.finishTime,
                        Status             = build.resource.status,
                        Reason             = build.resource.reason,
                        Link               = build.message.html,
                        User               = request != null ? request.requestedFor.uniqueName : "Unknown user",
                        CoverageCollection = new Collection <CoverageEntity>()
                    };

                    if (buildEntity.User == "Project Collection Service Accounts")
                    {
                        buildEntity.User = "******";
                    }

                    if (buildEntity.User == ConfigurationManager.AppSettings["VsoUsername"])
                    {
                        return(Ok());
                    }

                    if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["VSO.OrganisationName"]) || string.IsNullOrEmpty(ConfigurationManager.AppSettings["VSO.ProjectName"]))
                    {
                        throw new ApplicationException("Please configuration VSO.OrganisationName and VSO.ProjectName first.");
                    }

                    var api      = new ApiWrapper(ConfigurationManager.AppSettings["VSO.OrganisationName"], ConfigurationManager.AppSettings["VSO.ProjectName"]);
                    var coverage = await api.GetBuildCoverageAsync(build);

                    if (coverage != null)
                    {
                        foreach (var moduleCoverage in coverage)
                        {
                            buildEntity.CoverageCollection.Add(new CoverageEntity
                            {
                                Name             = moduleCoverage.Name,
                                ComputedAverage  = moduleCoverage.ComputedAverage,
                                BlocksCovered    = moduleCoverage.BlocksCovered,
                                BlocksNotCovered = moduleCoverage.BlocksNotCovered,
                                Build            = buildEntity
                            });
                        }
                    }

                    double currentRatio = ComputeRatio(buildEntity, context);
                    var    challenge    = ComputeCoverageChanges(buildEntity, context, currentRatio);
                    if (challenge != null)
                    {
                        context.Challenges.Add(challenge);
                    }

                    context.Builds.Add(buildEntity);
                    context.SaveChanges();
                }

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }