Example #1
0
        public Dictionary <string, string> GetRestorePoint(Methods method, DateTime loadDate)
        {
            DbDevOpsDashContext ctx = new DbDevOpsDashContext();

            var restorePoint = ctx.LoadRestorePoint
                               .AsNoTracking()
                               .Where(rp => rp.Method == (int)method && rp.LoadDate == loadDate)
                               .FirstOrDefault();

            var dict = new Dictionary <string, string>();

            if (restorePoint == null)
            {
                return(dict);
            }

            string[] keys = restorePoint.LastId.Split(',');

            for (var i = 0; i < keys.Length; i++)
            {
                string[] keyx = keys[i].Split('=');
                dict.Add(keyx[0], keyx[1]);
            }

            return(dict);
        }
Example #2
0
        public async Task SaveLoadHistory(Methods method, int result, bool isSuccessful, string message, DateTime?fromDate = null, DateTime?toDate = null)
        {
            var _reg = LoadHistoryHelper(method, result, isSuccessful, message, fromDate, toDate);

            using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
            {
                ctx.LoadHistory.Add(_reg);
                await ctx.SaveChangesAsync();
            };
        }
Example #3
0
        public DateTime GetLastToDate(Methods method)
        {
            DateTime?           _lastExecution = null;
            DbDevOpsDashContext ctx            = new DbDevOpsDashContext();

            _lastExecution = ctx.LoadHistory.Where(h => h.Method == (int)method).Max(h => h.ToDate);

            if (_lastExecution == null)
            {
                return(DateTime.MinValue);
            }

            // carga já executada
            return((DateTime)_lastExecution);
        }
Example #4
0
        public bool AlreadyBeenExecuted(Methods method)
        {
            DbDevOpsDashContext ctx = new DbDevOpsDashContext();

            var _lastExecution = ctx.LoadHistory
                                 .Where(y => y.IsSuccessful && y.Method == (int)method).OrderByDescending(x => x.Id).FirstOrDefault();

            if (_lastExecution == null)
            {
                return(false);
            }

            if (_lastExecution.LoadDate < DateTime.Today)
            {
                return(false);
            }

            //carga já executada
            return(true);
        }
Example #5
0
        public async Task <int> ListAllGroupsAsync()
        {
            int    ret        = 0;
            string restoreKey = "TbGroups.Id";

            if (_utils.AlreadyBeenExecuted(Methods.Groups))
            {
                await _utils.SaveLoadHistory(Methods.Groups, ret, true, "load has already been successfully executed");

                return(0);
            }

            // all projs
            string aux;
            int    intPId = 0;

            var restorePoint = _utils.GetRestorePoint(Methods.Groups, DateTime.Today);

            if (restorePoint.Count > 0)
            {
                restorePoint.TryGetValue(restoreKey, out aux);
                intPId = Convert.ToInt32(aux);
            }

            DbDevOpsDashContext projsCtx = new DbDevOpsDashContext();
            var projects = await projsCtx.Projects.AsNoTracking().Where(p => p.Id > intPId && p.IsActive == true).OrderBy(p => p.Id).ToListAsync();

            await projsCtx.DisposeAsync();

            // all pipelines
            foreach (var proj in projects)
            {
                var _fromApi = await GetAllGroupsByProjectIdAsync(proj.ProjectId);

                if (_fromApi.Count <= 0)
                {
                    continue;
                }


                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    var _inDb = await ctx.Groups
                                .Where(p => p.ProjectId == proj.ProjectId && p.IsActive == true)
                                .AsNoTracking()
                                .ToListAsync();

                    // get ID from Db
                    foreach (var inDb in _inDb)
                    {
                        if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.OriginId == inDb.OriginId).Any())
                        {
                            _fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.OriginId == inDb.OriginId)
                            .FirstOrDefault().Id = inDb.Id;
                        }
                    }

                    // to update
                    var _toUpdate = _fromApi
                                    .Intersect(_inDb, new GroupEqualityComparer())
                                    .Select(r => new GroupModel
                    {
                        IsActive      = r.IsActive,
                        LoadDate      = DateTime.Now,
                        ProjectId     = proj.ProjectId,
                        Url           = r.Url,
                        Id            = r.Id,
                        Descriptor    = r.Descriptor,
                        DisplayName   = r.DisplayName,
                        Domain        = r.Domain,
                        MailAddress   = r.MailAddress,
                        PrincipalName = r.PrincipalName,
                        Origin        = r.Origin,
                        OriginId      = r.OriginId,
                        OurGroupCode  = _utils.GetOurGroupCode(r.DisplayName),
                    });;

                    ret = ret + _toUpdate.Count();
                    ctx.Groups.UpdateRange(_toUpdate);

                    // to delete
                    var _toDelete = _inDb.Where(inDb => !_fromApi.Any(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.OriginId == inDb.OriginId)).ToList();
                    _toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                    ret = ret + _toDelete.Count;
                    ctx.Groups.UpdateRange(_toDelete);

                    // to add
                    var _toAdd = _fromApi.Where(fromApi => !_inDb.Any(inDb => fromApi.ProjectId == inDb.ProjectId && fromApi.OriginId == inDb.OriginId)).ToList();
                    await ctx.Groups.AddRangeAsync(_toAdd);

                    ret = ret + _toAdd.Count;

                    // set OurGroupCode
                    _toAdd.ForEach(x => { x.OurGroupCode = _utils.GetOurGroupCode(x.DisplayName); });

                    // restore point
                    var rpm = new LoadRestoreModel()
                    {
                        ExecutionDateTime = DateTime.Now,
                        LastId            = $"{restoreKey}={proj.Id}",
                        LoadDate          = DateTime.Today,
                        Method            = (int)Methods.Groups,
                        Id = (int)Methods.Groups,
                    };

                    ctx.LoadRestorePoint.Update(rpm);

                    // commit
                    await ctx.SaveChangesAsync();
                }
            }

            await _utils.SaveLoadHistory(Methods.Groups, ret, true, "");

            return(ret);
        }
