Skip to content

Commit fe8e4b5

Browse files
committed
updated trunk-bot to plastic version 11.0.16.7701
updated trunk-bot to plastic version 11.0.16.7701
1 parent e2792b3 commit fe8e4b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+4032
-1237
lines changed

src/AttributeComment.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace TrunkBot
6+
{
7+
public class AttributeComment
8+
{
9+
public static string Build(string[] values, string botName)
10+
{
11+
string signature = "Attribute automatically created by trunk-bot: " + botName;
12+
13+
IEnumerable<string> nonEmptyDistinctAttributeValues =
14+
GetNonEmptyDistinctAttributeValues(values ?? new string[0]);
15+
16+
if (nonEmptyDistinctAttributeValues.Count() == 0)
17+
return signature;
18+
19+
return string.Concat(
20+
signature,
21+
Environment.NewLine,
22+
"default: ",
23+
string.Join(", ", nonEmptyDistinctAttributeValues));
24+
}
25+
26+
static IEnumerable<string> GetNonEmptyDistinctAttributeValues(string[] values)
27+
{
28+
return values
29+
.Select(value => ProtectValue(value))
30+
.Where(value => value != string.Empty)
31+
.Distinct();
32+
}
33+
34+
static string ProtectValue(string value)
35+
{
36+
if (value == null || value.Trim() == string.Empty)
37+
return string.Empty;
38+
39+
if (value.Contains(' ') || value.Contains(','))
40+
return string.Concat("\"", value, "\"");
41+
42+
return value;
43+
}
44+
}
45+
}

