public async Task <IActionResult> UpdateApplicationAsync(
            [FromRoute] string organisationId,
            [FromRoute] string teamId,
            [FromRoute] string appId,
            [FromBody] DataContracts.Application application,
            [FromServices] IApplicationService service,
            [FromServices] IUserService userService)
        {
            if (string.IsNullOrWhiteSpace(organisationId) || string.IsNullOrWhiteSpace(teamId) || string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(application?.Name))
            {
                return(BadRequest(new { reason = $"Invalid parameters!" }));
            }

            if (!string.Equals(application?.Id, appId))
            {
                return(BadRequest(new { reason = $"Invalid application name!", request = application, appId }));
            }

            try
            {
                var hasPermission = await userService.IsOwner(organisationId, teamId, User.Identity.Name);

                if (!hasPermission)
                {
                    return(Forbid());
                }

                var appResult = await service.UpdateAsync(organisationId, teamId, application);

                if (appResult == null)
                {
                    return(NotFound());
                }

                return(Ok(appResult));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new { reason = ex.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(statusCode: (int)HttpStatusCode.InternalServerError));
            }
        }
        public async Task <IActionResult> AddApplicationAsync(
            [FromRoute] string organisationId,
            [FromRoute] string teamId,
            [FromBody] DataContracts.Application app,
            [FromServices] IApplicationService service,
            [FromServices] IUserService userService)
        {
            if (string.IsNullOrWhiteSpace(organisationId) || string.IsNullOrWhiteSpace(teamId) || string.IsNullOrWhiteSpace(app?.Name))
            {
                return(BadRequest(new { reason = $"Invalid parameters" }));
            }

            try
            {
                var hasPermission = await userService.IsOwner(organisationId, teamId, User.Identity.Name);

                if (!hasPermission)
                {
                    return(Forbid());
                }

                app.CreatedAt = DateTime.Now;
                app.Id        = null;
                var appResult = await service.AddAsync(organisationId, teamId, app);

                return(CreatedAtRoute(nameof(GetApplicationAsync), new { organisationId, teamId, appId = app.Name }, appResult));
            }
            catch (NameAlreadyUsedException ex)
            {
                return(BadRequest(new { reason = ex.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(statusCode: (int)HttpStatusCode.InternalServerError));
            }
        }
示例#3
0
 public static Domain.Entities.Application ToEntity(this DataContracts.Application source)
 => ToEntity <DataContracts.Application, Domain.Entities.Application>(CurrentMapper, source);
示例#4
0
        public async Task <DataContracts.Application> AddAsync(string organisationId, string teamId, DataContracts.Application application)
        {
            var orgEntity = await organisationRepository.GetByAsync(o => o.Id == organisationId);

            if (orgEntity == null)
            {
                throw new ArgumentException($"The organisation {organisationId} not exists");
            }

            var teamEntity = await this.teamRepository.GetByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId);

            if (teamEntity == null)
            {
                throw new ArgumentException($"The team {teamId} from organisation {organisationId} not exists");
            }

            var appEntity = application.ToEntity();

            appEntity.TeamCode       = teamId;
            appEntity.OrganisationId = organisationId;

            var invalidBranchSetting =
                application.Branches.Any(b => application.Branches.Count(x => x.Name == b.Name) > 1) ||
                application.Branches.Any(b => application.Branches.Count(x => x.BranchPattern == b.BranchPattern) > 1);

            if (invalidBranchSetting)
            {
                throw new ArgumentException("Invalid branch map settings.");
            }

            if (!application.KeyBranches.Any())
            {
                throw new ArgumentException("Must create one key branch unless.");
            }

            if (!application.Branches.Any())
            {
                throw new ArgumentException("Must create one normal branch unless.");
            }

            appEntity.KeyBranchVersionings = appEntity.KeyBranches.Select(b => new Domain.Entities.KeyBranchVersioning
            {
                KeyBranchName  = b.Name,
                UpdatedAt      = DateTime.UtcNow,
                CreatedAt      = DateTime.UtcNow,
                CurrentVersion = appEntity.InitialVersion
            }).ToList();

            teamEntity.Applications.Add(new Domain.Entities.TeamApplication(appEntity));

            await this.teamRepository.UpdateAsync(teamEntity);

            var result = await repository.AddAsync(appEntity);

            appEntity.KeyBranches.ForEach(kb =>
            {
                var history = new Domain.Entities.KeyBranchVersionHistory
                {
                    ApplicationId  = result.Id,
                    KeyBranchName  = kb.Name,
                    OrganisationId = organisationId,
                    TeamId         = teamId,
                    VersionHistory = new List <Domain.Entities.KeyBranchVersion>()
                };

                history.VersionHistory.Add(new Domain.Entities.KeyBranchVersion
                {
                    CreatedAt     = DateTime.UtcNow,
                    FormatVersion = kb.FormatVersion,
                    Request       = new Domain.Entities.BranchActionRequest
                    {
                        Message         = "Initial Version",
                        From            = "*",
                        To              = "*",
                        BuildLabel      = "",
                        PreReleaseLabel = "",
                        Commit          = ""
                    },
                    Version = appEntity.InitialVersion
                });

                this.versionHistoryRepository.AddAsync(history).GetAwaiter().GetResult();
            });

            return(result.ToContract());
        }
示例#5
0
        public async Task <DataContracts.Application> UpdateAsync(string organisationId, string teamId, DataContracts.Application application)
        {
            if (!await organisationRepository.ExistsByAsync(o => o.Id == organisationId))
            {
                throw new ArgumentException($"The organisation {organisationId} not exists");
            }

            if (!await this.teamRepository.ExistsByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId))
            {
                throw new ArgumentException($"The team {teamId} from organisation {organisationId} not exists");
            }

            var appLocated = await this.repository.GetByAsync(app => app.TeamCode == teamId && app.OrganisationId == organisationId && application.Id == app.Id);

            if (appLocated == null)
            {
                return(null);
            }

            var appEntity = application.ToEntity();

            appEntity.OrganisationId       = organisationId;
            appEntity.TeamCode             = teamId;
            appEntity.KeyBranchVersionings = appLocated.KeyBranchVersionings;
            appEntity.GithubSecretKey      = string.IsNullOrWhiteSpace(appEntity.GithubSecretKey) ? appLocated.GithubSecretKey : appEntity.GithubSecretKey;

            var invalidBranchSetting =
                application.Branches.Any(b => application.Branches.Count(x => x.Name == b.Name) > 1) ||
                application.Branches.Any(b => application.Branches.Count(x => x.BranchPattern == b.BranchPattern) > 1);

            if (invalidBranchSetting)
            {
                throw new ArgumentException("Invalid branch map settings.");
            }

            if (!application.KeyBranches.Any())
            {
                throw new ArgumentException("Must create one key branch unless.");
            }

            if (!application.Branches.Any())
            {
                throw new ArgumentException("Must create one normal branch unless.");
            }

            var removedKeys = appEntity.KeyBranchVersionings.Where(b => !appEntity.KeyBranches.Any(kb => kb.Name == b.KeyBranchName)).ToList();

            appEntity.KeyBranchVersionings.RemoveAll(b => !appEntity.KeyBranches.Any(kb => kb.Name == b.KeyBranchName));

            appEntity.KeyBranchVersionings.AddRange(
                appEntity.KeyBranches
                .Where(b => !appEntity.KeyBranchVersionings.Any(kb => kb.KeyBranchName == b.Name))
                .Select(b => new Domain.Entities.KeyBranchVersioning
            {
                KeyBranchName  = b.Name,
                UpdatedAt      = DateTime.UtcNow,
                CreatedAt      = DateTime.UtcNow,
                CurrentVersion = appEntity.InitialVersion
            }).ToArray());

            appEntity.KeyBranches.ForEach(kb =>
            {
                if (!this.versionHistoryRepository.ExistsByAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appLocated.Id && h.KeyBranchName == kb.Name).GetAwaiter().GetResult())
                {
                    var history = new Domain.Entities.KeyBranchVersionHistory
                    {
                        ApplicationId  = appLocated.Id,
                        KeyBranchName  = kb.Name,
                        OrganisationId = organisationId,
                        TeamId         = teamId,
                        VersionHistory = new List <Domain.Entities.KeyBranchVersion>()
                    };
                    history.VersionHistory.Add(new Domain.Entities.KeyBranchVersion
                    {
                        CreatedAt     = DateTime.UtcNow,
                        FormatVersion = kb.FormatVersion,
                        Request       = new Domain.Entities.BranchActionRequest
                        {
                            Message         = "Initial Version",
                            From            = "*",
                            To              = "*",
                            BuildLabel      = "",
                            PreReleaseLabel = "",
                            Commit          = ""
                        },
                        Version = appEntity.InitialVersion
                    });

                    this.versionHistoryRepository.AddAsync(history).GetAwaiter().GetResult();
                }
            });

            removedKeys.ForEach(rk =>
            {
                this.versionHistoryRepository.DeleteAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appLocated.Id && h.KeyBranchName == rk.KeyBranchName).GetAwaiter().GetResult();
            });

            var result = await repository.UpdateAsync(appEntity);

            return(result.ToContract());
        }