Example #6
0
        public async Task <int> ListAllAsync()
        {
            int      ret           = 0;
            DateTime maxToDate     = DateTime.Today;
            DateTime minFromDate   = Convert.ToDateTime(Environment.GetEnvironmentVariable("MinFromDate").ToString());
            int      dateIncrement = Convert.ToInt32(Environment.GetEnvironmentVariable("DateIncrement").ToString());

            //searchCriteria.fromDate - If provided, only include history entries created after this date(string)
            DateTime fromDate = _utils.GetLastToDate(Methods.Builds);

            // se fromDate = minvalue -> executar carga inicial a partir de 01/01/2020 até data de hoje
            if (fromDate >= DateTime.Today)
            {
                await _utils.SaveLoadHistory(Methods.Builds, ret, true, "load has already been successfully executed");

                return(0);
            }

            // list
            DbDevOpsDashContext pipesCtx = new DbDevOpsDashContext();
            var pipes = await pipesCtx.Projects
                        .Where(p => p.IsActive == true)
                        .OrderBy(p => p.Id)
                        .AsNoTracking().ToListAsync();

            await pipesCtx.DisposeAsync();

            //searchCriteria.fromDate - If provided, only include history entries created after this date(string)
            if (fromDate < minFromDate)
            {
                fromDate = minFromDate;
            }

            //searchCriteria.toDate   - If provided, only include history entries created before this date(string)
            DateTime toDate = fromDate.AddDays(dateIncrement);

            if (toDate > maxToDate)
            {
                toDate = maxToDate;
            }

            while (true)
            {
                List <BuildModel> _fromApi = new List <BuildModel>();

                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    try
                    {
                        foreach (var p in pipes)
                        {
                            var builds = await GetAllBuildsByProjectId(p.ProjectId, fromDate, toDate);

                            var _tmpApi = builds.value.Select(v => new BuildModel()
                            {
                                BuildId             = v.id,
                                BuildNumber         = v.buildNumber,
                                BuildNumberRevision = v.buildNumberRevision,
                                BuildUri            = v.uri,
                                BuildUrl            = v.url,
                                //Definition_Drafts = v.definition.drafts == null ? null : v.definition.drafts.ToString(),
                                Definition_Id   = v.definition.id,
                                Definition_Name = v.definition.name,
                                Definition_Url  = v.definition.url,
                                FinishTime      = v.finishTime,
                                Id             = 0,
                                IsActive       = true,
                                KeepForever    = v.keepForever,
                                LastChangeDate = v.lastChangedDate,
                                Links_Badge    = v._links.badge.href,
                                Links_Self     = v._links.self.href,
                                Links_SourceVersionDisplayUri = v._links.sourceVersionDisplayUri.href,
                                Links_Timeline    = v._links.timeline.href,
                                Links_Web         = v._links.web.href,
                                LoadDate          = DateTime.Today,
                                LogUrl            = v.logs.url,
                                OrchestrationPlan = v.orchestrationPlan.planId,
                                //Plans = v.plans == null ? null : v.plans.ToString(),
                                PoolId     = v.queue.pool.id.ToString(),
                                Priority   = v.priority,
                                ProjectId  = v.project.id,
                                ProjectUrl = v.project.url,
                                //Properties = v.properties == null ? null : v.properties.ToString(),
                                QueueId                   = v.queue.id.ToString(),
                                QueueName                 = v.queue.name,
                                QueueTime                 = v.queueTime,
                                Reason                    = v.reason,
                                RepositoryId              = v.repository.id,
                                RepositoryUrl             = v.repository.url,
                                RequestedForAadDescriptor = v.requestedFor.descriptor,
                                RequestedForId            = v.requestedFor.id,
                                RequestedForIdentitieLink = v.requestedFor.url,
                                RequestedForName          = v.requestedFor.displayName,
                                RequestedForUniqueName    = v.requestedFor.uniqueName,
                                Result                    = v.result,
                                RetainedByRelease         = v.retainedByRelease,
                                SourceBranch              = v.sourceBranch,
                                SourceVersion             = v.sourceVersion,
                                StartTime                 = v.startTime,
                                Status                    = v.status,
                                //Tags = v.tags == null ? null : v.tags.ToString(),
                                TriggeredByBuild = v.triggeredByBuild == null ? null : v.triggeredByBuild.ToString(),
                                //TriggerInfo = v.triggerInfo == null ? null : v.triggerInfo.ToString(),
                                //ValidationResults = v.validationResults == null ? null : v.validationResults.ToString(),
                            }).ToList();

                            _fromApi.AddRange(_tmpApi);
                        }

                        // add
                        await ctx.Builds.AddRangeAsync(_fromApi);

                        // history
                        await ctx.LoadHistory.AddAsync(_utils.LoadHistoryHelper(Methods.Builds, _fromApi.Count, true, "", fromDate, toDate));

                        // save
                        await ctx.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                ret += _fromApi.Count;

                // controle
                if (toDate >= maxToDate)
                {
                    break;
                }

                fromDate = toDate;
                toDate   = fromDate.AddDays(dateIncrement);

                if (toDate > maxToDate)
                {
                    toDate = maxToDate;
                }
            }

            return(ret);
        }
Example #7
0
        public async Task <int> ListAllRepositoriesAsync()
        {
            // verificar ultima atualização bem sucedida
            // se foi hoje, não precisa executar
            if (_utils.AlreadyBeenExecuted(Methods.Repositories))
            {
                await _utils.SaveLoadHistory(Methods.Repositories, 0, true, "load has already been successfully executed");

                return(0);
            }

            int ret = 0;

            var _uri = _uriHelper.Get(Methods.Repositories, new UriParameters());

            HttpClientHelper request = new HttpClientHelper(_credentials, _uri);
            var resp = await request.GetAsync();

            string json = resp.Content;

            RepositoryReference.Root repos = JsonConvert.DeserializeObject <RepositoryReference.Root>(json);

            var _fromApi = repos.value.Select(r => new RepositoryModel()
            {
                DefaultBranch = r.defaultBranch,
                RepositoryId  = r.id,
                IsActive      = true,
                LoadDate      = DateTime.Now,
                Name          = r.name,
                ProjectId     = r.project.id,
                RemoteUrl     = r.remoteUrl,
                Size          = r.size,
                SshUrl        = r.sshUrl,
                Url           = r.url,
                WebUrl        = r.webUrl
            }).ToList();

            using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
            {
                var _inDb = await ctx.Repositories.Where(r => r.IsActive == true).AsNoTracking().ToListAsync();

                // get ID from Db
                foreach (var inDb in _inDb)
                {
                    if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.RepositoryId == inDb.RepositoryId).Any())
                    {
                        _fromApi
                        .Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.RepositoryId == inDb.RepositoryId)
                        .FirstOrDefault().Id = inDb.Id;
                    }
                }

                // to update
                var _toUpdate = _fromApi
                                .Intersect(_inDb, new RepositoryEqualityComparer())
                                .Select(j => new RepositoryModel
                {
                    Id            = j.Id,
                    Name          = j.Name,
                    Url           = j.Url,
                    LoadDate      = DateTime.Now,
                    IsActive      = j.IsActive,
                    DefaultBranch = j.DefaultBranch,
                    ProjectId     = j.ProjectId,
                    RemoteUrl     = j.RemoteUrl,
                    Size          = j.Size,
                    SshUrl        = j.SshUrl,
                    WebUrl        = j.WebUrl,
                    RepositoryId  = j.RepositoryId,
                });

                ret = _toUpdate.Count();
                ctx.Repositories.UpdateRange(_toUpdate);

                // to delete
                var _toDelete = _inDb.Where(x => !_fromApi.Any(y => y.RepositoryId == x.RepositoryId && y.ProjectId == x.ProjectId)).ToList();
                _toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                ret = ret + _toDelete.Count;
                ctx.Repositories.UpdateRange(_toDelete);

                // to add
                var _toAdd = _fromApi.Where(x => !_inDb.Any(y => y.RepositoryId == x.RepositoryId && y.ProjectId == x.ProjectId)).ToList();
                await ctx.Repositories.AddRangeAsync(_toAdd);

                ret = ret + _toAdd.Count;

                // save history
                ctx.LoadHistory.Add(_utils.LoadHistoryHelper(Methods.Repositories, ret, true, ""));

                // commit
                await ctx.SaveChangesAsync();
            }
            return(ret);
        }
