Ejemplo n.º 1
0
        public static bool IsDevelopBranch(this BranchName branchName)
        {
            if (branchName == null)
            {
                throw new ArgumentNullException(nameof(branchName));
            }

            var developBranchNames = new[]
            {
                "develop",
                "dev"
            };

            bool isDevelopBranch =
                developBranchNames.Any(name => branchName.LogicalName.StartsWith(
                                           branchName.Name,
                                           StringComparison.InvariantCultureIgnoreCase));

            return(isDevelopBranch);
        }
Ejemplo n.º 2
0
        public static bool IsProductionBranch(this BranchName branchName)
        {
            if (branchName == null)
            {
                throw new ArgumentNullException(nameof(branchName));
            }

            var productionBranches = new List <string>(10)
            {
                "master",
                "release",
                "hotfix"
            };

            bool isProductionBranch =
                productionBranches.Any(productionBranch => branchName.LogicalName.StartsWith(
                                           productionBranch,
                                           StringComparison.InvariantCultureIgnoreCase));

            return(isProductionBranch);
        }
Ejemplo n.º 3
0
        public static bool IsFeatureBranch(this BranchName branchName)
        {
            if (branchName == null)
            {
                throw new ArgumentNullException(nameof(branchName));
            }

            var nonFeatureBranchNames = new[]
            {
                "dev",
                "develop",
                "master",
                "release",
                "hotfix"
            };

            bool isAStandardBranch =
                nonFeatureBranchNames.Any(
                    name => branchName.LogicalName.StartsWith(name, StringComparison.InvariantCultureIgnoreCase));

            return(!isAStandardBranch);
        }
