Exemplo n.º 1
0
        public async Task <LeadTimeForChangesModel> GetAzureDevOpsLeadTimeForChanges(bool getSampleData, string patToken,
                                                                                     string organization, string project, string repositoryId, string branch, string buildName,
                                                                                     int numberOfDays, int maxNumberOfItems, bool useCache)
        {
            LeadTimeForChangesModel model = new LeadTimeForChangesModel();

            try
            {
                TableStorageAuth     tableStorageAuth = Common.GenerateTableAuthorization(Configuration);
                LeadTimeForChangesDA da = new LeadTimeForChangesDA();
                model = await da.GetAzureDevOpsLeadTimesForChanges(getSampleData, patToken, tableStorageAuth,
                                                                   organization, project, repositoryId, branch, buildName, numberOfDays, maxNumberOfItems, useCache);
            }
            catch (Exception ex)
            {
                if (ex.Message == "Response status code does not indicate success: 403 (rate limit exceeded).")
                {
                    model.ProjectName  = project;
                    model.RateLimitHit = true;
                }
                else
                {
                    throw;
                }
            }
            return(model);
        }
Exemplo n.º 2
0
        public async Task <LeadTimeForChangesModel> GetGitHubLeadTimeForChanges(bool getSampleData, string clientId, string clientSecret,
                                                                                string owner, string repo, string branch, string workflowName, string workflowId,
                                                                                int numberOfDays, int maxNumberOfItems, bool useCache)
        {
            LeadTimeForChangesModel model = new LeadTimeForChangesModel();

            try
            {
                TableStorageAuth     tableStorageAuth = Common.GenerateTableAuthorization(Configuration);
                LeadTimeForChangesDA da = new LeadTimeForChangesDA();
                model = await da.GetGitHubLeadTimesForChanges(getSampleData, clientId, clientSecret, tableStorageAuth,
                                                              owner, repo, branch, workflowName, workflowId, numberOfDays, maxNumberOfItems, useCache);
            }
            catch (Exception ex)
            {
                if (ex.Message == "Response status code does not indicate success: 403 (rate limit exceeded).")
                {
                    model.ProjectName  = repo;
                    model.RateLimitHit = true;
                }
                else
                {
                    throw;
                }
            }
            return(model);
        }