Example #8
0
        public async Task <int> ListAllCommitsAsync()
        {
            int      ret           = 0;
            DateTime maxToDate     = DateTime.Today;
            DateTime minFromDate   = Convert.ToDateTime(Environment.GetEnvironmentVariable("MinFromDate").ToString());
            int      dateIncrement = Convert.ToInt32(Environment.GetEnvironmentVariable("DateIncrement").ToString());

            //searchCriteria.fromDate - If provided, only include history entries created after this date(string)
            DateTime fromDate = _utils.GetLastToDate(Methods.Commits);

            // se fromDate = minvalue -> executar carga inicial a partir de 01/01/2020 até data de hoje
            if (fromDate >= DateTime.Today)
            {
                await _utils.SaveLoadHistory(Methods.Commits, ret, true, "load has already been successfully executed");

                return(0);
            }

            //repos list
            DbDevOpsDashContext reposCtx = new DbDevOpsDashContext();
            var repos = await reposCtx.Repositories.Where(r => r.IsActive == true).AsNoTracking().ToListAsync();

            await reposCtx.DisposeAsync();

            //searchCriteria.fromDate - If provided, only include history entries created after this date(string)
            if (fromDate < minFromDate)
            {
                fromDate = minFromDate;
            }

            //searchCriteria.toDate   - If provided, only include history entries created before this date(string)
            DateTime toDate = fromDate.AddDays(dateIncrement);

            if (toDate > maxToDate)
            {
                toDate = maxToDate;
            }

            while (true)
            {
                List <CommitModel> _fromApi = new List <CommitModel>();

                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    foreach (var r in repos)
                    {
                        var commits = await GetAllCommitsByRepositoryId(r.RepositoryId, fromDate, toDate);

                        var _tmpApi = commits.value.Select(v => new CommitModel()
                        {
                            CommitId           = v.commitId,
                            RepositoryId       = r.RepositoryId,
                            AuthorDate         = v.author.date,
                            AuthorEmail        = v.author.email,
                            AuthorName         = v.author.name,
                            ChangeCountsAdd    = v.changeCounts.Add,
                            ChangeCountsDelete = v.changeCounts.Delete,
                            ChangeCountsEdit   = v.changeCounts.Edit,
                            Comment            = v.comment,
                            CommentTruncated   = v.commentTruncated,
                            CommitterDate      = v.committer.date,
                            CommitterEmail     = v.committer.email,
                            CommitterName      = v.committer.name,
                            RemoteUrl          = v.remoteUrl,
                            Url      = v.url,
                            LoadDate = toDate,
                            IsActive = true,
                        }).ToList();

                        _fromApi.AddRange(_tmpApi);
                    }

                    // add
                    await ctx.Commits.AddRangeAsync(_fromApi);

                    // history
                    await ctx.LoadHistory.AddAsync(_utils.LoadHistoryHelper(Methods.Commits, _fromApi.Count, true, "", fromDate, toDate));

                    // save
                    await ctx.SaveChangesAsync();
                }

                ret += _fromApi.Count;

                // controle
                if (toDate >= maxToDate)
                {
                    break;
                }

                fromDate = toDate;
                toDate   = fromDate.AddDays(dateIncrement);

                if (toDate > maxToDate)
                {
                    toDate = maxToDate;
                }
            }

            return(ret);
        }
Example #9
0
        public async Task <int> ListAllPipelinesAsync()
        {
            int    ret        = 0;
            string restoreKey = "TbProjects.Id";

            if (_utils.AlreadyBeenExecuted(Methods.Pipelines))
            {
                await _utils.SaveLoadHistory(Methods.Pipelines, ret, true, "load has already been successfully executed");

                return(0);
            }

            // all projs
            string aux;
            int    intPId = 0;

            var restorePoint = _utils.GetRestorePoint(Methods.Pipelines, DateTime.Today);

            if (restorePoint.Count > 0)
            {
                restorePoint.TryGetValue(restoreKey, out aux);
                intPId = Convert.ToInt32(aux);
            }

            DbDevOpsDashContext projsCtx = new DbDevOpsDashContext();
            var projects = await projsCtx.Projects.AsNoTracking().Where(p => p.Id > intPId && p.IsActive == true).OrderBy(p => p.Id).ToListAsync();

            await projsCtx.DisposeAsync();

            // all pipelines
            foreach (var proj in projects)
            {
                var pipelines = await GetAllPipelinesByProjectId(proj.ProjectId);

                if (pipelines.count <= 0)
                {
                    continue;
                }

                List <PipelineModel> _fromApi = new List <PipelineModel>();

                foreach (var p in pipelines.value)
                {
                    _fromApi.Add(new PipelineModel()
                    {
                        Folder     = p.folder,
                        PipelineId = p.id,
                        Revision   = p.revision,
                        SelfLink   = p._links.self.href,
                        WebLink    = p._links.web.href,
                        IsActive   = true,
                        LoadDate   = DateTime.Now,
                        Name       = p.name,
                        ProjectId  = proj.ProjectId,
                        Url        = p.url,
                    });
                }

                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    var _inDb = await ctx.Pipelines
                                .Where(p => p.ProjectId == proj.ProjectId && p.IsActive == true)
                                .AsNoTracking()
                                .ToListAsync();

                    // get ID from Db
                    foreach (var inDb in _inDb)
                    {
                        if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.PipelineId == inDb.PipelineId).Any())
                        {
                            _fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.PipelineId == inDb.PipelineId)
                            .FirstOrDefault().Id = inDb.Id;
                        }
                    }

                    // to update
                    var _toUpdate = _fromApi
                                    .Intersect(_inDb, new PipelineEqualityComparer())
                                    .Select(j => new PipelineModel
                    {
                        Id         = j.Id,
                        Name       = j.Name,
                        Url        = j.Url,
                        LoadDate   = DateTime.Now,
                        IsActive   = j.IsActive,
                        Folder     = j.Folder,
                        ProjectId  = j.ProjectId,
                        Revision   = j.Revision,
                        SelfLink   = j.SelfLink,
                        WebLink    = j.WebLink,
                        PipelineId = j.PipelineId
                    });

                    ret = ret + _toUpdate.Count();
                    ctx.Pipelines.UpdateRange(_toUpdate);

                    // to delete
                    var _toDelete = _inDb.Where(inDb => !_fromApi.Any(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.PipelineId == inDb.PipelineId)).ToList();
                    _toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                    ret = ret + _toDelete.Count;
                    ctx.Pipelines.UpdateRange(_toDelete);

                    // to add
                    var _toAdd = _fromApi.Where(fromApi => !_inDb.Any(inDb => fromApi.ProjectId == inDb.ProjectId && fromApi.PipelineId == inDb.PipelineId)).ToList();
                    await ctx.Pipelines.AddRangeAsync(_toAdd);

                    ret = ret + _toAdd.Count;

                    // restore point
                    var rpm = new LoadRestoreModel()
                    {
                        ExecutionDateTime = DateTime.Now,
                        LastId            = $"{restoreKey}={proj.Id}",
                        LoadDate          = DateTime.Today,
                        Method            = (int)Methods.Pipelines,
                        Id = (int)Methods.Pipelines,
                    };

                    ctx.LoadRestorePoint.Update(rpm);

                    // commit
                    await ctx.SaveChangesAsync();
                }
            }

            await _utils.SaveLoadHistory(Methods.Pipelines, ret, true, "");

            return(ret);
        }
