Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract mirroring implementation from the server module #836

Merged
merged 13 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ junit5 = { module = "org.junit:junit-bom", version.ref = "junit5" }

[libraries.armeria]
module = "com.linecorp.armeria:armeria"
javadocs = "https://www.javadoc.io/doc/com.linecorp.armeria/armeria-javadoc/1.24.0/"
javadocs = "https://www.javadoc.io/doc/com.linecorp.armeria/armeria-javadoc/1.24.3/"
[libraries.armeria-junit5]
module = "com.linecorp.armeria:armeria-junit5"
[libraries.armeria-logback]
Expand Down Expand Up @@ -264,11 +264,11 @@ javadocs = "https://www.javadoc.io/doc/ch.qos.logback/logback-classic/1.2.12/"
[libraries.micrometer-core]
module = "io.micrometer:micrometer-core"
version.ref = "micrometer"
javadocs = "https://www.javadoc.io/doc/io.micrometer/micrometer-core/1.10.5/"
javadocs = "https://www.javadoc.io/doc/io.micrometer/micrometer-core/1.11.1/"
[libraries.micrometer-prometheus]
module = "io.micrometer:micrometer-registry-prometheus"
version.ref = "micrometer"
javadocs = "https://www.javadoc.io/doc/io.micrometer/micrometer-registry-prometheus/1.10.5/"
javadocs = "https://www.javadoc.io/doc/io.micrometer/micrometer-registry-prometheus/1.11.1/"

[libraries.mockito-core]
module = "org.mockito:mockito-core"
Expand Down
21 changes: 16 additions & 5 deletions dist/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.google.common.base.CaseFormat
evaluationDependsOn(':server')
evaluationDependsOn(':server-auth:saml')
evaluationDependsOn(':server-auth:shiro')
evaluationDependsOn(':server-jgit6')
evaluationDependsOn(':server-mirror-git')

ext {
distDir = "${project.buildDir}/dist"
Expand Down Expand Up @@ -76,13 +76,24 @@ task copyLib(group: 'Build',
exclude group: 'org.eclipse.jgit'
}
from project(':server-auth:shiro').tasks.jar
from project(':server-jgit6').configurations.runtimeClasspath
from project(':server-jgit6').tasks.jar
into "${project.ext.distDir}/lib"
from project(':server-mirror-git').configurations.runtimeClasspath
from project(':server-mirror-git').tasks.jar

File libDir = new File("${project.ext.distDir}/lib")
into libDir

doLast {
// Create an empty directory for keeping external JARs such as authentication module.
project.mkdir("${project.ext.distDir}/lib/ext")
project.mkdir("${libDir}/ext")

// Check if only JGit 6.x JARs are included.
FileCollection jgits = layout.files { libDir.listFiles() }.filter { File file ->
file.name.contains("jgit")
}
// org.eclipse.jgit-6.x and org.eclipse.jgit.ssh.jsch-6.x are required.
if (jgits.size() != 2 || !jgits.any { File file -> file.name.startsWith("org.eclipse.jgit-6") }) {
throw new IllegalStateException("Unexpected JGit JARs: $jgits.files")
}
}
}

Expand Down
15 changes: 0 additions & 15 deletions it/build.gradle

This file was deleted.

7 changes: 7 additions & 0 deletions it/mirror/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
testImplementation libs.sshd
testImplementation libs.jsch

