Example #1
0
        public async Task <IActionResult> GenerateVersionAsync(
            [FromRoute] string organisationId,
            [FromRoute] string teamId,
            [FromRoute] string appId,
            [FromBody] BranchActionRequest data,
            [FromServices] IVersionService service)
        {
            if (string.IsNullOrWhiteSpace(organisationId) || string.IsNullOrWhiteSpace(teamId) || string.IsNullOrWhiteSpace(appId) || data == null)
            {
                return(BadRequest(new { reason = $"Invalid parameters!" }));
            }

            if (string.IsNullOrWhiteSpace(data.From))
            {
                return(BadRequest("Source branch must have valid value!"));
            }

            if (string.IsNullOrWhiteSpace(data.To))
            {
                var resultGet = await service.GetVersionFromSourceBranchAsync(organisationId, teamId, appId, data);

                if (resultGet == null)
                {
                    return(NotFound());
                }
                return(Ok(resultGet));
            }

            var resultUpdate = await service.UpdateVersionOnBranches(organisationId, teamId, appId, data);

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

            return(Ok(resultUpdate));
        }
Example #2
0
        public async Task <SemanticVersion> GetVersionFromSourceBranchAsync(string organisationId, string teamId, string appId, BranchActionRequest data)
        {
            if (!await organisationRepository.ExistsByAsync(o => o.Id == organisationId))
            {
                return(null);
            }

            if (!await this.teamRepository.ExistsByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId))
            {
                return(null);
            }

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

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

            var keyBranch = appLocated.KeyBranches.FirstOrDefault(b => Regex.IsMatch(data.From, b.BranchPattern));

            if (keyBranch == null)
            {
                var branch = appLocated.Branches.FirstOrDefault(b => Regex.IsMatch(data.From, b.BranchPattern));
                if (branch == null)
                {
                    return(null);
                }

                keyBranch = appLocated.KeyBranches.FirstOrDefault(b => b.Name == branch.InheritedFrom.Name);
                if (keyBranch == null)
                {
                    return(null);
                }
            }

            var keyBranchVersion = appLocated.KeyBranchVersionings.FirstOrDefault(k => k.KeyBranchName == keyBranch.Name);

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

            bool hasChanges = false;

            if (keyBranch.FormatVersion.ToLower().Contains("{prerelease}"))
            {
                var preRelease = data.PreReleaseLabel?.Trim().Length > 0 ? $"{data.PreReleaseLabel}" : keyBranchVersion.CurrentVersion.PreRelease;
                hasChanges |= keyBranchVersion.CurrentVersion.PreRelease != preRelease;
                keyBranchVersion.CurrentVersion.PreRelease = preRelease;
            }

            if (keyBranch.FormatVersion.ToLower().Contains("{build}"))
            {
                var build = data.BuildLabel?.Trim().Length > 0 ? $"{data.BuildLabel}" : keyBranchVersion.CurrentVersion.Build;
                hasChanges |= keyBranchVersion.CurrentVersion.Build != build;
                keyBranchVersion.CurrentVersion.Build = build;
            }

            var history = await this.versionHistoryRepository.GetByAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appId && h.KeyBranchName == keyBranch.Name);

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

            if (hasChanges)
            {
                history.VersionHistory.Add(new Domain.Entities.KeyBranchVersion
                {
                    CreatedAt     = DateTime.UtcNow,
                    FormatVersion = keyBranch.FormatVersion,
                    Request       = data.ToEntity(),
                    Version       = keyBranchVersion.CurrentVersion,
                });

                history.KeyBranchName  = keyBranchVersion.KeyBranchName;
                history.OrganisationId = organisationId;
                history.TeamId         = teamId;
                history.ApplicationId  = appId;

                await this.versionHistoryRepository.UpdateAsync(history);
            }

            await this.repository.UpdateAsync(appLocated);

            return(keyBranchVersion.CurrentVersion.ToContract());
        }
