Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Commit search API #351

Merged
merged 2 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/main/java/org/kohsuke/github/GHCommitSearchBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
* Search commits.
*
* @author Marc de Verdelhan
* @see GitHub#searchCommits()
*/
@Preview @Deprecated
public class GHCommitSearchBuilder extends GHSearchBuilder<GHCommit> {
/*package*/ GHCommitSearchBuilder(GitHub root) {
super(root,CommitSearchResult.class);
req = req.withPreview(Previews.CLOAK);
}

/**
* Search terms.
*/
public GHCommitSearchBuilder q(String term) {
super.q(term);
return this;
}

public GHCommitSearchBuilder author(String v) {
return q("author:"+v);
}

public GHCommitSearchBuilder committer(String v) {
return q("committer:"+v);
}

public GHCommitSearchBuilder authorName(String v) {
return q("author-name:"+v);
}

public GHCommitSearchBuilder committerName(String v) {
return q("committer-name:"+v);
}

public GHCommitSearchBuilder authorEmail(String v) {
return q("author-email:"+v);
}

public GHCommitSearchBuilder committerEmail(String v) {
return q("committer-email:"+v);
}

public GHCommitSearchBuilder authorDate(String v) {
return q("author-date:"+v);
}

public GHCommitSearchBuilder committerDate(String v) {
return q("committer-date:"+v);
}

public GHCommitSearchBuilder merge(boolean merge) {
return q("merge:"+Boolean.valueOf(merge).toString().toLowerCase());
}

public GHCommitSearchBuilder hash(String v) {
return q("hash:"+v);
}

public GHCommitSearchBuilder parent(String v) {
return q("parent:"+v);
}

public GHCommitSearchBuilder tree(String v) {
return q("tree:"+v);
}

public GHCommitSearchBuilder is(String v) {
return q("is:"+v);
}

public GHCommitSearchBuilder user(String v) {
return q("user:"+v);
}

public GHCommitSearchBuilder org(String v) {
return q("org:"+v);
}

public GHCommitSearchBuilder repo(String v) {
return q("repo:"+v);
}

public GHCommitSearchBuilder order(GHDirection v) {
req.with("order",v);
return this;
}

public GHCommitSearchBuilder sort(Sort sort) {
req.with("sort",sort);
return this;
}

public enum Sort { AUTHOR_DATE, COMMITTER_DATE }

private static class CommitSearchResult extends SearchResult<GHCommit> {
private GHCommit[] items;

@Override
/*package*/ GHCommit[] getItems(GitHub root) {
for (GHCommit commit : items) {
String repoName = getRepoName(commit.url);
try {
GHRepository repo = root.getRepository(repoName);
commit.wrapUp(repo);
} catch (IOException ioe) {}
}
return items;
}
}

/**
* @param commitUrl a commit URL
* @return the repo name ("username/reponame")
*/
private static String getRepoName(String commitUrl) {
if (StringUtils.isBlank(commitUrl)) {
return null;
}
int indexOfUsername = (GitHub.GITHUB_URL + "/repos/").length();
String[] tokens = commitUrl.substring(indexOfUsername).split("/", 3);
return tokens[0] + '/' + tokens[1];
}

@Override
protected String getApiUrl() {
return "/search/commits";
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public abstract class GHQueryBuilder<T> {
protected final GitHub root;
protected final Requester req;
protected Requester req;

/*package*/ GHQueryBuilder(GitHub root) {
this.root = root;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,14 @@ private boolean isPrivateModeEnabled() {
}
}

/**
* Search commits.
*/
@Preview @Deprecated
public GHCommitSearchBuilder searchCommits() {
return new GHCommitSearchBuilder(this);
}

/**
* Search issues.
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/kohsuke/github/Previews.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
static final String DRAX = "application/vnd.github.drax-preview+json";
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
static final String KORRA = "application/vnd.github.korra-preview";
static final String CLOAK = "application/vnd.github.cloak-preview";
}
9 changes: 9 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,15 @@ public void testMemberPagenation() throws IOException {
assertFalse(all.isEmpty());
}

@Test
public void testCommitSearch() throws IOException {
PagedSearchIterable<GHCommit> r = gitHub.searchCommits().author("kohsuke").list();
assertTrue(r.getTotalCount() > 0);

GHCommit firstCommit = r.iterator().next();
assertTrue(firstCommit.getFiles().size() > 0);
}

@Test
public void testIssueSearch() throws IOException {
PagedSearchIterable<GHIssue> r = gitHub.searchIssues().mentions("kohsuke").isOpen().list();
Expand Down