feat: add support for environment variables in branch labels#4944
feat: add support for environment variables in branch labels#4944robertcoltheart wants to merge 11 commits into
Conversation
|
1 check is failing because the Nuke website is down. |
| * [ChocolateyGUI](https://github.com/chocolatey/ChocolateyGUI) | ||
| * [GitLink](https://github.com/GitTools/GitLink) | ||
| * [OctopusDeploy](https://github.com/OctopusDeploy) | ||
| * [NUKE](https://nuke.build) |
There was a problem hiding this comment.
Nuke is abandoned now and the website is unlikely to come back online. This was failing the build.
| return label; | ||
| var labelPlaceholders = BuildLabelPlaceholders(configuration.RegularExpression, effectiveBranchName); | ||
|
|
||
| return label.FormatWith(labelPlaceholders, environment); |
There was a problem hiding this comment.
Throws if environment variable is not found and has no fallback. This is deliberate to stay consistent with other usages of environment variables, for example in assembly file version.
There was a problem hiding this comment.
Here are a few suggestions regarding this implementation:
-
Use
objectinstead of generics: Change the generic typeTtoobject. It makes more sense here since a generic type parameter provides no extra benefit for this use case. -
Clarify supported types: The method currently supports instances of type
objectandIDictionary<string, object>, but this intent is not clearly expressed in the method body. I suggest splitting this into explicit overloads for better readability, like this:
internal static class StringFormatWithExtension
{
extension(string template)
{
public string FormatWith(object? source, IEnvironment environment)
{
object? GetFromSource(string value)
{
// ...
}
object? GetFromEnvironment(string value) => environment.GetEnvironmentVariable(value);
return template.FormatWith(GetFromSource, GetFromEnvironment);
}
public string FormatWith(IDictionary<string, object> source, IEnvironment environment)
{
object? GetFromSource(string value)
{
source.TryGetValue(value, out var result);
return result;
}
object? GetFromEnvironment(string value) => environment.GetEnvironmentVariable(value);
return template.FormatWith(GetFromSource, GetFromEnvironment);
}
private string FormatWith(
Func<string, object?> getFromSource, Func<string, object?> getFromEnvironment)
{
// ...
}
}
}- Allow flexible placeholder combinations: It does not make sense to restrict how placeholders can occur. Why shouldn't all of the following combinations be valid?
{JustAProperty}{JustAProperty ?? "JustAFallback"}{JustAProperty} ?? {env:JustAVariable}{JustAProperty} ?? {env:JustAVariable} ?? "JustAFallback"{env:JustAVariable}{env:JustAVariable} ?? "JustAFallback"{env:JustAVariable} ?? {JustAProperty}{env:JustAVariable} ?? {JustAProperty} ?? "JustAFallback"
There was a problem hiding this comment.
1 and 2, fair enough, will make these changes.
For 3, it was written this way (and I'm guessing the original implementor in #4746 did it this way for the same reason) to remain compatible/easy to understand as the existing configuration options for label and for assembly-file-versioning-format, where the {env:} syntax is derived from. The understanding I have is that {Name} syntax is reserved for regex captures, whereas {env:} is used for environment variables. I think it would be more confusing given the language that the configuration has cultivated to conflate these 2 concepts.
If the longer-term intention is to further enhance the regex/env var to support this conflation, I'd suggest doing this in a follow up change rather than here. Let me know your thoughts on this.
…github.com/robertcoltheart/GitVersion into feature/enhance-branch-label-with-env-vars
|



Description
Supports environment variable substitution in the label configuration for a branch using the established format used elsewhere. Fallbacks are also supported if the environment variable is not found. The changes are backwards compatible and should not fail for existing configuration.
Documentation detailing the changes has been added to
configuration.md.Related Issue
Resolves #4745
Motivation and Context
Allow labels to include environment variables as well as regex-captured variables. This is useful for CICD environments like GitHub where the pull request head_ref can be provided instead of the merge commit target.
How Has This Been Tested?
Unit tests covering the new functionality have been added.
Screenshots (if appropriate):
Checklist: