public async Task <int> UpdateAzureDevOpsPullRequests(string patToken, TableStorageAuth tableStorageAuth, string organization, string project, string repositoryId, int numberOfDays, int maxNumberOfItems) { AzureDevOpsAPIAccess api = new AzureDevOpsAPIAccess(); JArray items = await api.GetAzureDevOpsPullRequestsJArray(patToken, organization, project, repositoryId); int itemsAdded = 0; TableStorageCommonDA tableDA = new TableStorageCommonDA(tableStorageAuth, tableStorageAuth.TableAzureDevOpsPRs); //Check each build to see if it's in storage, adding the items not in storage foreach (JToken item in items) { AzureDevOpsPR pullRequest = JsonConvert.DeserializeObject <AzureDevOpsPR>(item.ToString()); string partitionKey = CreateAzureDevOpsPRPartitionKey(organization, project); string rowKey = pullRequest.PullRequestId; AzureStorageTableModel newItem = new AzureStorageTableModel(partitionKey, rowKey, item.ToString()); if (await tableDA.AddItem(newItem) == true) { itemsAdded++; } itemsAdded += await UpdateAzureDevOpsPullRequestCommits(patToken, tableStorageAuth, organization, project, repositoryId, pullRequest.PullRequestId, numberOfDays, maxNumberOfItems); } return(itemsAdded); }
public async Task <AzureDevOpsPR> GetAzureDevOpsPullRequest(string patToken, TableStorageAuth tableStorageAuth, string organization, string project, string repositoryId, string branch, bool useCache) { List <AzureDevOpsPR> prs = new List <AzureDevOpsPR>(); Newtonsoft.Json.Linq.JArray list; if (useCache == true) { //Get the pull requests from Azure storage AzureTableStorageDA daTableStorage = new AzureTableStorageDA(); list = daTableStorage.GetTableStorageItems(tableStorageAuth, tableStorageAuth.TableAzureDevOpsPRs, daTableStorage.CreateGitHubPRPartitionKey(organization, project)); } else { //Get the pull requests from the Azure DevOps API AzureDevOpsAPIAccess api = new AzureDevOpsAPIAccess(); list = await api.GetAzureDevOpsPullRequestsJArray(patToken, organization, project, repositoryId); } if (list != null) { prs = JsonConvert.DeserializeObject <List <AzureDevOpsPR> >(list.ToString()); } //Find the PR id AzureDevOpsPR pr = null; foreach (AzureDevOpsPR item in prs) { if (item.sourceRefName == branch) { pr = item; break; } } return(pr); }
public void AzGetPRCommitsDAIntegrationTest() { //Arrange TableStorageAuth tableStorageAuth = Common.GenerateTableAuthorization(Configuration); string organization = "samsmithnz"; string project = "SamLearnsAzure"; //Act AzureTableStorageDA da = new AzureTableStorageDA(); JArray prList = da.GetTableStorageItems(tableStorageAuth, tableStorageAuth.TableAzureDevOpsPRs, da.CreateAzureDevOpsPRPartitionKey(organization, project)); int itemsAdded = 0; foreach (JToken item in prList) { AzureDevOpsPR pullRequest = JsonConvert.DeserializeObject <AzureDevOpsPR>(item.ToString()); string pullRequestId = pullRequest.PullRequestId; JArray list = da.GetTableStorageItems(tableStorageAuth, tableStorageAuth.TableAzureDevOpsPRCommits, da.CreateAzureDevOpsPRCommitPartitionKey(organization, project, pullRequestId)); if (list.Count > 0) { itemsAdded = list.Count; break; } } //Assert Assert.IsTrue(itemsAdded >= 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); } }