Example #10
0
        public async Task <int> ListAllPipelineRunsAsync()
        {
            if (_utils.AlreadyBeenExecuted(Methods.PipelineRuns))
            {
                await _utils.SaveLoadHistory(Methods.PipelineRuns, 0, true, "load has already been successfully executed");

                return(0);
            }

            int    ret        = 0;
            string restoreKey = "TbPipelines.Id";

            // all
            string aux;
            int    intPId = 0;

            var restorePoint = _utils.GetRestorePoint(Methods.PipelineRuns, DateTime.Today);

            if (restorePoint.Count > 0)
            {
                restorePoint.TryGetValue(restoreKey, out aux);
                intPId = Convert.ToInt32(aux);
            }


            // all pipelines runs
            DbDevOpsDashContext pipesCtx = new DbDevOpsDashContext();
            var pipes = await pipesCtx.Pipelines
                        .AsNoTracking()
                        .Where(p => p.Id > intPId && p.IsActive == true).OrderBy(p => p.Id)
                        .ToArrayAsync();

            pipesCtx.Dispose();

            foreach (var pipe in pipes)
            {
                List <PipelineRunModel> _fromApi = new List <PipelineRunModel>();

                var pipelines = await GetAllPipelinesRunsByPipelineId(pipe.ProjectId, pipe.PipelineId);

                _fromApi = pipelines.value.Select(p => new PipelineRunModel()
                {
                    CreatedDate   = p.createdDate,
                    Name          = p.name,
                    FinishedDate  = p.finishedDate,
                    Result        = p.result,
                    SelfLink      = p._links.self.href,
                    State         = p.state,
                    Url           = p.url,
                    WebLink       = p._links.web.href,
                    PipelineId    = pipe.PipelineId,
                    PipelineRunId = p.id,
                    IsActive      = true,
                    LoadDate      = DateTime.Now,
                    ProjectId     = pipe.ProjectId,
                }).ToList();

                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    // verificar alterações/inclusões/exclusões
                    var _inDb = await ctx.PipelineRuns
                                .Where(inDb => inDb.PipelineId == pipe.PipelineId && inDb.IsActive == true)
                                .AsNoTracking()
                                .ToListAsync();

                    // get ID from Db
                    foreach (var inDb in _inDb)
                    {
                        if (_fromApi.Where(fromApi => fromApi.PipelineRunId == inDb.PipelineId).Any())
                        {
                            _fromApi.Where(fromApi => fromApi.PipelineRunId == inDb.PipelineRunId).FirstOrDefault().Id = inDb.Id;
                        }
                    }

                    // projs to update
                    var _toUpdate = _fromApi
                                    .Intersect(_inDb, new PipelineRunEqualityComparer())
                                    .Select(j => new PipelineRunModel
                    {
                        Id            = j.Id,
                        Name          = j.Name,
                        Url           = j.Url,
                        LoadDate      = DateTime.Now,
                        IsActive      = j.IsActive,
                        SelfLink      = j.SelfLink,
                        WebLink       = j.WebLink,
                        CreatedDate   = j.CreatedDate,
                        FinishedDate  = j.FinishedDate,
                        PipelineId    = j.PipelineId,
                        PipelineRunId = j.PipelineRunId,
                        Result        = j.Result,
                        State         = j.State,
                        ProjectId     = j.ProjectId,
                    });

                    ret = ret + _toUpdate.Count();
                    ctx.UpdateRange(_toUpdate);

                    // to delete
                    var _toDelete = _inDb
                                    .Where(inDb => !_fromApi.Any(fromApi => fromApi.PipelineId == inDb.PipelineId && fromApi.PipelineRunId == inDb.PipelineRunId)).ToList();
                    _toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                    ret = ret + _toDelete.Count;
                    ctx.UpdateRange(_toDelete);

                    // projs to add
                    var _toAdd = _fromApi
                                 .Where(fromApi => !_inDb.Any(inDb => inDb.PipelineId == fromApi.PipelineId && inDb.PipelineRunId == fromApi.PipelineRunId)).ToList();
                    await ctx.AddRangeAsync(_toAdd);

                    ret = ret + _toAdd.Count;

                    // restore point
                    var rpm = new LoadRestoreModel()
                    {
                        ExecutionDateTime = DateTime.Now,
                        LastId            = $"{restoreKey}={pipe.Id}",
                        LoadDate          = DateTime.Today,
                        Method            = (int)Methods.PipelineRuns,
                        Id = (int)Methods.PipelineRuns,
                    };

                    ctx.LoadRestorePoint.Update(rpm);

                    await ctx.SaveChangesAsync();
                }
            }

            await _utils.SaveLoadHistory(Methods.PipelineRuns, ret, true, "");

            return(ret);
        }
Example #11
0
        public async Task <int> ListAllAsync()
        {
            // verificar ultima atualização bem sucedida -> se foi hoje, não precisa executar
            if (_utils.AlreadyBeenExecuted(Methods.Projects))
            {
                await _utils.SaveLoadHistory(Methods.Projects, 0, true, "load has already been successfully executed");

                return(0);
            }

            int ret  = 0;
            var _uri = _uriHelper.Get(Methods.Projects, new UriParameters());

            HttpClientHelper request = new HttpClientHelper(_credentials, _uri);
            var resp = await request.GetAsync();

            string json = resp.Content;

            ProjectReference.Root projects = JsonConvert.DeserializeObject <ProjectReference.Root>(json);

            var _fromApi = projects.value.Select(p => new ProjectModel()
            {
                Description    = p.description,
                ProjectId      = p.id,
                LastUpdateTime = p.lastUpdateTime,
                Name           = p.name,
                Revision       = p.revision,
                State          = p.state,
                Url            = p.url,
                Visibility     = p.visibility,
                IsActive       = true,
                LoadDate       = DateTime.Now
            }).ToList();

            using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
            {
                var _inDb = await ctx.Projects.Where(p => p.IsActive == true).AsNoTracking().ToListAsync();

                // get ID from Db
                foreach (var inDb in _inDb)
                {
                    if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId).Any())
                    {
                        _fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId).FirstOrDefault().Id = inDb.Id;
                    }
                }

                // projs to update
                var _toUpdate = _fromApi
                                .Intersect(_inDb, new ProjectEqualityComparer())
                                .Select(j => new ProjectModel
                {
                    Id             = j.Id,
                    Description    = j.Description,
                    LastUpdateTime = j.LastUpdateTime,
                    Name           = j.Name,
                    Revision       = j.Revision,
                    State          = j.State,
                    Url            = j.Url,
                    Visibility     = j.Visibility,
                    LoadDate       = DateTime.Now,
                    IsActive       = j.IsActive,
                    ProjectId      = j.ProjectId,
                });

                ret = _toUpdate.Count();
                ctx.Projects.UpdateRange(_toUpdate);

                // projs to delete
                var _toDelete = _inDb.Where(x => !_fromApi.Any(y => y.ProjectId == x.ProjectId)).ToList();
                _toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                ret = ret + _toDelete.Count;
                ctx.Projects.UpdateRange(_toDelete);

                // projs to add
                var _toAdd = _fromApi.Where(x => !_inDb.Any(y => y.ProjectId == x.ProjectId)).ToList();
                await ctx.Projects.AddRangeAsync(_toAdd);

                ret = ret + _toAdd.Count;

                // save history
                ctx.LoadHistory.Add(_utils.LoadHistoryHelper(Methods.Projects, ret, true, ""));

                // commit
                await ctx.SaveChangesAsync();
            }

            return(ret);
        }