src/Branch.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
namespace TrunkBot
22
{
3-
internal class Branch
3+
public class Branch
44
{
5-
internal readonly string Repository;
6-
internal readonly string Id;
7-
internal string FullName;
8-
internal readonly string Owner;
9-
internal readonly string Comment;
5+
public readonly string Repository;
6+
public readonly int Id;
7+
public string FullName;
8+
public readonly string Owner;
9+
public readonly string Comment;
1010

11-
internal Branch(
11+
public Branch(
1212
string repository,
13-
string id,
13+
int id,
1414
string fullName,
1515
string owner,
1616
string comment)

src/BranchesQueueStorage.cs

Lines changed: 45 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,213 +1,92 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
1+
using System.Collections.Generic;
42

5-
using log4net;
3+
using Codice.LogWrapper;
64

75
namespace TrunkBot
86
{
9-
internal static class BranchesQueueStorage
7+
internal class BranchesQueueStorage
108
{
11-
internal static bool HasQueuedBranches(string filePath)
9+
internal void AddQueuedBranches(List<Branch> branches)
1210
{
13-
return GetQueuedBranches(filePath).Count > 0;
11+
lock(mLock)
12+
foreach (Branch branch in branches)
13+
mBranches.AddFirst(branch);
1414
}
1515

16-
internal static bool Contains(string repository, string branchId, string filePath)
16+
internal void EnqueueBranchIfNotAlreadyAdded(Branch branch)
1717
{
18-
List<Branch> branches = GetQueuedBranches(filePath);
19-
20-
return BranchFinder.IndexOf(branches, repository, branchId) > -1;
21-
}
22-
23-
internal static void EnqueueBranch(Branch branch, string filePath)
24-
{
25-
string line = string.Empty;
26-
try
18+
lock (mLock)
2719
{
28-
line = BranchParser.ToString(branch);
29-
30-
if (Directory.Exists(Path.GetDirectoryName(filePath)))
31-
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
32-
33-
using (StreamWriter file = new StreamWriter(filePath, true))
34-
{
35-
file.WriteLine(line);
36-
}
20+
if (BranchFinder.Find(mBranches, branch.Repository, branch.Id) != null)
21+
return;
22+
mBranches.AddFirst(branch);
3723
}
38-
catch (Exception ex)
39-
{
40-
LogException("Error saving branch '{0}' to '{1}': {2}", ex, branch.FullName, filePath);
41-
}
42-
}
43-
44-
internal static Branch DequeueBranch(string filePath)
45-
{
46-
List<Branch> queuedBranches = GetQueuedBranches(filePath);
47-
48-
if (queuedBranches.Count == 0)
49-
return null;
50-
51-
Branch dequeueBranch = queuedBranches[0];
52-
queuedBranches.RemoveAt(0);
53-
54-
WriteQueuedBranches(queuedBranches, filePath);
55-
56-
return dequeueBranch;
5724
}
5825

59-
internal static void RemoveBranch(string repository, string branchId, string filePath)
26+
internal void EnqueueBranchOnTop(Branch branch)
6027
{
61-
List<Branch> queuedBranches = GetQueuedBranches(filePath);
62-
63-
int index = BranchFinder.IndexOf(queuedBranches, repository, branchId);
64-
if (index == -1)
65-
return;
66-
67-
queuedBranches.RemoveAt(index);
68-
WriteQueuedBranches(queuedBranches, filePath);
28+
lock (mLock)
29+
mBranches.AddLast(branch);
6930
}
7031

71-
internal static void WriteQueuedBranches(IEnumerable<Branch> branches, string filePath)
32+
internal Branch DequeueBranch()
7233
{
73-
if (branches == null)
74-
return;
75-
76-
try
34+
lock (mLock)
7735
{
78-
using (StreamWriter file = new StreamWriter(filePath))
79-
{
80-
foreach (Branch branch in branches)
81-
file.WriteLine(BranchParser.ToString(branch));
82-
}
83-
}
84-
catch (Exception ex)
85-
{
86-
LogException("Error writing the queued branches to '{0}': {1}", ex, filePath);
36+
if (mBranches.Count == 0)
37+
return null;
38+
39+
Branch result = mBranches.Last.Value;
40+
mBranches.RemoveLast();
41+
return result;
8742
}
8843
}
8944

90-
static List<Branch> GetQueuedBranches(string filePath)
45+
internal Branch PeekBranch()
9146
{
92-
List<Branch> result = new List<Branch>();
93-
94-
try
47+
lock (mLock)
9548
{
96-
if (!File.Exists(filePath))
97-
return result;
49+
if (mBranches.Count == 0)
50+
return null;
9851

99-
foreach (string line in File.ReadAllLines(filePath))
100-
{
101-
Branch branch;
102-
if (!BranchParser.TryParse(line, out branch))
103-
{
104-
mLog.ErrorFormat("Malformed line while reading branches file: {0}", line);
105-
continue;
106-
}
107-
result.Add(branch);
108-
}
109-
}
110-
catch (Exception ex)
111-
{
112-
LogException("Error reading the queued branches to '{0}': {1}", ex, filePath);
52+
return mBranches.Last.Value;
11353
}
114-
115-
return result;
116-
}
117-
118-
static void LogException(string message, Exception e, params string[] args)
119-
{
120-
mLog.ErrorFormat(message, args, e.Message);
121-
mLog.DebugFormat("StackTrace:{0}{1}", Environment.NewLine, e.StackTrace);
12254
}
12355

124-
static class BranchParser
56+
internal void RemoveBranch(string repository, int branchId)
12557
{
126-
internal static bool TryParse(string line, out Branch branch)
127-
{
128-
branch = null;
129-
130-
if (string.IsNullOrEmpty(line))
131-
return false;
132-
133-
string[] fields = ReadFieldsWithFinalComment(line, 5, SEPARATOR);
134-
if (fields == null)
135-
return false;
136-
137-
branch = new Branch(fields[0], fields[1], fields[2], fields[3], fields[4]);
138-
return true;
139-
}
140-
141-
internal static string ToString(Branch branch)
142-
{
143-
string comment = branch.Comment.
144-
Replace("\r\n", " ").Replace("\r", " ").Replace("\n", " ").Trim();
145-
146-
return string.Format("{0}|{1}|{2}|{3}|{4}",
147-
branch.Repository, branch.Id, branch.FullName, branch.Owner, comment);
148-
}
149-
150-
static string[] ReadFieldsWithFinalComment(
151-
string line, int count, string separator)
152-
{
153-
string pendingToProcess = line;
154-
155-
string[] result = new string[count];
156-
157-
for (int i = 0; i < count - 1; i++)
158-
{
159-
string field = GetNextField(ref pendingToProcess, separator);
160-
if (field == null)
161-
return null;
162-
163-
result[i] = field;
164-
}
165-
166-
result[count - 1] = pendingToProcess;
167-
return result;
168-
}
169-
170-
static string GetNextField(ref string contentToProcess, string separator)
58+
lock (mLock)
17159
{
172-
int index = contentToProcess.IndexOf(separator);
173-
if (index == -1)
174-
{
175-
contentToProcess = string.Empty;
176-
return null;
177-
}
178-
179-
string result = contentToProcess.Substring(0, index);
180-
if (index == contentToProcess.Length)
181-
contentToProcess = string.Empty;
182-
else
183-
contentToProcess = contentToProcess.Substring(index + 1);
184-
185-
return result;
60+
Branch branch = BranchFinder.Find(mBranches, repository, branchId);
61+
if (branch == null)
62+
return;
63+
mBranches.Remove(branch);
18664
}
187-
188-
const string SEPARATOR = "|";
18965
}
19066

19167
static class BranchFinder
19268
{
193-
internal static int IndexOf(
194-
List<Branch> branches, string repository, string branchId)
69+
internal static Branch Find(
70+
LinkedList<Branch> branches, string repository, int branchId)
19571
{
196-
for (int i = 0; i < branches.Count; i++)
72+
foreach(Branch branch in branches)
19773
{
198-
if (!RepositoryNameComparer.IsSameName(branches[i].Repository, repository))
74+
if (branch.Id != branchId)
19975
continue;
20076

201-
if (!branches[i].Id.Equals(branchId))
77+
if (!RepositoryNameComparer.IsSameName(branch.Repository, repository))
20278
continue;
20379

204-
return i;
80+
return branch;
20581
}
20682

207-
return -1;
83+
return null;
20884
}
20985
}
21086

211-
static readonly ILog mLog = LogManager.GetLogger("BranchesQueueStorage");
87+
LinkedList<Branch> mBranches = new LinkedList<Branch>();
88+
object mLock = new object();
89+
90+
static readonly ILog mLog = LogManager.GetLogger("trunkbot");
21291
}
21392
}

src/BuildMergeReport.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
using System;
22
using System.Collections.Generic;
33

4-
using TrunkBot.Api.Requests;
5-
using TrunkBot.Api.Responses;
4+
using Codice.CM.Server.Devops;
65

76
namespace TrunkBot
87
{
98
internal static class BuildMergeReport
109
{
11-
internal static MergeReport Build(BranchModel branch)
10+
internal static MergeReport Build(string repId, int branchId)
1211
{
1312
MergeReport result = new MergeReport();
1413
result.Timestamp = DateTime.UtcNow;
15-
result.RepositoryId = branch.RepositoryId;
16-
result.BranchId = branch.Id;
14+
result.RepositoryId = repId;
15+
result.BranchId = branchId;
1716
result.Properties = new List<MergeReport.Entry>();
1817
return result;
1918
}

0 commit comments

Comments
 (0)