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 static java.nio.charset.StandardCharsets.UTF_8; 18 : 19 : import com.google.common.collect.ImmutableList; 20 : import com.google.gerrit.common.Nullable; 21 : import com.google.gerrit.entities.Comment; 22 : import com.google.gerrit.entities.HumanComment; 23 : import com.google.gerrit.entities.SubmitRequirementResult; 24 : import java.io.ByteArrayInputStream; 25 : import java.io.IOException; 26 : import java.io.InputStream; 27 : import java.io.InputStreamReader; 28 : import java.io.Reader; 29 : import java.util.List; 30 : import org.eclipse.jgit.errors.ConfigInvalidException; 31 : import org.eclipse.jgit.lib.ObjectId; 32 : import org.eclipse.jgit.lib.ObjectReader; 33 : import org.eclipse.jgit.util.MutableInteger; 34 : 35 : /** Implements the parsing of comment data, handling JSON decoding and push certificates. */ 36 : class ChangeRevisionNote extends RevisionNote<HumanComment> { 37 : private final ChangeNoteJson noteJson; 38 : private final Comment.Status status; 39 : private String pushCert; 40 : 41 : /** 42 : * Submit requirement results stored in this revision note. If null, then no SRs were stored in 43 : * the revision note . Otherwise, there were stored SRs in this revision note. The list could be 44 : * empty, meaning that no SRs were configured for the project. 45 : */ 46 : @Nullable private ImmutableList<SubmitRequirementResult> submitRequirementsResult; 47 : 48 : ChangeRevisionNote( 49 : ChangeNoteJson noteJson, ObjectReader reader, ObjectId noteId, Comment.Status status) { 50 64 : super(reader, noteId); 51 64 : this.noteJson = noteJson; 52 64 : this.status = status; 53 64 : } 54 : 55 : /** 56 : * Returns null if no submit requirements were stored in the revision note. Otherwise, this method 57 : * returns a list of submit requirements, which can probably be empty if there were no SRs 58 : * configured for the project at the time when the SRs were stored. 59 : */ 60 : @Nullable 61 : public ImmutableList<SubmitRequirementResult> getSubmitRequirementsResult() { 62 64 : checkParsed(); 63 64 : return submitRequirementsResult; 64 : } 65 : 66 : public String getPushCert() { 67 64 : checkParsed(); 68 64 : return pushCert; 69 : } 70 : 71 : @Override 72 : protected List<HumanComment> parse(byte[] raw, int offset) 73 : throws IOException, ConfigInvalidException { 74 64 : MutableInteger p = new MutableInteger(); 75 64 : p.value = offset; 76 : 77 64 : ChangeRevisionNoteData data = parseJson(noteJson, raw, p.value); 78 64 : if (status == HumanComment.Status.PUBLISHED) { 79 64 : pushCert = data.pushCert; 80 : } else { 81 21 : pushCert = null; 82 : } 83 64 : this.submitRequirementsResult = 84 64 : data.submitRequirementResults == null 85 29 : ? null 86 64 : : ImmutableList.copyOf(data.submitRequirementResults); 87 64 : return data.comments; 88 : } 89 : 90 : private ChangeRevisionNoteData parseJson(ChangeNoteJson noteUtil, byte[] raw, int offset) 91 : throws IOException { 92 64 : try (InputStream is = new ByteArrayInputStream(raw, offset, raw.length - offset); 93 64 : Reader r = new InputStreamReader(is, UTF_8)) { 94 64 : return noteUtil.getGson().fromJson(r, ChangeRevisionNoteData.class); 95 : } 96 : } 97 : }