public async Task CreateVSTSFakeProject(Guid organizationId, Guid projectId) { var project = await _projectRepository.GetProjectWithOrgAndAccountOwnerByProjectId(organizationId, projectId); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } var cmsType = ConfigurationManagementService.VSTS; var projectName = this._slugService.GetSlug($"{project.Organization.Owner.Email} {project.Organization.Name} {project.Name}"); var resource = new { Name = projectName, Description = project.Description }; CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(cmsType).GetToken(_vstsFakeOptions.Value.AccountId, _vstsFakeOptions.Value.AccountId, _vstsFakeOptions.Value.AccessSecret); CMSProjectAvailabilityResultModel cmsProjectAvailability = await _cmsService(cmsType).ValidateProjectAvailability(cmsAuthCredential, string.Empty, resource.Name); if (!cmsProjectAvailability.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectAvailability.GetReasonForNoSuccess()}"); return; } CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(project.Organization.Name, resource.Name, resource.Description, ProjectVisibility.Private); CMSProjectCreateResultModel cmsProjectCreate = await _cmsService(cmsType).CreateProject(cmsAuthCredential, projectCreateModel); if (!cmsProjectCreate.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectCreate.GetReasonForNoSuccess()}"); return; } project.SetFakeVSTSProject(projectName); this._projectRepository.Update(project); await this._projectRepository.SaveChanges(); }
public async Task <CMSProjectCreateResultModel> CreateProject(CMSAuthCredentialModel authCredential, CMSProjectCreateModel model) { //default data var vstsmodel = new CMSVSTSProjectCreateModel(); vstsmodel.Name = model.Name; vstsmodel.Description = model.Description; vstsmodel.Visibility = model.ProjectVisibility == Domain.Models.ProjectVisibility.Private ? "private" : "public"; vstsmodel.Capabilities = new CMSVSTSProjectCapabilityModel(); vstsmodel.Capabilities.VersionControl = new CMSVSTSProjectVersionControlModel(); vstsmodel.Capabilities.VersionControl.SourceControlType = "Git"; vstsmodel.Capabilities.ProcessTemplate = new CMSVSTSProjectProcessTemplateModel(); vstsmodel.Capabilities.ProcessTemplate.TemplateTypeId = "adcc42ab-9882-485e-a3ed-7678f01f66bc"; CMSProjectCreateResultModel result = new CMSProjectCreateResultModel(); var response = await _httpProxyService.PostAsync($"/_apis/projects?api-version={_vstsOptions.Value.ApiVersion}", vstsmodel, authCredential); if (!response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { if (response.StatusCode == System.Net.HttpStatusCode.NonAuthoritativeInformation) { result.Fail($"Code: {response.StatusCode}, Reason: The credentials are not correct"); return(result); } result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } return(result); }
public async Task <CMSProjectCreateResultModel> CreateProject(CMSAuthCredentialModel authCredential, CMSProjectCreateModel model) { CMSProjectCreateResultModel result = new CMSProjectCreateResultModel(); var httpClient = new HttpClient(); var organizationKey = model.OrganizationName.Replace(" ", "").ToLower(); var projectKey = model.Name.Replace(" ", "").ToLower(); var gitLabModel = new { name = model.Name, path = $"{organizationKey}-{projectKey}", description = model.Description, visibility = model.ProjectVisibility == Domain.Models.ProjectVisibility.Private ? "private" : "public", }; var request = new HttpRequestMessage(HttpMethod.Post, $"{authCredential.Url}/groups"); request.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(gitLabModel), Encoding.UTF8, "application/json"); request.Headers.Add("Private-Token", authCredential.AccessToken); var response = await httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } var projectCreated = await response.MapTo <CMSGitLabProjectModel>(); result.ProjectExternalId = projectCreated.Id.ToString(); return(result); }
public async Task <CMSProjectCreateResultModel> CreateProject(CMSAuthCredentialModel authCredential, CMSProjectCreateModel model) { CMSProjectCreateResultModel result = new CMSProjectCreateResultModel(); var dic = new Dictionary <string, string>(); dic.Add("Accept", "application/vnd.github.inertia-preview+json"); dic.Add("User-Agent", API_UserAgent); var accountList = await GetAccounts(authCredential); var defaultTeam = accountList.Items.FirstOrDefault(c => c.AccountId.Equals(authCredential.AccountId)); var gitHubModel = new { name = model.Name, body = model.Description, }; if (defaultTeam.IsOrganization) { //var response = await _httpProxyService.PostAsync($"/orgs/{defaultTeam.Name}/projects", gitHubModel, authCredential, dic); //if (!response.IsSuccessStatusCode) //{ // result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); // return result; //} // //var projectCreated = await response.MapTo<CMSGitHubProjectModel>(); // //result.ProjectExternalId = projectCreated.Id; result.ProjectExternalId = $"{Guid.NewGuid()}"; } else { result.ProjectExternalId = $"{Guid.NewGuid()}"; } return(result); }
public async Task <CMSProjectCreateResultModel> CreateProject(CMSAuthCredentialModel authCredential, CMSProjectCreateModel model) { CMSProjectCreateResultModel result = new CMSProjectCreateResultModel(); var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authCredential.AccessToken); client.BaseAddress = new Uri(authCredential.Url); var teamResult = await GetAccountTeams(client, authCredential); var defaultTeam = teamResult.Teams.FirstOrDefault(c => c.TeamId.Equals(model.TeamId)); var projectKey = model.Name.Replace(" ", "").ToLower(); var bitbucketModel = new { name = model.Name, key = projectKey, description = model.Description, is_private = model.ProjectVisibility == Domain.Models.ProjectVisibility.Public ? true : false }; var response = await client.PostAsync($"/{API_VERSION}/teams/{defaultTeam.UserName}/projects/", new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(bitbucketModel), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { result.Fail($"Code: {response.StatusCode}, Reason: {await response.Content.ReadAsStringAsync()}"); return(result); } var projectCreated = await response.MapTo <CMSBitBucketProjectModel>(); result.ProjectExternalId = projectKey; return(result); }
public async Task CreateProject(Guid organizationId, ProjectPostRp resource) { string loggedUserId = _identityService.GetUserId(); User user = await _userRepository.GetUser(loggedUserId); Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists."); return; } OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(resource.OrganizationCMSId); if (organizationCMS == null) { await _domainManagerService.AddNotFound($"The configuration management service with id {resource.OrganizationCMSId} does not exists."); return; } if (organizationCMS.Type == ConfigurationManagementService.VSTS && resource.projectVisibility == ProjectVisibility.None) { await _domainManagerService.AddConflict($"The project visibility should be Private or Public."); return; } OrganizationCPS organizationCPS = null; if (resource.OrganizationCPSId.HasValue) { organizationCPS = organization.GetCloudProviderServiceById(resource.OrganizationCPSId.Value); if (organizationCPS == null) { await _domainManagerService.AddNotFound($"The cloud provider service with id {resource.OrganizationCPSId} does not exists."); return; } } else { organizationCPS = new OrganizationCPS { Type = CloudProviderService.None }; } ProjectTemplate projectTemplate = null; if (resource.ProjectTemplateId.HasValue) { projectTemplate = await _projectTemplateRepository.GetProjectTemplateById(resource.ProjectTemplateId.Value); if (projectTemplate == null) { await _domainManagerService.AddNotFound($"The project template with id {resource.ProjectTemplateId.Value} does not exists."); return; } } Project existingProject = organization.GetProjectByName(resource.Name); if (existingProject != null) { await _domainManagerService.AddConflict($"The project name {resource.Name} has already been taken."); return; } //Auth CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(organizationCMS.Type).GetToken( _dataProtectorService.Unprotect(organizationCMS.AccountId), _dataProtectorService.Unprotect(organizationCMS.AccountName), _dataProtectorService.Unprotect(organizationCMS.AccessSecret), _dataProtectorService.Unprotect(organizationCMS.AccessToken)); CMSProjectAvailabilityResultModel cmsProjectAvailability = await _cmsService(organizationCMS.Type).ValidateProjectAvailability(cmsAuthCredential, resource.TeamId, resource.Name); if (!cmsProjectAvailability.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectAvailability.GetReasonForNoSuccess()}"); return; } Project newProject = user.CreateProject(organizationId, resource.TeamId, resource.Name, resource.Description, resource.ProjectType, resource.OrganizationCMSId, resource.OrganizationCPSId, resource.ProjectTemplateId, resource.AgentPoolId, resource.projectVisibility, organizationCPS.Type, organizationCMS.Type); //SaveChanges in CSM CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(organization.Name, resource.Name, resource.Description, resource.projectVisibility); projectCreateModel.TeamId = resource.TeamId; CMSProjectCreateResultModel cmsProjectCreate = await _cmsService(organizationCMS.Type).CreateProject(cmsAuthCredential, projectCreateModel); if (!cmsProjectCreate.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectCreate.GetReasonForNoSuccess()}"); return; } newProject.UpdateExternalInformation(cmsProjectCreate.ProjectExternalId, resource.Name); _userRepository.Update(user); await _userRepository.SaveChanges(); await _domainManagerService.AddResult("ProjectId", newProject.ProjectId); //send event var @event = new ProjectCreatedEvent(_correlationId) { OrganizationId = organization.OrganizationId, ProjectId = newProject.ProjectId, ProjectName = newProject.Name, InternalProjectName = newProject.InternalName, ProjectVSTSFake = this._slugService.GetSlug($"{organization.Owner.Email} {organization.Name} {newProject.Name}"), AgentPoolId = newProject.AgentPoolId, CMSType = organizationCMS.Type, CMSAccountId = _dataProtectorService.Unprotect(organizationCMS.AccountId), CMSAccountName = _dataProtectorService.Unprotect(organizationCMS.AccountName), CMSAccessId = _dataProtectorService.Unprotect(organizationCMS.AccessId), CMSAccessSecret = _dataProtectorService.Unprotect(organizationCMS.AccessSecret), CMSAccessToken = _dataProtectorService.Unprotect(organizationCMS.AccessToken), CPSType = organizationCPS.Type, CPSAccessId = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessId) : string.Empty, CPSAccessName = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessName) : string.Empty, CPSAccessSecret = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessSecret) : string.Empty, CPSAccessAppId = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessAppId) : string.Empty, CPSAccessAppSecret = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessAppSecret) : string.Empty, CPSAccessDirectory = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessDirectory) : string.Empty, UserId = loggedUserId }; if (resource.ProjectTemplateId.HasValue) { @event.ProjectTemplate = new ProjectTemplateCreatedEvent(); @event.ProjectTemplate.Services = projectTemplate.Services.Select(x => new ProjectTemplateServiceCreatedEvent() { Name = x.Name, ProjectServiceTemplateId = x.ProjectServiceTemplateId }).ToList(); } await _eventBusService.Publish(queueName : "ProjectCreatedEvent", @event : @event); }
public async Task ImportProject(Guid organizationId, ProjectImportPostRp resource) { string loggedUserId = _identityService.GetUserId(); User user = await _userRepository.GetUser(loggedUserId); Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists."); return; } OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(resource.OrganizationCMSId); if (organizationCMS == null) { await _domainManagerService.AddNotFound($"The configuration management service with id {resource.OrganizationCMSId} does not exists."); return; } if (organizationCMS.Type == ConfigurationManagementService.VSTS && resource.projectVisibility == ProjectVisibility.None) { await _domainManagerService.AddConflict($"The project visibility should be Private or Public."); return; } OrganizationCPS organizationCPS = null; if (resource.OrganizationCPSId.HasValue) { organizationCPS = organization.GetCloudProviderServiceById(resource.OrganizationCPSId.Value); if (organizationCPS == null) { await _domainManagerService.AddNotFound($"The cloud provider service with id {resource.OrganizationCPSId} does not exists."); return; } } else { organizationCPS = new OrganizationCPS { Type = CloudProviderService.None }; } Project existingProject = organization.GetProjectByName(resource.Name); if (existingProject != null) { await _domainManagerService.AddConflict($"The project name {resource.Name} has already been taken."); return; } //Auth CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(organizationCMS.Type).GetToken( _dataProtectorService.Unprotect(organizationCMS.AccountId), _dataProtectorService.Unprotect(organizationCMS.AccountName), _dataProtectorService.Unprotect(organizationCMS.AccessSecret), _dataProtectorService.Unprotect(organizationCMS.AccessToken)); Project newProject = user.ImportProject(organizationId, string.Empty, resource.Name, resource.Description, resource.ProjectType, resource.OrganizationCMSId, resource.OrganizationCPSId, null, resource.AgentPoolId, resource.projectVisibility, organizationCPS.Type, organizationCMS.Type); //SaveChanges in CSM CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(organization.Name, resource.Name, resource.Description, resource.projectVisibility); newProject.UpdateExternalInformation(resource.ProjectExternalId, resource.ProjectExternalName); _userRepository.Update(user); await _userRepository.SaveChanges(); await _domainManagerService.AddResult("ProjectId", newProject.ProjectId); //send event var @event = new ProjectImportedEvent(_correlationId) { OrganizationId = organization.OrganizationId, OrganizationCMSId = resource.OrganizationCMSId, ProjectId = newProject.ProjectId, ProjectName = resource.Name, InternalProjectName = newProject.InternalName, ProjectExternalId = resource.ProjectExternalId, ProjectExternalName = resource.ProjectExternalName, BuildDefinitionYML = resource.BuildDefinitionYML, ProjectServiceTemplateId = resource.ProjectServiceTemplateId, ProjectVSTSFake = this._slugService.GetSlug($"{organization.Owner.Email} {organization.Name} {newProject.Name}"), AgentPoolId = newProject.AgentPoolId, CMSType = organizationCMS.Type, CMSAccountId = _dataProtectorService.Unprotect(organizationCMS.AccountId), CMSAccountName = _dataProtectorService.Unprotect(organizationCMS.AccountName), CMSAccessId = _dataProtectorService.Unprotect(organizationCMS.AccessId), CMSAccessSecret = _dataProtectorService.Unprotect(organizationCMS.AccessSecret), CMSAccessToken = _dataProtectorService.Unprotect(organizationCMS.AccessToken), CPSType = organizationCPS.Type, CPSAccessId = _dataProtectorService.Unprotect(organizationCPS.AccessId), CPSAccessName = _dataProtectorService.Unprotect(organizationCPS.AccessName), CPSAccessSecret = _dataProtectorService.Unprotect(organizationCPS.AccessSecret), CPSAccessAppId = _dataProtectorService.Unprotect(organizationCPS.AccessAppId), CPSAccessAppSecret = _dataProtectorService.Unprotect(organizationCPS.AccessAppSecret), CPSAccessDirectory = _dataProtectorService.Unprotect(organizationCPS.AccessDirectory), UserId = loggedUserId, ProjectRepository = new ProjectRepositoryCreatedEvent { Repositories = resource.Repositories.Select(c => new ProjectRepositoryServiceCreatedEvent { Id = c.Id, Name = c.Name, Link = c.Link, BranchName = c.BranchName, ExternalName = c.ExternalName }).ToList(), } }; await _eventBusService.Publish(queueName : "ProjectImportedEvent", @event : @event); }