Skip to content

[Bug] GitVersion behaves different if it is used the first time where the fallback version strategy applies #3436

@HHobeck

Description

@HHobeck

Describe the bug
I came over this issue in the discussion of #3428: Use gitversion when promoting builds across environments. The scenario is quite simple that the user wants to tag a commit in one branch (or even with merging) with not only the same label name. If the branch is not labeled and the name is set to null then I would expect that all pre-release tags are considered. The other problem here is that the system behaves different if it is used the first time where the fallback version strategy applies.

Expected Behavior

[TestCase(1)]
[TestCase(2)]
public void ExpectedBehavior(long patchNumber)
{
    var configuration = GitHubFlowConfigurationBuilder.New
        .WithLabel(null)
        .WithBranch("main", branchBuilder => branchBuilder
            .WithLabel(null).WithIncrement(IncrementStrategy.Patch)
        ).Build();

    using var fixture = new EmptyRepositoryFixture("main");

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver("0.0.1+1", configuration);

    fixture.ApplyTag($"0.0.{patchNumber}-alpha.1");

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.1", configuration);

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.2+1", configuration);

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-alpha.2+2", configuration);

    fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.1");

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-beta.1", configuration);

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-beta.2+1", configuration);

    fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.2");

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-beta.2", configuration);

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}-beta.3+1", configuration);

    fixture.ApplyTag($"0.0.{patchNumber}");

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber + 1}+1", configuration);
}

Actual Behavior

[TestCase(1)]
[TestCase(2)]
public void ActualBehavior(long patchNumber)
{
    var configuration = GitHubFlowConfigurationBuilder.New
        .WithLabel(null)
        .WithBranch("main", branchBuilder => branchBuilder
            .WithLabel(null).WithIncrement(IncrementStrategy.Patch)
        ).Build();

    using var fixture = new EmptyRepositoryFixture("main");

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver("0.0.1+1", configuration);

    fixture.ApplyTag($"0.0.{patchNumber}-alpha.1");

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-alpha.1
        fixture.AssertFullSemver($"0.0.{patchNumber}+1", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-alpha.1
        fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);

    fixture.MakeACommit();

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-alpha.2+1
        fixture.AssertFullSemver($"0.0.{patchNumber}+2", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-alpha.2+1
        fixture.AssertFullSemver($"0.0.{patchNumber}+1", configuration);

    fixture.MakeACommit();

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-alpha.2+2
        fixture.AssertFullSemver($"0.0.{patchNumber}+3", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-alpha.2+2
        fixture.AssertFullSemver($"0.0.{patchNumber}+2", configuration);

    fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.1");

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-beta.1
        fixture.AssertFullSemver($"0.0.{patchNumber}+4", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-beta.1
        fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);

    fixture.MakeACommit();

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-beta.2+1
        fixture.AssertFullSemver($"0.0.{patchNumber}+5", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-beta.2+1
        fixture.AssertFullSemver($"0.0.{patchNumber}+1", configuration);

    fixture.MakeATaggedCommit($"0.0.{patchNumber}-beta.2");

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-beta.2
        fixture.AssertFullSemver($"0.0.{patchNumber}+6", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-beta.2
        fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);

    fixture.MakeACommit();

    if (patchNumber == 1)
        // ❌ expected: 0.0.{patchNumber}-beta.3+1
        fixture.AssertFullSemver($"0.0.{patchNumber}+7", configuration);
    else
        // ❌ expected: 0.0.{patchNumber}-beta.3+1
        fixture.AssertFullSemver($"0.0.{patchNumber}+1", configuration);

    fixture.ApplyTag($"0.0.{patchNumber}");

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber}", configuration);

    fixture.MakeACommit();

    // ✅ succeeds as expected
    fixture.AssertFullSemver($"0.0.{patchNumber + 1}+1", configuration);
}

Possible Fix

  1. The business logic how the pre-release label are created needs to be changed in NextVersionCalculator::FindVersion
  2. Removing the FallbackVersionStrategy and move the logic to NextVersionCalculator::GetNextVersions

Steps to Reproduce

Please see the integration tests above.

Context

Your Environment

I have used the 6.0.0-beta.1 version

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions