spring cloud 获取git所有分支和tag

获取git所有分支和tag

添加引用(pom.xml)

         <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>5.5.1.201910021850-r</version>
        </dependency>

接口实现方法

 @PostMapping("gitInfo")
 public FebsResponse getGitInfo(@RequestParam HashMap<String, Object> param) throws FebsException {
        try {
            String url = String.valueOf(param.get("gitUrl"));
            String username = String.valueOf(param.get("gitAccount"));
            String password = String.valueOf(param.get("gitPassword"));
            UsernamePasswordCredentialsProvider pro = new UsernamePasswordCredentialsProvider(username, password);
            Collection<Ref> refList = Git.lsRemoteRepository().setRemote(url).setCredentialsProvider(pro).call();
            Map<String, Object> mapReturn = new HashMap<String, Object>();

            List<String> branchnameList = new ArrayList<>(4);
            List<String> tagList = new ArrayList<>(4);
            for (Ref ref : refList) {
                String refName = ref.getName();
                if (refName.startsWith("refs/heads/")) {                       //需要进行筛选
                    String branchName = refName.replace("refs/heads/", "");
                    branchnameList.add(branchName);
                }else  if (refName.startsWith("refs/tags/")) {
                    String tagName = refName.replace("refs/tags/", "");
                    tagList.add(tagName);
                }
            }
            mapReturn.put("branch",branchnameList);
            mapReturn.put("tag",tagList);
            return new FebsResponse().data(mapReturn);
        } catch (Exception e) {
            String message = "git信息获取失败";
            log.error(message, e);
            return new FebsResponse().error(message);
        }
    }

git服务器需将自己的服务器添加白名单

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页