public async Task <Guid> Handle(CreateRepositoryCommand request, CancellationToken cancellationToken) { var repo = new Data.Models.Repository { Name = request.Name, Description = request.Description, CreationDate = DateTime.Now }; await _context.Repository.AddAsync(repo, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(repo.RepositoryId); }
public async Task <IActionResult> Create([FromBody, Required] SubscriptionData subscription) { Data.Models.Channel channel = await _context.Channels.Where(c => c.Name == subscription.ChannelName) .FirstOrDefaultAsync(); if (channel == null) { return(BadRequest( new ApiError( "the request is invalid", new[] { $"The channel '{subscription.ChannelName}' could not be found." }))); } Data.Models.Repository repo = await _context.Repositories.FindAsync(subscription.TargetRepository); if (subscription.TargetRepository.Contains("github.com")) { // If we have no repository information or an invalid installation id // then we will fail when trying to update things, so we fail early. if (repo == null || repo.InstallationId <= 0) { return(BadRequest( new ApiError( "the request is invalid", new[] { $"The repository '{subscription.TargetRepository}' does not have an associated github installation. " + "The Maestro github application must be installed by the repository's owner and given access to the repository." }))); } } // In the case of a dev.azure.com repository, we don't have an app installation, // but we should add an entry in the repositories table, as this is required when // adding a new subscription policy. // NOTE: // There is a good chance here that we will need to also handle <account>.visualstudio.com // but leaving it out for now as it would be preferred to use the new format else if (subscription.TargetRepository.Contains("dev.azure.com")) { if (repo == null) { _context.Repositories.Add( new Data.Models.Repository { RepositoryName = subscription.TargetRepository, InstallationId = default });
private async Task SynchronizeInstallationRepositoriesAsync(long installationId) { string token = await GitHubTokenProvider.GetTokenForInstallationAsync(installationId); IReadOnlyList <Repository> gitHubRepos = await GetAllInstallationRepositories(token); List <Data.Models.Repository> toRemove = await Context.Repositories.Where(r => r.InstallationId == installationId).ToListAsync(); foreach (Repository repo in gitHubRepos) { Data.Models.Repository existing = await Context.Repositories.FindAsync(repo.HtmlUrl); if (existing == null) { Context.Repositories.Add( new Data.Models.Repository { RepositoryName = repo.HtmlUrl, InstallationId = installationId }); } else { toRemove.Remove(existing); existing.InstallationId = installationId; Context.Update(existing); } } foreach (Data.Models.Repository repository in toRemove) { repository.InstallationId = 0; Context.Update(repository); } await Context.SaveChangesAsync(); }