package devops;

import cn.reghao.bnt.web.devops.build.model.po.RepoAuthConfig;
import cn.reghao.bnt.web.devops.builder.model.constant.RepoAuthType;
import cn.reghao.bnt.web.devops.builder.tool.repo.GitImpl;
import cn.reghao.jutil.jdk.shell.ShellExecutor;
import cn.reghao.jutil.jdk.shell.ShellResult;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2022-05-07 09:32:40
 */
public class GitTest {
    @Test
    public void gitTest() throws Exception {
        String repo = "";
        String username = "";
        String password = "";

        RepoAuthConfig repoAuthConfig = new RepoAuthConfig();
        repoAuthConfig.setAuthType(RepoAuthType.http.getName());
        repoAuthConfig.setUsername(username);
        repoAuthConfig.setPassword(password);

        GitImpl git = new GitImpl(repoAuthConfig.getRepoAuth());
        List<String> list = git.remoteBranches(repo);
        System.out.println();
    }

    @Test
    public void repoTest() throws IOException, GitAPIException {
        String localRepo = "/home/reghao/code/java/jutil/.git";
        //checkout(localRepo);

        Repository repo = new FileRepository(localRepo);
        String branch = null;
        String commit = null;
        getLogsSinceCommit(repo, branch, commit);
    }

    private List<String> getLogsSinceCommit(Repository repository, String branch, String commit) throws IOException, GitAPIException {
        if (branch == null) {
            branch = repository.getBranch();
        }

        List<String> commits = new ArrayList<>();
        Ref head = repository.findRef("refs/heads/" + branch);
        if (head != null) {
            try (RevWalk revWalk = new RevWalk(repository)) {
                revWalk.markStart(revWalk.parseCommit(head.getObjectId()));
                for (RevCommit revCommit : revWalk) {
                    String commitId = revCommit.getId().getName();
                    if (commitId.equals(commit)) {
                        break;
                    }

                    long commitTime = revCommit.getCommitTime()*1000L;
                    String fullMessage = revCommit.getFullMessage();
                    commits.add(fullMessage);
                    System.out.println("\nCommit-Message: " + fullMessage);
                }
                revWalk.dispose();
            }
        }

        return commits;
    }

    private void checkout(String localRepo) throws IOException, GitAPIException {
        Git git = Git.open(new File(localRepo));
        String branch = git.getRepository().getBranch();
        for (RevCommit revCommit : git.log().call()) {
            String commitId = revCommit.getId().getName();
            git.checkout().setStartPoint(revCommit);
        }
    }

    @Test
    public void compilerTest() {
        ShellExecutor shell = new ShellExecutor();

        String versionCmd = "/opt/env/java/maven/maven-3.6.0/bin/mvn -v";
        versionCmd = "/opt/env/dotnet/dotnet-sdk-7.0.203-linux-x64/dotnet --info";
        versionCmd = "/opt/env/node/node-v14.19.3-linux-x64/bin/node -v";
        versionCmd = "/usr/bin/python --version";
        versionCmd = "/usr/bin/java -version";
        versionCmd = "/usr/bin/cmake --version";
        versionCmd = "/usr/bin/g++ -v";
        versionCmd = "/usr/bin/gcc -v";

        ShellResult shellResult = shell.exec(versionCmd.split("\\s+"));
        if (shellResult.getExitCode() == 0) {
            System.out.println(shellResult.getResult());
        }
    }
}
