Skip to content

Commit

Permalink
add gitlab function (#792) (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanwb authored Feb 27, 2024
1 parent 5fe72f7 commit 4dbc169
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
4 changes: 2 additions & 2 deletions jcommon/gitlab/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<version>1.4-jdk20-SNAPSHOT</version>
</parent>
<artifactId>gitlab</artifactId>
<version>1.6-jdk21</version>
<version>1.6.1-jdk21-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.odiszapc</groupId>
Expand All @@ -34,7 +34,7 @@
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.12.2</version>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down
111 changes: 111 additions & 0 deletions jcommon/gitlab/src/main/java/com/xiaomi/youpin/gitlab/Gitlab.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.xiaomi.youpin.gitlab;

import com.github.odiszapc.nginxparser.*;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;
import com.google.common.collect.Maps;
import com.google.gson.JsonSyntaxException;
Expand All @@ -36,15 +37,22 @@
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.TreeItem;
import org.springframework.beans.BeanUtils;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand All @@ -64,6 +72,25 @@ public class Gitlab {

private static Gson gson = new Gson();

//帮我生成一个org.gitlab4j.api.GitLabApi类的缓存方法,避免每次都new一个对象,新建方式如下GitLabApi gitLabApi = new GitLabApi(baseUrl, gitToken); (method)
private static final ConcurrentHashMap<String, GitLabApi> gitLabApiCache = new ConcurrentHashMap<>();

public static GitLabApi getGitLabApi(String baseUrl, String gitToken) {
String key = baseUrl + "#" + gitToken;
return gitLabApiCache.computeIfAbsent(key, k -> new GitLabApi(baseUrl, gitToken));
}

private static List<String> SUPPORT_FILE_TYPES = Arrays.asList(
".java", ".xml", ".html", ".css", ".js", ".json", ".md",
".yml", ".yaml", ".properties", ".sql", ".txt", ".sh", ".bat", ".py", ".c", ".cpp",
".h", ".rb", ".php", ".cs", ".swift", ".go", ".rs"
);

//给一个文件名,判断后缀是否在SUPPORT_FILE_TYPES 结合中 (class)
public boolean isSupportedFileType(String fileName) {
return SUPPORT_FILE_TYPES.stream().anyMatch(fileName::endsWith);
}

public Gitlab(String gitlabBaseUrl) {
if (StringUtils.isBlank(gitlabBaseUrl)) {
this.gitlabApiUrl = GIT_API_URL;
Expand Down Expand Up @@ -1348,4 +1375,88 @@ private boolean getServers(String config, String name) {
}
return res;
}

/**
* 获取指定远程git文件的raw内容
* @param branch
* @param gitDomain
* @param gitToken
* @param projectId
* @param filePath
* @return
*/
public String getFileContent(String branch, String gitDomain, String gitToken, String projectId, String filePath) {
try {
Preconditions.checkArgument(null != gitDomain, "gitDomain can not be null");
Preconditions.checkArgument(null != gitToken, "gitToken can not be null");
Preconditions.checkArgument(null != projectId, "projectId can not be null");
Preconditions.checkArgument(null != filePath, "filePath can not be null");
Preconditions.checkArgument(isSupportedFileType(filePath), "not support this file type");

String baseUrl = "https://" + gitDomain;
GitLabApi gitLabApi = getGitLabApi(baseUrl, gitToken);

RepositoryFile file = gitLabApi.getRepositoryFileApi().getFile(projectId, filePath, branch);
String encodedContent = file.getContent();
byte[] decodedBytes = Base64.getDecoder().decode(encodedContent);

return new String(decodedBytes, StandardCharsets.UTF_8);
} catch (GitLabApiException e) {
throw new RuntimeException("Error retrieving file content from GitLab", e);
}
}


/**
* 获取git工程树结构
*
* @param branch
* @param gitDomain
* @param gitToken
* @param projectId
* @return
*/
public List<GitTreeItem> getProjectStructureTree(String branch, String gitDomain, String gitToken, String projectId) {
try {
Preconditions.checkArgument(null != gitDomain, "gitDomain can not be null");
Preconditions.checkArgument(null != gitToken, "gitToken can not be null");
Preconditions.checkArgument(null != projectId, "projectId can not be null");

String baseUrl = "https://" + gitDomain;
GitLabApi gitLabApi = getGitLabApi(baseUrl, gitToken);
List<TreeItem> treeItems = gitLabApi.getRepositoryApi().getTree(projectId, null, branch, true);

List<GitTreeItem> treeItemList = transformProjectStructureTree(treeItems);

return treeItemList;
} catch (GitLabApiException e) {
throw new RuntimeException("Error retrieving project structure from GitLab", e);
}
}

//将调用getProjectStructureTree获取到的List<TreeItem>进行结构改造,新结构TreeItem2较TreeItem新增属性parentId,父子关系的判定基于TreeItem的path和type (class)
private List<GitTreeItem> transformProjectStructureTree(List<TreeItem> treeItems) {
Map<String, GitTreeItem> itemMap = new HashMap<>();
List<GitTreeItem> result = new ArrayList<>();

// First, convert all TreeItems to TreeItem2 and store them in a map for easy lookup
for (TreeItem item : treeItems) {
GitTreeItem gitTreeItem = new GitTreeItem();
BeanUtils.copyProperties(item, gitTreeItem);
gitTreeItem.setType(item.getType().name());
itemMap.put(item.getPath(), gitTreeItem);
}

// Next, establish parent-child relationships
for (GitTreeItem gitTreeItem : itemMap.values()) {
String path = gitTreeItem.getPath();
String parentPath = path.contains("/") ? path.substring(0, path.lastIndexOf('/')) : null;
gitTreeItem.setParentId(parentPath != null ? itemMap.get(parentPath).getId() : null);

result.add(gitTreeItem);
}

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,12 @@ public void getDomainByIP() {
gitlab.getDomainByIP("https://xxx.com","xx","", Arrays.asList("127.0.0.1"),"token");
}


@Test
public void getProjectCode() {

gitlab.getDomainByIP("https://xxx.com","xx","", Arrays.asList("127.0.0.1"),"token");
}


}

0 comments on commit 4dbc169

Please sign in to comment.