Skip to content

Commit 5a30169

Browse files
committed
Add change-staged and change-unstaged events
Task-number: QTQAINFRA-3558 Change-Id: If88527eed80a303f53a6a8cfaa0516336ff9384a Reviewed-by: Daniel Smith <[email protected]> Reviewed-by: Paul Wicking <[email protected]>
1 parent 00a1b53 commit 5a30169

File tree

6 files changed

+105
-4
lines changed

6 files changed

+105
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright (C) 2020 The Qt Company
3+
//
4+
5+
package com.googlesource.gerrit.plugins.qtcodereview;
6+
7+
import com.google.common.base.Supplier;
8+
import com.google.gerrit.reviewdb.client.Change;
9+
import com.google.gerrit.server.data.AccountAttribute;
10+
import com.google.gerrit.server.events.PatchSetEvent;
11+
12+
public class QtChangeStagedEvent extends PatchSetEvent {
13+
public static final String TYPE = "change-staged";
14+
public Supplier<AccountAttribute> submitter;
15+
public String newRev;
16+
17+
public QtChangeStagedEvent(Change change) {
18+
super(TYPE, change);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright (C) 2020 The Qt Company
3+
//
4+
5+
package com.googlesource.gerrit.plugins.qtcodereview;
6+
7+
import com.google.common.base.Supplier;
8+
import com.google.gerrit.reviewdb.client.Change;
9+
import com.google.gerrit.server.data.AccountAttribute;
10+
import com.google.gerrit.server.events.PatchSetEvent;
11+
12+
public class QtChangeUnStagedEvent extends PatchSetEvent {
13+
public static final String TYPE = "change-unstaged";
14+
public Supplier<AccountAttribute> submitter;
15+
public String newRev;
16+
17+
public QtChangeUnStagedEvent(Change change) {
18+
super(TYPE, change);
19+
}
20+
}

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

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

55
package com.googlesource.gerrit.plugins.qtcodereview;
@@ -12,11 +12,17 @@
1212
import com.google.gerrit.extensions.registration.DynamicSet;
1313
import com.google.gerrit.extensions.restapi.RestApiModule;
1414
import com.google.gerrit.server.git.ChangeMessageModifier;
15+
import com.google.gerrit.server.events.EventTypes;
1516

1617
public class QtModule extends FactoryModule {
1718

1819
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
1920

21+
static {
22+
EventTypes.register(QtChangeStagedEvent.TYPE, QtChangeStagedEvent.class);
23+
EventTypes.register(QtChangeUnStagedEvent.TYPE, QtChangeUnStagedEvent.class);
24+
}
25+
2026
@Override
2127
protected void configure() {
2228

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

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

55
package com.googlesource.gerrit.plugins.qtcodereview;
@@ -205,6 +205,7 @@ private Change changeToStaging(RevisionResource rsrc, IdentifiedUser submitter,
205205
change = changeData.reloadChange();
206206
switch (change.getStatus()) {
207207
case STAGED:
208+
qtUtil.postChangeStagedEvent(change);
208209
logger.atInfo().log(
209210
"qtcodereview: changeToStaging %s added to %s", change, stagingBranchKey);
210211
return change; // this doesn't return data to client, if needed use ChangeJson to convert it

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

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

55
package com.googlesource.gerrit.plugins.qtcodereview;
@@ -149,6 +149,7 @@ private Change removeChangeFromStaging(RevisionResource rsrc, IdentifiedUser sub
149149
qtUtil.rebuildStagingBranch(git, submitter, projectKey, stagingBranchKey, destBranchShortKey);
150150

151151
change = op.getChange();
152+
qtUtil.postChangeUnStagedEvent(change);
152153
logger.atInfo().log("qtcodereview: unstaged %s from %s", change, stagingBranchKey);
153154

154155
} catch (ResourceConflictException e) {

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (C) 2011 The Android Open Source Project
22
// Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
3-
// Copyright (C) 2019 The Qt Company
3+
// Copyright (C) 2020 The Qt Company
44
//
55
// Licensed under the Apache License, Version 2.0 (the "License");
66
// you may not use this file except in compliance with the License.
@@ -16,16 +16,26 @@
1616

1717
package com.googlesource.gerrit.plugins.qtcodereview;
1818

19+
import com.google.common.base.Supplier;
20+
import com.google.common.base.Suppliers;
1921
import com.google.common.flogger.FluentLogger;
2022
import com.google.gerrit.common.FooterConstants;
23+
import com.google.gerrit.exceptions.StorageException;
24+
import com.google.gerrit.extensions.registration.DynamicItem;
2125
import com.google.gerrit.extensions.restapi.RestApiException;
2226
import com.google.gerrit.reviewdb.client.Branch;
2327
import com.google.gerrit.reviewdb.client.Change;
2428
import com.google.gerrit.reviewdb.client.PatchSet;
2529
import com.google.gerrit.reviewdb.client.Project;
2630
import com.google.gerrit.server.ChangeMessagesUtil;
2731
import com.google.gerrit.server.IdentifiedUser;
32+
import com.google.gerrit.server.data.ChangeAttribute;
2833
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
34+
import com.google.gerrit.server.events.ChangeEvent;
35+
import com.google.gerrit.server.events.EventDispatcher;
36+
import com.google.gerrit.server.events.EventFactory;
37+
import com.google.gerrit.server.notedb.ChangeNotes;
38+
import com.google.gerrit.server.permissions.PermissionBackendException;
2939
import com.google.gerrit.server.project.NoSuchRefException;
3040
import com.google.gerrit.server.query.change.ChangeData;
3141
import com.google.gerrit.server.query.change.InternalChangeQuery;
@@ -79,6 +89,9 @@ public class QtUtil {
7989
private final Provider<InternalChangeQuery> queryProvider;
8090
private final GitReferenceUpdated referenceUpdated;
8191
private final BatchUpdate.Factory updateFactory;
92+
private final ChangeNotes.Factory changeNotesFactory;
93+
private final DynamicItem<EventDispatcher> eventDispatcher;
94+
private final EventFactory eventFactory;
8295
private final QtCherryPickPatch qtCherryPickPatch;
8396
private final QtChangeUpdateOp.Factory qtUpdateFactory;
8497

@@ -87,11 +100,17 @@ public class QtUtil {
87100
Provider<InternalChangeQuery> queryProvider,
88101
GitReferenceUpdated referenceUpdated,
89102
BatchUpdate.Factory updateFactory,
103+
ChangeNotes.Factory changeNotesFactory,
104+
EventFactory eventFactory,
105+
DynamicItem<EventDispatcher> eventDispatcher,
90106
QtCherryPickPatch qtCherryPickPatch,
91107
QtChangeUpdateOp.Factory qtUpdateFactory) {
92108
this.queryProvider = queryProvider;
93109
this.referenceUpdated = referenceUpdated;
94110
this.updateFactory = updateFactory;
111+
this.changeNotesFactory = changeNotesFactory;
112+
this.eventDispatcher = eventDispatcher;
113+
this.eventFactory = eventFactory;
95114
this.qtCherryPickPatch = qtCherryPickPatch;
96115
this.qtUpdateFactory = qtUpdateFactory;
97116
}
@@ -642,4 +661,38 @@ private static RefUpdate.Result mergeObjectToBranch(
642661
revWalk.dispose();
643662
}
644663
}
664+
665+
private Supplier<ChangeAttribute> changeAttributeSupplier(Change change, ChangeNotes notes) {
666+
return Suppliers.memoize(
667+
() -> {
668+
try {
669+
return eventFactory.asChangeAttribute(change, notes);
670+
} catch (StorageException e) {
671+
throw new RuntimeException(e);
672+
}
673+
});
674+
}
675+
676+
public void postChangeStagedEvent(Change change) {
677+
try {
678+
ChangeNotes notes = changeNotesFactory.createChecked(change.getId());
679+
QtChangeStagedEvent event = new QtChangeStagedEvent(change);
680+
event.change = changeAttributeSupplier(change, notes);
681+
eventDispatcher.get().postEvent(event);
682+
} catch (StorageException | PermissionBackendException e) {
683+
logger.atWarning().log("qtcodereview: postChangeStagedEvent failed: %s", e);
684+
}
685+
}
686+
687+
public void postChangeUnStagedEvent(Change change) {
688+
try {
689+
ChangeNotes notes = changeNotesFactory.createChecked(change.getId());
690+
QtChangeUnStagedEvent event = new QtChangeUnStagedEvent(change);
691+
event.change = changeAttributeSupplier(change, notes);
692+
eventDispatcher.get().postEvent(event);
693+
} catch (StorageException | PermissionBackendException e) {
694+
logger.atWarning().log("qtcodereview: postChangeUnStagedEvent failed: %s", e);
695+
}
696+
}
697+
645698
}

0 commit comments

Comments
 (0)