Skip to content

Commit

Permalink
[Feat] git clone by tag support. (#4008)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfboys authored Aug 30, 2024
1 parent 9058c9e commit b0dfff8
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -70,7 +79,7 @@ public static Git clone(GitCloneRequest request) throws GitAPIException {
}
}

public static List<String> getBranchList(GitGetRequest request) throws GitAPIException {
public static List<String> getBranches(GitGetRequest request) throws GitAPIException {
try {
LsRemoteCommand command = Git.lsRemoteRepository().setRemote(request.getUrl()).setHeads(true);
setCredentials(command, request);
Expand All @@ -88,7 +97,31 @@ public static List<String> 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<String> getTags(GitGetRequest request) throws GitAPIException {
try {
LsRemoteCommand command = Git.lsRemoteRepository().setRemote(request.getUrl()).setTags(true);
setCredentials(command, request);
Collection<Ref> refList = command.call();
List<String> 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;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ public interface ProjectService extends IService<Project> {
List<String> getAllBranches(Project project);

GitAuthorizedError gitCheck(Project project);

List<String> getAllTags(Project project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public List<String> 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);
}
Expand All @@ -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();
Expand All @@ -349,6 +349,20 @@ public GitAuthorizedError gitCheck(Project project) {
}
}

@Override
public List<String> 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<Map<String, Object>> listConf(Project project) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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("[email protected]: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("[email protected]:apache/incubator-streampark.git");
request.setBranch("2.1.4");
request.setStoreDir(new File("/tmp"));
GitUtils.clone(request);
}
}

0 comments on commit b0dfff8

Please sign in to comment.