Exemple #1
0
        public async Task RefreshVersionControlByIdAsync(Guid VcsId)
        {
            Vcs versionControl = await context.VcsSources.FindAsync(VcsId);

            await context.Entry(versionControl).Collection(vcs => vcs.Repositories).LoadAsync();

            context.VcsRepositories.RemoveRange(versionControl.Repositories.Where(r => r.IsIgnored == false));
            await context.SaveChangesAsync();

            var versionControlDto = new VersionControlDto {
                ApiKey = versionControl.ApiKey, Endpoint = versionControl.Endpoint, Kind = versionControl.Kind, VcsId = versionControl.Id
            };

            encryptionService.Decrypt(versionControlDto);

            IEnumerable <VcsRepository> items = await this.versionControlProvider.GetRepositoriesAsync(versionControlDto);

            foreach (VcsRepository repository in items)
            {
                context.VcsRepositories.Add(repository);
                await context.SaveChangesAsync();
            }

            foreach (var repository in context.VcsRepositories.Where(r => r.VcsId == VcsId))
            {
                await RefreshRepositoryByIdAsync(repository.Id);
            }
        }
Exemple #2
0
        public async Task RefreshRepositoryByIdAsync(Guid repositoryId)
        {
            VcsRepository repository = await context.VcsRepositories.FindAsync(repositoryId);

            Vcs versionControl = await context.VcsSources.FindAsync(repository.VcsId);

            logger.LogInformation($"Refreshing repository: {repository.Url}");
            await context.Entry(repository).Collection(r => r.Assets).LoadAsync();

            context.Assets.RemoveRange(repository.Assets);
            await context.SaveChangesAsync();

            var versionControlDto = new VersionControlDto {
                ApiKey = versionControl.ApiKey, Endpoint = versionControl.Endpoint, VcsId = versionControl.Id, Kind = versionControl.Kind, IsEnabled = versionControl.IsEnabled
            };
            var repositoryDto = new RepositoryDto {
                VcsId = repository.VcsId, Url = repository.Url, WebUrl = repository.WebUrl
            };

            encryptionService.Decrypt(versionControlDto);
            IEnumerable <Asset> items = await versionControlProvider.GetAssetsAsync(versionControlDto, repositoryDto);

            foreach (Asset asset in items)
            {
                context.Assets.Add(asset);
                await context.SaveChangesAsync();
                await RefreshAssetByIdAsync(asset.Id);
            }
        }
Exemple #3
0
        public async Task <IPaginatedList <AssetDto> > GetByVcsIdAsync(Guid VcsId, string search, IEnumerable <EcosystemKind> kinds, int pageIndex = 1, int pageSize = 10)
        {
            Vcs versionControl = context.VcsSources.Find(VcsId);

            var query  = context.Assets.Where(asset => context.VcsRepositories.Any(repository => repository.VcsId == VcsId && asset.RepositoryId == repository.Id));
            var filter = kinds.ToIntArray();

            if (filter.Any())
            {
                query = query.Where(asset => filter.Contains((int)asset.Kind));
            }

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(asset => asset.Path.Contains(search));
            }

            var paging = query.Select(asset => new AssetDto
            {
                Asset        = asset.Path,
                AssetId      = asset.Id,
                Dependencies = context.AssetDependencies.Count(ad => ad.AssetId == asset.Id),
                Repository   = asset.Repository.Url,
                RepositoryId = asset.RepositoryId,
                VcsId        = asset.Repository.VcsId
            })
                         .OrderByDescending(a => a.Dependencies);

            return(await PaginatedList <AssetDto> .CreateAsync(paging, pageIndex, pageSize));
        }
        public async Task <VersionControlDto> GetByIdAsync(Guid VcsId)
        {
            Vcs vcs = await context.VcsSources.FindAsync(VcsId);

            return(new VersionControlDto
            {
                Endpoint = vcs.Endpoint,
                ApiKey = vcs.ApiKey,
                Kind = vcs.Kind,
                VcsId = vcs.Id,
                Repositories = context.VcsRepositories.Count(x => x.VcsId == vcs.Id)
            });
        }
        public async Task <VersionControlDto> CreateVersionControl(VersionControlDto dto)
        {
            encryptionService.Encrypt(dto);

            Vcs vcs = new Vcs
            {
                Id        = Guid.NewGuid(),
                ApiKey    = dto.ApiKey,
                Endpoint  = dto.Endpoint,
                Kind      = dto.Kind,
                IsEnabled = dto.IsEnabled
            };

            context.VcsSources.Add(vcs);
            await context.SaveChangesAsync();

            return(new VersionControlDto {
                Endpoint = vcs.Endpoint, ApiKey = vcs.ApiKey, Kind = vcs.Kind, VcsId = vcs.Id
            });
        }
        public async Task <VersionControlDto> UpdateAsync(VersionControlDto dto)
        {
            encryptionService.Encrypt(dto);

            Vcs versionControl = context.VcsSources.Find(dto.VcsId);

            versionControl.Endpoint  = dto.Endpoint;
            versionControl.Kind      = dto.Kind;
            versionControl.IsEnabled = dto.IsEnabled;
            versionControl.ApiKey    = dto.ApiKey;

            context.VcsSources.Update(versionControl);
            await context.SaveChangesAsync();

            return(new VersionControlDto
            {
                Endpoint = versionControl.Endpoint,
                ApiKey = versionControl.ApiKey,
                Kind = versionControl.Kind,
                VcsId = versionControl.Id,
                Repositories = context.VcsRepositories.Count(x => x.VcsId == versionControl.Id)
            });
        }
