Java Code Examples for org.kohsuke.github.GHCommit#getParentSHA1s()

The following examples show how to use org.kohsuke.github.GHCommit#getParentSHA1s() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitHelper.java    From repairnator with MIT License 5 votes vote down vote up
private String getLastKnowParent(GitHub gh, GHRepository ghRepo, Git git, String oldCommitSha, AbstractStep step) throws IOException {
    showGitHubRateInformation(gh, step);
    GHCommit commit = ghRepo.getCommit(oldCommitSha); // get the deleted
    // commit from GH
    List<String> commitParents = commit.getParentSHA1s();

    if (commitParents.isEmpty()) {
        step.addStepError("The following commit does not have any parent in GitHub: " + oldCommitSha
                + ". It cannot be resolved.");
        return null;
    }

    if (commitParents.size() > 1) {
        this.getLogger().debug("Step " + step.getName() + " - The commit has more than one parent : " + commit.getHtmlUrl());
    }

    String parent = commitParents.get(0);

    try {
        ObjectId commitObject = git.getRepository().resolve(parent);
        git.getRepository().open(commitObject);

        return parent;
    } catch (MissingObjectException e) {
        return getLastKnowParent(gh, ghRepo, git, parent, step);
    }
}
 
Example 2
Source File: DynamicBuildModel.java    From DotCi with MIT License 5 votes vote down vote up
private String getParentSha(final GHCommit commit, final GitBranch gitBranch) throws IOException {
    final String parentSha;
    if (gitBranch.isPullRequest()) {
        final GHPullRequest pullRequest = this.githubRepositoryService.getGithubRepository().getPullRequest(gitBranch.pullRequestNumber());
        parentSha = pullRequest.getBase().getSha();

    } else {
        parentSha = (commit.getParentSHA1s() != null && commit.getParentSHA1s().size() > 0) ? commit.getParentSHA1s().get(0) : null;
    }
    return parentSha;
}
 
Example 3
Source File: GitHubSCMFileSystem.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean changesSince(SCMRevision revision, @NonNull OutputStream changeLogStream)
        throws UnsupportedOperationException, IOException, InterruptedException {
    if (Objects.equals(getRevision(), revision)) {
        // special case where somebody is asking one of two stupid questions:
        // 1. what has changed between the latest and the latest
        // 2. what has changed between the current revision and the current revision
        return false;
    }
    int count = 0;
    FastDateFormat iso = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZ");
    StringBuilder log = new StringBuilder(1024);
    String endHash;
    if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) {
        endHash = ((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash().toLowerCase(Locale.ENGLISH);
    } else {
        endHash = null;
    }
    // this is the format expected by GitSCM, so we need to format each GHCommit with the same format
    // commit %H%ntree %T%nparent %P%nauthor %aN <%aE> %ai%ncommitter %cN <%cE> %ci%n%n%w(76,4,4)%s%n%n%b
    for (GHCommit commit: repo.queryCommits().from(ref).pageSize(GitSCM.MAX_CHANGELOG).list()) {
        if (commit.getSHA1().toLowerCase(Locale.ENGLISH).equals(endHash)) {
            break;
        }
        log.setLength(0);
        log.append("commit ").append(commit.getSHA1()).append('\n');
        log.append("tree ").append(commit.getTree().getSha()).append('\n');
        log.append("parent");
        for (String parent: commit.getParentSHA1s()) {
            log.append(' ').append(parent);
        }
        log.append('\n');
        GHCommit.ShortInfo info = commit.getCommitShortInfo();
        log.append("author ")
                .append(info.getAuthor().getName())
                .append(" <")
                .append(info.getAuthor().getEmail())
                .append("> ")
                .append(iso.format(info.getAuthoredDate()))
                .append('\n');
        log.append("committer ")
                .append(info.getCommitter().getName())
                .append(" <")
                .append(info.getCommitter().getEmail())
                .append("> ")
                .append(iso.format(info.getCommitDate()))
                .append('\n');
        log.append('\n');
        String msg = info.getMessage();
        if (msg.endsWith("\r\n")) {
            msg = msg.substring(0, msg.length() - 2);
        } else  if (msg.endsWith("\n")) {
            msg = msg.substring(0, msg.length() - 1);
        }
        msg = msg.replace("\r\n", "\n").replace("\r", "\n").replace("\n", "\n    ");
        log.append("    ").append(msg).append('\n');
        changeLogStream.write(log.toString().getBytes(StandardCharsets.UTF_8));
        changeLogStream.flush();
        count++;
        if (count >= GitSCM.MAX_CHANGELOG) {
            break;
        }
    }
    return count > 0;
}