Ejemplo n.º 4
0
        public Task <IEnumerable <IVariable> > GetEnvironmentVariablesAsync(
            ILogger logger,
            IReadOnlyCollection <IVariable> buildVariables,
            CancellationToken cancellationToken)
        {
            var variables = new List <IVariable>();

            string branchName = buildVariables.Require(WellKnownVariables.BranchName).ThrowIfEmptyValue().Value;

            if (branchName.StartsWith("refs/heads/", StringComparison.Ordinal))
            {
                variables.Add(new EnvironmentVariable(WellKnownVariables.BranchFullName, branchName));
            }

            string logicalName = BranchHelper.GetLogicalName(branchName).Name;

            variables.Add(new EnvironmentVariable(WellKnownVariables.BranchLogicalName, logicalName));

            Maybe <BranchName> maybeBranch = BranchName.TryParse(logicalName);

            if (maybeBranch.HasValue && maybeBranch.Value.IsDevelopBranch())
            {
                logger.WriteDebug($"Branch '{maybeBranch.Value.Name}' is develop branch, checking if release build is explicitely set");
                Maybe <IVariable> releaseBuildEnabled =
                    buildVariables.GetOptionalVariable(WellKnownVariables.ReleaseBuildEnabled);

                if (!releaseBuildEnabled.HasValue ||
                    !bool.TryParse(releaseBuildEnabled.Value.Value, out bool isReleaseBuildEnabled))
                {
                    variables.Add(new EnvironmentVariable(WellKnownVariables.ReleaseBuildEnabled, "false"));
                    logger.WriteDebug(
                        "Release build is not explicitely set when branch is develop branch, skipping release build");
                }
                else
                {
                    logger.WriteDebug(
                        $"Release build is explicitely set when branch is develop branch, value {isReleaseBuildEnabled}");
                }

                Maybe <IVariable> isDebugBuildEnabled =
                    buildVariables.GetOptionalVariable(WellKnownVariables.DebugBuildEnabled);

                if (!isDebugBuildEnabled.HasValue)
                {
                    variables.Add(new EnvironmentVariable(WellKnownVariables.DebugBuildEnabled, "true"));
                }
            }

            if (maybeBranch.HasValue && maybeBranch.Value.IsProductionBranch())
            {
                logger.WriteDebug("Branch is production branch, checking if release build is explicitely set");
                Maybe <IVariable> debugBuildEnabled =
                    buildVariables.GetOptionalVariable(WellKnownVariables.DebugBuildEnabled);

                if (!debugBuildEnabled.HasValue ||
                    !bool.TryParse(debugBuildEnabled.Value.Value, out bool isDebugBuildEnabled))
                {
                    variables.Add(new EnvironmentVariable(WellKnownVariables.DebugBuildEnabled, "false"));
                    logger.WriteDebug(
                        "Debug build is not explicitely set when branch is production branch, skipping debug build");
                }
                else
                {
                    logger.WriteDebug(
                        $"Debug build is explicitely set when branch is production branch, value {isDebugBuildEnabled}");
                }

                Maybe <IVariable> releaseBuildEnabled =
                    buildVariables.GetOptionalVariable(WellKnownVariables.ReleaseBuildEnabled);

                if (!releaseBuildEnabled.HasValue)
                {
                    variables.Add(new EnvironmentVariable(WellKnownVariables.ReleaseBuildEnabled, "true"));
                }
            }

            if (BranchHelper.BranchNameHasVersion(branchName))
            {
                string version = BranchHelper.BranchSemVerMajorMinorPatch(branchName).ToString();

                logger.WriteDebug($"Branch has version {version}");

                variables.Add(new EnvironmentVariable(WellKnownVariables.BranchNameVersion, version));

                if (buildVariables.GetBooleanByKey(WellKnownVariables.BranchNameVersionOverrideEnabled, false))
                {
                    logger.WriteVerbose(
                        $"Variable '{WellKnownVariables.BranchNameVersionOverrideEnabled}' is set to true, using version number '{version}' from branch");

                    SemanticVersion semVer = SemanticVersion.Parse(version);

                    string major = semVer.Major.ToString(CultureInfo.InvariantCulture);
                    logger.WriteVerbose(
                        $"Overriding {WellKnownVariables.VersionMajor} from '{Environment.GetEnvironmentVariable(WellKnownVariables.VersionMajor)}' to '{major}'");
                    Environment.SetEnvironmentVariable(
                        WellKnownVariables.VersionMajor,
                        major);
                    variables.Add(new EnvironmentVariable(WellKnownVariables.VersionMajor, major));

                    string minor = semVer.Minor.ToString(CultureInfo.InvariantCulture);
                    logger.WriteVerbose(
                        $"Overriding {WellKnownVariables.VersionMinor} from '{Environment.GetEnvironmentVariable(WellKnownVariables.VersionMinor)}' to '{minor}'");
                    Environment.SetEnvironmentVariable(
                        WellKnownVariables.VersionMinor,
                        minor);
                    variables.Add(new EnvironmentVariable(WellKnownVariables.VersionMinor, minor));

                    string patch = semVer.Patch.ToString(CultureInfo.InvariantCulture);
                    logger.WriteVerbose(
                        $"Overriding {WellKnownVariables.VersionPatch} from '{Environment.GetEnvironmentVariable(WellKnownVariables.VersionPatch)}' to '{patch}'");
                    Environment.SetEnvironmentVariable(
                        WellKnownVariables.VersionPatch,
                        patch);
                    variables.Add(new EnvironmentVariable(WellKnownVariables.VersionPatch, patch));
                }
                else
                {
                    logger.WriteDebug("Branch name version override is not enabled");
                }
            }
            else
            {
                logger.WriteDebug("Branch has no version in name");
            }

            if (!buildVariables.HasKey(WellKnownVariables.GitHash))
            {
                if (buildVariables.HasKey(WellKnownVariables.TeamCity.TeamCityVcsNumber))
                {
                    string gitCommitHash = buildVariables.GetVariableValueOrDefault(
                        WellKnownVariables.TeamCity.TeamCityVcsNumber,
                        string.Empty);

                    var environmentVariable = new EnvironmentVariable(
                        WellKnownVariables.GitHash,
                        gitCommitHash);

                    logger.WriteDebug(
                        $"Setting commit hash variable '{WellKnownVariables.GitHash}' from TeamCity variable '{WellKnownVariables.TeamCity.TeamCityVcsNumber}', value '{gitCommitHash}'");

                    variables.Add(environmentVariable);
                }
            }

            return(Task.FromResult <IEnumerable <IVariable> >(variables));
        }