Skip to content

Commit 765d16c

Browse files
jujokinigladhorn
authored andcommitted
Add Stage functionality
Stage button and search added to UI. Also corresponding REST API added. Fixes: QTBI-1402 Change-Id: I49e6999813aa48d142b885a1713f54b8d559286c Reviewed-by: Frederik Gladhorn <[email protected]>
1 parent 3019b11 commit 765d16c

File tree

11 files changed

+1183
-14
lines changed

11 files changed

+1183
-14
lines changed

qt-gerrit-ui-plugin/qt-gerrit-ui-plugin.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
'gerrit-plugin-qt-workflow~reopen' : {
2121
header: 'Reopen the change?',
2222
action_name: 'reopen'
23+
},
24+
'gerrit-plugin-qt-workflow~stage' : {
25+
header: 'Submit to staging?',
26+
action_name: 'stage'
2327
}
2428
};
2529

@@ -72,11 +76,18 @@
7276
var li_elem;
7377
var link_elem;
7478

79+
li_elem = htmlToElement(ul_elem.children[1].outerHTML);
80+
link_elem = li_elem.children[0].children[1];
81+
link_elem.text = 'Staged';
82+
link_elem.href = '/q/status:staged';
83+
ul_elem.insertBefore(li_elem, ul_elem.children[2]);
84+
85+
7586
li_elem = htmlToElement(ul_elem.children[1].outerHTML);
7687
link_elem = li_elem.children[0].children[1];
7788
link_elem.text = 'Deferred';
7889
link_elem.href = '/q/status:deferred';
79-
ul_elem.insertBefore(li_elem, ul_elem.children[3]);
90+
ul_elem.insertBefore(li_elem, ul_elem.children[4]);
8091
});
8192

8293
// Customize change view

src/main/java/com/googlesource/gerrit/plugins/qtcodereview/QtAbandon.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (C) 2018 The Qt Company
2+
// Copyright (C) 2019 The Qt Company
33
//
44

55
package com.googlesource.gerrit.plugins.qtcodereview;
@@ -81,7 +81,8 @@ protected ChangeInfo applyImpl(BatchUpdate.Factory updateFactory,
8181
QtChangeUpdateOp op = qtUpdateFactory.create(Change.Status.ABANDONED,
8282
"Abandoned",
8383
input.message,
84-
ChangeMessagesUtil.TAG_ABANDON);
84+
ChangeMessagesUtil.TAG_ABANDON,
85+
null);
8586
try (BatchUpdate u = updateFactory.create(dbProvider.get(), change.getProject(), rsrc.getUser(), TimeUtil.nowTs())) {
8687
u.addOp(rsrc.getId(), op).execute();
8788
}

src/main/java/com/googlesource/gerrit/plugins/qtcodereview/QtChangeUpdateOp.java

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,76 @@
11
//
2-
// Copyright (C) 2018 The Qt Company
2+
// Copyright (C) 2019 The Qt Company
33
//
44

55
package com.googlesource.gerrit.plugins.qtcodereview;
66

77
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;
811
import com.google.gerrit.common.Nullable;
912
import com.google.gerrit.extensions.restapi.ResourceConflictException;
1013
import com.google.gerrit.reviewdb.client.Change;
1114
import com.google.gerrit.reviewdb.client.ChangeMessage;
15+
import com.google.gerrit.reviewdb.client.LabelId;
1216
import com.google.gerrit.reviewdb.client.PatchSet;
17+
import com.google.gerrit.reviewdb.client.PatchSetApproval;
18+
import com.google.gerrit.server.ApprovalsUtil;
1319
import com.google.gerrit.server.ChangeMessagesUtil;
20+
import com.google.gerrit.server.git.CodeReviewCommit;
21+
import com.google.gerrit.server.change.LabelNormalizer;
1422
import com.google.gerrit.server.notedb.ChangeUpdate;
1523
import com.google.gerrit.server.update.BatchUpdateOp;
1624
import com.google.gerrit.server.update.ChangeContext;
1725
import com.google.gwtorm.server.OrmException;
1826
import com.google.inject.Inject;
1927
import com.google.inject.assistedinject.Assisted;
2028
import java.io.IOException;
29+
import java.util.HashMap;
30+
import java.util.Map;
2131

2232
public class QtChangeUpdateOp implements BatchUpdateOp {
2333

2434
public interface Factory {
2535
QtChangeUpdateOp create(Change.Status newStatus,
2636
@Assisted("defaultMessage") String defaultMessage,
2737
@Assisted("inputMessage") String inputMessage,
28-
@Assisted("tag") String tag);
38+
@Assisted("tag") String tag,
39+
CodeReviewCommit copyApprovalsFrom);
2940
}
3041

42+
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
43+
3144
private final Change.Status newStatus;
3245
private final String defaultMessage;
3346
private final String inputMessage;
3447
private final String tag;
48+
private CodeReviewCommit copyApprovalsFrom;
3549

3650
private Change change;
51+
private PatchSetApproval submitter;
3752

3853
private final ChangeMessagesUtil cmUtil;
39-
54+
private final ApprovalsUtil approvalsUtil;
55+
private final LabelNormalizer labelNormalizer;
4056

4157
@Inject
4258
QtChangeUpdateOp(ChangeMessagesUtil cmUtil,
59+
ApprovalsUtil approvalsUtil,
60+
LabelNormalizer labelNormalizer,
4361
@Nullable @Assisted Change.Status newStatus,
4462
@Nullable @Assisted("defaultMessage") String defaultMessage,
4563
@Nullable @Assisted("inputMessage") String inputMessage,
46-
@Nullable @Assisted("tag") String tag) {
64+
@Nullable @Assisted("tag") String tag,
65+
@Nullable @Assisted CodeReviewCommit copyApprovalsFrom) {
4766
this.cmUtil = cmUtil;
67+
this.approvalsUtil = approvalsUtil;
68+
this.labelNormalizer = labelNormalizer;
4869
this.newStatus = newStatus;
4970
this.defaultMessage = defaultMessage;
5071
this.inputMessage = inputMessage;
5172
this.tag = tag;
73+
this.copyApprovalsFrom = copyApprovalsFrom;
5274
}
5375

5476
public Change getChange() {
@@ -64,6 +86,22 @@ public boolean updateChange(ChangeContext ctx) throws OrmException, IOException,
6486

6587
if (newStatus != null) {
6688
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);
67105
updated = true;
68106
}
69107

@@ -89,4 +127,72 @@ private ChangeMessage newMessage(ChangeContext ctx) {
89127
return ChangeMessagesUtil.newMessage(ctx, msg.toString(), tag);
90128
}
91129

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+
92198
}

0 commit comments

Comments
 (0)