Search in sources :

Example 26 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project zaproxy by zaproxy.

the class CreateTagAndGitHubRelease method createTag.

private void createTag() throws Exception {
    Repository repository = new FileRepositoryBuilder().setGitDir(new File(getProject().getRootDir(), ".git")).build();
    try (Git git = new Git(repository)) {
        URIish originUri = new URIish(GITHUB_BASE_URL + getRepo().get());
        git.remoteSetUrl().setRemoteName(GIT_REMOTE_ORIGIN).setRemoteUri(originUri).call();
        GitHubUser ghUser = getUser().get();
        PersonIdent personIdent = new PersonIdent(ghUser.getName(), ghUser.getEmail());
        Ref tag = git.tag().setName(getTag().get()).setMessage(getTagMessage().get()).setAnnotated(true).setTagger(personIdent).call();
        git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(ghUser.getName(), ghUser.getAuthToken())).add(tag).call();
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) GitHubUser(org.zaproxy.zap.GitHubUser) PersonIdent(org.eclipse.jgit.lib.PersonIdent) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) File(java.io.File)

Example 27 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project zaproxy by zaproxy.

the class GradleBuildWithGitRepos method buildWeeklyAddOns.

@TaskAction
public void buildWeeklyAddOns() throws GitAPIException, IOException {
    Path reposDir = repositoriesDirectory.get().getAsFile().toPath();
    List<RepoData> reposData = readRepoData();
    for (RepoData repoData : reposData) {
        String cloneUrl = repoData.getCloneUrl();
        String repoName = extractRepoName(cloneUrl);
        Path repoDir = reposDir.resolve(repoName);
        if (Files.notExists(repoDir)) {
            if (!cloneRepositories.get()) {
                getLogger().warn("The directory {} does not exist, the {} add-on(s) will not be built.", repoDir, repoName);
                continue;
            }
            getProject().mkdir(reposDir);
            // XXX Rely just on JGit once it supports depth arg:
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=475615
            getProject().exec(spec -> {
                spec.setWorkingDir(reposDir);
                spec.setExecutable("git");
                spec.setEnvironment(Collections.emptyMap());
                List<String> execArgs = new ArrayList<>();
                execArgs.add("clone");
                if (quiet.get()) {
                    execArgs.add("-q");
                }
                String branch = repoData.getBranch();
                if (branch != null && !branch.isEmpty()) {
                    execArgs.add("--branch");
                    execArgs.add(branch);
                }
                execArgs.add("--depth");
                execArgs.add("1");
                execArgs.add(cloneUrl);
                execArgs.add(repoName);
                spec.args(execArgs);
            }).assertNormalExitValue();
            getLogger().lifecycle("Cloned {} into {}", cloneUrl, repoDir);
        } else if (updateRepositories.get()) {
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            Repository repository = builder.setGitDir(repoDir.resolve(".git").toFile()).build();
            try (Git git = new Git(repository)) {
                git.pull().call();
                getLogger().lifecycle("Pulled from {} into {}", cloneUrl, repoDir);
            }
        }
        List<String> projects = repoData.getProjects() == null || repoData.getProjects().isEmpty() ? Arrays.asList("") : repoData.getProjects();
        if (clean.get()) {
            // Execute clean separately to avoid Gradle issue:
            // https://github.com/gradle/gradle/issues/2488
            getLogger().lifecycle("Running clean task for {}", repoDir.getFileName());
            runGradle(repoDir, Arrays.asList("clean"));
        }
        runTasks(repoDir, projects);
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) ListProperty(org.gradle.api.provider.ListProperty) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) ArrayList(java.util.ArrayList) TaskAction(org.gradle.api.tasks.TaskAction) Gson(com.google.gson.Gson) DirectoryProperty(org.gradle.api.file.DirectoryProperty) DefaultTask(org.gradle.api.DefaultTask) RepoData(org.zaproxy.zap.internal.RepoData) Path(java.nio.file.Path) Console(org.gradle.api.tasks.Console) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) Action(org.gradle.api.Action) Files(java.nio.file.Files) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Project(org.gradle.api.Project) IOException(java.io.IOException) Reader(java.io.Reader) File(java.io.File) List(java.util.List) RegularFileProperty(org.gradle.api.file.RegularFileProperty) Os(org.apache.tools.ant.taskdefs.condition.Os) ObjectFactory(org.gradle.api.model.ObjectFactory) Type(java.lang.reflect.Type) Property(org.gradle.api.provider.Property) InputFile(org.gradle.api.tasks.InputFile) Nested(org.gradle.api.tasks.Nested) Git(org.eclipse.jgit.api.Git) Input(org.gradle.api.tasks.Input) Collections(java.util.Collections) InvalidUserDataException(org.gradle.api.InvalidUserDataException) Repository(org.eclipse.jgit.lib.Repository) Named(org.gradle.api.Named) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) ArrayList(java.util.ArrayList) List(java.util.List) RepoData(org.zaproxy.zap.internal.RepoData) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) TaskAction(org.gradle.api.tasks.TaskAction)