Example #12
0
        private async Task <List <ReleaseApprovalModel> > GetAllApprovalsByProjectIdAndGroupId(string projectId, string groupId)
        {
            // get continuation token
            string continuationToken = "0";

            using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
            {
                if (ctx.ReleaseApprovals.Where(r => r.ProjectId == projectId && r.ApproverId == groupId && r.IsActive == true).Any())
                {
                    continuationToken = ctx.ReleaseApprovals
                                        .Where(r => r.ProjectId == projectId && r.ApproverId == groupId && r.IsActive == true)
                                        .Max(r => r.ApprovalId + 1).ToString();
                }
            }

            List <ReleaseApprovalModel> _fromApi = new List <ReleaseApprovalModel>();

            while (true)
            {
                var _uri = _uriHelper.Get(Methods.ReleaseApprovals,
                                          new UriParameters()
                {
                    project           = projectId,
                    continuationToken = continuationToken,
                    queryOrder        = "ascending",
                    assignedToFilter  = groupId,
                    statusFilter      = "undefined",
                });

                HttpClientHelper request = new HttpClientHelper(_credentials, _uri);

                var resp = await request.GetAsync();

                continuationToken = resp.ContinuationToken;
                string json = resp.Content;

                ReleaseApprovalReference.Root approvals = JsonConvert.DeserializeObject <ReleaseApprovalReference.Root>(json);

                var _approvals = approvals.value.Select(p => new ReleaseApprovalModel()
                {
                    ApprovalId         = p.id,
                    ApprovalType       = p.approvalType,
                    Approver           = p.approver.displayName,
                    ApproverDescriptor = p.approver.descriptor,
                    ApproverId         = p.approver.id,
                    ApproverImageUrl   = p.approver.imageUrl,
                    ApproverUniqueName = p.approver.uniqueName,
                    ApproverUrl        = p.approver.url,
                    Attempt            = p.attempt,
                    Comments           = p.comments,
                    CreatedOn          = p.createdOn,
                    DefinitionId       = p.releaseDefinition.id,
                    DefinitionName     = p.releaseDefinition.name,
                    DefinitionPath     = p.releaseDefinition.path,
                    DefinitionUrl      = p.releaseDefinition.url,
                    EnvironmentId      = p.releaseEnvironment.id,
                    EnvironmentName    = p.releaseEnvironment.name,
                    EnvironmentUrl     = p.releaseEnvironment.url,
                    IsAutomated        = p.isAutomated,
                    IsNotificationOn   = p.isNotificationOn,
                    Rank        = p.rank,
                    ReleaseId   = p.release.id,
                    ReleaseName = p.release.name,
                    ReleaseUrl  = p.release.url,
                    Revision    = p.revision,
                    Status      = p.status,
                    TrialNumber = p.trialNumber,
                    Url         = p.url,

                    ProjectId  = projectId,
                    Id         = 0,
                    IsActive   = true,
                    LoadDate   = DateTime.Now,
                    ModifiedOn = p.modifiedOn,

                    ApprovedBy           = p.approvedBy == null ? null : p.approvedBy.displayName,
                    ApprovedByDescriptor = p.approvedBy == null ? null : p.approvedBy.descriptor,
                    ApprovedById         = p.approvedBy == null ? null : p.approvedBy.id,
                    ApprovedByImageUrl   = p.approvedBy == null ? null : p.approvedBy.imageUrl,
                    ApprovedByUniqueName = p.approvedBy == null ? null : p.approvedBy.uniqueName,
                    ApprovedByUrl        = p.approvedBy == null ? null : p.approvedBy.url,
                }).ToList();

                _fromApi.AddRange(_approvals);

                if (string.IsNullOrEmpty(continuationToken))
                {
                    break;
                }
            }

            return(_fromApi);
        }
Example #13
0
        public async Task <int> CheckPendingApprovalsAsync()
        {
            int ret = 0;

            if (_utils.AlreadyBeenExecuted(Methods.ReleasePendingApprovals))
            {
                await _utils.SaveLoadHistory(Methods.ReleasePendingApprovals, ret, true, "load has already been successfully executed");

                return(0);
            }

            // list
            DbDevOpsDashContext approvalsCtx = new DbDevOpsDashContext();
            var _pendingsInDb = await approvalsCtx.ReleaseApprovals
                                .Where(p => p.IsActive == true && p.Status.Contains("pending"))
                                .OrderBy(p => p.Id)
                                .AsNoTracking().ToListAsync();

            await approvalsCtx.DisposeAsync();

            List <ReleaseApprovalModel> _approvalsFromApi = new List <ReleaseApprovalModel>();

            using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
            {
                try
                {
                    foreach (var a in _pendingsInDb)
                    {
                        var _uri = _uriHelper.Get(Methods.ReleasePendingApprovals,
                                                  new UriParameters()
                        {
                            project          = a.ProjectId,
                            assignedToFilter = a.ApproverId,
                            statusFilter     = "undefined",
                            releaseIdsFilter = a.ReleaseId.ToString(),
                        });

                        HttpClientHelper request = new HttpClientHelper(_credentials, _uri);

                        var resp = await request.GetAsync();

                        string json = resp.Content;

                        ReleaseApprovalReference.Root _fromApi = JsonConvert.DeserializeObject <ReleaseApprovalReference.Root>(json);

                        var _approvals = _fromApi.value.Select(p => new ReleaseApprovalModel()
                        {
                            ApprovalId         = p.id,
                            ApprovalType       = p.approvalType,
                            Approver           = p.approver.displayName,
                            ApproverDescriptor = p.approver.descriptor,
                            ApproverId         = p.approver.id,
                            ApproverImageUrl   = p.approver.imageUrl,
                            ApproverUniqueName = p.approver.uniqueName,
                            ApproverUrl        = p.approver.url,
                            Attempt            = p.attempt,
                            Comments           = p.comments,
                            CreatedOn          = p.createdOn,
                            DefinitionId       = p.releaseDefinition.id,
                            DefinitionName     = p.releaseDefinition.name,
                            DefinitionPath     = p.releaseDefinition.path,
                            DefinitionUrl      = p.releaseDefinition.url,
                            EnvironmentId      = p.releaseEnvironment.id,
                            EnvironmentName    = p.releaseEnvironment.name,
                            EnvironmentUrl     = p.releaseEnvironment.url,
                            IsAutomated        = p.isAutomated,
                            IsNotificationOn   = p.isNotificationOn,
                            Rank        = p.rank,
                            ReleaseId   = p.release.id,
                            ReleaseName = p.release.name,
                            ReleaseUrl  = p.release.url,
                            Revision    = p.revision,
                            Status      = p.status,
                            TrialNumber = p.trialNumber,
                            Url         = p.url,

                            ProjectId  = a.ProjectId,
                            Id         = 0,
                            IsActive   = true,
                            LoadDate   = DateTime.Now,
                            ModifiedOn = p.modifiedOn,

                            ApprovedBy           = p.approvedBy == null ? null : p.approvedBy.displayName,
                            ApprovedByDescriptor = p.approvedBy == null ? null : p.approvedBy.descriptor,
                            ApprovedById         = p.approvedBy == null ? null : p.approvedBy.id,
                            ApprovedByImageUrl   = p.approvedBy == null ? null : p.approvedBy.imageUrl,
                            ApprovedByUniqueName = p.approvedBy == null ? null : p.approvedBy.uniqueName,
                            ApprovedByUrl        = p.approvedBy == null ? null : p.approvedBy.url,
                        });

                        _approvalsFromApi.AddRange(_approvals);
                    }

                    // get ID from Db
                    foreach (var inDb in _pendingsInDb)
                    {
                        if (_approvalsFromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId &&
                                                    fromApi.ReleaseId == inDb.ReleaseId &&
                                                    fromApi.ApprovalId == inDb.ApprovalId).Any())
                        {
                            _approvalsFromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId &&
                                                    fromApi.ReleaseId == inDb.ReleaseId &&
                                                    fromApi.ApprovalId == inDb.ApprovalId).FirstOrDefault().Id = inDb.Id;
                        }
                    }

                    // to update
                    var _toUpdate = _approvalsFromApi
                                    .Intersect(_pendingsInDb, new ReleaseApprovalEqualityComparer())
                                    .Select(j => new ReleaseApprovalModel
                    {
                        ReleaseId            = j.ReleaseId,
                        CreatedOn            = j.CreatedOn,
                        DefinitionId         = j.DefinitionId,
                        DefinitionName       = j.DefinitionName,
                        DefinitionPath       = j.DefinitionPath,
                        DefinitionUrl        = j.DefinitionUrl,
                        Id                   = j.Id,
                        IsActive             = j.IsActive,
                        LoadDate             = DateTime.Now,
                        ModifiedOn           = j.ModifiedOn,
                        ProjectId            = j.ProjectId,
                        ApprovalId           = j.ApprovalId,
                        Status               = j.Status,
                        Url                  = j.Url,
                        ApprovedByDescriptor = j.ApprovedByDescriptor,
                        ApprovalType         = j.ApprovalType,
                        ApprovedBy           = j.ApprovedBy,
                        ApprovedById         = j.ApprovedById,
                        ApprovedByImageUrl   = j.ApprovedByImageUrl,
                        ApprovedByUniqueName = j.ApprovedByUniqueName,
                        ApprovedByUrl        = j.ApprovedByUrl,
                        Approver             = j.Approver,
                        ApproverDescriptor   = j.ApproverDescriptor,
                        ApproverId           = j.ApproverId,
                        ApproverImageUrl     = j.ApproverImageUrl,
                        ApproverUniqueName   = j.ApproverUniqueName,
                        ApproverUrl          = j.ApproverUrl,
                        Attempt              = j.Attempt,
                        Comments             = j.Comments,
                        EnvironmentId        = j.EnvironmentId,
                        EnvironmentName      = j.EnvironmentName,
                        EnvironmentUrl       = j.EnvironmentUrl,
                        IsAutomated          = j.IsAutomated,
                        IsNotificationOn     = j.IsNotificationOn,
                        Rank                 = j.Rank,
                        ReleaseName          = j.ReleaseName,
                        ReleaseUrl           = j.ReleaseUrl,
                        Revision             = j.Revision,
                        TrialNumber          = j.TrialNumber,
                    });

                    ret = _toUpdate.Count();
                    ctx.ReleaseApprovals.UpdateRange(_toUpdate);

                    // history
                    await ctx.LoadHistory.AddAsync(_utils.LoadHistoryHelper(Methods.ReleasePendingApprovals, ret, true, "", null, null));

                    await ctx.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(ret);
        }
