-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildInformation.cs
More file actions
256 lines (235 loc) · 9.85 KB
/
BuildInformation.cs
File metadata and controls
256 lines (235 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) DEMA Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
namespace DemaConsulting.BuildMark;
/// <summary>
/// Represents information about an issue.
/// </summary>
/// <param name="Id">Issue ID.</param>
/// <param name="Title">Issue title.</param>
/// <param name="Url">Issue URL.</param>
public record IssueInfo(string Id, string Title, string Url);
/// <summary>
/// Represents build information for a release.
/// </summary>
/// <param name="FromVersion">Starting version (null if from beginning of history).</param>
/// <param name="ToVersion">Ending version.</param>
/// <param name="FromHash">Starting git hash (null if from beginning of history).</param>
/// <param name="ToHash">Ending git hash.</param>
/// <param name="ChangeIssues">Non-bug changes performed between versions.</param>
/// <param name="BugIssues">Bugs fixed between versions.</param>
/// <param name="KnownIssues">Known issues (unfixed or fixed but not in this build).</param>
public record BuildInformation(
Version? FromVersion,
Version ToVersion,
string? FromHash,
string ToHash,
List<IssueInfo> ChangeIssues,
List<IssueInfo> BugIssues,
List<IssueInfo> KnownIssues)
{
/// <summary>
/// Creates a BuildInformation record from a repository connector.
/// </summary>
/// <param name="connector">Repository connector to fetch information from.</param>
/// <param name="version">Optional target version. If not provided, uses the most recent tag if it matches current commit.</param>
/// <returns>BuildInformation record with all collected data.</returns>
/// <exception cref="InvalidOperationException">Thrown if version cannot be determined.</exception>
public static async Task<BuildInformation> CreateAsync(IRepoConnector connector, Version? version = null)
{
// Retrieve tag history and current commit hash from the repository
var tags = await connector.GetTagHistoryAsync();
var currentHash = await connector.GetHashForTagAsync(null);
// Determine the target version and hash for build information
Version toTagInfo;
string toHash;
if (version != null)
{
// Use explicitly specified version as target
toTagInfo = version;
toHash = currentHash;
}
else if (tags.Count > 0)
{
// Verify current commit matches latest tag when no version specified
var latestTag = tags[^1];
var latestTagHash = await connector.GetHashForTagAsync(latestTag.Tag);
if (latestTagHash.Trim() == currentHash.Trim())
{
// Current commit matches latest tag, use it as target
toTagInfo = latestTag;
toHash = currentHash;
}
else
{
// Current commit doesn't match any tag, cannot determine version
throw new InvalidOperationException(
"Target version not specified and current commit does not match any tag. " +
"Please provide a version parameter.");
}
}
else
{
// No tags in repository and no version provided
throw new InvalidOperationException(
"No tags found in repository and no version specified. " +
"Please provide a version parameter.");
}
// Determine the starting version for comparing changes
Version? fromTagInfo = null;
string? fromHash = null;
if (tags.Count > 0)
{
// Find the position of target version in tag history
var toIndex = FindTagIndex(tags, toTagInfo.FullVersion);
if (toTagInfo.IsPreRelease)
{
// Pre-release versions use the immediately previous tag as baseline
if (toIndex > 0)
{
// Target version exists in history, use previous tag
fromTagInfo = tags[toIndex - 1];
}
else if (toIndex == -1)
{
// Target version not in history, use most recent tag as baseline
fromTagInfo = tags[^1];
}
// If toIndex == 0, this is the first tag, no baseline
}
else
{
// Release versions skip pre-releases and use previous release as baseline
int startIndex;
if (toIndex > 0)
{
// Target version exists in history, start search from previous position
startIndex = toIndex - 1;
}
else if (toIndex == -1)
{
// Target version not in history, start from most recent tag
startIndex = tags.Count - 1;
}
else
{
// Target is first tag, no previous release exists
startIndex = -1;
}
// Search backward for previous non-pre-release version
for (var i = startIndex; i >= 0; i--)
{
if (!tags[i].IsPreRelease)
{
fromTagInfo = tags[i];
break;
}
}
}
// Get commit hash for baseline version if one was found
if (fromTagInfo != null)
{
fromHash = await connector.GetHashForTagAsync(fromTagInfo.Tag);
}
}
// Collect all pull requests and their associated issues in version range
var pullRequests = await connector.GetPullRequestsBetweenTagsAsync(fromTagInfo, toTagInfo);
var allIssues = new HashSet<string>();
var bugIssues = new List<IssueInfo>();
var changeIssues = new List<IssueInfo>();
// Process each pull request to extract and categorize issues
foreach (var pr in pullRequests)
{
// Get all issues referenced by this pull request
var issueIds = await connector.GetIssuesForPullRequestAsync(pr);
foreach (var issueId in issueIds)
{
// Skip issues already processed
if (allIssues.Contains(issueId))
{
continue;
}
// Mark issue as processed
allIssues.Add(issueId);
// Fetch issue details
var title = await connector.GetIssueTitleAsync(issueId);
var url = await connector.GetIssueUrlAsync(issueId);
var type = await connector.GetIssueTypeAsync(issueId);
// Create issue record
var issueInfo = new IssueInfo(issueId, title, url);
// Categorize issue by type
if (type == "bug")
{
bugIssues.Add(issueInfo);
}
else
{
changeIssues.Add(issueInfo);
}
}
}
// Collect known issues (open bugs not fixed in this build)
var knownIssues = new List<IssueInfo>();
var openIssueIds = await connector.GetOpenIssuesAsync();
foreach (var issueId in openIssueIds)
{
// Skip issues already fixed in this build
if (allIssues.Contains(issueId))
{
continue;
}
// Only include bugs in known issues list
var type = await connector.GetIssueTypeAsync(issueId);
if (type == "bug")
{
var title = await connector.GetIssueTitleAsync(issueId);
var url = await connector.GetIssueUrlAsync(issueId);
knownIssues.Add(new IssueInfo(issueId, title, url));
}
}
// Create and return build information with all collected data
return new BuildInformation(
fromTagInfo,
toTagInfo,
fromHash?.Trim(),
toHash.Trim(),
changeIssues,
bugIssues,
knownIssues);
}
/// <summary>
/// Finds the index of a tag in the tag history by normalized version.
/// </summary>
/// <param name="tags">List of tags.</param>
/// <param name="normalizedVersion">Normalized version to find.</param>
/// <returns>Index of the tag, or -1 if not found.</returns>
private static int FindTagIndex(List<Version> tags, string normalizedVersion)
{
// Search for tag matching the normalized version
for (var i = 0; i < tags.Count; i++)
{
if (tags[i].FullVersion.Equals(normalizedVersion, StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
// Tag not found in history
return -1;
}
}