Line data Source code
1 : // Copyright (C) 2016 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.server.notedb; 16 : 17 : import com.google.common.collect.ImmutableListMultimap; 18 : import com.google.common.flogger.FluentLogger; 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.entities.Change; 21 : import com.google.gerrit.entities.Project; 22 : import com.google.gerrit.entities.RefNames; 23 : import com.google.gerrit.entities.RobotComment; 24 : import com.google.inject.Inject; 25 : import com.google.inject.assistedinject.Assisted; 26 : import java.io.IOException; 27 : import org.eclipse.jgit.errors.ConfigInvalidException; 28 : import org.eclipse.jgit.lib.ObjectId; 29 : import org.eclipse.jgit.lib.ObjectReader; 30 : import org.eclipse.jgit.notes.NoteMap; 31 : import org.eclipse.jgit.revwalk.RevCommit; 32 : 33 : public class RobotCommentNotes extends AbstractChangeNotes<RobotCommentNotes> { 34 103 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 35 : 36 : public interface Factory { 37 : RobotCommentNotes create(Change change); 38 : } 39 : 40 : private final Change change; 41 : 42 : private ImmutableListMultimap<ObjectId, RobotComment> comments; 43 : private RevisionNoteMap<RobotCommentsRevisionNote> revisionNoteMap; 44 : private ObjectId metaId; 45 : 46 : @Inject 47 : RobotCommentNotes(Args args, @Assisted Change change) { 48 103 : super(args, change.getId(), null); 49 103 : this.change = change; 50 103 : } 51 : 52 : RevisionNoteMap<RobotCommentsRevisionNote> getRevisionNoteMap() { 53 3 : return revisionNoteMap; 54 : } 55 : 56 : public ImmutableListMultimap<ObjectId, RobotComment> getComments() { 57 103 : return comments; 58 : } 59 : 60 : public boolean containsComment(RobotComment c) { 61 0 : for (RobotComment existing : comments.values()) { 62 0 : if (c.key.equals(existing.key)) { 63 0 : return true; 64 : } 65 0 : } 66 0 : return false; 67 : } 68 : 69 : @Override 70 : public String getRefName() { 71 103 : return RefNames.robotCommentsRef(getChangeId()); 72 : } 73 : 74 : @Nullable 75 : public ObjectId getMetaId() { 76 103 : return metaId; 77 : } 78 : 79 : @Override 80 : protected void onLoad(LoadHandle handle) throws IOException, ConfigInvalidException { 81 103 : metaId = handle.id(); 82 103 : if (metaId == null) { 83 103 : loadDefaults(); 84 103 : return; 85 : } 86 9 : metaId = metaId.copy(); 87 : 88 9 : logger.atFine().log( 89 9 : "Load robot comment notes for change %s of project %s", getChangeId(), getProjectName()); 90 9 : RevCommit tipCommit = handle.walk().parseCommit(metaId); 91 9 : ObjectReader reader = handle.walk().getObjectReader(); 92 9 : revisionNoteMap = 93 9 : RevisionNoteMap.parseRobotComments( 94 9 : args.changeNoteJson, reader, NoteMap.read(reader, tipCommit)); 95 9 : ImmutableListMultimap.Builder<ObjectId, RobotComment> cs = ImmutableListMultimap.builder(); 96 9 : for (RobotCommentsRevisionNote rn : revisionNoteMap.revisionNotes.values()) { 97 9 : for (RobotComment c : rn.getEntities()) { 98 9 : cs.put(c.getCommitId(), c); 99 9 : } 100 9 : } 101 9 : comments = cs.build(); 102 9 : } 103 : 104 : @Override 105 : protected void loadDefaults() { 106 103 : comments = ImmutableListMultimap.of(); 107 103 : } 108 : 109 : @Override 110 : public Project.NameKey getProjectName() { 111 103 : return change.getProject(); 112 : } 113 : }