public async Task <GitHubPR> GetGitHubPullRequest(string clientId, string clientSecret, TableStorageAuth tableStorageAuth, string owner, string repo, string branch, bool useCache) { List <GitHubPR> prs = new List <GitHubPR>(); Newtonsoft.Json.Linq.JArray list; if (useCache == true) { //Get the pull requests from Azure storage AzureTableStorageDA daTableStorage = new AzureTableStorageDA(); list = daTableStorage.GetTableStorageItems(tableStorageAuth, tableStorageAuth.TableGitHubPRs, daTableStorage.CreateGitHubPRPartitionKey(owner, repo)); } else { //Get the pull requests from the GitHub API GitHubAPIAccess api = new GitHubAPIAccess(); list = await api.GetGitHubPullRequestsJArray(clientId, clientSecret, owner, repo, branch); } if (list != null) { prs = JsonConvert.DeserializeObject <List <GitHubPR> >(list.ToString()); } //Find the PR id GitHubPR pr = null; foreach (GitHubPR item in prs) { if (item.head.@ref == branch) { pr = item; break; } } return(pr); }
public async Task <int> UpdateGitHubActionPullRequests(string clientId, string clientSecret, TableStorageAuth tableStorageAuth, string owner, string repo, string branch, int numberOfDays, int maxNumberOfItems) { GitHubAPIAccess api = new GitHubAPIAccess(); JArray items = await api.GetGitHubPullRequestsJArray(clientId, clientSecret, owner, repo, branch); int itemsAdded = 0; TableStorageCommonDA tableDA = new TableStorageCommonDA(tableStorageAuth, tableStorageAuth.TableGitHubPRs); //Check each build to see if it's in storage, adding the items not in storage foreach (JToken item in items) { GitHubPR pr = JsonConvert.DeserializeObject <GitHubPR>(item.ToString()); if (pr.state == "closed") { string partitionKey = CreateGitHubPRPartitionKey(owner, repo); string rowKey = pr.number; AzureStorageTableModel newItem = new AzureStorageTableModel(partitionKey, rowKey, item.ToString()); if (await tableDA.AddItem(newItem) == true) { itemsAdded++; } itemsAdded += await UpdateGitHubActionPullRequestCommits(clientId, clientSecret, tableStorageAuth, owner, repo, pr.number); } } return(itemsAdded); }
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); } }