-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
461 lines (384 loc) · 17.1 KB
/
Copy pathMainForm.cs
File metadata and controls
461 lines (384 loc) · 17.1 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
using LibGit2Sharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GitChangelog
{
public partial class MainForm : Form
{
private SettingsDialog _settings = new SettingsDialog();
private AboutBox _about = new AboutBox();
private GitRepository _repo = new GitRepository();
private IIssueTracker _issues = new IssueTrackerJira();
private string _matchPrefix = "";
private struct GitMessage
{
public string subject;
public string body;
}
private const int WM_USER = 0x0400;
private const int EM_SETEVENTMASK = (WM_USER + 69);
private const int WM_SETREDRAW = 0x0b;
private IntPtr OldEventMask;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private void BeginUpdate()
{
SendMessage(textChangelog.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero);
OldEventMask = (IntPtr)SendMessage(textChangelog.Handle, EM_SETEVENTMASK, IntPtr.Zero, IntPtr.Zero);
}
private void EndUpdate()
{
SendMessage(textChangelog.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
SendMessage(textChangelog.Handle, EM_SETEVENTMASK, IntPtr.Zero, OldEventMask);
}
public MainForm()
{
InitializeComponent();
RefreshFromSettings();
}
private List<string> GetMatches(string text)
{
if (_matchPrefix.Length > 0 && text != null && text.Length > 0)
{
var result = new List<string>();
Regex expression = new Regex(_matchPrefix);
var results = expression.Matches(text);
foreach (Match match in results)
{
result.Add(match.Groups["Identifier"].Value);
}
if (result.Count == 0)
{
return null;
}
return result;
}
return null;
}
private Dictionary<string, Issue> GetRelatedIssues(List<Commit> commits)
{
if (_matchPrefix == null || _matchPrefix.Length == 0)
{
return null;
}
HashSet<string> uniqueIssues = new HashSet<string>();
Dictionary<string, Issue> relatedIssues = new Dictionary<string, Issue>();
foreach (Commit c in commits)
{
var matches = GetMatches(c.Message);
if (matches != null)
{
foreach (string match in matches)
{
if (!uniqueIssues.Contains(match))
{
uniqueIssues.Add(match);
}
}
}
}
var issues = _issues.GetIssues(null, uniqueIssues.ToArray());
if (issues != null && issues.Count > 0)
{
foreach (Issue issue in issues)
{
relatedIssues[issue.Key] = issue;
}
}
return relatedIssues;
}
private GitMessage FilterMessage(string message)
{
var result = new GitMessage();
// Fix linebreaks
message = message.Replace("\n", Environment.NewLine);
// Remove Change-Id, and separate into subject/body parameters
var lines = message.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
int idx = 0;
foreach (var line in lines)
{
if (idx > 1)
{
if (line.IndexOf("Change-Id") == -1)
{
result.body += line + Environment.NewLine;
}
}
else if (idx == 0)
{
result.subject = line;
}
idx++;
}
result.subject = result.subject.Trim();
result.body = !string.IsNullOrWhiteSpace(result.body) ? result.body.Trim() : string.Empty;
return result;
}
private void AppendStyledText(Color color, Color backColor, FontStyle fontStyle, Font font, bool bulleted, string text)
{
if (font == null)
{
font = new Font(textChangelog.Font, fontStyle);
}
// Change properties
textChangelog.SelectionStart = textChangelog.TextLength;
textChangelog.SelectionLength = 0;
textChangelog.SelectionColor = color;
textChangelog.SelectionBackColor = backColor;
textChangelog.SelectionFont = font;
textChangelog.SelectionBullet = bulleted;
textChangelog.BulletIndent = 30;
textChangelog.SelectionIndent = bulleted ? 15 : 0;
// Print text
textChangelog.AppendText(text);
// Restore properties
textChangelog.SelectionColor = textChangelog.ForeColor;
textChangelog.SelectionBackColor = textChangelog.BackColor;
textChangelog.SelectionFont = textChangelog.Font;
textChangelog.SelectionBullet = false;
textChangelog.SelectionIndent = 0;
}
private DateTime RoundToNextDay(DateTime d)
{
return new DateTime(d.Year, d.Month, d.Day, 0, 0, 0).AddDays(1);
}
private void btnGenerateChangelog_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
BeginUpdate();
try
{
textChangelog.Clear();
object since = comboReferenceSince.SelectedItem != null ? ((ComboBoxItem)comboReferenceSince.SelectedItem).Value : null;
object until = comboReferenceUntil.SelectedItem != null ? ((ComboBoxItem)comboReferenceUntil.SelectedItem).Value : null;
if (since == null && comboReferenceSince.Text != null && comboReferenceSince.Text.Length > 0)
{
if (!comboReferenceSince.Text.Equals("TAIL"))
{
since = comboReferenceSince.Text;
}
}
else if (since == null)
{
MessageBox.Show("Invalid Git reference starting point");
return;
}
if (until == null && comboReferenceUntil.Text != null && comboReferenceUntil.Text.Length > 0)
{
until = comboReferenceSince.Text;
}
else if (until == null)
{
MessageBox.Show("Invalid Git reference end point");
return;
}
object branch = comboBranches.SelectedItem != null ? ((ComboBoxItem)comboBranches.SelectedItem).Value : null;
var commits = _repo.GetCommits((Branch)branch, since, until);
if (commits != null && commits.Any())
{
var from = commits.First().Committer.When.DateTime;
var to = commits.Last().Committer.When.DateTime;
var relatedIssues = GetRelatedIssues(commits);
if (relatedIssues != null && relatedIssues.Count > 0)
{
var roundedTo = RoundToNextDay(to);
string resolved = "";
string unresolved = "";
foreach (var issue in relatedIssues)
{
if (issue.Value.ResolutionDateTime != null && roundedTo > issue.Value.ResolutionDateTime)
{
resolved += issue.Key + ": " + issue.Value.Summary + Environment.NewLine;
}
else
{
unresolved += issue.Key + ": " + issue.Value.Summary + Environment.NewLine;
}
}
if (resolved.Length > 0)
{
var font = new Font(textChangelog.Font.FontFamily, 14.0f);
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Regular, font, false, "Resolved issues:" + Environment.NewLine);
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Italic, null, true, resolved);
textChangelog.AppendText(Environment.NewLine);
}
if (unresolved.Length > 0)
{
var font = new Font(textChangelog.Font.FontFamily, 14.0f);
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Regular, font, false, "Issues in progress, not fully resolved:" + Environment.NewLine);
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Italic, null, true, unresolved);
textChangelog.AppendText(Environment.NewLine);
}
}
var f = new Font(textChangelog.Font.FontFamily, 14.0f, FontStyle.Underline);
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Regular, f, false, "Change details:" + Environment.NewLine + Environment.NewLine);
foreach (Commit c in commits)
{
var message = FilterMessage(c.Message);
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Bold | FontStyle.Italic, null, false, "[" + c.Author.When.ToString("yyyy-MM-dd HH:mm:ss") + "] " + message.subject);
textChangelog.AppendText(Environment.NewLine);
if (message.body != null && message.body.Length > 0)
{
textChangelog.AppendText(Environment.NewLine);
textChangelog.AppendText(message.body);
textChangelog.AppendText(Environment.NewLine);
}
#if false
var matches = GetMatches(c.Message);
if (matches != null)
{
textChangelog.AppendText(Environment.NewLine);
foreach (string match in matches)
{
if (relatedIssues.ContainsKey(match))
{
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Italic, null, true, match + ": " + relatedIssues[match].Summary + Environment.NewLine);
}
}
}
#endif
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Regular, null, false, Environment.NewLine + "----------------------------" + Environment.NewLine + Environment.NewLine);
}
AppendStyledText(textChangelog.ForeColor, textChangelog.BackColor, FontStyle.Italic, null, false, "FROM: " + from + ", TO: " + to);
}
}
finally
{
Cursor.Current = Cursors.Arrow;
EndUpdate();
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
_repo.Close();
comboBranches.Items.Clear();
comboReferenceSince.Items.Clear();
}
private void InitializeComboBoxes()
{
// Add all branches, and select current branch
comboBranches.Items.Clear();
var branches = _repo.GetBranches();
if (branches != null)
{
foreach (Branch b in branches)
{
string suffix = "";
if (b.IsCurrentRepositoryHead)
{
suffix = " (current)";
}
var obj = new ComboBoxItem(b.FriendlyName + suffix, b);
comboBranches.Items.Add(obj);
if (b.IsCurrentRepositoryHead)
{
comboBranches.SelectedItem = obj;
}
}
}
comboReferenceSince.Items.Clear();
comboReferenceSince.Text = "";
comboReferenceUntil.Items.Clear();
comboReferenceUntil.Text = "";
// Add HEAD/TAIL references
comboReferenceUntil.Items.Add(new ComboBoxItem("HEAD", _repo.Head));
comboReferenceUntil.SelectedItem = comboReferenceUntil.Items[comboReferenceUntil.Items.Count - 1];
comboReferenceSince.Items.Add(new ComboBoxItem("TAIL", null));
comboReferenceSince.SelectedItem = comboReferenceSince.Items[comboReferenceSince.Items.Count - 1];
// Add all tags as references
var tags = _repo.GetTags();
if (tags != null)
{
foreach (Tag t in tags)
{
var obj = new ComboBoxItem("Tag: " + t.FriendlyName, t);
comboReferenceSince.Items.Add(obj);
comboReferenceUntil.Items.Add(obj);
}
}
// Add all branches as references
if (branches != null)
{
foreach (Branch b in branches)
{
if (b.IsRemote)
{
var obj = new ComboBoxItem("Branch: " + b.FriendlyName, b);
comboReferenceSince.Items.Add(obj);
comboReferenceUntil.Items.Add(obj);
}
}
}
}
private void RefreshFromSettings()
{
if (!Directory.Exists(Properties.Settings.Default.GitRepositoryPath + @"\.git"))
{
MessageBox.Show("Invalid Git repository path: " + Properties.Settings.Default.GitRepositoryPath);
return;
}
try
{
var issues = new IssueTrackerJira(Properties.Settings.Default.JiraHost, Properties.Settings.Default.JiraUsername, Properties.Settings.Default.JiraPassword, Properties.Settings.Default.JiraProject);
issues.Open();
}
catch(Exception e)
{
MessageBox.Show("Invalid Jira settings :: " + e.Message);
return;
}
// Reinitialize the repository
_repo.Close();
_repo.Open(Properties.Settings.Default.GitRepositoryPath);
// Reinit Jira stuff
_issues = new IssueTrackerJira(Properties.Settings.Default.JiraHost, Properties.Settings.Default.JiraUsername, Properties.Settings.Default.JiraPassword, Properties.Settings.Default.JiraProject);
_issues.Open();
InitializeComboBoxes();
if (Properties.Settings.Default.JiraMatchPrefix != null && Properties.Settings.Default.JiraMatchPrefix.Length > 0)
{
_matchPrefix = @"(?<Identifier>" + Properties.Settings.Default.JiraMatchPrefix + ")";
}
else
{
_matchPrefix = "";
}
}
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_settings.ShowDialog() == DialogResult.OK)
{
RefreshFromSettings();
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
_about.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
internal class ComboBoxItem
{
public string FriendlyText { get; set; }
public object Value { get; set; }
public ComboBoxItem(string friendlyText, object value)
{
FriendlyText = friendlyText;
Value = value;
}
public override string ToString()
{
return FriendlyText;
}
}
}