Skip to content

Commit 6db6864

Browse files
committed
Use pull request owner identity for Change comments.
When creating a new Change OR adding a new PatchSet to an existing Change, the Gerrit comment is owned by the pull request owner rather than from the Gerrit logged in user. Change-Id: I45f261ea7fba8d305204102c22cf50fc7f221103
1 parent aabfaf4 commit 6db6864

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

github-plugin/src/main/java/com/googlesrouce/gerrit/plugins/github/git/PullRequestCreateChange.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.google.gerrit.server.ChangeUtil;
4747
import com.google.gerrit.server.GerritPersonIdent;
4848
import com.google.gerrit.server.IdentifiedUser;
49+
import com.google.gerrit.server.IdentifiedUser.GenericFactory;
4950
import com.google.gerrit.server.change.ChangeInserter;
5051
import com.google.gerrit.server.change.PatchSetInserter;
5152
import com.google.gerrit.server.change.PatchSetInserter.ValidatePolicy;
@@ -76,8 +77,8 @@ public class PullRequestCreateChange {
7677
private final ChangeInserter.Factory changeInserterFactory;
7778
final MergeUtil.Factory mergeUtilFactory;
7879
private final PatchSetInserter.Factory patchSetInserterFactory;
79-
80-
private Factory projectControlFactor;
80+
private final Factory projectControlFactor;
81+
private final GenericFactory userFactory;
8182

8283

8384
@Inject
@@ -86,22 +87,23 @@ public class PullRequestCreateChange {
8687
final ChangeInserter.Factory changeInserterFactory,
8788
final MergeUtil.Factory mergeUtilFactory,
8889
final PatchSetInserter.Factory patchSetInserterFactory,
89-
final ProjectControl.Factory projectControlFactory) {
90+
final ProjectControl.Factory projectControlFactory,
91+
final IdentifiedUser.GenericFactory userFactory) {
9092
this.currentUser = currentUser;
9193
this.commitValidatorsFactory = commitValidatorsFactory;
9294
this.changeInserterFactory = changeInserterFactory;
9395
this.mergeUtilFactory = mergeUtilFactory;
9496
this.patchSetInserterFactory = patchSetInserterFactory;
9597
this.projectControlFactor = projectControlFactory;
98+
this.userFactory = userFactory;
9699
}
97100

98101
public Change.Id addCommitToChange(final ReviewDb db, final Project project,
99102
final Repository git, final String destinationBranch,
100-
final Account.Id pullRequestOwner,
101-
final RevCommit pullRequestCommit, final String pullRequestMesage,
102-
final String topic, boolean doValidation) throws NoSuchChangeException,
103-
EmailException, OrmException, MissingObjectException,
104-
IncorrectObjectTypeException, IOException,
103+
final Account.Id pullRequestOwner, final RevCommit pullRequestCommit,
104+
final String pullRequestMesage, final String topic, boolean doValidation)
105+
throws NoSuchChangeException, EmailException, OrmException,
106+
MissingObjectException, IncorrectObjectTypeException, IOException,
105107
InvalidChangeOperationException, MergeException, NoSuchProjectException {
106108
Id newChange = null;
107109
if (destinationBranch == null || destinationBranch.length() == 0) {
@@ -165,20 +167,21 @@ public Change.Id addCommitToChange(final ReviewDb db, final Project project,
165167
// patch-set
166168
Change destChange = destChanges.get(0);
167169
return insertPatchSet(git, revWalk, destChange, pullRequestCommit,
168-
refControl, pullRequestMesage, doValidation);
170+
refControl, pullRequestOwner, pullRequestMesage, doValidation);
169171
} else {
170172
// Change key not found on destination branch. We can create a new
171173
// change.
172174
return (newChange =
173175
createNewChange(db, git, revWalk, changeKey,
174-
project.getNameKey(), destRef, pullRequestOwner, pullRequestCommit, refControl,
175-
pullRequestMesage, topic, doValidation));
176+
project.getNameKey(), destRef, pullRequestOwner,
177+
pullRequestCommit, refControl, pullRequestMesage, topic,
178+
doValidation));
176179
}
177180
} finally {
178181
revWalk.release();
179182
if (newChange == null) {
180183
db.rollback();
181-
}
184+
}
182185
}
183186
} finally {
184187
git.close();
@@ -187,12 +190,12 @@ public Change.Id addCommitToChange(final ReviewDb db, final Project project,
187190

188191
private Change.Id insertPatchSet(Repository git, RevWalk revWalk,
189192
Change change, RevCommit cherryPickCommit, RefControl refControl,
190-
String pullRequestMessage, boolean doValidation)
191-
throws InvalidChangeOperationException, IOException, OrmException,
192-
NoSuchChangeException {
193+
Account.Id pullRequestOwnerId, String pullRequestMessage,
194+
boolean doValidation) throws InvalidChangeOperationException,
195+
IOException, OrmException, NoSuchChangeException {
193196
PatchSetInserter patchSetInserter =
194-
patchSetInserterFactory.create(git, revWalk, refControl, currentUser,
195-
change, cherryPickCommit);
197+
patchSetInserterFactory.create(git, revWalk, refControl,
198+
userFactory.create(pullRequestOwnerId), change, cherryPickCommit);
196199
// This apparently useless method call is made for triggering
197200
// the creation of patchSet inside PatchSetInserter and thus avoiding a NPE
198201
patchSetInserter.getPatchSetId();
@@ -206,15 +209,13 @@ private Change.Id insertPatchSet(Repository git, RevWalk revWalk,
206209

207210
private Change.Id createNewChange(ReviewDb db, Repository git,
208211
RevWalk revWalk, Change.Key changeKey, Project.NameKey project,
209-
Ref destRef,
210-
Account.Id pullRequestOwner,
211-
RevCommit pullRequestCommit, RefControl refControl,
212-
String pullRequestMessage, String topic, boolean doValidation)
213-
throws OrmException, InvalidChangeOperationException, IOException {
212+
Ref destRef, Account.Id pullRequestOwner, RevCommit pullRequestCommit,
213+
RefControl refControl, String pullRequestMessage, String topic,
214+
boolean doValidation) throws OrmException,
215+
InvalidChangeOperationException, IOException {
214216
Change change =
215217
new Change(changeKey, new Change.Id(db.nextChangeId()),
216-
pullRequestOwner, new Branch.NameKey(project,
217-
destRef.getName()));
218+
pullRequestOwner, new Branch.NameKey(project, destRef.getName()));
218219
if (topic != null) {
219220
change.setTopic(topic);
220221
}
@@ -236,7 +237,9 @@ private Change.Id createNewChange(ReviewDb db, Repository git,
236237
ru.getResult()));
237238
}
238239

239-
ins.setMessage(buildChangeMessage(db, change, pullRequestMessage)).insert();
240+
ins.setMessage(
241+
buildChangeMessage(db, change, pullRequestOwner, pullRequestMessage))
242+
.insert();
240243

241244
return change.getId();
242245
}
@@ -260,10 +263,11 @@ private void validate(Repository git, RevCommit pullRequestCommit,
260263
}
261264

262265
private ChangeMessage buildChangeMessage(ReviewDb db, Change dest,
263-
String pullRequestMessage) throws OrmException {
266+
Account.Id pullRequestAuthorId, String pullRequestMessage)
267+
throws OrmException {
264268
ChangeMessage cmsg =
265269
new ChangeMessage(new ChangeMessage.Key(dest.getId(),
266-
ChangeUtil.messageUUID(db)), currentUser.getAccountId(), null);
270+
ChangeUtil.messageUUID(db)), pullRequestAuthorId, null);
267271
cmsg.setMessage(pullRequestMessage);
268272
return cmsg;
269273
}

0 commit comments

Comments
 (0)