Пример #1
0
        /// <summary>
        /// This method is used to get latest build result of given build definition Ids and branch name.
        /// The results should always contain same number of vsts build entity of given build definitions.
        /// If result is not found for a build definition Id, it will return vsts build entity with no result.
        /// Note: no validation of build definition ids is taken.
        /// Reference: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/list?view=azure-devops-rest-5.1
        /// </summary>
        /// <param name="buildDefinitionIds">build definition Ids</param>
        /// <param name="branchName">github repository branch name</param>
        /// <returns>List of vsts build entities</returns>
        public async Task <IList <VstsBuild> > GetLatestBuildsAsync(HashSet <BuildDefinitionId> buildDefinitionIds, string branchName)
        {
            ValidationUtil.ThrowIfNulOrEmptySet(buildDefinitionIds, nameof(buildDefinitionIds));
            ValidationUtil.ThrowIfNullOrWhiteSpace(branchName, nameof(branchName));

            // TODO: need to think about how to handle unexpected exception during REST API call
            string        requestPath        = string.Format(LatestBuildPathSegmentFormat, this.accessSetting.Organization, this.accessSetting.Project);
            IFlurlRequest latestBuildRequest = DevOpsAccessSetting.BaseUrl
                                               .AppendPathSegment(requestPath)
                                               .SetQueryParam("definitions", string.Join(",", buildDefinitionIds.Select(b => b.IdString())))
                                               .SetQueryParam("queryOrder", "finishTimeDescending")
                                               .SetQueryParam("maxBuildsPerDefinition", "1")
                                               .SetQueryParam("api-version", "5.1")
                                               .SetQueryParam("branchName", branchName)
                                               .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

            string resultJson = await latestBuildRequest.GetStringAsync().ConfigureAwait(false);

            JObject result = JObject.Parse(resultJson);

            if (!result.ContainsKey("count") || (int)result["count"] <= 0)
            {
                return(buildDefinitionIds.Select(i => VstsBuild.GetBuildWithNoResult(i, branchName)).ToList());
            }

            Dictionary <BuildDefinitionId, VstsBuild> latestBuilds = JsonConvert.DeserializeObject <VstsBuild[]>(result["value"].ToString()).ToDictionary(b => b.DefinitionId, b => b);

            return(buildDefinitionIds.Select(i => latestBuilds.ContainsKey(i) ? latestBuilds[i] : VstsBuild.GetBuildWithNoResult(i, branchName)).ToList());
        }
Пример #2
0
        private async Task <ResultMultiple <ObjectT> > GetListAsync <ObjectT>(ObjectT seed, IFlurlRequest flurlRequest)
            where ObjectT : ICloudObject, new()
        {
            string json = await flurlRequest.GetStringAsync();

            return(ResultMultiple <ObjectT> .ConvertObject(json, seed));
        }
Пример #3
0
        /// <summary>
        /// This method is used to get latest release result of given release definition Id and branch name with descending order.
        /// Reference: https://docs.microsoft.com/en-us/rest/api/azure/devops/release/releases/list?view=azure-devops-rest-5.1
        /// </summary>
        /// <param name="definitionId">release definition Ids</param>
        /// <param name="branchName">github repository branch name</param>
        /// <returns>List of IoT Edge releases</returns>
        public async Task <List <IoTEdgeRelease> > GetReleasesAsync(ReleaseDefinitionId definitionId, string branchName, int top = 5)
        {
            ValidationUtil.ThrowIfNullOrWhiteSpace(branchName, nameof(branchName));
            ValidationUtil.ThrowIfNonPositive(top, nameof(top));

            // TODO: need to think about how to handle unexpected exception during REST API call
            string        requestPath        = string.Format(ReleasePathSegmentFormat, this.accessSetting.Organization, this.accessSetting.Project);
            IFlurlRequest latestBuildRequest = DevOpsAccessSetting.ReleaseManagementBaseUrl
                                               .AppendPathSegment(requestPath)
                                               .SetQueryParam("definitionId", definitionId.IdString())
                                               .SetQueryParam("queryOrder", "descending")
                                               .SetQueryParam("$expand", "environments")
                                               .SetQueryParam("$top", top)
                                               .SetQueryParam("api-version", "5.1")
                                               .SetQueryParam("sourceBranchFilter", branchName)
                                               .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

            string resultJson = await latestBuildRequest.GetStringAsync().ConfigureAwait(false);

            JObject result = JObject.Parse(resultJson);

            if (!result.ContainsKey("count") || (int)result["count"] <= 0)
            {
                return(new List <IoTEdgeRelease>());
            }

            VstsRelease[] releases = JsonConvert.DeserializeObject <VstsRelease[]>(result["value"].ToString());
            return(releases.Select(r => IoTEdgeRelease.Create(r, branchName)).ToList());
        }
        private static async Task <decimal?> GetRateFromRequest(IFlurlRequest request)
        {
            string rawRate = await request.GetStringAsync();

            // Default empty value is "".
            rawRate = rawRate?.Trim('"');

            bool canParse = decimal.TryParse(rawRate, out var rate);

            return(canParse ? rate : (decimal?)null);
        }