testImplementation project(':server-auth:shiro')
testImplementation project(':server-mirror-git')
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private void pushMirror(String gitUri, MirrorDirection mirrorDirection) {
Change.ofJsonUpsert("/mirrors.json",
"[{" +
" \"type\": \"single\"," +
" \"direction\": \"" + mirrorDirection.name() + "\"," +
" \"direction\": \"" + mirrorDirection.name() + "\"," +
" \"localRepo\": \"" + REPO_FOO + "\"," +
" \"localPath\": \"/\"," +
" \"remoteUri\": \"" + gitUri + "\"," +
Expand Down
7 changes: 7 additions & 0 deletions it/server/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies {
testImplementation libs.curator.test
testImplementation libs.sshd

testImplementation project(':server')
testImplementation project(':server-auth:shiro')
}
2 changes: 0 additions & 2 deletions server-auth/saml/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
dependencies {
implementation project(':server')
implementation libs.armeria.saml

testImplementation project(':testing-internal')
}
1 change: 0 additions & 1 deletion server-auth/shiro/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
dependencies {
api project(':server')
testImplementation project(':testing-internal')

// Caffeine
implementation libs.caffeine
Expand Down

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -112,21 +111,6 @@ public final class GitMirror extends AbstractMirror {

private static final String HEAD_REF_MASTER = Constants.R_HEADS + Constants.MASTER;

@Nullable
private static final Constructor<?> GIT_WITH_AUTH_CONSTRUCTOR;

static {
Constructor<?> constr = null;
try {
constr = Class.forName("com.linecorp.centraldogma.server" +
".internal.mirror.Git6WithAuth")
.getDeclaredConstructor(GitMirror.class, File.class);
} catch (Exception e) {
logger.info("Falling back to JGit 5:", e);
}
GIT_WITH_AUTH_CONSTRUCTOR = constr;
}

@Nullable
private IgnoreNode ignoreNode;

Expand Down Expand Up @@ -368,12 +352,7 @@ private GitWithAuth openGit(File workDir) throws Exception {
workDir,
CONSECUTIVE_UNDERSCORES.matcher(DISALLOWED_CHARS.matcher(
remoteRepoUri().toASCIIString()).replaceAll("_")).replaceAll("_"));
final GitWithAuth git;
if (GIT_WITH_AUTH_CONSTRUCTOR != null) {
git = (GitWithAuth) GIT_WITH_AUTH_CONSTRUCTOR.newInstance(this, repoDir);
} else {
git = new Git5WithAuth(this, repoDir);
}
final GitWithAuth git = new GitWithAuth(this, repoDir);
boolean success = false;
try {
// Set the remote URLs.
Expand Down Expand Up @@ -476,7 +455,7 @@ private Revision remoteCurrentRevision(

private static ObjectId fetchRemoteHeadAndGetCommitId(
GitWithAuth git, String headBranchRefName) throws GitAPIException, IOException {
final FetchCommand fetch = git.fetch(1);
final FetchCommand fetch = git.fetch().setDepth(1);
final FetchResult fetchResult = fetch.setRefSpecs(new RefSpec(headBranchRefName))
.setRemoveDeletedRefs(true)
.setTagOpt(TagOpt.NO_TAGS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.centraldogma.server.internal.mirror;

import static com.linecorp.centraldogma.server.internal.storage.repository.MirrorUtil.split;
import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT;
import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_FILE;
import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_HTTP;
import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_HTTPS;
import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_SSH;
import static java.util.Objects.requireNonNull;

import java.net.URI;

import com.linecorp.centraldogma.server.mirror.Mirror;
import com.linecorp.centraldogma.server.mirror.MirrorContext;
import com.linecorp.centraldogma.server.mirror.MirrorProvider;

public final class GitMirrorProvider implements MirrorProvider {

@Override
public Mirror newMirror(MirrorContext context) {
requireNonNull(context, "context");

final URI remoteUri = context.remoteUri();
final String scheme = remoteUri.getScheme();
if (scheme == null) {
return null;

Check warning on line 42 in server-mirror-git/src/main/java/com/linecorp/centraldogma/server/internal/mirror/GitMirrorProvider.java

View check run for this annotation

Codecov / codecov/patch

server-mirror-git/src/main/java/com/linecorp/centraldogma/server/internal/mirror/GitMirrorProvider.java#L42

Added line #L42 was not covered by tests
}

switch (scheme) {
case SCHEME_GIT:
case SCHEME_GIT_SSH:
case SCHEME_GIT_HTTP:
case SCHEME_GIT_HTTPS:
case SCHEME_GIT_FILE: {
final String[] components = split(remoteUri, "git");
return new GitMirror(context.schedule(), context.direction(), context.credential(),
context.localRepo(), context.localPath(),
URI.create(components[0]), components[1], components[2],
context.gitignore());
}
}

return null;
}
}
Loading
Loading