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 com.google.common.base.Preconditions.checkArgument;
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.List;
22  import java.util.concurrent.CompletableFuture;
23  
24  import com.linecorp.centraldogma.common.Commit;
25  import com.linecorp.centraldogma.common.PathPattern;
26  import com.linecorp.centraldogma.common.Revision;
27  import com.linecorp.centraldogma.internal.HistoryConstants;
28  
29  /**
30   * Prepares to send a {@link CentralDogma#getHistory(String, String, Revision, Revision, PathPattern, int)}
31   * request to the Central Dogma repository.
32   */
33  public final class HistoryRequest {
34  
35      private final CentralDogmaRepository centralDogmaRepo;
36      private final PathPattern pathPattern;
37      private int maxCommits;
38  
39      HistoryRequest(CentralDogmaRepository centralDogmaRepo, PathPattern pathPattern) {
40          this.centralDogmaRepo = centralDogmaRepo;
41          this.pathPattern = pathPattern;
42      }
43  
44      /**
45       * Sets the maximum number of commits to retrieve. {@code 0} is used by default which means to retreive
46       * all commits. The number of retrieved commits can't be greater than
47       * {@value HistoryConstants#MAX_MAX_COMMITS}.
48       */
49      public HistoryRequest maxCommits(int maxCommits) {
50          checkArgument(maxCommits >= 0 && maxCommits <= HistoryConstants.MAX_MAX_COMMITS,
51                        "maxCommits: %s (expected: 0 <= maxCommits <= %s)",
52                        maxCommits, HistoryConstants.MAX_MAX_COMMITS);
53          this.maxCommits = maxCommits;
54          return this;
55      }
56  
57      /**
58       * Retrieves the history of the files matched by the given path pattern between two {@link Revision}s.
59       *
60       * <p>Note that this method does not retrieve the diffs but only metadata about the changes.
61       *
62       * @return a {@link List} that contains the {@link Commit}s of the files matched by the given
63       *         {@link PathPattern} in the specified repository
64       */
65      public CompletableFuture<List<Commit>> get(Revision from, Revision to) {
66          requireNonNull(from, "from");
67          requireNonNull(to, "to");
68          return centralDogmaRepo.centralDogma().getHistory(centralDogmaRepo.projectName(),
69                                                            centralDogmaRepo.repositoryName(),
70                                                            from, to, pathPattern, maxCommits);
71      }
72  }