Exemple #7
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity <RefreshTask>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Created).IsRequired();
                entity.Property(x => x.Scope).IsRequired();
            });

            modelBuilder.Entity <Vcs>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.ApiKey).IsRequired();
                entity.Property(x => x.Kind).IsRequired();
                entity.HasMany(x => x.Repositories).WithOne(r => r.Vcs).HasForeignKey(s => s.VcsId);
            });

            modelBuilder.Entity <VcsRepository>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Url).IsRequired();
                entity.Property(x => x.WebUrl).IsRequired();
            });

            modelBuilder.Entity <EcoRegistry>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Endpoint).IsRequired();
                entity.Property(x => x.Kind).IsRequired();
                entity.Property(x => x.ApiKey);
                entity.Property(x => x.Username);
                entity.Property(x => x.Password);
            });

            modelBuilder.Entity <Asset>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Path).IsRequired();
                entity.Property(x => x.Raw).IsRequired();
                entity.HasOne(x => x.AssetEcosystem).WithOne(x => x.Asset).HasForeignKey <AssetEcosystem>(x => x.AssetId);
                entity.HasMany(x => x.Dependencies).WithOne(x => x.Asset).HasForeignKey(x => x.AssetId);
            });

            modelBuilder.Entity <AssetDependency>(entity =>
            {
                entity.HasKey(x => x.Id);
                entity.HasOne(x => x.Dependency).WithMany(x => x.Assets).HasForeignKey(x => x.DependencyId).OnDelete(DeleteBehavior.Cascade);
                entity.HasOne(x => x.DependencyVersion).WithMany(x => x.Assets).HasForeignKey(x => x.DependencyVersionId).OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity <Dependency>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(e => e.Kind);
                entity.HasMany(x => x.Versions).WithOne(v => v.Dependency).HasForeignKey(x => x.DependencyId);
                entity.HasMany(x => x.Assets).WithOne(x => x.Dependency).HasForeignKey(x => x.DependencyId);
            });

            modelBuilder.Entity <Vulnerability>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Link).IsRequired();
                entity.Property(x => x.ResponseData).IsRequired();
            });

            modelBuilder.Entity <DependencyVersion>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Version).IsRequired();

                entity.HasMany(x => x.Vulnerabilities)
                .WithOne(x => x.DependencyVersion)
                .HasForeignKey(x => x.DependencyVersionId)
                .OnDelete(DeleteBehavior.Cascade);
            });

            modelBuilder.Entity <Ecosystem>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Name).IsRequired();
                entity.HasMany(x => x.EcosystemVersions)
                .WithOne(x => x.Ecosystem)
                .HasForeignKey(x => x.EcosystemId)
                .OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity <EcosystemVersion>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.EcosystemId).IsRequired();
                entity.Property(x => x.Version);
            });

            modelBuilder.Entity <CiCd>((entity) =>
            {
                entity.HasKey(x => x.Id);
                entity.Property(x => x.Endpoint).IsRequired();
            });

            modelBuilder.Entity <Vcs>().HasData(Vcs.MockData());
            modelBuilder.Entity <EcoRegistry>().HasData(EcoRegistry.MockData());
            modelBuilder.Entity <CiCd>().HasData(CiCd.MockData());
        }