Пример #5
0
        /// <summary>
        /// This method is used to get latest release result of given release definition Id and branch name with descending order.
        /// Reference: https://docs.microsoft.com/en-us/rest/api/azure/devops/release/releases/list?view=azure-devops-rest-5.1
        /// </summary>
        /// <param name="definitionId">release definition Ids</param>
        /// <param name="branchName">github repository branch name</param>
        /// <returns>List of IoT Edge releases</returns>
        public async Task <List <IoTEdgeRelease> > GetReleasesAsync(ReleaseDefinitionId definitionId, string branchName, int top = 200)
        {
            ValidationUtil.ThrowIfNullOrWhiteSpace(branchName, nameof(branchName));
            ValidationUtil.ThrowIfNonPositive(top, nameof(top));

            // TODO: need to think about how to handle unexpected exception during REST API call
            string        requestPath         = string.Format(ReleasePathSegmentFormat, this.accessSetting.Organization, this.accessSetting.Project);
            IFlurlRequest listReleasesRequest = DevOpsAccessSetting.ReleaseManagementBaseUrl
                                                .AppendPathSegment(requestPath)
                                                .SetQueryParam("definitionId", definitionId.IdString())
                                                .SetQueryParam("queryOrder", "descending")
                                                .SetQueryParam("$top", top)
                                                .SetQueryParam("api-version", "5.1")
                                                .SetQueryParam("sourceBranchFilter", branchName)
                                                .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

            string releasesJson = await listReleasesRequest.GetStringAsync().ConfigureAwait(false);

            JObject releasesJObject = JObject.Parse(releasesJson);

            if (!releasesJObject.ContainsKey("count") || (int)releasesJObject["count"] <= 0)
            {
                return(new List <IoTEdgeRelease>());
            }

            VstsRelease[] vstsReleases    = JsonConvert.DeserializeObject <VstsRelease[]>(releasesJObject["value"].ToString());
            var           iotEdgeReleases = new List <IoTEdgeRelease>();

            foreach (VstsRelease vstsRelease in vstsReleases)
            {
                IFlurlRequest getReleaseRequest = DevOpsAccessSetting.ReleaseManagementBaseUrl
                                                  .AppendPathSegment(requestPath)
                                                  .SetQueryParam("api-version", "5.1")
                                                  .SetQueryParam("releaseId", vstsRelease.Id)
                                                  .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

                string releaseJson = await getReleaseRequest.GetStringAsync().ConfigureAwait(false);

                try
                {
                    VstsRelease releaseWithDetails = JsonConvert.DeserializeObject <VstsRelease>(releaseJson);
                    iotEdgeReleases.Add(IoTEdgeRelease.Create(releaseWithDetails, branchName));
                }
                catch (System.Exception ex)
                {
                    // TODO: log exception
                    Console.WriteLine(ex.ToString());
                }
            }

            return(iotEdgeReleases);
        }
Пример #6
0
        public async Task <VstsBuild> GetLatestBuildAsync(int buildDefinitionId, string branchName)
        {
            string        requestPath        = string.Format(LatestBuildPathSegmentFormat, this.accessSetting.Organization, this.accessSetting.Project, buildDefinitionId);
            IFlurlRequest latestBuildRequest = DevOpsAccessSetting.BaseUrl
                                               .AppendPathSegment(requestPath)
                                               .SetQueryParam("api-version", "5.1-preview.1")
                                               .SetQueryParam("branchName", branchName)
                                               .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

            string resultJson = await latestBuildRequest.GetStringAsync().ConfigureAwait(false);

            JObject result = JObject.Parse(resultJson);

            if (result.ContainsKey("result"))
            {
                var latestBuild = JsonConvert.DeserializeObject <VstsBuild>(resultJson);
                return(latestBuild);
            }

            return(EmptyBuild);
        }
