Search in sources :

Example 1 with GitHubRepo

use of org.zaproxy.zap.GitHubRepo 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)

Aggregations

File (java.io.File)1 Git (org.eclipse.jgit.api.Git)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1 Repository (org.eclipse.jgit.lib.Repository)1 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)1 URIish (org.eclipse.jgit.transport.URIish)1 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)1 TaskAction (org.gradle.api.tasks.TaskAction)1 GHPullRequest (org.kohsuke.github.GHPullRequest)1 GHRepository (org.kohsuke.github.GHRepository)1 GitHubRepo (org.zaproxy.zap.GitHubRepo)1 GitHubUser (org.zaproxy.zap.GitHubUser)1