Example #14
0
        public async Task <int> ListAllApprovalsAsync()
        {
            int ret = 0;

            if (_utils.AlreadyBeenExecuted(Methods.ReleaseApprovals))
            {
                await _utils.SaveLoadHistory(Methods.ReleaseApprovals, ret, true, "load has already been successfully executed");

                return(0);
            }

            // list
            DbDevOpsDashContext groupsCtx = new DbDevOpsDashContext();
            var groups = await groupsCtx.Groups
                         .Where(p => p.IsActive == true && p.OurGroupCode == (int)Groups.ApplicationOwners)
                         .OrderBy(p => p.Id)
                         .AsNoTracking().ToListAsync();

            await groupsCtx.DisposeAsync();

            List <ReleaseApprovalModel> _fromApi = new List <ReleaseApprovalModel>();

            using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
            {
                try
                {
                    foreach (var g in groups)
                    {
                        var _tmpApi = await GetAllApprovalsByProjectIdAndGroupId(g.ProjectId, g.OriginId);

                        _fromApi.AddRange(_tmpApi);
                    }


                    #region comments
                    //var _inDb = await ctx.ReleaseApprovals.Where(p => p.IsActive == true).AsNoTracking().ToListAsync();

                    //// get ID from Db
                    //foreach (var inDb in _inDb)
                    //{
                    //    if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId &&
                    //                                  fromApi.ReleaseId == inDb.ReleaseId &&
                    //                                  fromApi.ApprovalId == inDb.ApprovalId).Any())
                    //        _fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId &&
                    //                                  fromApi.ReleaseId == inDb.ReleaseId &&
                    //                                  fromApi.ApprovalId == inDb.ApprovalId).FirstOrDefault().Id = inDb.Id;
                    //}

                    //// to update
                    //var _toUpdate = _fromApi
                    //    .Intersect(_inDb, new ReleaseApprovalEqualityComparer())
                    //    .Select(j => new ReleaseApprovalModel
                    //    {
                    //        ReleaseId = j.ReleaseId,
                    //        CreatedOn = j.CreatedOn,
                    //        DefinitionId = j.DefinitionId,
                    //        DefinitionName = j.DefinitionName,
                    //        DefinitionPath = j.DefinitionPath,
                    //        DefinitionUrl = j.DefinitionUrl,
                    //        Id = j.Id,
                    //        IsActive = j.IsActive,
                    //        LoadDate = DateTime.Now,
                    //        ModifiedOn = j.ModifiedOn,
                    //        ProjectId = j.ProjectId,
                    //        ApprovalId = j.ApprovalId,
                    //        Status = j.Status,
                    //        Url = j.Url,
                    //        ApprovedByDescriptor = j.ApprovedByDescriptor,
                    //        ApprovalType = j.ApprovalType,
                    //        ApprovedBy = j.ApprovedBy,
                    //        ApprovedById  = j.ApprovedById,
                    //        ApprovedByImageUrl = j.ApprovedByImageUrl,
                    //        ApprovedByUniqueName = j.ApprovedByUniqueName,
                    //        ApprovedByUrl = j.ApprovedByUrl,
                    //        Approver = j.Approver,
                    //        ApproverDescriptor = j.ApproverDescriptor,
                    //        ApproverId = j.ApproverId,
                    //        ApproverImageUrl = j.ApproverImageUrl,
                    //        ApproverUniqueName = j.ApproverUniqueName,
                    //        ApproverUrl = j.ApproverUrl,
                    //        Attempt = j.Attempt,
                    //        Comments = j.Comments,
                    //        EnvironmentId = j.EnvironmentId,
                    //        EnvironmentName = j.EnvironmentName,
                    //        EnvironmentUrl = j.EnvironmentUrl,
                    //        IsAutomated = j.IsAutomated,
                    //        IsNotificationOn = j.IsNotificationOn,
                    //        Rank = j.Rank,
                    //        ReleaseName = j.ReleaseName,
                    //        ReleaseUrl = j.ReleaseUrl,
                    //        Revision = j.Revision,
                    //        TrialNumber = j.TrialNumber,
                    //    });

                    //ret = _toUpdate.Count();
                    //ctx.ReleaseApprovals.UpdateRange(_toUpdate);

                    //  to delete
                    //var _toDelete = _inDb.Where(x => !_fromApi.Any(y => y.ProjectId == x.ProjectId && y.ReleaseId == x.ReleaseId)).ToList();
                    //_toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                    //ret = ret + _toDelete.Count;
                    //ctx.Releases.UpdateRange(_toDelete);

                    //  to add
                    //var _toAdd = _fromApi.Where(x => !_inDb.Any(y => y.ProjectId == x.ProjectId && y.ReleaseId == x.ReleaseId)).ToList();
                    //var _toAdd = _fromApi.Where(x => x.Id == 0).ToList();
                    #endregion


                    await ctx.ReleaseApprovals.AddRangeAsync(_fromApi);

                    ret = ret + _fromApi.Count;

                    // history
                    await ctx.LoadHistory.AddAsync(_utils.LoadHistoryHelper(Methods.ReleaseApprovals, ret, true, "", null, null));

                    await ctx.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }



            return(ret);
        }
Example #15
0
        public async Task <int> ListAllAsync()
        {
            int      ret           = 0;
            DateTime maxToDate     = DateTime.Today;
            DateTime minFromDate   = Convert.ToDateTime(Environment.GetEnvironmentVariable("MinFromDate").ToString());
            int      dateIncrement = Convert.ToInt32(Environment.GetEnvironmentVariable("DateIncrement").ToString());
            int      daysBack      = Convert.ToInt32(Environment.GetEnvironmentVariable("BackDaysForInspect").ToString()) * -1;

            //searchCriteria.fromDate - If provided, only include history entries created after this date(string)
            DateTime fromDate = _utils.GetLastToDate(Methods.Releases);

            // se fromDate = minvalue -> executar carga inicial a partir de 01/01/2020 até data de hoje
            if (fromDate >= DateTime.Today)
            {
                await _utils.SaveLoadHistory(Methods.Releases, ret, true, "load has already been successfully executed");

                return(0);
            }

            // list
            DbDevOpsDashContext projsCtx = new DbDevOpsDashContext();
            var projs = await projsCtx.Projects
                        .Where(p => p.IsActive == true)
                        .OrderBy(p => p.Id)
                        .AsNoTracking().ToListAsync();

            await projsCtx.DisposeAsync();

            //searchCriteria.fromDate - If provided, only include history entries created after this date(string)
            if (fromDate < minFromDate)
            {
                fromDate = minFromDate;
            }

            //searchCriteria.toDate   - If provided, only include history entries created before this date(string)
            DateTime toDate = fromDate.AddDays(dateIncrement);

            if (toDate > maxToDate)
            {
                toDate = maxToDate;
            }

            while (true)
            {
                List <ReleaseModel> _fromApi = new List <ReleaseModel>();

                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    try
                    {
                        foreach (var p in projs)
                        {
                            // days back
                            DateTime fromDateBack = fromDate.AddDays(daysBack);
                            var      _tmpApi      = await GetAllReleasesByProjectId(p.ProjectId, fromDateBack, toDate);

                            _fromApi.AddRange(_tmpApi);
                        }

                        var _inDb = await ctx.Releases.Where(p => p.IsActive == true).AsNoTracking().ToListAsync();

                        // get ID from Db
                        foreach (var inDb in _inDb)
                        {
                            if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.ReleaseId == inDb.ReleaseId).Any())
                            {
                                _fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.ReleaseId == inDb.ReleaseId).FirstOrDefault().Id = inDb.Id;
                            }
                        }

                        // to update
                        var _toUpdate = _fromApi
                                        .Intersect(_inDb, new ReleaseEqualityComparer())
                                        .Select(j => new ReleaseModel
                        {
                            CreatedBy                  = j.CreatedBy,
                            CreatedByDescriptor        = j.CreatedByDescriptor,
                            CreatedById                = j.CreatedById,
                            CreatedByImageUrl          = j.CreatedByImageUrl,
                            CreatedByUniqueName        = j.CreatedByUniqueName,
                            CreatedByUrl               = j.CreatedByUrl,
                            CreatedFor                 = j.CreatedFor,
                            ReleaseId                  = j.ReleaseId,
                            CreatedForDescriptor       = j.CreatedForDescriptor,
                            CreatedForId               = j.CreatedForId,
                            CreatedForImageUrl         = j.CreatedForImageUrl,
                            CreatedForUniqueName       = j.CreatedForUniqueName,
                            CreatedForUrl              = j.CreatedForUrl,
                            CreatedOn                  = j.CreatedOn,
                            DefinitionId               = j.DefinitionId,
                            DefinitionName             = j.DefinitionName,
                            DefinitionPath             = j.DefinitionPath,
                            DefinitionSelfLink         = j.DefinitionSelfLink,
                            DefinitionSnapshotRevision = j.DefinitionSnapshotRevision,
                            DefinitionUrl              = j.DefinitionUrl,
                            DefinitionWebLink          = j.DefinitionWebLink,
                            Description                = j.Description,
                            Id                        = j.Id,
                            IsActive                  = j.IsActive,
                            KeepForever               = j.KeepForever,
                            LoadDate                  = DateTime.Now,
                            LogsContainerUrl          = j.LogsContainerUrl,
                            ModifiedBy                = j.ModifiedBy,
                            ModifiedByDescriptor      = j.ModifiedByDescriptor,
                            ModifiedById              = j.ModifiedById,
                            ModifiedByImageUrl        = j.ModifiedByImageUrl,
                            ModifiedByUniqueName      = j.ModifiedByUniqueName,
                            ModifiedByUrl             = j.ModifiedByUrl,
                            ModifiedOn                = j.ModifiedOn,
                            Name                      = j.Name,
                            ProjectId                 = j.ProjectId,
                            ProjectName               = j.ProjectName,
                            Reason                    = j.Reason,
                            ReleaseDefinitionRevision = j.ReleaseDefinitionRevision,
                            ReleaseNameFormat         = j.ReleaseNameFormat,
                            SelfLink                  = j.SelfLink,
                            Status                    = j.Status,
                            Tags                      = j.Tags,
                            TriggeringArtifactAlias   = j.TriggeringArtifactAlias,
                            Url                       = j.Url,
                            VariableGroups            = j.VariableGroups,
                            Variables                 = j.Variables,
                            WebLink                   = j.WebLink,
                        });

                        ret = _toUpdate.Count();
                        ctx.Releases.UpdateRange(_toUpdate);

                        //  to delete
                        //var _toDelete = _inDb.Where(x => !_fromApi.Any(y => y.ProjectId == x.ProjectId && y.ReleaseId == x.ReleaseId)).ToList();
                        //_toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                        //ret = ret + _toDelete.Count;
                        //ctx.Releases.UpdateRange(_toDelete);

                        //  to add
                        //var _toAdd = _fromApi.Where(x => !_inDb.Any(y => y.ProjectId == x.ProjectId && y.ReleaseId == x.ReleaseId)).ToList();
                        var _toAdd = _fromApi.Where(x => x.Id == 0).ToList();
                        await ctx.Releases.AddRangeAsync(_toAdd);

                        ret = ret + _toAdd.Count;

                        // history
                        await ctx.LoadHistory.AddAsync(_utils.LoadHistoryHelper(Methods.Releases, ret, true, "", fromDate, toDate));

                        await ctx.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                ret += _fromApi.Count;

                // controle
                if (toDate >= maxToDate)
                {
                    break;
                }

                fromDate = toDate;
                toDate   = fromDate.AddDays(dateIncrement);

                if (toDate > maxToDate)
                {
                    toDate = maxToDate;
                }
            }

            return(ret);
        }
