Skip to content

Commit e2792b3

Browse files
committed
updated trunk-bot to plastic version 8.0.16.3685
1 parent eddd705 commit e2792b3

29 files changed

+1343
-130
lines changed

src/Branch.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ internal Branch(
2222
Comment = comment;
2323
}
2424
}
25+
26+
internal class BranchWithReview
27+
{
28+
internal Branch Branch;
29+
internal Review Review;
30+
}
2531
}

src/BranchesQueueStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ internal static int IndexOf(
195195
{
196196
for (int i = 0; i < branches.Count; i++)
197197
{
198-
if (!branches[i].Repository.Equals(repository))
198+
if (!RepositoryNameComparer.IsSameName(branches[i].Repository, repository))
199199
continue;
200200

201201
if (!branches[i].Id.Equals(branchId))

src/ChangeTaskStatus.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
using log4net;
45

@@ -52,10 +53,14 @@ internal static void SetTaskAsFailed(
5253
Branch branch,
5354
string taskNumber,
5455
string message,
55-
TrunkBotConfiguration botConfig)
56+
TrunkBotConfiguration botConfig,
57+
string codeReviewsStorageFile)
5658
{
5759
try
5860
{
61+
if (botConfig.Plastic.IsApprovedCodeReviewFilterEnabled)
62+
SetBranchReviewsAsPending(restApi, branch.Repository, branch.Id, codeReviewsStorageFile);
63+
5964
TrunkMergebotApi.ChangeBranchAttribute(
6065
restApi, branch.Repository, branch.FullName,
6166
botConfig.Plastic.StatusAttribute.Name,
@@ -86,10 +91,17 @@ internal static void SetTaskAsMerged(
8691
Branch branch,
8792
string taskNumber,
8893
string message,
89-
TrunkBotConfiguration botConfig)
94+
TrunkBotConfiguration botConfig,
95+
string codeReviewsStorageFile)
9096
{
9197
try
9298
{
99+
if (botConfig.Plastic.IsApprovedCodeReviewFilterEnabled)
100+
{
101+
ReviewsStorage.DeleteBranchReviews(
102+
branch.Repository, branch.Id, codeReviewsStorageFile);
103+
}
104+
93105
TrunkMergebotApi.ChangeBranchAttribute(
94106
restApi, branch.Repository, branch.FullName,
95107
botConfig.Plastic.StatusAttribute.Name,
@@ -115,6 +127,27 @@ internal static void SetTaskAsMerged(
115127
}
116128
}
117129

130+
static void SetBranchReviewsAsPending(
131+
RestApi restApi,
132+
string repoName,
133+
string branchId,
134+
string codeReviewsStorageFile)
135+
{
136+
List<Review> branchReviews = ReviewsStorage.GetBranchReviews(
137+
repoName, branchId, codeReviewsStorageFile);
138+
139+
foreach (Review review in branchReviews)
140+
{
141+
TrunkMergebotApi.CodeReviews.Update(
142+
restApi,
143+
repoName,
144+
review.ReviewId,
145+
Review.PENDING_STATUS_ID,
146+
review.ReviewTitle);
147+
}
148+
149+
}
150+
118151
static readonly ILog mLog = LogManager.GetLogger("trunkbot");
119152
}
120153
}

src/FindQueries.cs

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ internal static List<Branch> FindResolvedBranches(
3535
string resolvedStatusAttributeValue)
3636
{
3737
string query = string.Format(
38-
"branch where ( name like '{0}%' or name like '{1}%' ) " +
39-
"and date > '{2}' and attribute='{3}' and ( attrvalue='{4}' or attrvalue='{5}')",
38+
"branch where ( name like '{0}%' or name like '{1}%' or name like '{2}%' ) " +
39+
"and date > '{3}' " +
40+
"and attribute='{4}' and ( attrvalue='{5}' or attrvalue='{6}' or attrvalue='{7}') ",
41+
prefix,
4042
prefix.ToLowerInvariant(),
4143
prefix.ToUpperInvariant(),
4244
DateTime.Now.AddYears(-1).ToString(DATE_FORMAT),
4345
statusAttributeName,
46+
resolvedStatusAttributeValue,
4447
resolvedStatusAttributeValue.ToLowerInvariant(),
4548
resolvedStatusAttributeValue.ToUpperInvariant());
4649

@@ -65,6 +68,98 @@ internal static List<Branch> FindResolvedBranches(
6568
return result;
6669
}
6770

71+
internal static List<BranchWithReview> FindPendingBranchesWithReviews(
72+
RestApi restApi,
73+
string repository,
74+
string prefix,
75+
string statusAttributeName,
76+
string mergedStatusAttributeValue)
77+
{
78+
string reviewTypeConditionClause = string.Empty;
79+
80+
//branches from a year ago matching with prefix with status!=merged (even those without any status set)
81+
string branchTypeConditionClause = string.Format(
82+
"( " +
83+
" name like '{0}%' or name like '{1}%' or name like '{2}%' " +
84+
") " +
85+
"and " +
86+
"( " +
87+
" date > '{3}' " +
88+
") " +
89+
"and " +
90+
"( " +
91+
" (not attribute='{4}') or " +
92+
" (attribute='{4}' and not ( attrvalue='{5}' or attrvalue='{6}' or attrvalue='{7}' )) " +
93+
") ",
94+
prefix,
95+
prefix.ToUpperInvariant(),
96+
prefix.ToLowerInvariant(),
97+
DateTime.Now.AddYears(-1).ToString(DATE_FORMAT),
98+
statusAttributeName,
99+
mergedStatusAttributeValue,
100+
mergedStatusAttributeValue.ToUpperInvariant(),
101+
mergedStatusAttributeValue.ToLowerInvariant());
102+
103+
string[] outputFields = new string[]
104+
{"branchid", "branchname","branchowner","branchcomment",
105+
"reviewid","reviewtargetid","reviewstatus","reviewtitle"};
106+
107+
JArray findResult = TrunkMergebotApi.FindBranchesWithReviews(
108+
restApi,
109+
repository,
110+
reviewTypeConditionClause,
111+
branchTypeConditionClause,
112+
DATE_FORMAT,
113+
"retrieve the list of branches with reviews to process",
114+
outputFields);
115+
116+
List<BranchWithReview> result = new List<BranchWithReview>();
117+
Branch branch = null;
118+
Review review = null;
119+
120+
foreach (JObject obj in findResult)
121+
{
122+
branch = new Branch(
123+
repository,
124+
GetStringValue(obj, "branchid"),
125+
GetStringValue(obj, "branchname"),
126+
GetStringValue(obj, "branchowner"),
127+
GetStringValue(obj, "branchcomment"));
128+
129+
review = new Review(
130+
repository,
131+
GetStringValue(obj, "reviewid"),
132+
GetStringValue(obj, "reviewtargetid"),
133+
TranslateCodeReviewStatus(GetStringValue(obj, "reviewstatus")),
134+
GetStringValue(obj, "reviewtitle"));
135+
136+
result.Add(new BranchWithReview()
137+
{ Branch = branch,
138+
Review = review
139+
});
140+
}
141+
142+
return result;
143+
}
144+
145+
internal static bool ExistsAttributeName(
146+
RestApi restApi,
147+
string repository,
148+
string attributeName)
149+
{
150+
string query = string.Format("attributetype where name='{0}' ", attributeName);
151+
152+
JArray findResult = TrunkMergebotApi.Find(
153+
restApi,
154+
repository,
155+
query,
156+
DATE_FORMAT,
157+
"retrieve the list of attributes named " + attributeName,
158+
new string[] { "name" });
159+
160+
return findResult != null && findResult.Count > 0;
161+
}
162+
68163
internal static Label FindMostRecentLabel(
69164
RestApi restApi,
70165
string repository,
@@ -123,6 +218,18 @@ static string GetStringValue(JObject obj, string fieldName)
123218
return value == null ? string.Empty : value.ToString();
124219
}
125220

221+
static string TranslateCodeReviewStatus(string reviewStatusId)
222+
{
223+
if (string.IsNullOrEmpty(reviewStatusId))
224+
return string.Empty;
225+
226+
int parsedInt = -1;
227+
if (!int.TryParse(reviewStatusId, out parsedInt))
228+
return reviewStatusId;
229+
230+
return Review.ParseStatusId(parsedInt);
231+
}
232+
126233
const string DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
127234
}
128235
}

src/MergeToOperations.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal static bool TryMergeToShelve(
1515
string comment,
1616
string taskNumber,
1717
TrunkBotConfiguration botConfig,
18+
string codeReviewsStorageFile,
1819
out int shelveId)
1920
{
2021
shelveId = -1;
@@ -23,6 +24,21 @@ internal static bool TryMergeToShelve(
2324
restApi, branch.Repository, branch.FullName, destinationBranch,
2425
comment, TrunkMergebotApi.MergeToOptions.CreateShelve);
2526

27+
if (result.Status == MergeToResultStatus.MergeNotNeeded)
28+
{
29+
ChangeTaskStatus.SetTaskAsMerged(
30+
restApi,
31+
branch,
32+
taskNumber,
33+
string.Format(
34+
"Branch {0} was already merged to {1} (MergeNotNeeded).",
35+
branch.FullName,
36+
botConfig.TrunkBranch),
37+
botConfig,
38+
codeReviewsStorageFile);
39+
return false;
40+
}
41+
2642
if (result.Status == MergeToResultStatus.AncestorNotFound ||
2743
result.Status == MergeToResultStatus.Conflicts ||
2844
result.Status == MergeToResultStatus.Error ||
@@ -36,27 +52,14 @@ internal static bool TryMergeToShelve(
3652
string.Format(
3753
"Can't merge branch {0}. Reason: {1}",
3854
branch.FullName, result.Message),
39-
botConfig);
55+
botConfig,
56+
codeReviewsStorageFile);
4057
return false;
4158
}
4259

4360
shelveId = result.ChangesetNumber;
4461
BuildMergeReport.AddSucceededMergeProperty(mergeReport, result.Status);
4562

46-
if (result.Status == MergeToResultStatus.MergeNotNeeded)
47-
{
48-
ChangeTaskStatus.SetTaskAsMerged(
49-
restApi,
50-
branch,
51-
taskNumber,
52-
string.Format(
53-
"Branch {0} was already merged to {1} (MergeNotNeeded).",
54-
branch.FullName,
55-
botConfig.TrunkBranch),
56-
botConfig);
57-
return false;
58-
}
59-
6063
return true;
6164
}
6265

@@ -69,6 +72,7 @@ internal static bool TryApplyShelve(
6972
string taskNumber,
7073
int shelveId,
7174
TrunkBotConfiguration botConfig,
75+
string codeReviewsStorageFile,
7276
out int csetId)
7377
{
7478
MergeToResponse mergeResult = TrunkMergebotApi.MergeShelveTo(
@@ -96,7 +100,8 @@ internal static bool TryApplyShelve(
96100
"Can't merge branch {0}. Reason: {1}",
97101
branch.FullName,
98102
mergeResult.Message),
99-
botConfig);
103+
botConfig,
104+
codeReviewsStorageFile);
100105

101106
return false;
102107
}

src/Notifier.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,14 @@ static List<string> GetNotificationRecipients(
5757
string owner,
5858
TrunkBotConfiguration.Notifier notificationsConfig)
5959
{
60-
List<string> result = new List<string>();
61-
62-
string recipientForOwner = ResolveUserProfile.ResolveField(
63-
restApi, owner, notificationsConfig.UserProfileField);
64-
65-
if (!string.IsNullOrEmpty(recipientForOwner))
66-
result.Add(recipientForOwner);
60+
List<string> recipients = new List<string>();
61+
recipients.Add(owner);
6762

6863
if (notificationsConfig.FixedRecipients != null)
69-
result.AddRange(notificationsConfig.FixedRecipients);
64+
recipients.AddRange(notificationsConfig.FixedRecipients);
7065

71-
return result;
66+
return ResolveUserProfile.ResolveFieldForUsers(
67+
restApi, recipients, notificationsConfig.UserProfileField);
7268
}
7369

7470
static readonly ILog mLog = LogManager.GetLogger("notifier");

0 commit comments

Comments
 (0)