1
1
//
2
- // Copyright (C) 2018 The Qt Company
2
+ // Copyright (C) 2019 The Qt Company
3
3
//
4
4
5
5
package com .googlesource .gerrit .plugins .qtcodereview ;
6
6
7
7
import com .google .common .base .Strings ;
8
+ import com .google .common .base .Function ;
9
+ import com .google .common .collect .Iterables ;
10
+ import com .google .common .flogger .FluentLogger ;
8
11
import com .google .gerrit .common .Nullable ;
9
12
import com .google .gerrit .extensions .restapi .ResourceConflictException ;
10
13
import com .google .gerrit .reviewdb .client .Change ;
11
14
import com .google .gerrit .reviewdb .client .ChangeMessage ;
15
+ import com .google .gerrit .reviewdb .client .LabelId ;
12
16
import com .google .gerrit .reviewdb .client .PatchSet ;
17
+ import com .google .gerrit .reviewdb .client .PatchSetApproval ;
18
+ import com .google .gerrit .server .ApprovalsUtil ;
13
19
import com .google .gerrit .server .ChangeMessagesUtil ;
20
+ import com .google .gerrit .server .git .CodeReviewCommit ;
21
+ import com .google .gerrit .server .change .LabelNormalizer ;
14
22
import com .google .gerrit .server .notedb .ChangeUpdate ;
15
23
import com .google .gerrit .server .update .BatchUpdateOp ;
16
24
import com .google .gerrit .server .update .ChangeContext ;
17
25
import com .google .gwtorm .server .OrmException ;
18
26
import com .google .inject .Inject ;
19
27
import com .google .inject .assistedinject .Assisted ;
20
28
import java .io .IOException ;
29
+ import java .util .HashMap ;
30
+ import java .util .Map ;
21
31
22
32
public class QtChangeUpdateOp implements BatchUpdateOp {
23
33
24
34
public interface Factory {
25
35
QtChangeUpdateOp create (Change .Status newStatus ,
26
36
@ Assisted ("defaultMessage" ) String defaultMessage ,
27
37
@ Assisted ("inputMessage" ) String inputMessage ,
28
- @ Assisted ("tag" ) String tag );
38
+ @ Assisted ("tag" ) String tag ,
39
+ CodeReviewCommit copyApprovalsFrom );
29
40
}
30
41
42
+ private static final FluentLogger logger = FluentLogger .forEnclosingClass ();
43
+
31
44
private final Change .Status newStatus ;
32
45
private final String defaultMessage ;
33
46
private final String inputMessage ;
34
47
private final String tag ;
48
+ private CodeReviewCommit copyApprovalsFrom ;
35
49
36
50
private Change change ;
51
+ private PatchSetApproval submitter ;
37
52
38
53
private final ChangeMessagesUtil cmUtil ;
39
-
54
+ private final ApprovalsUtil approvalsUtil ;
55
+ private final LabelNormalizer labelNormalizer ;
40
56
41
57
@ Inject
42
58
QtChangeUpdateOp (ChangeMessagesUtil cmUtil ,
59
+ ApprovalsUtil approvalsUtil ,
60
+ LabelNormalizer labelNormalizer ,
43
61
@ Nullable @ Assisted Change .Status newStatus ,
44
62
@ Nullable @ Assisted ("defaultMessage" ) String defaultMessage ,
45
63
@ Nullable @ Assisted ("inputMessage" ) String inputMessage ,
46
- @ Nullable @ Assisted ("tag" ) String tag ) {
64
+ @ Nullable @ Assisted ("tag" ) String tag ,
65
+ @ Nullable @ Assisted CodeReviewCommit copyApprovalsFrom ) {
47
66
this .cmUtil = cmUtil ;
67
+ this .approvalsUtil = approvalsUtil ;
68
+ this .labelNormalizer = labelNormalizer ;
48
69
this .newStatus = newStatus ;
49
70
this .defaultMessage = defaultMessage ;
50
71
this .inputMessage = inputMessage ;
51
72
this .tag = tag ;
73
+ this .copyApprovalsFrom = copyApprovalsFrom ;
52
74
}
53
75
54
76
public Change getChange () {
@@ -64,6 +86,22 @@ public boolean updateChange(ChangeContext ctx) throws OrmException, IOException,
64
86
65
87
if (newStatus != null ) {
66
88
change .setStatus (newStatus );
89
+ update .fixStatus (newStatus );
90
+ updated = true ;
91
+ }
92
+
93
+ if (copyApprovalsFrom != null ) {
94
+ Change .Id id = ctx .getChange ().getId ();
95
+ PatchSet .Id oldPsId = copyApprovalsFrom .getPatchsetId ();
96
+
97
+ logger .atFine ().log ("Copy approval for %s oldps=%s newps=%s" , id , oldPsId , psId );
98
+ ChangeUpdate origPsUpdate = ctx .getUpdate (oldPsId );
99
+ LabelNormalizer .Result normalized = approve (ctx , origPsUpdate );
100
+
101
+ ChangeUpdate newPsUpdate = ctx .getUpdate (psId );
102
+
103
+ saveApprovals (normalized , ctx , newPsUpdate , true );
104
+ submitter = convertPatchSet (psId ).apply (submitter );
67
105
updated = true ;
68
106
}
69
107
@@ -89,4 +127,72 @@ private ChangeMessage newMessage(ChangeContext ctx) {
89
127
return ChangeMessagesUtil .newMessage (ctx , msg .toString (), tag );
90
128
}
91
129
130
+ private LabelNormalizer .Result approve (ChangeContext ctx , ChangeUpdate update )
131
+ throws OrmException , IOException {
132
+ PatchSet .Id psId = update .getPatchSetId ();
133
+ Map <PatchSetApproval .Key , PatchSetApproval > byKey = new HashMap <>();
134
+ for (PatchSetApproval psa : approvalsUtil .byPatchSet (ctx .getDb (),
135
+ ctx .getNotes (),
136
+ psId ,
137
+ ctx .getRevWalk (),
138
+ ctx .getRepoView ().getConfig ())) {
139
+ byKey .put (psa .getKey (), psa );
140
+ }
141
+
142
+ submitter = ApprovalsUtil .newApproval (psId , ctx .getUser (), LabelId .legacySubmit (), 1 , ctx .getWhen ());
143
+ byKey .put (submitter .getKey (), submitter );
144
+
145
+ LabelNormalizer .Result normalized = labelNormalizer .normalize (ctx .getNotes (), byKey .values ());
146
+ update .putApproval (submitter .getLabel (), submitter .getValue ());
147
+ saveApprovals (normalized , ctx , update , false );
148
+ return normalized ;
149
+ }
150
+
151
+ private void saveApprovals (LabelNormalizer .Result normalized ,
152
+ ChangeContext ctx ,
153
+ ChangeUpdate update ,
154
+ boolean includeUnchanged )
155
+ throws OrmException {
156
+ PatchSet .Id psId = update .getPatchSetId ();
157
+ ctx .getDb ().patchSetApprovals ().upsert (convertPatchSet (normalized .getNormalized (), psId ));
158
+ ctx .getDb ().patchSetApprovals ().upsert (zero (convertPatchSet (normalized .deleted (), psId )));
159
+ for (PatchSetApproval psa : normalized .updated ()) {
160
+ update .putApprovalFor (psa .getAccountId (), psa .getLabel (), psa .getValue ());
161
+ }
162
+ for (PatchSetApproval psa : normalized .deleted ()) {
163
+ update .removeApprovalFor (psa .getAccountId (), psa .getLabel ());
164
+ }
165
+
166
+ for (PatchSetApproval psa : normalized .unchanged ()) {
167
+ if (includeUnchanged || psa .isLegacySubmit ()) {
168
+ logger .atFine ().log ("Adding submit label %s" , psa );
169
+ update .putApprovalFor (psa .getAccountId (), psa .getLabel (), psa .getValue ());
170
+ }
171
+ }
172
+ }
173
+
174
+ private Function <PatchSetApproval , PatchSetApproval > convertPatchSet (final PatchSet .Id psId ) {
175
+ return psa -> {
176
+ if (psa .getPatchSetId ().equals (psId )) {
177
+ return psa ;
178
+ }
179
+ return new PatchSetApproval (psId , psa );
180
+ };
181
+ }
182
+
183
+ private Iterable <PatchSetApproval > convertPatchSet (Iterable <PatchSetApproval > approvals , PatchSet .Id psId ) {
184
+ return Iterables .transform (approvals , convertPatchSet (psId ));
185
+ }
186
+
187
+ private Iterable <PatchSetApproval > zero (Iterable <PatchSetApproval > approvals ) {
188
+ return Iterables .transform (
189
+ approvals ,
190
+ a -> {
191
+ PatchSetApproval copy = new PatchSetApproval (a .getPatchSetId (), a );
192
+ copy .setValue ((short ) 0 );
193
+ return copy ;
194
+ }
195
+ );
196
+ }
197
+
92
198
}
0 commit comments