1   /*
2    * Copyright 2023 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  
17  package com.linecorp.centraldogma.server.internal.mirror;
18  
19  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT;
20  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_FILE;
21  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_HTTP;
22  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_HTTPS;
23  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_SSH;
24  import static java.util.Objects.requireNonNull;
25  
26  import java.net.URI;
27  
28  import com.linecorp.centraldogma.server.internal.storage.repository.RepositoryUri;
29  import com.linecorp.centraldogma.server.mirror.Mirror;
30  import com.linecorp.centraldogma.server.mirror.MirrorContext;
31  import com.linecorp.centraldogma.server.mirror.MirrorProvider;
32  
33  public final class GitMirrorProvider implements MirrorProvider {
34  
35      @Override
36      public Mirror newMirror(MirrorContext context) {
37          requireNonNull(context, "context");
38  
39          final URI remoteUri = context.remoteUri();
40          final String scheme = remoteUri.getScheme();
41          if (scheme == null) {
42              return null;
43          }
44  
45          switch (scheme) {
46              case SCHEME_GIT_SSH: {
47                  final RepositoryUri repositoryUri = RepositoryUri.parse(remoteUri, "git");
48                  return new SshGitMirror(context.id(), context.enabled(), context.schedule(),
49                                          context.direction(), context.credential(),
50                                          context.localRepo(), context.localPath(),
51                                          repositoryUri.uri(), repositoryUri.path(), repositoryUri.branch(),
52                                          context.gitignore(), context.zone());
53              }
54              case SCHEME_GIT_HTTP:
55              case SCHEME_GIT_HTTPS:
56              case SCHEME_GIT:
57              case SCHEME_GIT_FILE: {
58                  final RepositoryUri repositoryUri = RepositoryUri.parse(remoteUri, "git");
59                  return new DefaultGitMirror(context.id(), context.enabled(), context.schedule(),
60                                              context.direction(), context.credential(),
61                                              context.localRepo(), context.localPath(),
62                                              repositoryUri.uri(), repositoryUri.path(), repositoryUri.branch(),
63                                              context.gitignore(), context.zone());
64              }
65          }
66  
67          return null;
68      }
69  }