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.client;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.util.concurrent.CompletableFuture;
21  
22  import com.linecorp.centraldogma.common.Change;
23  import com.linecorp.centraldogma.common.Commit;
24  import com.linecorp.centraldogma.common.Markup;
25  import com.linecorp.centraldogma.common.PushResult;
26  import com.linecorp.centraldogma.common.Revision;
27  
28  /**
29   * Prepares to send a {@link CentralDogma#push(String, String, Revision, String, String, Markup, Iterable)}
30   * request to the Central Dogma repository.
31   */
32  public final class CommitRequest {
33  
34      private final CentralDogmaRepository centralDogmaRepo;
35      private final String summary;
36      private final Iterable<? extends Change<?>> changes;
37  
38      private String detail = "";
39      private Markup markup = Markup.PLAINTEXT;
40  
41      CommitRequest(CentralDogmaRepository centralDogmaRepo,
42                    String summary, Iterable<? extends Change<?>> changes) {
43          this.centralDogmaRepo = centralDogmaRepo;
44          this.summary = summary;
45          this.changes = changes;
46      }
47  
48      /**
49       * Sets the detail and {@link Markup} of a {@link Commit}.
50       */
51      public CommitRequest detail(String detail, Markup markup) {
52          this.detail = requireNonNull(detail, "detail");
53          this.markup = requireNonNull(markup, "markup");
54          return this;
55      }
56  
57      /**
58       * Pushes the {@link Change}s to the repository with {@link Revision#HEAD}.
59       *
60       * @return the {@link PushResult} which tells the {@link Revision} and timestamp of the new {@link Commit}
61       */
62      public CompletableFuture<PushResult> push() {
63          return push(Revision.HEAD);
64      }
65  
66      /**
67       * Pushes the {@link Change}s to the repository with the {@link Revision}.
68       *
69       * @return the {@link PushResult} which tells the {@link Revision} and timestamp of the new {@link Commit}
70       */
71      public CompletableFuture<PushResult> push(Revision baseRevision) {
72          requireNonNull(baseRevision, "baseRevision");
73          return centralDogmaRepo.centralDogma().push(centralDogmaRepo.projectName(),
74                                                      centralDogmaRepo.repositoryName(),
75                                                      baseRevision, summary, detail, markup, changes);
76      }
77  }