Exemplo n.º 3
0
        public void LeadTimeForChangesSingleOneDayTest()
        {
            //Arrange
            LeadTimeForChanges metrics = new LeadTimeForChanges();
            int numberOfDays           = 1;
            List <KeyValuePair <DateTime, TimeSpan> > leadTimeForChangesList = new List <KeyValuePair <DateTime, TimeSpan> >
            {
                new KeyValuePair <DateTime, TimeSpan>(DateTime.Now, new TimeSpan(0, 45, 0))
            };

            //Act
            float result = metrics.ProcessLeadTimeForChanges(leadTimeForChangesList, numberOfDays);
            LeadTimeForChangesModel model = new LeadTimeForChangesModel
            {
                LeadTimeForChangesMetric            = result,
                LeadTimeForChangesMetricDescription = metrics.GetLeadTimeForChangesRating(result),
                IsProjectView = true,
                ItemOrder     = 1,
                RateLimitHit  = true
            };

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(0.75f, model.LeadTimeForChangesMetric);
            Assert.AreEqual("Elite", model.LeadTimeForChangesMetricDescription);
            Assert.AreEqual(0.75f, model.LeadTimeForChangesMetricDisplayMetric);
            Assert.AreEqual("hours", model.LeadTimeForChangesMetricDisplayUnit);
            Assert.AreEqual(true, model.IsProjectView);
            Assert.AreEqual(1, model.ItemOrder);
            Assert.AreEqual(true, model.RateLimitHit);
        }
        public async Task GHFeatureFlagsLeadTimeControllerAPILiveIntegrationTest()
        {
            //Arrange
            bool   getSampleData    = false;
            string clientId         = Configuration["AppSettings:GitHubClientId"];
            string clientSecret     = Configuration["AppSettings:GitHubClientSecret"];
            string owner            = "samsmithnz";
            string repo             = "SamsFeatureFlags";
            string branch           = "master";
            string workflowName     = "SamsFeatureFlags.CI/CD";
            string workflowId       = "108084";
            int    numberOfDays     = 20;
            int    maxNumberOfItems = 60;
            bool   useCache         = true;
            LeadTimeForChangesController controller = new LeadTimeForChangesController(Configuration);

            //Act
            LeadTimeForChangesModel model = await controller.GetGitHubLeadTimeForChanges(getSampleData, clientId, clientSecret, owner, repo, branch, workflowName, workflowId, numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            if (model.RateLimitHit == false)
            {
                Assert.AreEqual(repo, model.ProjectName);
                Assert.IsTrue(model.PullRequests.Count >= 0);
                if (model.PullRequests.Count > 0)
                {
                    Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].PullRequestId) == false);
                    Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].Branch) == false);
                    Assert.IsTrue(model.PullRequests[0].BuildCount >= 0);
                    Assert.IsTrue(model.PullRequests[0].Commits.Count > 0);
                    if (model.PullRequests[0].Commits.Count > 0)
                    {
                        Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].Commits[0].commitId) == false);
                        Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
                        Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].Commits[0].name) == false);
                    }
                    Assert.IsTrue(Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0) >= 0);
                    Assert.IsTrue(model.PullRequests[0].DurationPercent >= 0);
                    Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
                    Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
                }
                Assert.IsTrue(model.AverageBuildHours >= 0);
                Assert.IsTrue(model.AveragePullRequestHours >= 0);
                Assert.IsTrue(model.LeadTimeForChangesMetric >= 0);
                Assert.IsTrue(string.IsNullOrEmpty(model.LeadTimeForChangesMetricDescription) == false);
                Assert.AreEqual(numberOfDays, model.NumberOfDays);
                Assert.IsTrue(model.MaxNumberOfItems > 0);
                Assert.IsTrue(model.TotalItems > 0);
            }
        }
        public async Task AzLeadTimeControllerAPILiveIntegrationTest()
        {
            //Arrange
            bool   getSampleData    = false;
            string patToken         = "";//Configuration["AppSettings:AzureDevOpsPatToken"];
            string organization     = "samsmithnz";
            string project          = "SamLearnsAzure";
            string repositoryId     = "SamLearnsAzure";
            string branch           = "refs/heads/master";
            string buildName        = "SamLearnsAzure.CI";
            int    numberOfDays     = 30;
            int    maxNumberOfItems = 20;
            bool   useCache         = true;
            LeadTimeForChangesController controller = new LeadTimeForChangesController(Configuration);

            //Act
            LeadTimeForChangesModel model = await controller.GetAzureDevOpsLeadTimeForChanges(getSampleData, patToken, organization, project, repositoryId, branch, buildName, numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            if (model.RateLimitHit == false)
            {
                Assert.AreEqual(project, model.ProjectName);
                Assert.IsTrue(model.PullRequests.Count >= 0);
                if (model.PullRequests.Count > 0)
                {
                    Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].PullRequestId) == false);
                    Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].Branch) == false);
                    Assert.IsTrue(model.PullRequests[0].BuildCount >= 0);
                    Assert.IsTrue(model.PullRequests[0].Commits.Count >= 0);
                    if (model.PullRequests[0].Commits.Count > 0)
                    {
                        Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].Commits[0].commitId) == false);
                        Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
                        Assert.IsTrue(string.IsNullOrEmpty(model.PullRequests[0].Commits[0].name) == false);
                    }
                    Assert.IsTrue(Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0) >= 0);
                    Assert.IsTrue(model.PullRequests[0].DurationPercent >= 0);
                    Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
                    Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
                }
                Assert.IsTrue(model.AverageBuildHours >= 0);
                Assert.IsTrue(model.AveragePullRequestHours >= 0);
                Assert.IsTrue(model.LeadTimeForChangesMetric >= 0);
                Assert.IsTrue(string.IsNullOrEmpty(model.LeadTimeForChangesMetricDescription) == false);
                Assert.AreEqual(numberOfDays, model.NumberOfDays);
                Assert.IsTrue(model.MaxNumberOfItems > 0);
                Assert.IsTrue(model.TotalItems > 0);
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> LeadTimeForChanges()
        {
            int              maxNumberOfItems    = 20;   //20 is the optimium max that looks good with the current UI
            int              numberOfDays        = 30;   //TODO: Move number of days variable to a drop down list on the current UI
            bool             getSampleData       = false;
            bool             useCache            = true; //Use Azure storage instead of hitting the API. Quicker, but data may be up to 4 hours out of date
            string           patToken            = Configuration["AppSettings:AzureDevOpsPatToken"];
            string           clientId            = Configuration["AppSettings:GitHubClientId"];
            string           clientSecret        = Configuration["AppSettings:GitHubClientSecret"];
            ServiceApiClient serviceApiClient    = new ServiceApiClient(Configuration);
            List <LeadTimeForChangesModel> items = new List <LeadTimeForChangesModel>();

            //Get a list of settings
            List <AzureDevOpsSettings> azureDevOpsSettings = await serviceApiClient.GetAzureDevOpsSettings();

            List <GitHubSettings> githubSettings = await serviceApiClient.GetGitHubSettings();

            //Create lead time for changes models from each Azure DevOps setting object
            foreach (AzureDevOpsSettings item in azureDevOpsSettings)
            {
                LeadTimeForChangesModel newLeadTimeForChangesModel = await serviceApiClient.GetAzureDevOpsLeadTimeForChanges(getSampleData, patToken,
                                                                                                                             item.Organization, item.Project, item.Repository, item.Branch, item.BuildName, item.BuildId,
                                                                                                                             numberOfDays, maxNumberOfItems, useCache);

                newLeadTimeForChangesModel.ItemOrder = item.ItemOrder;
                if (newLeadTimeForChangesModel != null)
                {
                    items.Add(newLeadTimeForChangesModel);
                }
            }
            //Create lead time for changes models from each GitHub setting object
            foreach (GitHubSettings item in githubSettings)
            {
                LeadTimeForChangesModel newLeadTimeForChangesModel = await serviceApiClient.GetGitHubLeadTimeForChanges(getSampleData, clientId, clientSecret,
                                                                                                                        item.Owner, item.Repo, item.Branch, item.WorkflowName, item.WorkflowId,
                                                                                                                        numberOfDays, maxNumberOfItems, useCache);

                newLeadTimeForChangesModel.ItemOrder = item.ItemOrder;
                if (newLeadTimeForChangesModel != null)
                {
                    items.Add(newLeadTimeForChangesModel);
                }
            }

            //sort the final list
            items = items.OrderBy(o => o.ItemOrder).ToList();
            return(View(items));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> LeadTimeForChanges()
        {
            string           patToken            = Configuration["AppSettings:AzureDevOpsPatToken"];
            string           clientId            = Configuration["AppSettings:GitHubClientId"];
            string           clientSecret        = Configuration["AppSettings:GitHubClientSecret"];
            int              maxNumberOfItems    = 20;
            int              numberOfDays        = 60;
            bool             getSampleData       = false;
            bool             useCache            = true;
            ServiceApiClient serviceApiClient    = new ServiceApiClient(Configuration);
            List <LeadTimeForChangesModel> items = new List <LeadTimeForChangesModel>();

            //Get a list of settings
            List <AzureDevOpsSettings> azureDevOpsSettings = await serviceApiClient.GetAzureDevOpsSettings();

            List <GitHubSettings> githubSettings = await serviceApiClient.GetGitHubSettings();

            //Create deployment frequency models from each setting object
            foreach (AzureDevOpsSettings item in azureDevOpsSettings)
            {
                LeadTimeForChangesModel newLeadTimeForChangesModel = await serviceApiClient.GetAzureDevOpsLeadTimeForChanges(getSampleData, patToken,
                                                                                                                             item.Organization, item.Project, item.Repository, item.Branch, item.BuildName, item.BuildId,
                                                                                                                             numberOfDays, maxNumberOfItems, useCache);

                newLeadTimeForChangesModel.ItemOrder = item.ItemOrder;
                if (newLeadTimeForChangesModel != null)
                {
                    items.Add(newLeadTimeForChangesModel);
                }
            }
            foreach (GitHubSettings item in githubSettings)
            {
                LeadTimeForChangesModel newLeadTimeForChangesModel = await serviceApiClient.GetGitHubLeadTimeForChanges(getSampleData, clientId, clientSecret,
                                                                                                                        item.Owner, item.Repo, item.Branch, item.WorkflowName, item.WorkflowId,
                                                                                                                        numberOfDays, maxNumberOfItems, useCache);

                newLeadTimeForChangesModel.ItemOrder = item.ItemOrder;
                if (newLeadTimeForChangesModel != null)
                {
                    items.Add(newLeadTimeForChangesModel);
                }
            }

            //sort the list
            items = items.OrderBy(o => o.ItemOrder).ToList();
            return(View(items));
        }
Exemplo n.º 8
0
        public async Task GHLeadTimeForChangesDAIntegrationTest()
        {
            //Arrange
            bool             getSampleData    = true;
            string           clientId         = Configuration["AppSettings:GitHubClientId"];
            string           clientSecret     = Configuration["AppSettings:GitHubClientSecret"];
            TableStorageAuth tableStorageAuth = Common.GenerateTableAuthorization(Configuration);
            string           owner            = "samsmithnz";
            string           repo             = "devopsmetrics";
            string           masterBranch     = "master";
            string           workflowName     = "DevOpsMetrics.CI";
            string           workflowId       = "1162561";
            int  numberOfDays     = 7;
            int  maxNumberOfItems = 20;
            bool useCache         = false;

            //Act
            LeadTimeForChangesDA    da    = new LeadTimeForChangesDA();
            LeadTimeForChangesModel model = await da.GetGitHubLeadTimesForChanges(getSampleData, clientId, clientSecret, tableStorageAuth,
                                                                                  owner, repo, masterBranch, workflowName, workflowId,
                                                                                  numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(repo, model.ProjectName);
            Assert.IsTrue(model.PullRequests.Count > 0);
            Assert.IsTrue(model.PullRequests.Count <= 20);
            Assert.AreEqual("123", model.PullRequests[0].PullRequestId);
            Assert.AreEqual("branch1", model.PullRequests[0].Branch);
            Assert.AreEqual(1, model.PullRequests[0].BuildCount);
            Assert.IsTrue(model.PullRequests[0].Commits.Count > 0);
            Assert.AreEqual("abc", model.PullRequests[0].Commits[0].commitId);
            Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
            Assert.AreEqual("name1", model.PullRequests[0].Commits[0].name);
            Assert.AreEqual(60, Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0));
            Assert.AreEqual(33f, model.PullRequests[0].DurationPercent);
            Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
            Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
            Assert.AreEqual(1f, model.AverageBuildHours);
            Assert.AreEqual(20.33f, model.AveragePullRequestHours);
            Assert.AreEqual(21.33f, model.LeadTimeForChangesMetric);
            Assert.AreEqual("Elite", model.LeadTimeForChangesMetricDescription);
            Assert.AreEqual(numberOfDays, model.NumberOfDays);
            Assert.IsTrue(model.MaxNumberOfItems > 0);
            Assert.IsTrue(model.TotalItems > 0);
        }
        public async Task GHLeadTimeControllerIntegrationTest()
        {
            //https://devopsmetrics-prod-eu-service.azurewebsites.net/api/LeadTimeForChanges/GetGitHubLeadTimeForChanges?
            //getSampleData=False&clientId=&clientSecret=&owner=samsmithnz&repo=DevOpsMetrics&
            //branch=master&workflowId=1162561&numberOfDays=30&maxNumberOfItems=20
            //Arrange
            bool   getSampleData    = true;
            string clientId         = Configuration["AppSettings:GitHubClientId"];
            string clientSecret     = Configuration["AppSettings:GitHubClientSecret"];
            string owner            = "samsmithnz";
            string repo             = "devopsmetrics";
            string branch           = "master";
            string workflowName     = "DevOpsMetrics.CICD";
            string workflowId       = "1162561";
            int    numberOfDays     = 30;
            int    maxNumberOfItems = 20;
            bool   useCache         = true;
            LeadTimeForChangesController controller = new LeadTimeForChangesController(Configuration);

            //Act
            LeadTimeForChangesModel model = await controller.GetGitHubLeadTimeForChanges(getSampleData, clientId, clientSecret, owner, repo, branch, workflowName, workflowId, numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(repo, model.ProjectName);
            Assert.IsTrue(model.PullRequests.Count > 0);
            Assert.AreEqual("123", model.PullRequests[0].PullRequestId);
            Assert.AreEqual("branch1", model.PullRequests[0].Branch);
            Assert.AreEqual(1, model.PullRequests[0].BuildCount);
            Assert.IsTrue(model.PullRequests[0].Commits.Count > 0);
            Assert.AreEqual("abc", model.PullRequests[0].Commits[0].commitId);
            Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
            Assert.AreEqual("name1", model.PullRequests[0].Commits[0].name);
            Assert.AreEqual(60, Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0));
            Assert.AreEqual(33f, model.PullRequests[0].DurationPercent);
            Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
            Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
            Assert.AreEqual(1f, model.AverageBuildHours);
            Assert.AreEqual(20.33f, model.AveragePullRequestHours);
            Assert.AreEqual(21.33f, model.LeadTimeForChangesMetric);
            Assert.AreEqual("Elite", model.LeadTimeForChangesMetricDescription);
            Assert.AreEqual(numberOfDays, model.NumberOfDays);
            Assert.IsTrue(model.MaxNumberOfItems > 0);
            Assert.IsTrue(model.TotalItems > 0);
        }
Exemplo n.º 10
0
        public async Task AzLeadTimeForChangesDAIntegrationTest()
        {
            //Arrange
            bool             getSampleData    = true;
            string           patToken         = Configuration["AppSettings:AzureDevOpsPatToken"];
            TableStorageAuth tableStorageAuth = Common.GenerateTableAuthorization(Configuration);
            string           organization     = "samsmithnz";
            string           project          = "SamLearnsAzure";
            string           repositoryId     = "SamLearnsAzure";
            string           masterBranch     = "refs/heads/master";
            string           buildName        = "SamLearnsAzure.CI";
            int  numberOfDays     = 7;
            int  maxNumberOfItems = 20;
            bool useCache         = true;

            //Act
            LeadTimeForChangesDA    da    = new LeadTimeForChangesDA();
            LeadTimeForChangesModel model = await da.GetAzureDevOpsLeadTimesForChanges(getSampleData, patToken, tableStorageAuth,
                                                                                       organization, project, repositoryId, masterBranch, buildName,
                                                                                       numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(project, model.ProjectName);
            Assert.IsTrue(model.PullRequests.Count > 0);
            Assert.IsTrue(model.PullRequests.Count <= 20);
            Assert.AreEqual("123", model.PullRequests[0].PullRequestId);
            Assert.AreEqual("branch1", model.PullRequests[0].Branch);
            Assert.AreEqual(1, model.PullRequests[0].BuildCount);
            Assert.IsTrue(model.PullRequests[0].Commits.Count > 0);
            Assert.AreEqual("abc", model.PullRequests[0].Commits[0].commitId);
            Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
            Assert.AreEqual("name1", model.PullRequests[0].Commits[0].name);
            Assert.AreEqual(60, Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0));
            Assert.AreEqual(33f, model.PullRequests[0].DurationPercent);
            Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
            Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
            Assert.AreEqual(1f, model.AverageBuildHours);
            Assert.AreEqual(12f, model.AveragePullRequestHours);
            Assert.AreEqual(13f, model.LeadTimeForChangesMetric);
            Assert.AreEqual("Elite", model.LeadTimeForChangesMetricDescription);
            Assert.AreEqual(numberOfDays, model.NumberOfDays);
            Assert.IsTrue(model.MaxNumberOfItems > 0);
            Assert.IsTrue(model.TotalItems > 0);
        }
        public async Task GHLeadTimeControllerAPIIntegrationTest()
        {
            //Arrange
            bool   getSampleData    = true;
            string owner            = "samsmithnz";
            string repo             = "devopsmetrics";
            string branch           = "main";
            string workflowName     = "DevOpsMetrics.CICD";
            string workflowId       = "1162561";
            int    numberOfDays     = 7;
            int    maxNumberOfItems = 20;
            bool   useCache         = true;
            LeadTimeForChangesController controller = new(base.Configuration);

            //Act
            LeadTimeForChangesModel model = await controller.GetGitHubLeadTimeForChanges(getSampleData, owner, repo, branch, workflowName, workflowId, numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(repo, model.ProjectName);
            Assert.IsTrue(model.PullRequests.Count > 0);
            Assert.AreEqual("123", model.PullRequests[0].PullRequestId);
            Assert.AreEqual("branch1", model.PullRequests[0].Branch);
            Assert.AreEqual(1, model.PullRequests[0].BuildCount);
            Assert.IsTrue(model.PullRequests[0].Commits.Count > 0);
            Assert.AreEqual("abc", model.PullRequests[0].Commits[0].commitId);
            Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
            Assert.AreEqual("name1", model.PullRequests[0].Commits[0].name);
            Assert.AreEqual(60, Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0));
            Assert.AreEqual(33f, model.PullRequests[0].DurationPercent);
            Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
            Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
            Assert.AreEqual(1f, model.AverageBuildHours);
            Assert.AreEqual(20.33f, model.AveragePullRequestHours);
            Assert.AreEqual(21.33f, model.LeadTimeForChangesMetric);
            Assert.AreEqual("Elite", model.LeadTimeForChangesMetricDescription);
            Assert.AreEqual(numberOfDays, model.NumberOfDays);
            Assert.IsTrue(model.MaxNumberOfItems > 0);
            Assert.IsTrue(model.TotalItems > 0);
        }
        public async Task AzLeadTimeControllerIntegrationTest()
        {
            //Arrange
            bool   getSampleData    = true;
            string organization     = "samsmithnz";
            string project          = "SamLearnsAzure";
            string repository       = "SamLearnsAzure";
            string branch           = "refs/heads/master";
            string buildName        = "SamLearnsAzure.CI";
            int    numberOfDays     = 7;
            int    maxNumberOfItems = 20;
            bool   useCache         = true;
            LeadTimeForChangesController controller = new(base.Configuration);

            //Act
            LeadTimeForChangesModel model = await controller.GetAzureDevOpsLeadTimeForChanges(getSampleData, organization, project, repository, branch, buildName, numberOfDays, maxNumberOfItems, useCache);

            //Assert
            Assert.IsTrue(model != null);
            Assert.AreEqual(project, model.ProjectName);
            Assert.IsTrue(model.PullRequests.Count > 0);
            Assert.AreEqual("123", model.PullRequests[0].PullRequestId);
            Assert.AreEqual("branch1", model.PullRequests[0].Branch);
            Assert.AreEqual(1, model.PullRequests[0].BuildCount);
            Assert.IsTrue(model.PullRequests[0].Commits.Count > 0);
            Assert.AreEqual("abc", model.PullRequests[0].Commits[0].commitId);
            Assert.IsTrue(model.PullRequests[0].Commits[0].date >= DateTime.MinValue);
            Assert.AreEqual("name1", model.PullRequests[0].Commits[0].name);
            Assert.AreEqual(60, Math.Round(model.PullRequests[0].Duration.TotalMinutes, 0));
            Assert.AreEqual(33f, model.PullRequests[0].DurationPercent);
            Assert.IsTrue(model.PullRequests[0].StartDateTime >= DateTime.MinValue);
            Assert.IsTrue(model.PullRequests[0].EndDateTime >= DateTime.MinValue);
            Assert.AreEqual(1f, model.AverageBuildHours);
            Assert.AreEqual(12f, model.AveragePullRequestHours);
            Assert.AreEqual(13f, model.LeadTimeForChangesMetric);
            Assert.AreEqual("Elite", model.LeadTimeForChangesMetricDescription);
            Assert.AreEqual(numberOfDays, model.NumberOfDays);
            Assert.IsTrue(model.MaxNumberOfItems > 0);
            Assert.IsTrue(model.TotalItems > 0);
        }
Exemplo n.º 13
0
        public async Task <IActionResult> Project(string rowKey, int numberOfDays = 30)
        {
            int              maxNumberOfItems = 20;
            bool             getSampleData    = false;
            bool             useCache         = true;
            string           patToken         = Configuration["AppSettings:AzureDevOpsPatToken"];
            string           clientId         = Configuration["AppSettings:GitHubClientId"];
            string           clientSecret     = Configuration["AppSettings:GitHubClientSecret"];
            ProjectViewModel model            = new ProjectViewModel();

            //Find the right project to load
            ServiceApiClient           serviceApiClient    = new ServiceApiClient(Configuration);
            List <AzureDevOpsSettings> azureDevOpsSettings = await serviceApiClient.GetAzureDevOpsSettings();

            List <GitHubSettings> githubSettings = await serviceApiClient.GetGitHubSettings();

            //Create the days to view dropdown
            List <NumberOfDaysItem> numberOfDaysList = new List <NumberOfDaysItem>
            {
                new NumberOfDaysItem {
                    NumberOfDays = 7
                },
                new NumberOfDaysItem {
                    NumberOfDays = 14
                },
                new NumberOfDaysItem {
                    NumberOfDays = 21
                },
                new NumberOfDaysItem {
                    NumberOfDays = 30
                },
                new NumberOfDaysItem {
                    NumberOfDays = 60
                },
                new NumberOfDaysItem {
                    NumberOfDays = 90
                }
            };

            //Get Azure DevOps project details
            AzureDevOpsSettings azureDevOpsSetting;

            foreach (AzureDevOpsSettings item in azureDevOpsSettings)
            {
                if (item.RowKey == rowKey)
                {
                    azureDevOpsSetting = item;

                    DeploymentFrequencyModel deploymentFrequencyModel = await serviceApiClient.GetAzureDevOpsDeploymentFrequency(getSampleData, patToken,
                                                                                                                                 item.Organization, item.Project, item.Branch, item.BuildName, item.BuildId,
                                                                                                                                 numberOfDays, maxNumberOfItems, useCache);

                    LeadTimeForChangesModel leadTimeForChangesModel = await serviceApiClient.GetAzureDevOpsLeadTimeForChanges(getSampleData, patToken,
                                                                                                                              item.Organization, item.Project, item.Repository, item.Branch, item.BuildName, item.BuildId,
                                                                                                                              numberOfDays, maxNumberOfItems, useCache);

                    MeanTimeToRestoreModel meanTimeToRestoreModel = await serviceApiClient.GetAzureMeanTimeToRestore(getSampleData,
                                                                                                                     DevOpsPlatform.AzureDevOps, item.ProductionResourceGroup, numberOfDays, maxNumberOfItems);

                    ChangeFailureRateModel changeFailureRateModel = await serviceApiClient.GetChangeFailureRate(getSampleData,
                                                                                                                DevOpsPlatform.AzureDevOps, item.Organization, item.Project, item.Branch, item.BuildName,
                                                                                                                numberOfDays, maxNumberOfItems);

                    deploymentFrequencyModel.IsProjectView = true;
                    leadTimeForChangesModel.IsProjectView  = true;
                    meanTimeToRestoreModel.IsProjectView   = true;
                    changeFailureRateModel.IsProjectView   = true;
                    model = new ProjectViewModel
                    {
                        RowKey               = item.RowKey,
                        ProjectName          = item.Project,
                        TargetDevOpsPlatform = DevOpsPlatform.AzureDevOps,
                        DeploymentFrequency  = deploymentFrequencyModel,
                        LeadTimeForChanges   = leadTimeForChangesModel,
                        MeanTimeToRestore    = meanTimeToRestoreModel,
                        ChangeFailureRate    = changeFailureRateModel,
                        NumberOfDays         = new SelectList(numberOfDaysList, "NumberOfDays", "NumberOfDays"),
                        NumberOfDaysSelected = numberOfDays
                    };
                }
            }
            //Get GitHub project details
            GitHubSettings githubSetting;

            foreach (GitHubSettings item in githubSettings)
            {
                if (item.RowKey == rowKey)
                {
                    githubSetting = item;

                    DeploymentFrequencyModel deploymentFrequencyModel = await serviceApiClient.GetGitHubDeploymentFrequency(getSampleData, clientId, clientSecret,
                                                                                                                            item.Owner, item.Repo, item.Branch, item.WorkflowName, item.WorkflowId,
                                                                                                                            numberOfDays, maxNumberOfItems, useCache);

                    LeadTimeForChangesModel leadTimeForChangesModel = await serviceApiClient.GetGitHubLeadTimeForChanges(getSampleData, clientId, clientSecret,
                                                                                                                         item.Owner, item.Repo, item.Branch, item.WorkflowName, item.WorkflowId,
                                                                                                                         numberOfDays, maxNumberOfItems, useCache);

                    MeanTimeToRestoreModel meanTimeToRestoreModel = await serviceApiClient.GetAzureMeanTimeToRestore(getSampleData,
                                                                                                                     DevOpsPlatform.GitHub, item.ProductionResourceGroup, numberOfDays, maxNumberOfItems);

                    ChangeFailureRateModel changeFailureRateModel = await serviceApiClient.GetChangeFailureRate(getSampleData,
                                                                                                                DevOpsPlatform.GitHub, item.Owner, item.Repo, item.Branch, item.WorkflowName,
                                                                                                                numberOfDays, maxNumberOfItems);

                    deploymentFrequencyModel.IsProjectView = true;
                    leadTimeForChangesModel.IsProjectView  = true;
                    meanTimeToRestoreModel.IsProjectView   = true;
                    changeFailureRateModel.IsProjectView   = true;
                    model = new ProjectViewModel
                    {
                        RowKey               = item.RowKey,
                        ProjectName          = item.Repo,
                        TargetDevOpsPlatform = DevOpsPlatform.GitHub,
                        DeploymentFrequency  = deploymentFrequencyModel,
                        LeadTimeForChanges   = leadTimeForChangesModel,
                        MeanTimeToRestore    = meanTimeToRestoreModel,
                        ChangeFailureRate    = changeFailureRateModel,
                        NumberOfDays         = new SelectList(numberOfDaysList, "NumberOfDays", "NumberOfDays"),
                        NumberOfDaysSelected = numberOfDays
                    };
                }
            }

            return(View(model));
        }
Exemplo n.º 14
0
        public async Task <LeadTimeForChangesModel> GetGitHubLeadTimesForChanges(bool getSampleData, string clientId, string clientSecret, TableStorageAuth tableStorageAuth,
                                                                                 string owner, string repo, string mainBranch, string workflowName, string workflowId,
                                                                                 int numberOfDays, int maxNumberOfItems, bool useCache)
        {
            ListUtility <PullRequestModel> utility            = new ListUtility <PullRequestModel>();
            LeadTimeForChanges             leadTimeForChanges = new LeadTimeForChanges();
            List <PullRequestModel>        pullRequests       = new List <PullRequestModel>();

            if (getSampleData == false)
            {
                List <GitHubActionsRun> initialRuns = new List <GitHubActionsRun>();
                BuildsDA buildsDA = new BuildsDA();
                initialRuns = await buildsDA.GetGitHubActionRuns(clientId, clientSecret, tableStorageAuth, owner, repo, workflowName, workflowId, useCache);

                //Process all builds, filtering by main and feature branchs
                List <GitHubActionsRun> mainBranchRuns    = new List <GitHubActionsRun>();
                List <GitHubActionsRun> featureBranchRuns = new List <GitHubActionsRun>();
                List <string>           branches          = new List <string>();
                foreach (GitHubActionsRun item in initialRuns)
                {
                    if (item.status == "completed" && item.created_at > DateTime.Now.AddDays(-numberOfDays))
                    {
                        if (item.head_branch == mainBranch)
                        {
                            //Save the main branch
                            mainBranchRuns.Add(item);
                        }
                        else
                        {
                            //Save the feature branches
                            featureBranchRuns.Add(item);
                            //Record all unique branches
                            if (branches.Contains(item.head_branch) == false)
                            {
                                branches.Add(item.head_branch);
                            }
                        }
                    }
                }

                //Process the lead time for changes
                List <KeyValuePair <DateTime, TimeSpan> > leadTimeForChangesList = new List <KeyValuePair <DateTime, TimeSpan> >();
                foreach (string branch in branches)
                {
                    List <GitHubActionsRun> branchBuilds  = featureBranchRuns.Where(a => a.head_branch == branch).ToList();
                    PullRequestDA           pullRequestDA = new PullRequestDA();
                    GitHubPR pr = await pullRequestDA.GetGitHubPullRequest(clientId, clientSecret, tableStorageAuth, owner, repo, branch, useCache);

                    if (pr != null)
                    {
                        List <GitHubPRCommit> pullRequestCommits = await pullRequestDA.GetGitHubPullRequestCommits(clientId, clientSecret, tableStorageAuth, owner, repo, pr.number, useCache);

                        List <Commit> commits = new List <Commit>();
                        foreach (GitHubPRCommit item in pullRequestCommits)
                        {
                            commits.Add(new Commit
                            {
                                commitId = item.sha,
                                name     = item.commit.committer.name,
                                date     = item.commit.committer.date
                            });
                        }

                        DateTime minTime = DateTime.MaxValue;
                        DateTime maxTime = DateTime.MinValue;
                        foreach (GitHubPRCommit pullRequestCommit in pullRequestCommits)
                        {
                            if (minTime > pullRequestCommit.commit.committer.date)
                            {
                                minTime = pullRequestCommit.commit.committer.date;
                            }
                            if (maxTime < pullRequestCommit.commit.committer.date)
                            {
                                maxTime = pullRequestCommit.commit.committer.date;
                            }
                        }
                        foreach (GitHubActionsRun branchBuild in branchBuilds)
                        {
                            if (minTime > branchBuild.updated_at)
                            {
                                minTime = branchBuild.updated_at;
                            }
                            if (maxTime < branchBuild.updated_at)
                            {
                                maxTime = branchBuild.updated_at;
                            }
                        }

                        PullRequestModel pullRequest = new PullRequestModel
                        {
                            PullRequestId = pr.number,
                            Branch        = branch,
                            BuildCount    = branchBuilds.Count,
                            Commits       = commits,
                            StartDateTime = minTime,
                            EndDateTime   = maxTime,
                            Status        = pr.state,
                            Url           = $"https://github.com/{owner}/{repo}/pull/{pr.number}"
                        };
                        //Convert the pull request status to the standard UI status
                        if (pullRequest.Status == "closed")
                        {
                            pullRequest.Status = "completed";
                        }
                        else if (pullRequest.Status == "open")
                        {
                            pullRequest.Status = "inProgress";
                        }
                        else
                        {
                            pullRequest.Status = pullRequest.Status;
                        }

                        leadTimeForChangesList.Add(new KeyValuePair <DateTime, TimeSpan>(minTime, pullRequest.Duration));
                        pullRequests.Add(pullRequest);
                    }
                }

                //Calculate the lead time for changes value, in hours
                float leadTime = leadTimeForChanges.ProcessLeadTimeForChanges(leadTimeForChangesList, numberOfDays);

                List <PullRequestModel> uiPullRequests = utility.GetLastNItems(pullRequests, maxNumberOfItems);
                float maxPullRequestDuration           = 0f;
                foreach (PullRequestModel item in uiPullRequests)
                {
                    if (item.Duration.TotalMinutes > maxPullRequestDuration)
                    {
                        maxPullRequestDuration = (float)item.Duration.TotalMinutes;
                    }
                }
                foreach (PullRequestModel item in uiPullRequests)
                {
                    float interiumResult = (((float)item.Duration.TotalMinutes / maxPullRequestDuration) * 100f);
                    item.DurationPercent = Scaling.ScaleNumberToRange(interiumResult, 0, 100, 20, 100);
                }
                double totalHours = 0;
                foreach (GitHubActionsRun item in mainBranchRuns)
                {
                    totalHours += (item.updated_at - item.created_at).TotalHours;
                }
                float averageBuildHours = 0;
                if (mainBranchRuns.Count > 0)
                {
                    averageBuildHours = (float)totalHours / (float)mainBranchRuns.Count;
                }

                LeadTimeForChangesModel model = new LeadTimeForChangesModel
                {
                    ProjectName                         = repo,
                    TargetDevOpsPlatform                = DevOpsPlatform.GitHub,
                    AverageBuildHours                   = averageBuildHours,
                    AveragePullRequestHours             = leadTime,
                    LeadTimeForChangesMetric            = leadTime + averageBuildHours,
                    LeadTimeForChangesMetricDescription = leadTimeForChanges.GetLeadTimeForChangesRating(leadTime),
                    PullRequests                        = utility.GetLastNItems(pullRequests, maxNumberOfItems),
                    NumberOfDays                        = numberOfDays,
                    MaxNumberOfItems                    = uiPullRequests.Count,
                    TotalItems = pullRequests.Count
                };

                return(model);
            }
            else
            {
                List <PullRequestModel> samplePullRequests = utility.GetLastNItems(CreatePullRequestsSample(DevOpsPlatform.GitHub), maxNumberOfItems);
                LeadTimeForChangesModel model = new LeadTimeForChangesModel
                {
                    ProjectName                         = repo,
                    TargetDevOpsPlatform                = DevOpsPlatform.GitHub,
                    AverageBuildHours                   = 1f,
                    AveragePullRequestHours             = 20.33f,
                    LeadTimeForChangesMetric            = 20.33f + 1f,
                    LeadTimeForChangesMetricDescription = "Elite",
                    PullRequests                        = samplePullRequests,
                    NumberOfDays                        = numberOfDays,
                    MaxNumberOfItems                    = samplePullRequests.Count,
                    TotalItems = samplePullRequests.Count
                };

                return(model);
            }
        }
Exemplo n.º 15
0
        public async Task <LeadTimeForChangesModel> GetAzureDevOpsLeadTimesForChanges(bool getSampleData, string patToken, TableStorageAuth tableStorageAuth,
                                                                                      string organization, string project, string repositoryId, string mainBranch, string buildName, int numberOfDays, int maxNumberOfItems, bool useCache)
        {
            ListUtility <PullRequestModel> utility            = new ListUtility <PullRequestModel>();
            LeadTimeForChanges             leadTimeForChanges = new LeadTimeForChanges();
            List <PullRequestModel>        pullRequests       = new List <PullRequestModel>();

            if (getSampleData == false)
            {
                List <AzureDevOpsBuild> initialBuilds = new List <AzureDevOpsBuild>();
                BuildsDA buildsDA = new BuildsDA();
                initialBuilds = await buildsDA.GetAzureDevOpsBuilds(patToken, tableStorageAuth, organization, project, buildName, useCache);

                //Process all builds, filtering by main and feature branchs
                List <AzureDevOpsBuild> mainBranchBuilds    = new List <AzureDevOpsBuild>();
                List <AzureDevOpsBuild> featureBranchBuilds = new List <AzureDevOpsBuild>();
                List <string>           branches            = new List <string>();
                foreach (AzureDevOpsBuild item in initialBuilds)
                {
                    if (item.status == "completed" && item.queueTime > DateTime.Now.AddDays(-numberOfDays))
                    {
                        if (item.sourceBranch == mainBranch)
                        {
                            //Save the main branch
                            mainBranchBuilds.Add(item);
                        }
                        else
                        {
                            //Save the feature branches
                            featureBranchBuilds.Add(item);
                            //Record all unique branches
                            if (branches.Contains(item.branch) == false)
                            {
                                branches.Add(item.branch);
                            }
                        }
                    }
                }

                //Process the lead time for changes
                List <KeyValuePair <DateTime, TimeSpan> > leadTimeForChangesList = new List <KeyValuePair <DateTime, TimeSpan> >();
                foreach (string branch in branches)
                {
                    List <AzureDevOpsBuild> branchBuilds  = featureBranchBuilds.Where(a => a.sourceBranch == branch).ToList();
                    PullRequestDA           pullRequestDA = new PullRequestDA();
                    AzureDevOpsPR           pr            = await pullRequestDA.GetAzureDevOpsPullRequest(patToken, tableStorageAuth, organization, project, repositoryId, branch, useCache);

                    if (pr != null)
                    {
                        List <AzureDevOpsPRCommit> pullRequestCommits = await pullRequestDA.GetAzureDevOpsPullRequestCommits(patToken, tableStorageAuth, organization, project, repositoryId, pr.PullRequestId, useCache);

                        List <Commit> commits = new List <Commit>();
                        foreach (AzureDevOpsPRCommit item in pullRequestCommits)
                        {
                            commits.Add(new Commit
                            {
                                commitId = item.commitId,
                                name     = item.committer.name,
                                date     = item.committer.date
                            });
                        }

                        DateTime minTime = DateTime.MaxValue;
                        DateTime maxTime = DateTime.MinValue;
                        foreach (AzureDevOpsPRCommit pullRequestCommit in pullRequestCommits)
                        {
                            if (minTime > pullRequestCommit.committer.date)
                            {
                                minTime = pullRequestCommit.committer.date;
                            }
                            if (maxTime < pullRequestCommit.committer.date)
                            {
                                maxTime = pullRequestCommit.committer.date;
                            }
                        }
                        foreach (AzureDevOpsBuild branchBuild in branchBuilds)
                        {
                            if (minTime > branchBuild.finishTime)
                            {
                                minTime = branchBuild.finishTime;
                            }
                            if (maxTime < branchBuild.finishTime)
                            {
                                maxTime = branchBuild.finishTime;
                            }
                        }
                        PullRequestModel pullRequest = new PullRequestModel
                        {
                            PullRequestId = pr.PullRequestId,
                            Branch        = branch,
                            BuildCount    = branchBuilds.Count,
                            Commits       = commits,
                            StartDateTime = minTime,
                            EndDateTime   = maxTime,
                            Status        = pr.status,
                            Url           = $"https://dev.azure.com/{organization}/{project}/_git/{repositoryId}/pullrequest/{pr.PullRequestId}"
                        };

                        leadTimeForChangesList.Add(new KeyValuePair <DateTime, TimeSpan>(minTime, pullRequest.Duration));
                        pullRequests.Add(pullRequest);
                    }
                }

                //Calculate the lead time for changes value, in hours
                float leadTime = leadTimeForChanges.ProcessLeadTimeForChanges(leadTimeForChangesList, numberOfDays);

                List <PullRequestModel> uiPullRequests = utility.GetLastNItems(pullRequests, maxNumberOfItems);
                float maxPullRequestDuration           = 0f;
                foreach (PullRequestModel item in uiPullRequests)
                {
                    if (item.Duration.TotalMinutes > maxPullRequestDuration)
                    {
                        maxPullRequestDuration = (float)item.Duration.TotalMinutes;
                    }
                }
                foreach (PullRequestModel item in uiPullRequests)
                {
                    float interiumResult = (((float)item.Duration.TotalMinutes / maxPullRequestDuration) * 100f);
                    item.DurationPercent = Scaling.ScaleNumberToRange(interiumResult, 0, 100, 20, 100);
                }
                double totalHours = 0;
                foreach (AzureDevOpsBuild item in mainBranchBuilds)
                {
                    totalHours += (item.finishTime - item.queueTime).TotalHours;
                }
                float averageBuildHours = 0;
                if (mainBranchBuilds.Count > 0)
                {
                    averageBuildHours = (float)totalHours / (float)mainBranchBuilds.Count;
                }

                LeadTimeForChangesModel model = new LeadTimeForChangesModel
                {
                    ProjectName                         = project,
                    TargetDevOpsPlatform                = DevOpsPlatform.AzureDevOps,
                    AverageBuildHours                   = averageBuildHours,
                    AveragePullRequestHours             = leadTime,
                    LeadTimeForChangesMetric            = leadTime + averageBuildHours,
                    LeadTimeForChangesMetricDescription = leadTimeForChanges.GetLeadTimeForChangesRating(leadTime),
                    PullRequests                        = uiPullRequests,
                    NumberOfDays                        = numberOfDays,
                    MaxNumberOfItems                    = uiPullRequests.Count,
                    TotalItems = pullRequests.Count
                };

                return(model);
            }
            else
            {
                //Get sample data
                List <PullRequestModel> samplePullRequests = utility.GetLastNItems(CreatePullRequestsSample(DevOpsPlatform.AzureDevOps), maxNumberOfItems);
                LeadTimeForChangesModel model = new LeadTimeForChangesModel
                {
                    ProjectName                         = project,
                    TargetDevOpsPlatform                = DevOpsPlatform.AzureDevOps,
                    AverageBuildHours                   = 1f,
                    AveragePullRequestHours             = 12f,
                    LeadTimeForChangesMetric            = 12f + 1f,
                    LeadTimeForChangesMetricDescription = "Elite",
                    PullRequests                        = samplePullRequests,
                    NumberOfDays                        = numberOfDays,
                    MaxNumberOfItems                    = samplePullRequests.Count,
                    TotalItems = samplePullRequests.Count
                };

                return(model);
            }
        }