Пример #7
0
        public async Task <IList <VstsBuild> > GetBuildsAsync(HashSet <BuildDefinitionId> buildDefinitionIds, string branchName, DateTime?minTime = null, int?maxBuildsPerDefinition = null)
        {
            ValidationUtil.ThrowIfNullOrEmptySet(buildDefinitionIds, nameof(buildDefinitionIds));
            ValidationUtil.ThrowIfNullOrWhiteSpace(branchName, nameof(branchName));

            // TODO: need to think about how to handle unexpected exception during REST API call
            string        requestPath        = string.Format(LatestBuildPathSegmentFormat, this.accessSetting.Organization, this.accessSetting.Project);
            IFlurlRequest latestBuildRequest = GetBuildsRequestUri(buildDefinitionIds, branchName, requestPath, minTime, maxBuildsPerDefinition)
                                               .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

            string resultJson = await latestBuildRequest.GetStringAsync().ConfigureAwait(false);

            JObject result = JObject.Parse(resultJson);

            if (!result.ContainsKey("count") || (int)result["count"] <= 0)
            {
                return(buildDefinitionIds.Select(i => VstsBuild.CreateBuildWithNoResult(i, branchName)).ToList());
            }

            return(JsonConvert.DeserializeObject <VstsBuild[]>(result["value"].ToString()).ToList());
        }
Пример #8
0
        /// <summary>
        /// This method is used to execute a Dev Ops work item query and get the number of bugs for a given query.
        /// If result is not found for a query Id, it will return 0.
        /// Note: there is no validation of work item query ids.
        /// Reference: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/wiql/get?view=azure-devops-rest-5.1
        /// </summary>
        /// <param name="bugQueryId">bug query id from azure dev ops shared queries</param>
        /// <returns>Number of bugs output by query</returns>
        public async Task <int> GetBugsQuery(string bugQueryId)
        {
            ValidationUtil.ThrowIfNullOrEmptySet(bugQueryId, nameof(bugQueryId));

            // TODO: need to think about how to handle unexpected exception during REST API call
            string        requestPath          = string.Format(WorkItemPathSegmentFormat, DevOpsAccessSetting.BaseUrl, this.accessSetting.Organization, this.accessSetting.Project, this.accessSetting.Team, bugQueryId);
            IFlurlRequest workItemQueryRequest = ((Url)requestPath)
                                                 .WithBasicAuth(string.Empty, this.accessSetting.PersonalAccessToken);

            string resultJson = await workItemQueryRequest.GetStringAsync().ConfigureAwait(false);

            JObject result = JObject.Parse(resultJson);

            if (!result.ContainsKey("queryType"))
            {
                return(0);
            }
            else
            {
                return(result["workItems"].Count());
            }
        }
Пример #9
0
        /// <summary>
        /// This method is used to get latest build result of given build definition Ids and branch name.
        /// The results should always contain same number of vsts build entity of given build definitions.
        /// If result is not found for a build definition Id, it will return vsts build entity with no result.
        /// Note: no validation of build definition ids is taken.
        /// Reference: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-5.1
        /// </summary>
        /// <param name="buildDefinitionIds">build definition Ids</param>
        /// <param name="branchName">github repository branch name</param>
        /// <returns>List of vsts build entities</returns>
        public async Task <IList <VstsBuild> > GetLatestBuildsAsync(HashSet <BuildDefinitionId> buildDefinitionIds, string branchName)
        {
            ValidationUtil.ThrowIfNullOrEmptySet(buildDefinitionIds, nameof(buildDefinitionIds));
            ValidationUtil.ThrowIfNullOrWhiteSpace(branchName, nameof(branchName));

            // TODO: need to think about how to handle unexpected exception during REST API call
            string        requestPath        = string.Format(LatestBuildPathSegmentFormat, DevOpsAccessSetting.AzureOrganization, DevOpsAccessSetting.AzureProject);
            IFlurlRequest latestBuildRequest = GetBuildsRequestUri(buildDefinitionIds, branchName, requestPath, null, 1)
                                               .WithBasicAuth(string.Empty, this.accessSetting.MsazurePAT);

            string resultJson = await latestBuildRequest.GetStringAsync().ConfigureAwait(false);

            JObject result = JObject.Parse(resultJson);

            if (!result.ContainsKey("count") || (int)result["count"] <= 0)
            {
                return(buildDefinitionIds.Select(i => VstsBuild.CreateBuildWithNoResult(i, branchName)).ToList());
            }

            Dictionary <BuildDefinitionId, VstsBuild> latestBuilds = JsonConvert.DeserializeObject <VstsBuild[]>(result["value"].ToString()).ToDictionary(b => b.DefinitionId, b => b);

            return(buildDefinitionIds.Select(i => latestBuilds.ContainsKey(i) ? latestBuilds[i] : VstsBuild.CreateBuildWithNoResult(i, branchName)).ToList());
        }