示例#1
0
        public async Task <LeadTimeForChangesModel> GetGitHubLeadTimesForChanges(bool getSampleData, string clientId, string clientSecret, TableStorageConfiguration tableStorageConfig,
                                                                                 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, tableStorageConfig, 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();
                    PullRequestsDA          pullRequestDA = new PullRequestsDA();
                    GitHubPR pr = await pullRequestDA.GetGitHubPullRequest(clientId, clientSecret, tableStorageConfig, owner, repo, branch, useCache);

                    if (pr != null)
                    {
                        List <GitHubPRCommit> pullRequestCommits = await pullRequestDA.GetGitHubPullRequestCommits(clientId, clientSecret, tableStorageConfig, 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);
            }
        }
示例#2
0
        public async Task <LeadTimeForChangesModel> GetAzureDevOpsLeadTimesForChanges(bool getSampleData, string patToken, TableStorageConfiguration tableStorageConfig,
                                                                                      string organization, string project, string repository, 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, tableStorageConfig, 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();
                    PullRequestsDA          pullRequestDA = new PullRequestsDA();
                    AzureDevOpsPR           pr            = await pullRequestDA.GetAzureDevOpsPullRequest(patToken, tableStorageConfig, organization, project, repository, branch, useCache);

                    if (pr != null)
                    {
                        List <AzureDevOpsPRCommit> pullRequestCommits = await pullRequestDA.GetAzureDevOpsPullRequestCommits(patToken, tableStorageConfig, organization, project, repository, 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/{repository}/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);
            }
        }