1   /*
2    * Copyright 2021 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.command;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import javax.annotation.Nullable;
21  
22  import com.fasterxml.jackson.annotation.JsonCreator;
23  import com.fasterxml.jackson.annotation.JsonProperty;
24  
25  import com.linecorp.centraldogma.common.Author;
26  import com.linecorp.centraldogma.common.Change;
27  import com.linecorp.centraldogma.common.Markup;
28  import com.linecorp.centraldogma.common.Revision;
29  import com.linecorp.centraldogma.server.storage.repository.Repository;
30  
31  /**
32   * A {@link Command} which is used for pushing changes to the repository. The changes are normalized via
33   * {@link Repository#previewDiff(Revision, Iterable)} before they are applied.
34   * You can find the normalized changes from the {@link CommitResult#changes()} that is the result of
35   * {@link CommandExecutor#execute(Command)}.
36   */
37  public final class NormalizingPushCommand extends AbstractPushCommand<CommitResult> {
38  
39      @JsonCreator
40      NormalizingPushCommand(@JsonProperty("timestamp") @Nullable Long timestamp,
41                             @JsonProperty("author") @Nullable Author author,
42                             @JsonProperty("projectName") String projectName,
43                             @JsonProperty("repositoryName") String repositoryName,
44                             @JsonProperty("baseRevision") Revision baseRevision,
45                             @JsonProperty("summary") String summary,
46                             @JsonProperty("detail") String detail,
47                             @JsonProperty("markup") Markup markup,
48                             @JsonProperty("changes") Iterable<Change<?>> changes) {
49          super(CommandType.NORMALIZING_PUSH, timestamp, author, projectName, repositoryName,
50                baseRevision, summary, detail, markup, changes);
51      }
52  
53      /**
54       * Returns a new {@link PushAsIsCommand} which is converted from this {@link NormalizingPushCommand}
55       * for replicating to other replicas. Unlike the {@link NormalizingPushCommand},
56       * the changes of this {@link Command} are not normalized and applied as they are.
57       */
58      public PushAsIsCommand asIs(CommitResult commitResult) {
59          requireNonNull(commitResult, "commitResult");
60          return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
61                                     commitResult.revision().backward(1), summary(), detail(),
62                                     markup(), commitResult.changes());
63      }
64  }