コード例 #1
0
        private static async Task <PullRequest.Rootobject> GetPullRequests(HttpClient httpClient, string memberId)
        {
            var httpResponseMessage = httpClient.GetAsync($"_apis/git/pullrequests?api-version=4.1&searchCriteria.creatorId={memberId}&searchCriteria.status=1").Result;
            var responseBody        = await httpResponseMessage.Content.ReadAsStringAsync();

            httpResponseMessage.Dispose();
            PullRequest.Rootobject pullRequestRootObject = JsonConvert.DeserializeObject <PullRequest.Rootobject>(responseBody);
            return(pullRequestRootObject);
        }
コード例 #2
0
        private static async Task <HttpResponseMessage> VstsApiRequest(TraceWriter log, AccessToken accessToken, string intent)
        {
            using (var httpClient = GetHttpClient("https://mseng.visualstudio.com", accessToken))
            {
                var memberId = GetMemberId(httpClient);
                if (memberId == null)
                {
                    return(AlexaResponse($"There was a problem communicating with the v. s. t. s. API"));
                }

                PullRequest.Rootobject pullRequestRootObject = await GetPullRequests(httpClient, memberId);

                int pullRequestCount = pullRequestRootObject.count;

                if (pullRequestCount == 0)
                {
                    return(AlexaResponse($"You don't have any active pull requests"));
                }

                PlainTextOutputSpeech outputSpeech = new PlainTextOutputSpeech();
                //string firstName = (req.Request as IntentRequest)?.Intent.Slots.FirstOrDefault(s => s.Key == "FirstName").Value?.Value;
                string text = $"You have {pullRequestCount} active pull request{((pullRequestCount == 1) ? "" : "s")}. ";

                text += String.Join(", ", pullRequestRootObject.value.Select(pr => pr.title).ToArray());
                text += ".";

                var policyEvaluationsList = new List <PolicyEvaluations.Evaluation>();

                var pullRequestsWithExpiredBuilds = pullRequestRootObject.value.Where(pr =>
                {
                    string artifactid     = "vstfs:///CodeReview/CodeReviewId/" + pr.repository.project.id + "/" + pr.pullRequestId;
                    var policyEvaluations = GetPolicyEvaluations(httpClient, pr.repository.project.id, artifactid).Result;
                    if (!policyEvaluations.Any(pe => pe.configuration.isBlocking && pe.configuration.isEnabled && pe.configuration.type.displayName == "Build" && pe.status == "rejected"))
                    {
                        var requeueList = policyEvaluations
                                          .Where(pe => pe.configuration.isBlocking && pe.configuration.isEnabled && pe.configuration.type.displayName == "Build" && pe.context.isExpired)
                                          .Select(pe => new PolicyEvaluations.Evaluation()
                        {
                            EvaluationId = pe.evaluationId, ProjectId = pr.repository.project.id
                        }).ToList();
                        if (requeueList.Count > 0)
                        {
                            policyEvaluationsList.AddRange(requeueList);
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }).ToList();

                if (pullRequestsWithExpiredBuilds.Count == 1)
                {
                    text += $" It looks like {pullRequestsWithExpiredBuilds.First().title} has at least one expired build.";
                }
                else if (pullRequestsWithExpiredBuilds.Count > 1)
                {
                    text += $" You have {pullRequestsWithExpiredBuilds.Count} pull requests with expired builds.";
                }

                if (intent == "queueBuilds")
                {
                    text = "";

                    if (pullRequestsWithExpiredBuilds.Count == 0)
                    {
                        text = $"I couldn't find any pull requests with expired builds.";
                    }
                    else
                    {
                        foreach (var pe in policyEvaluationsList)
                        {
                            RequeueBuild(httpClient, pe);
                        }

                        text = $"I requeued {policyEvaluationsList.Count} build{(policyEvaluationsList.Count > 1 ? "s" : "")}";
                        if (pullRequestsWithExpiredBuilds.Count == 1)
                        {
                            text += $" for {pullRequestsWithExpiredBuilds.First().title}.";
                        }
                        else if (pullRequestsWithExpiredBuilds.Count > 1)
                        {
                            text += $" for {pullRequestsWithExpiredBuilds.Count} pull request{(pullRequestsWithExpiredBuilds.Count > 1 ? "s" : "")}.";
                        }
                    }
                }
                return(AlexaResponse(text));
            }
        }