|
|
@@ -20,10 +20,7 @@ import org.eclipse.jgit.treewalk.CanonicalTreeParser;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Git 实现
|
|
|
@@ -32,18 +29,7 @@ import java.util.List;
|
|
|
* @date 2019-10-12 22:22:00
|
|
|
*/
|
|
|
@Component
|
|
|
-public class GitImpl implements CodeUpdater {
|
|
|
- private UsernamePasswordCredentialsProvider credentials;
|
|
|
- private SshSessionFactory sshSessionFactory;
|
|
|
-
|
|
|
- private void httpAuth(String username, String password) {
|
|
|
- this.credentials = new UsernamePasswordCredentialsProvider(username, password);
|
|
|
- }
|
|
|
-
|
|
|
- private void sshAuth(String privateKey) {
|
|
|
- this.sshSessionFactory = new CustomSshSessionFactory(privateKey);
|
|
|
- }
|
|
|
-
|
|
|
+public class GitClient implements CodeUpdater {
|
|
|
/**
|
|
|
* 不存在则 clone,存在则 pull
|
|
|
*
|
|
|
@@ -51,8 +37,7 @@ public class GitImpl implements CodeUpdater {
|
|
|
* @return
|
|
|
* @date 2020-05-13 下午9:22
|
|
|
*/
|
|
|
- @Override
|
|
|
- public CommitInfo pull(String remote, String branch, String local) throws Exception {
|
|
|
+ public CommitInfo pull(String remote, String branch, String local, RepoAuthConfig repoAuthConfig) throws Exception {
|
|
|
File file = new File(local);
|
|
|
if (!file.exists() && !file.mkdirs()) {
|
|
|
throw new Exception("创建 " + local + " 目录失败");
|
|
|
@@ -61,28 +46,29 @@ public class GitImpl implements CodeUpdater {
|
|
|
File localRepo = new File(local + "/.git");
|
|
|
CommitInfo commitInfo;
|
|
|
if (!localRepo.exists()) {
|
|
|
- commitInfo = clone(remote, branch, local);
|
|
|
+ commitInfo = clone(remote, branch, local, repoAuthConfig);
|
|
|
} else {
|
|
|
try (Repository repo = new FileRepository(localRepo.getAbsolutePath())) {
|
|
|
- PullCommand gitPull = new Git(repo)
|
|
|
+ PullCommand pullCommand = new Git(repo)
|
|
|
.pull()
|
|
|
// 超时 60s
|
|
|
.setTimeout(60)
|
|
|
.setFastForward(MergeCommand.FastForwardMode.FF);
|
|
|
- if (sshSessionFactory != null) {
|
|
|
- gitPull.setTransportConfigCallback(transport -> {
|
|
|
+
|
|
|
+ Map<String, Object> map = getRepoAuth(repoAuthConfig);
|
|
|
+ if (map.get(RepoAuthType.http.name()) != null) {
|
|
|
+ UsernamePasswordCredentialsProvider credentials = (UsernamePasswordCredentialsProvider) map.get(RepoAuthType.http.name());
|
|
|
+ pullCommand.setCredentialsProvider(credentials);
|
|
|
+ } else if (map.get(RepoAuthType.ssh.name()) != null) {
|
|
|
+ SshSessionFactory sshSessionFactory = (SshSessionFactory) map.get(RepoAuthType.ssh.name());
|
|
|
+ pullCommand.setTransportConfigCallback(transport -> {
|
|
|
if (transport instanceof SshTransport) {
|
|
|
- setSshTransport((SshTransport) transport);
|
|
|
+ setSshTransport((SshTransport) transport, sshSessionFactory);
|
|
|
}
|
|
|
});
|
|
|
- gitPull.call();
|
|
|
- } else if (credentials != null) {
|
|
|
- gitPull.setCredentialsProvider(credentials);
|
|
|
- gitPull.call();
|
|
|
- } else {
|
|
|
- gitPull.call();
|
|
|
}
|
|
|
|
|
|
+ pullCommand.call();
|
|
|
commitInfo = commitInfo(repo, branch);
|
|
|
commitInfo.setChangedFiles(changedFileList(repo, branch));
|
|
|
}
|
|
|
@@ -92,33 +78,27 @@ public class GitImpl implements CodeUpdater {
|
|
|
return commitInfo;
|
|
|
}
|
|
|
|
|
|
- private CommitInfo clone(String remote, String branch, String local) throws Exception {
|
|
|
- CloneCommand gitClone;
|
|
|
- if (sshSessionFactory != null) {
|
|
|
- gitClone = Git.cloneRepository()
|
|
|
- .setTimeout(600)
|
|
|
- .setURI(remote)
|
|
|
- .setBranch(branch)
|
|
|
- .setDirectory(new File(local))
|
|
|
- .setTransportConfigCallback(transport -> {
|
|
|
- if (transport instanceof SshTransport) {
|
|
|
- setSshTransport((SshTransport) transport);
|
|
|
- }
|
|
|
- });
|
|
|
- } else if (credentials != null) {
|
|
|
- gitClone = Git.cloneRepository()
|
|
|
- .setURI(remote)
|
|
|
- .setBranch(branch)
|
|
|
- .setDirectory(new File(local))
|
|
|
- .setCredentialsProvider(credentials);
|
|
|
- } else {
|
|
|
- gitClone = Git.cloneRepository()
|
|
|
- .setURI(remote)
|
|
|
- .setBranch(branch)
|
|
|
- .setDirectory(new File(local));
|
|
|
+ private CommitInfo clone(String remote, String branch, String local, RepoAuthConfig repoAuthConfig) throws Exception {
|
|
|
+ CloneCommand cloneCommand = Git.cloneRepository()
|
|
|
+ .setURI(remote)
|
|
|
+ .setBranch(branch)
|
|
|
+ .setTimeout(600)
|
|
|
+ .setDirectory(new File(local));
|
|
|
+
|
|
|
+ Map<String, Object> map = getRepoAuth(repoAuthConfig);
|
|
|
+ if (map.get(RepoAuthType.http.name()) != null) {
|
|
|
+ UsernamePasswordCredentialsProvider credentials = (UsernamePasswordCredentialsProvider) map.get(RepoAuthType.http.name());
|
|
|
+ cloneCommand.setCredentialsProvider(credentials);
|
|
|
+ } else if (map.get(RepoAuthType.ssh.name()) != null) {
|
|
|
+ SshSessionFactory sshSessionFactory = (SshSessionFactory) map.get(RepoAuthType.ssh.name());
|
|
|
+ cloneCommand.setTransportConfigCallback(transport -> {
|
|
|
+ if (transport instanceof SshTransport) {
|
|
|
+ setSshTransport((SshTransport) transport, sshSessionFactory);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- try (Git git = gitClone.setDepth(1).call()) {
|
|
|
+ try (Git git = cloneCommand.setDepth(1).call()) {
|
|
|
Repository repo = git.getRepository();
|
|
|
CommitInfo commitInfo = commitInfo(repo, branch);
|
|
|
commitInfo.setChangedFiles(changedFileList(repo, branch));
|
|
|
@@ -126,7 +106,7 @@ public class GitImpl implements CodeUpdater {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void setSshTransport(SshTransport sshTransport) {
|
|
|
+ private void setSshTransport(SshTransport sshTransport, SshSessionFactory sshSessionFactory) {
|
|
|
// Set passphrase using a CustomCredentialsProvider
|
|
|
sshTransport.setCredentialsProvider(new CustomCredentialProvider(null));
|
|
|
sshTransport.setSshSessionFactory(sshSessionFactory);
|
|
|
@@ -204,13 +184,6 @@ public class GitImpl implements CodeUpdater {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public CommitInfo update(String remote, String branch, String local, String commitId) throws Exception {
|
|
|
- // TODO 待实现
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
public CommitInfo latestCommitInfo(String local, String branch) throws Exception {
|
|
|
File localRepo = new File(local + "/.git");
|
|
|
if (!localRepo.exists()) {
|
|
|
@@ -222,26 +195,6 @@ public class GitImpl implements CodeUpdater {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public List<String> remoteBranches(String remote) {
|
|
|
- List<String> branches = new ArrayList<>();
|
|
|
- try {
|
|
|
- Collection<Ref> refList = Git.lsRemoteRepository().setRemote(remote)
|
|
|
- .setCredentialsProvider(credentials).call();
|
|
|
- for (Ref ref : refList) {
|
|
|
- String refName = ref.getName();
|
|
|
- if (refName.startsWith("refs/heads/")) {
|
|
|
- // 需要进行筛选
|
|
|
- String branchName = refName.replace("refs/heads/", "");
|
|
|
- branches.add(branchName);
|
|
|
- }
|
|
|
- }
|
|
|
- return branches;
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return branches;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 提交代码并 push 到远程仓库
|
|
|
*
|
|
|
@@ -278,27 +231,68 @@ public class GitImpl implements CodeUpdater {
|
|
|
// git commit -m "commit message"
|
|
|
RevCommit revCommit = commitCommand.setMessage(commitMsg).call();
|
|
|
PushCommand pushCommand = git.push();
|
|
|
- pushCommand.setRemote(pushUrl).setCredentialsProvider(credentials).call();
|
|
|
+ //pushCommand.setRemote(pushUrl).setCredentialsProvider(credentials).call();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public CommitInfo update(PipelineContext ctx, RepoAuthConfig repoAuthConfig) throws Exception {
|
|
|
+ public boolean checkRemoteBranch(String repoUrl, String branch, RepoAuthConfig repoAuthConfig) {
|
|
|
+ try {
|
|
|
+ LsRemoteCommand lsRemoteCommand = Git.lsRemoteRepository()
|
|
|
+ .setRemote(repoUrl)
|
|
|
+ .setHeads(true) // 只看分支,不看 Tags
|
|
|
+ .setTags(false);
|
|
|
+
|
|
|
+ // 如果是私有仓库,需要设置凭据
|
|
|
+ Map<String, Object> map = getRepoAuth(repoAuthConfig);
|
|
|
+ if (map.get(RepoAuthType.http.name()) != null) {
|
|
|
+ UsernamePasswordCredentialsProvider credentials = (UsernamePasswordCredentialsProvider) map.get(RepoAuthType.http.name());
|
|
|
+ lsRemoteCommand.setCredentialsProvider(credentials);
|
|
|
+ } else if (map.get(RepoAuthType.ssh.name()) != null) {
|
|
|
+ SshSessionFactory sshSessionFactory = (SshSessionFactory) map.get(RepoAuthType.ssh.name());
|
|
|
+ lsRemoteCommand.setTransportConfigCallback(transport -> {
|
|
|
+ if (transport instanceof SshTransport) {
|
|
|
+ setSshTransport((SshTransport) transport, sshSessionFactory);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 获取远程所有引用
|
|
|
+ Collection<Ref> refs = lsRemoteCommand.call();
|
|
|
+ // 检查是否存在匹配的分支名
|
|
|
+ // 远程分支在 refs 中通常以 refs/heads/branchName 形式存在
|
|
|
+ String fullBranchName = "refs/heads/" + branch;
|
|
|
+ return refs.stream().anyMatch(ref -> ref.getName().equals(fullBranchName));
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 如果仓库不存在或鉴权失败,会抛出异常
|
|
|
+ System.err.println("仓库连接失败或不存在: " + e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> getRepoAuth(RepoAuthConfig repoAuthConfig) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
switch (RepoAuthType.valueOf(repoAuthConfig.getAuthType())) {
|
|
|
case http:
|
|
|
- httpAuth(repoAuthConfig.getUsername(), repoAuthConfig.getPassword());
|
|
|
+ UsernamePasswordCredentialsProvider credentials0 =
|
|
|
+ new UsernamePasswordCredentialsProvider(repoAuthConfig.getUsername(), repoAuthConfig.getPassword());
|
|
|
+ map.put(RepoAuthType.http.name(), credentials0);
|
|
|
break;
|
|
|
case ssh:
|
|
|
- sshAuth(repoAuthConfig.getRsaPrikey());
|
|
|
+ SshSessionFactory sshSessionFactory0 = new CustomSshSessionFactory(repoAuthConfig.getRsaPrikey());
|
|
|
+ map.put(RepoAuthType.ssh.name(), sshSessionFactory0);
|
|
|
break;
|
|
|
- default:
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommitInfo update(PipelineContext ctx, RepoAuthConfig repoAuthConfig) throws Exception {
|
|
|
String remote = ctx.getGitUrl();
|
|
|
String branch = ctx.getBranch();
|
|
|
String local = ctx.getGitLocal();
|
|
|
CommitInfo last = latestCommitInfo(local, branch);
|
|
|
- CommitInfo current = pull(remote, branch, local);
|
|
|
+ CommitInfo current = pull(remote, branch, local, repoAuthConfig);
|
|
|
if (last == null || last.getMsCommitTime() <= current.getMsCommitTime()) {
|
|
|
// 本地仓库不存在 or 本地仓库版本和远程仓库相同 or 本地仓库版本落后于远程仓库
|
|
|
BuilderUtil.copyToCompileDir(ctx);
|