Search in sources :

Example 1 with GitHubUser

use of org.zaproxy.zap.GitHubUser in project zaproxy by zaproxy.

the class CreateGitHubRelease method createRelease.

@TaskAction
public void createRelease() throws IOException {
    if (bodyFile.isPresent() && body.isPresent()) {
        throw new InvalidUserDataException("Only one type of body property must be set.");
    }
    if (checksumAlgorithm.get().isEmpty()) {
        throw new IllegalArgumentException("The checksum algorithm must not be empty.");
    }
    GitHubUser ghUser = getUser().get();
    GHRepository ghRepo = GitHub.connect(ghUser.getName(), ghUser.getAuthToken()).getRepository(repo.get());
    validateTagExists(ghRepo, tag.get());
    validateReleaseDoesNotExist(ghRepo, tag.get());
    StringBuilder releaseBody = new StringBuilder(250);
    releaseBody.append(bodyFile.isPresent() ? readContents(bodyFile.getAsFile().get().toPath()) : body.getOrElse(""));
    if (addChecksums.get()) {
        if (releaseBody.length() != 0) {
            releaseBody.append("\n\n---\n");
        }
        appendChecksumsTable(releaseBody);
    }
    GHRelease release = ghRepo.createRelease(tag.get()).name(title.get()).body(releaseBody.toString()).prerelease(prerelease.get()).draft(true).create();
    for (Asset asset : assets) {
        release.uploadAsset(asset.getFile().getAsFile().get(), asset.getContentType().get());
    }
    if (!draft.get()) {
        release.update().draft(false).update();
    }
}
Also used : GitHubUser(org.zaproxy.zap.GitHubUser) GHRepository(org.kohsuke.github.GHRepository) InvalidUserDataException(org.gradle.api.InvalidUserDataException) GHRelease(org.kohsuke.github.GHRelease) TaskAction(org.gradle.api.tasks.TaskAction)

Example 2 with GitHubUser

use of org.zaproxy.zap.GitHubUser 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 3 with GitHubUser

use of org.zaproxy.zap.GitHubUser 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 4 with GitHubUser

use of org.zaproxy.zap.GitHubUser in project zaproxy by zaproxy.

the class SendRepositoryDispatch method createConnection.

private HttpURLConnection createConnection() {
    String url = String.format("https://api.github.com/repos/%s/dispatches", getGitHubRepo().get().toString());
    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
    } catch (IOException e) {
        throw new BuildException("Failed to create the connection:", e);
    }
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    try {
        connection.setRequestMethod("POST");
    } catch (ProtocolException e) {
        throw new BuildException("Failed to create the connection:", e);
    }
    connection.setRequestProperty("Accept", "application/vnd.github.v3+json");
    connection.setRequestProperty("Content-Type", "application/json");
    GitHubUser user = getGitHubUser().get();
    String userName = user.getName();
    String token = user.getAuthToken();
    byte[] usernameAuthToken = (userName + ":" + token).getBytes(StandardCharsets.UTF_8);
    String authorization = "Basic " + Base64.getEncoder().encodeToString(usernameAuthToken);
    connection.setRequestProperty("Authorization", authorization);
    return connection;
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) GitHubUser(org.zaproxy.zap.GitHubUser) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

GitHubUser (org.zaproxy.zap.GitHubUser)4 File (java.io.File)2 Git (org.eclipse.jgit.api.Git)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 Repository (org.eclipse.jgit.lib.Repository)2 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)2 URIish (org.eclipse.jgit.transport.URIish)2 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)2 TaskAction (org.gradle.api.tasks.TaskAction)2 GHRepository (org.kohsuke.github.GHRepository)2 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 URL (java.net.URL)1 Ref (org.eclipse.jgit.lib.Ref)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 GHPullRequest (org.kohsuke.github.GHPullRequest)1 GHRelease (org.kohsuke.github.GHRelease)1 GitHubRepo (org.zaproxy.zap.GitHubRepo)1