Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.entities.converter; 16 : 17 : import com.google.errorprone.annotations.Immutable; 18 : import com.google.gerrit.entities.Account; 19 : import com.google.gerrit.entities.BranchNameKey; 20 : import com.google.gerrit.entities.Change; 21 : import com.google.gerrit.entities.PatchSet; 22 : import com.google.gerrit.proto.Entities; 23 : import com.google.protobuf.Parser; 24 : import java.time.Instant; 25 : 26 104 : @Immutable 27 104 : public enum ChangeProtoConverter implements ProtoConverter<Entities.Change, Change> { 28 104 : INSTANCE; 29 : 30 104 : private final ProtoConverter<Entities.Change_Id, Change.Id> changeIdConverter = 31 : ChangeIdProtoConverter.INSTANCE; 32 104 : private final ProtoConverter<Entities.PatchSet_Id, PatchSet.Id> patchSetIdConverter = 33 : PatchSetIdProtoConverter.INSTANCE; 34 104 : private final ProtoConverter<Entities.Change_Key, Change.Key> changeKeyConverter = 35 : ChangeKeyProtoConverter.INSTANCE; 36 104 : private final ProtoConverter<Entities.Account_Id, Account.Id> accountIdConverter = 37 : AccountIdProtoConverter.INSTANCE; 38 104 : private final ProtoConverter<Entities.Branch_NameKey, BranchNameKey> branchNameConverter = 39 : BranchNameKeyProtoConverter.INSTANCE; 40 : 41 : @Override 42 : public Entities.Change toProto(Change change) { 43 : Entities.Change.Builder builder = 44 104 : Entities.Change.newBuilder() 45 104 : .setChangeId(changeIdConverter.toProto(change.getId())) 46 104 : .setChangeKey(changeKeyConverter.toProto(change.getKey())) 47 104 : .setCreatedOn(change.getCreatedOn().toEpochMilli()) 48 104 : .setLastUpdatedOn(change.getLastUpdatedOn().toEpochMilli()) 49 104 : .setOwnerAccountId(accountIdConverter.toProto(change.getOwner())) 50 104 : .setDest(branchNameConverter.toProto(change.getDest())) 51 104 : .setStatus(change.getStatus().getCode()) 52 104 : .setIsPrivate(change.isPrivate()) 53 104 : .setWorkInProgress(change.isWorkInProgress()) 54 104 : .setReviewStarted(change.hasReviewStarted()); 55 104 : PatchSet.Id currentPatchSetId = change.currentPatchSetId(); 56 : // Special behavior necessary to ensure binary compatibility. 57 104 : builder.setCurrentPatchSetId(currentPatchSetId == null ? 0 : currentPatchSetId.get()); 58 104 : String subject = change.getSubject(); 59 104 : if (subject != null) { 60 104 : builder.setSubject(subject); 61 : } 62 104 : String topic = change.getTopic(); 63 104 : if (topic != null) { 64 31 : builder.setTopic(topic); 65 : } 66 104 : String originalSubject = change.getOriginalSubjectOrNull(); 67 104 : if (originalSubject != null) { 68 104 : builder.setOriginalSubject(originalSubject); 69 : } 70 104 : String submissionId = change.getSubmissionId(); 71 104 : if (submissionId != null) { 72 57 : builder.setSubmissionId(submissionId); 73 : } 74 104 : Account.Id assignee = change.getAssignee(); 75 104 : if (assignee != null) { 76 7 : builder.setAssignee(accountIdConverter.toProto(assignee)); 77 : } 78 104 : Change.Id revertOf = change.getRevertOf(); 79 104 : if (revertOf != null) { 80 15 : builder.setRevertOf(changeIdConverter.toProto(revertOf)); 81 : } 82 104 : PatchSet.Id cherryPickOf = change.getCherryPickOf(); 83 104 : if (cherryPickOf != null) { 84 10 : builder.setCherryPickOf(patchSetIdConverter.toProto(cherryPickOf)); 85 : } 86 104 : return builder.build(); 87 : } 88 : 89 : @Override 90 : public Change fromProto(Entities.Change proto) { 91 101 : Change.Id changeId = changeIdConverter.fromProto(proto.getChangeId()); 92 : Change.Key key = 93 101 : proto.hasChangeKey() ? changeKeyConverter.fromProto(proto.getChangeKey()) : null; 94 : Account.Id owner = 95 101 : proto.hasOwnerAccountId() ? accountIdConverter.fromProto(proto.getOwnerAccountId()) : null; 96 : BranchNameKey destination = 97 101 : proto.hasDest() ? branchNameConverter.fromProto(proto.getDest()) : null; 98 101 : Change change = 99 101 : new Change(key, changeId, owner, destination, Instant.ofEpochMilli(proto.getCreatedOn())); 100 101 : if (proto.hasLastUpdatedOn()) { 101 101 : change.setLastUpdatedOn(Instant.ofEpochMilli(proto.getLastUpdatedOn())); 102 : } 103 101 : Change.Status status = Change.Status.forCode((char) proto.getStatus()); 104 101 : if (status != null) { 105 101 : change.setStatus(status); 106 : } 107 101 : String subject = proto.hasSubject() ? proto.getSubject() : null; 108 101 : String originalSubject = proto.hasOriginalSubject() ? proto.getOriginalSubject() : null; 109 101 : change.setCurrentPatchSet( 110 101 : PatchSet.id(changeId, proto.getCurrentPatchSetId()), subject, originalSubject); 111 101 : if (proto.hasTopic()) { 112 31 : change.setTopic(proto.getTopic()); 113 : } 114 101 : if (proto.hasSubmissionId()) { 115 52 : change.setSubmissionId(proto.getSubmissionId()); 116 : } 117 101 : if (proto.hasAssignee()) { 118 7 : change.setAssignee(accountIdConverter.fromProto(proto.getAssignee())); 119 : } 120 101 : change.setPrivate(proto.getIsPrivate()); 121 101 : change.setWorkInProgress(proto.getWorkInProgress()); 122 101 : change.setReviewStarted(proto.getReviewStarted()); 123 101 : if (proto.hasRevertOf()) { 124 15 : change.setRevertOf(changeIdConverter.fromProto(proto.getRevertOf())); 125 : } 126 101 : if (proto.hasCherryPickOf()) { 127 10 : change.setCherryPickOf(patchSetIdConverter.fromProto(proto.getCherryPickOf())); 128 : } 129 101 : return change; 130 : } 131 : 132 : @Override 133 : public Parser<Entities.Change> getParser() { 134 100 : return Entities.Change.parser(); 135 : } 136 : }