Skip to content

Commit 4264d97

Browse files
committed
Add full release workflow demo with workaround
1 parent bc3204d commit 4264d97

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using GitTools.Testing;
2+
using GitVersion.Model.Configuration;
3+
using LibGit2Sharp;
4+
using NUnit.Framework;
5+
using Shouldly;
6+
7+
namespace GitVersion.Core.Tests.IntegrationTests;
8+
9+
public class ReleaseWorkflowDemoWithWorkaround : IDisposable
10+
{
11+
private readonly EmptyRepositoryFixture _fixture = new();
12+
13+
private readonly Config _config = new()
14+
{
15+
// ❓ In my GitVersion.yml I actually have set the version to "1.0"
16+
// but that will cause an exception when I do it here in the tests
17+
NextVersion = "1.0.0"
18+
};
19+
20+
public void Dispose() => _fixture.Dispose();
21+
22+
[Test]
23+
public void Demo()
24+
{
25+
// create main and develop branches
26+
// develop is one commits ahead of main
27+
MakeACommit("Commit 1 (made on main)");
28+
CreateAndCheckoutBranch("develop");
29+
MakeACommit("Commit 2 (made on develop)");
30+
31+
// ✅ succeeds as expected
32+
GetCurrentSemVer().ShouldBe("1.0.0-alpha.1");
33+
34+
// now we are ready to start with the preparation of the 1.0.0 release
35+
CreateAndCheckoutBranch("release/1.0.0");
36+
37+
// ✅ succeeds as expected
38+
GetCurrentSemVer().ShouldBe("1.0.0-beta.1");
39+
GetCurrentSemVer("develop").ShouldBe("1.0.0-alpha.1");
40+
41+
// make another commit on release/1.0.0 to prepare the actual beta1 release
42+
MakeACommit("Commit 3 (made on release/1.0.0)");
43+
44+
// ✅ succeeds as expected
45+
GetCurrentSemVer().ShouldBe("1.0.0-beta.1");
46+
GetCurrentSemVer("develop").ShouldBe("1.0.0-alpha.1");
47+
48+
// now we makes changes on develop that may or may not end up in the 1.0.0 release
49+
CheckoutBranch("develop");
50+
MakeACommit("Commit 4 (made on develop)");
51+
52+
// ✅ succeeds as expected
53+
GetCurrentSemVer().ShouldBe("1.1.0-alpha.1");
54+
GetCurrentSemVer("release/1.0.0").ShouldBe("1.0.0-beta.1");
55+
56+
// now we do the actual release of beta 1
57+
CheckoutBranch("release/1.0.0");
58+
ApplyTag("1.0.0-beta1");
59+
60+
// ✅ succeeds as expected
61+
GetCurrentSemVer().ShouldBe("1.0.0-beta.1");
62+
GetCurrentSemVer("develop").ShouldBe("1.1.0-alpha.1");
63+
64+
// continue with more work on develop that may or may not end up in the 1.0.0 release
65+
CheckoutBranch("develop");
66+
MakeACommit("Commit 5 (made on develop)");
67+
68+
// ✅ succeeds as expected
69+
GetCurrentSemVer().ShouldBe("1.1.0-alpha.2");
70+
GetCurrentSemVer("release/1.0.0").ShouldBe("1.0.0-beta.1");
71+
72+
// now we decide that Commit 5 made on develop should be part of the beta 2 release
73+
// se we cherry pick it
74+
CheckoutBranch("release/1.0.0");
75+
CherryPickLatestCommitFromBranch("develop");
76+
77+
// ✅ succeeds as expected
78+
GetCurrentSemVer().ShouldBe("1.0.0-beta.2");
79+
GetCurrentSemVer("develop").ShouldBe("1.1.0-alpha.2");
80+
81+
// now we do an important bugfix that we found while preparing beta 2
82+
MakeACommit("Commit 6 (made on release/1.0.0)");
83+
84+
// ✅ succeeds as expected
85+
GetCurrentSemVer().ShouldBe("1.0.0-beta.2");
86+
GetCurrentSemVer("develop").ShouldBe("1.1.0-alpha.2");
87+
88+
// we want everything (Commit 3 and 6) that we made only on release/1.0.0 be in develop
89+
CheckoutBranch("develop");
90+
MergeWithNoFF("release/1.0.0");
91+
92+
// ✅ succeeds as expected
93+
GetCurrentSemVer().ShouldBe("1.1.0-alpha.6");
94+
}
95+
96+
private void MakeACommit(string? message = null) => _fixture.Repository.MakeACommit(message);
97+
98+
private void CheckoutBranch(string branchName) => Commands.Checkout(_fixture.Repository, _fixture.Repository.Branches[branchName]);
99+
100+
private void CreateAndCheckoutBranch(string branchName) => Commands.Checkout(_fixture.Repository, _fixture.Repository.CreateBranch(branchName));
101+
102+
private string GetCurrentSemVer(string? branch = null)
103+
{
104+
if (branch == null)
105+
{
106+
return _fixture.GetVersion(_config).SemVer;
107+
}
108+
109+
if (_fixture.Repository.Branches.All(b => b.FriendlyName != branch))
110+
{
111+
throw new InvalidOperationException($"Branch {branch} does not exist");
112+
}
113+
114+
return _fixture.GetVersion(_config, branch: branch).SemVer;
115+
}
116+
117+
private void ApplyTag(string tag) => _fixture.Repository.ApplyTag(tag);
118+
119+
private void MergeWithNoFF(string sourceBranch) => _fixture.Repository.MergeNoFF(sourceBranch);
120+
121+
private void CherryPickLatestCommitFromBranch(string sourceBranch) => this._fixture.Repository.CherryPick(this._fixture.Repository.Branches[sourceBranch].Commits.First(), Generate.SignatureNow());
122+
}

0 commit comments

Comments
 (0)