From b0dfff8a8b9375f739388e9b2968e3876f790fc3 Mon Sep 17 00:00:00 2001 From: benjobs Date: Fri, 30 Aug 2024 11:41:09 +0800 Subject: [PATCH] [Feat] git clone by tag support. (#4008) --- .../console/base/util/GitUtils.java | 50 ++++++++++++++++--- .../console/core/service/ProjectService.java | 2 + .../core/service/impl/ProjectServiceImpl.java | 18 ++++++- .../console/core/task/ProjectBuildTask.java | 2 +- .../core/service/ProjectServiceTest.java | 7 +++ .../console/core/utils/GitUtilsTest.java | 49 ++++++++++++++++++ 6 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/utils/GitUtilsTest.java diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/GitUtils.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/GitUtils.java index f13c158a23..bfee0ab95b 100644 --- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/GitUtils.java +++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/GitUtils.java @@ -53,13 +53,22 @@ public static Git clone(GitCloneRequest request) throws GitAPIException { try { CloneCommand cloneCommand = Git.cloneRepository().setURI(request.getUrl()).setDirectory(request.getStoreDir()); - if (request.getBranches() != null) { - cloneCommand.setBranch(Constants.R_HEADS + request.getBranches()); + setCredentials(cloneCommand, request); + if (StringUtils.isNotBlank(request.getBranch())) { + cloneCommand.setBranch(Constants.R_HEADS + request.getBranch()); cloneCommand.setBranchesToClone( - Collections.singletonList(Constants.R_HEADS + request.getBranches())); + Collections.singletonList(Constants.R_HEADS + request.getBranch())); } - setCredentials(cloneCommand, request); - return cloneCommand.call(); + + Git git = cloneCommand.call(); + if (StringUtils.isNotBlank(request.getBranch())) { + git.checkout().setName(request.getBranch()).call(); + } else if (StringUtils.isNotBlank(request.getTag())) { + git.checkout().setName(request.getTag()).call(); + } else { + throw new IllegalArgumentException("git clone failed, No tag or branch specified"); + } + return git; } catch (Exception e) { if (e instanceof InvalidRemoteException && request.getAuthType() == GitAuthType.HTTP) { String url = httpUrlToSSH(request.getUrl()); @@ -70,7 +79,7 @@ public static Git clone(GitCloneRequest request) throws GitAPIException { } } - public static List getBranchList(GitGetRequest request) throws GitAPIException { + public static List getBranches(GitGetRequest request) throws GitAPIException { try { LsRemoteCommand command = Git.lsRemoteRepository().setRemote(request.getUrl()).setHeads(true); setCredentials(command, request); @@ -88,7 +97,31 @@ public static List getBranchList(GitGetRequest request) throws GitAPIExc if (e instanceof InvalidRemoteException && request.getAuthType() == GitAuthType.HTTP) { String url = httpUrlToSSH(request.getUrl()); request.setUrl(url); - return getBranchList(request); + return getBranches(request); + } + throw e; + } + } + + public static List getTags(GitGetRequest request) throws GitAPIException { + try { + LsRemoteCommand command = Git.lsRemoteRepository().setRemote(request.getUrl()).setTags(true); + setCredentials(command, request); + Collection refList = command.call(); + List branchList = new ArrayList<>(4); + for (Ref ref : refList) { + String refName = ref.getName(); + if (refName.startsWith(Constants.R_TAGS)) { + String branchName = refName.replace(Constants.R_TAGS, ""); + branchList.add(branchName); + } + } + return branchList; + } catch (Exception e) { + if (e instanceof InvalidRemoteException && request.getAuthType() == GitAuthType.HTTP) { + String url = httpUrlToSSH(request.getUrl()); + request.setUrl(url); + return getTags(request); } throw e; } @@ -197,6 +230,7 @@ public void setUrl(String url) { @Setter public static class GitCloneRequest extends GitGetRequest { private File storeDir; - private String branches; + private String branch; + private String tag; } } diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/ProjectService.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/ProjectService.java index ae4eb87d4e..19f6937fec 100644 --- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/ProjectService.java +++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/ProjectService.java @@ -62,4 +62,6 @@ public interface ProjectService extends IService { List getAllBranches(Project project); GitAuthorizedError gitCheck(Project project); + + List getAllTags(Project project); } diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java index 8b9c167b68..c82a7006d8 100644 --- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java +++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/service/impl/ProjectServiceImpl.java @@ -322,7 +322,7 @@ public List getAllBranches(Project project) { request.setUsername(project.getUserName()); request.setPassword(project.getPassword()); request.setPrivateKey(project.getPrvkeyPath()); - return GitUtils.getBranchList(request); + return GitUtils.getBranches(request); } catch (Exception e) { throw new ApiDetailException(e); } @@ -336,7 +336,7 @@ public GitAuthorizedError gitCheck(Project project) { request.setUsername(project.getUserName()); request.setPassword(project.getPassword()); request.setPrivateKey(project.getPrvkeyPath()); - GitUtils.getBranchList(request); + GitUtils.getBranches(request); return GitAuthorizedError.SUCCESS; } catch (Exception e) { String err = e.getMessage(); @@ -349,6 +349,20 @@ public GitAuthorizedError gitCheck(Project project) { } } + @Override + public List getAllTags(Project project) { + try { + GitUtils.GitGetRequest request = new GitUtils.GitGetRequest(); + request.setUrl(project.getUrl()); + request.setUsername(project.getUserName()); + request.setPassword(project.getPassword()); + request.setPrivateKey(project.getPrvkeyPath()); + return GitUtils.getTags(request); + } catch (Exception e) { + throw new ApiDetailException(e); + } + } + @Override public List> listConf(Project project) { try { diff --git a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/task/ProjectBuildTask.java b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/task/ProjectBuildTask.java index 5fe20cd130..4859f4f2ad 100644 --- a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/task/ProjectBuildTask.java +++ b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/task/ProjectBuildTask.java @@ -93,7 +93,7 @@ private boolean cloneSourceCode(Project project) { GitUtils.GitCloneRequest request = new GitUtils.GitCloneRequest(); request.setUrl(project.getUrl()); - request.setBranches(project.getBranches()); + request.setBranch(project.getBranches()); request.setStoreDir(project.getAppSource()); request.setUsername(project.getUserName()); request.setPassword(project.getPassword()); diff --git a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/ProjectServiceTest.java b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/ProjectServiceTest.java index e82a61cd98..151dd2070d 100644 --- a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/ProjectServiceTest.java +++ b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/ProjectServiceTest.java @@ -46,6 +46,13 @@ void testGitBranches() { branches.forEach(System.out::println); } + @Disabled("This test case can't be runnable due to external service is not available.") + @Test + void testGitTags() { + List branches = projectService.getAllTags(project); + branches.forEach(System.out::println); + } + @Disabled("This test case can't be runnable due to external service is not available.") @Test void testGitCheckAuth() { diff --git a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/utils/GitUtilsTest.java b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/utils/GitUtilsTest.java new file mode 100644 index 0000000000..579f778975 --- /dev/null +++ b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/utils/GitUtilsTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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 + * + * http://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 org.apache.streampark.console.core.utils; + +import org.apache.streampark.console.base.util.GitUtils; + +import org.eclipse.jgit.api.errors.GitAPIException; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.io.File; + +public class GitUtilsTest { + + @Disabled("This test case can't be runnable due to external service is not available.") + @Test + void testGitCloneByTag() throws GitAPIException { + GitUtils.GitCloneRequest request = new GitUtils.GitCloneRequest(); + request.setUrl("git@github.com:apache/incubator-streampark.git"); + request.setTag("v1.2.3"); + request.setStoreDir(new File("/tmp")); + GitUtils.clone(request); + } + + @Disabled("This test case can't be runnable due to external service is not available.") + @Test + void testGitCloneByBranch() throws GitAPIException { + GitUtils.GitCloneRequest request = new GitUtils.GitCloneRequest(); + request.setUrl("git@github.com:apache/incubator-streampark.git"); + request.setBranch("2.1.4"); + request.setStoreDir(new File("/tmp")); + GitUtils.clone(request); + } +}