Exemple #1
0
        async Task <IList <VstsBuild> > GetBuildsAndTrackLastUpdatedAsync(BuildManagement buildManagement, BuildDefinitionId buildDefinitionId, string branch)
        {
            Dictionary <BuildDefinitionId, DateTime> buildDefinitionIdToLastUpdate = this.buildLastUpdatePerBranchPerDefinition.GetIfExists(branch);

            if (buildDefinitionIdToLastUpdate == null)
            {
                buildDefinitionIdToLastUpdate = new Dictionary <BuildDefinitionId, DateTime>();
            }

            DateTime          lastUpdate   = buildDefinitionIdToLastUpdate.GetIfExists(buildDefinitionId);
            IList <VstsBuild> buildResults = await buildManagement.GetBuildsAsync(new HashSet <BuildDefinitionId> {
                buildDefinitionId
            }, branch, lastUpdate);

            Console.WriteLine($"Query VSTS builds for branch [{branch}] and build definition [{buildDefinitionId.ToString()}]: last update={lastUpdate} => result count={buildResults.Count}");

            DateTime maxLastChange = DateTime.MinValue;

            foreach (VstsBuild build in buildResults.Where(r => r.HasResult()))
            {
                if (build.LastChangedDate > maxLastChange)
                {
                    maxLastChange = build.LastChangedDate;
                }
            }

            buildDefinitionIdToLastUpdate.Upsert(buildDefinitionId, maxLastChange);
            this.buildLastUpdatePerBranchPerDefinition.Upsert(branch, buildDefinitionIdToLastUpdate);

            return(buildResults);
        }
        async Task <DateTime> ImportVstsBuildsDataForSpecificDefinitionAsync(
            BuildManagement buildManagement,
            string branch,
            BuildDefinitionId buildDefinitionId,
            DateTime lastUpdate)
        {
            SqlConnection sqlConnection = null;

            try
            {
                sqlConnection = new SqlConnection(this.dbConnectionString);
                sqlConnection.Open();

                IList <VstsBuild> buildResults = await buildManagement.GetBuildsAsync(new HashSet <BuildDefinitionId> {
                    buildDefinitionId
                }, branch, lastUpdate);

                Console.WriteLine($"Query VSTS for branch [{branch}] and build definition [{buildDefinitionId.ToString()}]: last update={lastUpdate} => result count={buildResults.Count}");
                DateTime maxLastChange = lastUpdate;

                foreach (VstsBuild build in buildResults.Where(r => r.HasResult()))
                {
                    UpsertVstsBuildToDb(sqlConnection, build);

                    if (build.LastChangedDate > maxLastChange)
                    {
                        maxLastChange = build.LastChangedDate;
                    }
                }

                return(maxLastChange);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                sqlConnection?.Close();
            }
        }