Example 28 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project zaproxy by zaproxy.

the class CreatePullRequest method pullRequest.

@TaskAction
public void pullRequest() throws Exception {
    GitHubRepo ghRepo = getRepo().get();
    Repository repository = new FileRepositoryBuilder().setGitDir(new File(ghRepo.getDir(), ".git")).build();
    try (Git git = new Git(repository)) {
        if (git.status().call().getModified().isEmpty()) {
            return;
        }
        GitHubUser ghUser = getUser().get();
        URIish originUri = new URIish(GITHUB_BASE_URL + ghUser.getName() + "/" + ghRepo.getName());
        git.remoteSetUrl().setRemoteName(GIT_REMOTE_ORIGIN).setRemoteUri(originUri).call();
        git.checkout().setCreateBranch(true).setName(getBranchName().get()).setStartPoint(GIT_REMOTE_ORIGIN + "/" + baseBranchName.get()).call();
        PersonIdent personIdent = new PersonIdent(ghUser.getName(), ghUser.getEmail());
        git.commit().setAll(true).setSign(false).setAuthor(personIdent).setCommitter(personIdent).setMessage(getCommitSummary().get() + "\n\n" + getCommitDescription().get() + signedOffBy(personIdent)).call();
        git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(ghUser.getName(), ghUser.getAuthToken())).setForce(true).add(getBranchName().get()).call();
        GHRepository ghRepository = GitHub.connect(ghUser.getName(), ghUser.getAuthToken()).getRepository(ghRepo.toString());
        List<GHPullRequest> pulls = ghRepository.queryPullRequests().base(baseBranchName.get()).head(ghUser.getName() + ":" + getBranchName().get()).state(GHIssueState.OPEN).list().asList();
        String description = getPullRequestDescription().getOrElse(getCommitDescription().get());
        if (pulls.isEmpty()) {
            String title = getPullRequestTitle().getOrElse(getCommitSummary().get());
            createPullRequest(ghRepository, title, description);
        } else {
            pulls.get(0).setBody(description);
        }
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) GitHubUser(org.zaproxy.zap.GitHubUser) GHRepository(org.kohsuke.github.GHRepository) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) GHRepository(org.kohsuke.github.GHRepository) Repository(org.eclipse.jgit.lib.Repository) GHPullRequest(org.kohsuke.github.GHPullRequest) Git(org.eclipse.jgit.api.Git) PersonIdent(org.eclipse.jgit.lib.PersonIdent) File(java.io.File) GitHubRepo(org.zaproxy.zap.GitHubRepo) TaskAction(org.gradle.api.tasks.TaskAction)

Example 29 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project gitblit by gitblit.

the class GitBlitSuite method getRepository.

private static Repository getRepository(String name) {
    try {
        File gitDir = FileKey.resolve(new File(REPOSITORIES, name), FS.DETECTED);
        Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
        return repository;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) GitBlitException(com.gitblit.GitBlitException) IOException(java.io.IOException)

Example 30 with FileRepositoryBuilder

use of org.eclipse.jgit.storage.file.FileRepositoryBuilder in project gitblit by gitblit.

the class GitServletTest method testPushLog.

@Test
public void testPushLog() throws IOException {
    String name = "refchecks/ticgit.git";
    File refChecks = new File(GitBlitSuite.REPOSITORIES, name);
    Repository repository = new FileRepositoryBuilder().setGitDir(refChecks).build();
    List<RefLogEntry> pushes = RefLogUtils.getRefLog(name, repository);
    GitBlitSuite.close(repository);
    assertTrue("Repository has an empty push log!", pushes.size() > 0);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) RefLogEntry(com.gitblit.models.RefLogEntry) Test(org.junit.Test)

Aggregations

FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)32 Repository (org.eclipse.jgit.lib.Repository)30 File (java.io.File)25 Git (org.eclipse.jgit.api.Git)12 IOException (java.io.IOException)8 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)6 ObjectId (org.eclipse.jgit.lib.ObjectId)4 Path (java.nio.file.Path)3 CloneCommand (org.eclipse.jgit.api.CloneCommand)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 Ref (org.eclipse.jgit.lib.Ref)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)3 URIish (org.eclipse.jgit.transport.URIish)3 RefLogEntry (com.gitblit.models.RefLogEntry)2 Reader (java.io.Reader)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 TaskAction (org.gradle.api.tasks.TaskAction)2 Test (org.junit.Test)2 GHRepository (org.kohsuke.github.GHRepository)2