Example #16
0
        public async Task <int> ListAllDefinitionsAsync()
        {
            int    ret        = 0;
            string restoreKey = "TbReleaseDefinitions.Id";

            if (_utils.AlreadyBeenExecuted(Methods.ReleaseDefinitions))
            {
                await _utils.SaveLoadHistory(Methods.ReleaseDefinitions, ret, true, "load has already been successfully executed");

                return(0);
            }

            // all projs
            string aux;
            int    intPId = 0;

            var restorePoint = _utils.GetRestorePoint(Methods.ReleaseDefinitions, DateTime.Today);

            if (restorePoint.Count > 0)
            {
                restorePoint.TryGetValue(restoreKey, out aux);
                intPId = Convert.ToInt32(aux);
            }

            DbDevOpsDashContext projsCtx = new DbDevOpsDashContext();
            var projects = await projsCtx.Projects.AsNoTracking().Where(p => p.Id > intPId && p.IsActive == true).OrderBy(p => p.Id).ToListAsync();

            await projsCtx.DisposeAsync();

            // all pipelines
            foreach (var proj in projects)
            {
                var releases = await GetAllReleaseDefinitionsByProjectId(proj.ProjectId);

                if (releases.count <= 0)
                {
                    continue;
                }

                List <ReleaseDefinitionModel> _fromApi = new List <ReleaseDefinitionModel>();

                foreach (var r in releases.value)
                {
                    _fromApi.Add(new ReleaseDefinitionModel()
                    {
                        CreatedBy            = r.createdBy.displayName,
                        CreatedByDescriptor  = r.createdBy.descriptor,
                        CreatedById          = r.createdBy.id,
                        CreatedByImageUrl    = r.createdBy.imageUrl,
                        CreatedByUniqueName  = r.createdBy.uniqueName,
                        CreatedByUrl         = r.createdBy.url,
                        CreatedOn            = r.createdOn,
                        DefinitionId         = r.id,
                        Description          = r.description,
                        IsActive             = true,
                        IsDeleted            = r.isDeleted,
                        LoadDate             = DateTime.Now,
                        ModifiedBy           = r.modifiedBy.displayName,
                        ModifiedByDescriptor = r.modifiedBy.descriptor,
                        ModifiedById         = r.modifiedBy.id,
                        ModifiedByImageUrl   = r.modifiedBy.imageUrl,
                        ModifiedByUniqueName = r.modifiedBy.uniqueName,
                        ModifiedByUrl        = r.modifiedBy.url,
                        ModifiedOn           = r.modifiedOn,
                        Name              = r.name,
                        Path              = r.path,
                        ProjectId         = proj.ProjectId,
                        ReleaseNameFormat = r.releaseNameFormat,
                        Revision          = r.revision,
                        SelfLink          = r._links.self.href,
                        Source            = r.source,
                        Url     = r.url,
                        WebLink = r._links.web.href,
                        Id      = 0,
                    });
                }

                using (DbDevOpsDashContext ctx = new DbDevOpsDashContext())
                {
                    var _inDb = await ctx.ReleaseDefinitions
                                .Where(p => p.ProjectId == proj.ProjectId && p.IsActive == true)
                                .AsNoTracking()
                                .ToListAsync();

                    // get ID from Db
                    foreach (var inDb in _inDb)
                    {
                        if (_fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.DefinitionId == inDb.DefinitionId).Any())
                        {
                            _fromApi.Where(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.DefinitionId == inDb.DefinitionId)
                            .FirstOrDefault().Id = inDb.Id;
                        }
                    }

                    // to update
                    var _toUpdate = _fromApi
                                    .Intersect(_inDb, new ReleaseDefinitionEqualityComparer())
                                    .Select(r => new ReleaseDefinitionModel
                    {
                        CreatedBy            = r.CreatedBy,
                        CreatedByDescriptor  = r.CreatedByDescriptor,
                        CreatedById          = r.CreatedById,
                        CreatedByImageUrl    = r.CreatedByImageUrl,
                        CreatedByUniqueName  = r.CreatedByUniqueName,
                        CreatedByUrl         = r.CreatedByUrl,
                        CreatedOn            = r.CreatedOn,
                        DefinitionId         = r.DefinitionId,
                        Description          = r.Description,
                        IsActive             = r.IsActive,
                        IsDeleted            = r.IsDeleted,
                        LoadDate             = DateTime.Now,
                        ModifiedBy           = r.ModifiedBy,
                        ModifiedByDescriptor = r.ModifiedByDescriptor,
                        ModifiedById         = r.ModifiedById,
                        ModifiedByImageUrl   = r.ModifiedByImageUrl,
                        ModifiedByUniqueName = r.ModifiedByUniqueName,
                        ModifiedByUrl        = r.ModifiedByUrl,
                        ModifiedOn           = r.ModifiedOn,
                        Name              = r.Name,
                        Path              = r.Path,
                        ProjectId         = proj.ProjectId,
                        ReleaseNameFormat = r.ReleaseNameFormat,
                        Revision          = r.Revision,
                        SelfLink          = r.SelfLink,
                        Source            = r.Source,
                        Url     = r.Url,
                        WebLink = r.WebLink,
                        Id      = r.Id,
                    });

                    ret = ret + _toUpdate.Count();
                    ctx.ReleaseDefinitions.UpdateRange(_toUpdate);

                    // to delete
                    var _toDelete = _inDb.Where(inDb => !_fromApi.Any(fromApi => fromApi.ProjectId == inDb.ProjectId && fromApi.DefinitionId == inDb.DefinitionId)).ToList();
                    _toDelete.ToList().ForEach(x => { x.IsActive = false; x.LoadDate = DateTime.Now; });
                    ret = ret + _toDelete.Count;
                    ctx.ReleaseDefinitions.UpdateRange(_toDelete);

                    // to add
                    var _toAdd = _fromApi.Where(fromApi => !_inDb.Any(inDb => fromApi.ProjectId == inDb.ProjectId && fromApi.DefinitionId == inDb.DefinitionId)).ToList();
                    await ctx.ReleaseDefinitions.AddRangeAsync(_toAdd);

                    ret = ret + _toAdd.Count;

                    // restore point
                    var rpm = new LoadRestoreModel()
                    {
                        ExecutionDateTime = DateTime.Now,
                        LastId            = $"{restoreKey}={proj.Id}",
                        LoadDate          = DateTime.Today,
                        Method            = (int)Methods.ReleaseDefinitions,
                        Id = (int)Methods.ReleaseDefinitions,
                    };

                    ctx.LoadRestorePoint.Update(rpm);

                    // commit
                    await ctx.SaveChangesAsync();
                }
            }

            await _utils.SaveLoadHistory(Methods.ReleaseDefinitions, ret, true, "");

            return(ret);
        }