Example #3
0
        public async Task <SemanticVersion> UpdateVersionOnBranches(string organisationId, string teamId, string appId, BranchActionRequest data)
        {
            if (!await organisationRepository.ExistsByAsync(o => o.Id == organisationId))
            {
                return(null);
            }

            if (!await this.teamRepository.ExistsByAsync(t => t.TeamCode == teamId && t.OrganisationId == organisationId))
            {
                return(null);
            }

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

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

            var keyBranch = appLocated.KeyBranches.FirstOrDefault(b => Regex.IsMatch(data.To, b.BranchPattern));

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

            var keyBranchVersion = appLocated.KeyBranchVersionings.FirstOrDefault(k => k.KeyBranchName == keyBranch.Name);

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

            var hasChanges = false;

            if (keyBranchVersion.Commit != data.Commit)
            {
                var branch = appLocated.Branches.FirstOrDefault(b => Regex.IsMatch(data.From, b.BranchPattern));
                if (branch == null)
                {
                    var fromKeyBranch = appLocated.KeyBranches.FirstOrDefault(b => Regex.IsMatch(data.From, b.BranchPattern));
                    if (fromKeyBranch == null)
                    {
                        return(null);
                    }

                    var fromKeyBranchVersion = appLocated.KeyBranchVersionings.FirstOrDefault(kv => kv.KeyBranchName == fromKeyBranch.Name);
                    if (fromKeyBranchVersion == null)
                    {
                        return(null);
                    }

                    keyBranchVersion.CurrentVersion.Major      = fromKeyBranchVersion.CurrentVersion.Major;
                    keyBranchVersion.CurrentVersion.Minor      = fromKeyBranchVersion.CurrentVersion.Minor;
                    keyBranchVersion.CurrentVersion.Patch      = fromKeyBranchVersion.CurrentVersion.Patch;
                    keyBranchVersion.CurrentVersion.Build      = fromKeyBranchVersion.CurrentVersion.Build;
                    keyBranchVersion.CurrentVersion.PreRelease = fromKeyBranchVersion.CurrentVersion.PreRelease;
                    hasChanges = true;
                }
                else
                {
                    var incrementMode = branch.AllowBumpMajor && data.Message.Contains(branch.BumpMajorText) ? Domain.Entities.IncrementMode.Major : branch.Increment;
                    keyBranchVersion.CurrentVersion.Major += incrementMode == Domain.Entities.IncrementMode.Major ? 1 : 0;
                    keyBranchVersion.CurrentVersion.Minor += incrementMode == Domain.Entities.IncrementMode.Minor ? 1 : 0;
                    keyBranchVersion.CurrentVersion.Patch += incrementMode == Domain.Entities.IncrementMode.Patch ? 1 : 0;

                    keyBranchVersion.CurrentVersion.Minor = incrementMode == Domain.Entities.IncrementMode.Major ? 0 : keyBranchVersion.CurrentVersion.Minor;
                    keyBranchVersion.CurrentVersion.Patch = incrementMode == Domain.Entities.IncrementMode.Minor || incrementMode == Domain.Entities.IncrementMode.Major ? 0 : keyBranchVersion.CurrentVersion.Patch;
                    hasChanges = true;
                }
            }

            if (keyBranch.FormatVersion.ToLower().Contains("{prerelease}"))
            {
                keyBranchVersion.CurrentVersion.PreRelease = $"{data.PreReleaseLabel}";
                hasChanges = true;
            }
            if (keyBranch.FormatVersion.ToLower().Contains("{build}"))
            {
                keyBranchVersion.CurrentVersion.Build = $"{data.BuildLabel}";
                hasChanges = true;
            }

            keyBranchVersion.UpdatedAt = DateTime.UtcNow;
            keyBranchVersion.Commit    = data.Commit;

            if (hasChanges)
            {
                var history = await this.versionHistoryRepository.GetByAsync(h => h.OrganisationId == organisationId && h.TeamId == teamId && h.ApplicationId == appId && h.KeyBranchName == keyBranch.Name);

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

                history.VersionHistory.Add(new Domain.Entities.KeyBranchVersion
                {
                    CreatedAt     = DateTime.UtcNow,
                    FormatVersion = keyBranch.FormatVersion,
                    Request       = data.ToEntity(),
                    Version       = keyBranchVersion.CurrentVersion,
                });

                history.KeyBranchName  = keyBranchVersion.KeyBranchName;
                history.OrganisationId = organisationId;
                history.TeamId         = teamId;
                history.ApplicationId  = appId;

                await this.versionHistoryRepository.UpdateAsync(history);
            }
            await this.repository.UpdateAsync(appLocated);

            return(keyBranchVersion.CurrentVersion.ToContract());
        }