Example #1
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 #2
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 #3
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 #4
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);
        }