From be795a9ec912d3dc9e218493cb5759abaadd668a Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Fri, 13 Sep 2019 09:25:28 -0700 Subject: [PATCH] Initial commit (#425). --- src/main/java/org/gitlab4j/api/SearchApi.java | 290 ++++++++++++++++++ .../org/gitlab4j/api/models/SearchBlob.java | 75 +++++ 2 files changed, 365 insertions(+) create mode 100644 src/main/java/org/gitlab4j/api/SearchApi.java create mode 100644 src/main/java/org/gitlab4j/api/models/SearchBlob.java diff --git a/src/main/java/org/gitlab4j/api/SearchApi.java b/src/main/java/org/gitlab4j/api/SearchApi.java new file mode 100644 index 00000000..16159cee --- /dev/null +++ b/src/main/java/org/gitlab4j/api/SearchApi.java @@ -0,0 +1,290 @@ +package org.gitlab4j.api; + +import java.util.List; +import java.util.stream.Stream; + +import org.gitlab4j.api.models.Commit; +import org.gitlab4j.api.models.Issue; +import org.gitlab4j.api.models.MergeRequest; +import org.gitlab4j.api.models.Milestone; +import org.gitlab4j.api.models.Note; +import org.gitlab4j.api.models.Project; +import org.gitlab4j.api.models.SearchBlob; +import org.gitlab4j.api.models.Snippet; +import org.gitlab4j.api.models.User; + +/** + * This class provides an entry point to all the GitLab API Search API calls. + * @see Search API + */ +public class SearchApi extends AbstractApi { + + public SearchApi(GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * Search globally across the GitLab instance. + * + *
GitLab Endpoint: POST /search?scope=:scope&search=:search-query
+ * + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users + * @param search the search query + * @return a List containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public List globalSearch(SearchScope scope, String search) throws GitLabApiException { + return (globalSearch(scope, search, this.getDefaultPerPage()).all()); + } + + /** + * Search globally across the GitLab instance. + * + *
GitLab Endpoint: POST /search?scope=:scope&search=:search-query
+ * + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users + * @param search the search query + * @return a Stream containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public Stream globalSearchStream(SearchScope scope, String search) throws GitLabApiException { + return (globalSearch(scope, search, getDefaultPerPage()).stream()); + } + + /** + * Search globally across the GitLab instance. + * + *
GitLab Endpoint: POST /search?scope=:scope&search=:search-query
+ * + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users + * @param search the search query + * @param itemsPerPage the number of items that will be fetched per page + * @return a Pager containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public Pager globalSearch(SearchScope scope, String search, int itemsPerPage) throws GitLabApiException { + + GitLabApiForm formData = new GitLabApiForm() + .withParam("scope", scope, true) + .withParam("search", search, true); + + switch (scope) { + case BLOBS: + return (new Pager(this, SearchBlob.class, itemsPerPage, formData.asMap(), "search")); + + case COMMITS: + return (new Pager(this, Commit.class, itemsPerPage, formData.asMap(), "search")); + + case PROJECTS: + return (new Pager(this, Project.class, itemsPerPage, formData.asMap(), "search")); + + case ISSUES: + return (new Pager(this, Issue.class, itemsPerPage, formData.asMap(), "search")); + + case MERGE_REQUESTS: + return (new Pager(this, MergeRequest.class, itemsPerPage, formData.asMap(), "search")); + + case MILESTONES: + return (new Pager(this, Milestone.class, itemsPerPage, formData.asMap(), "search")); + + case SNIPPET_TITLES: + return (new Pager(this, Snippet.class, itemsPerPage, formData.asMap(), "search")); + + case SNIPPET_BLOBS: + return (new Pager(this, Snippet.class, itemsPerPage, formData.asMap(), "search")); + + case USERS: + return (new Pager(this, User.class, itemsPerPage, formData.asMap(), "search")); + + case WIKI_BLOBS: + return (new Pager(this, SearchBlob.class, itemsPerPage, formData.asMap(), "search")); + + default: + throw new GitLabApiException("Invalid SearchScope [" + scope + "]"); + } + } + + /** + * Search within the specified group. If a user is not a member of a group and the group is private, + * a request on that group will result to a 404 status code. + * + *
GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&search=:search-query
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * projects, issues, merge_requests, milestones, users + * @param search the search query + * @return a List containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public List groupSearch(Object groupIdOrPath, GroupSearchScope scope, String search) throws GitLabApiException { + return (groupSearch(groupIdOrPath, scope, search, this.getDefaultPerPage()).all()); + } + + /** + * Search within the specified group. If a user is not a member of a group and the group is private, + * a request on that group will result to a 404 status code. + * + *
GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&search=:search-query
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * projects, issues, merge_requests, milestones, users + * @param search the search query + * @return a Stream containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public Stream groupSearchStream(Object groupIdOrPath, GroupSearchScope scope, String search) throws GitLabApiException { + return (groupSearch(groupIdOrPath, scope, search, getDefaultPerPage()).stream()); + } + + /** + * Search within the specified group. If a user is not a member of a group and the group is private, + * a request on that group will result to a 404 status code. + * + *
GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&search=:search-query
+ * + * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * projects, issues, merge_requests, milestones, users + * @param search the search query + * @param itemsPerPage the number of items that will be fetched per page + * @return a Pager containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public Pager groupSearch(Object groupIdOrPath, GroupSearchScope scope, String search, int itemsPerPage) throws GitLabApiException { + + GitLabApiForm formData = new GitLabApiForm() + .withParam("scope", scope, true) + .withParam("search", search, true); + + switch (scope) { + case PROJECTS: + return (new Pager(this, Project.class, itemsPerPage, formData.asMap(), + "groups", getGroupIdOrPath(groupIdOrPath), "search")); + + case ISSUES: + return (new Pager(this, Issue.class, itemsPerPage, formData.asMap(), + "groups", getGroupIdOrPath(groupIdOrPath), "search")); + + case MERGE_REQUESTS: + return (new Pager(this, MergeRequest.class, itemsPerPage, formData.asMap(), + "groups", getGroupIdOrPath(groupIdOrPath), "search")); + + case MILESTONES: + return (new Pager(this, Milestone.class, itemsPerPage, formData.asMap(), + "groups", getGroupIdOrPath(groupIdOrPath), "search")); + + case USERS: + return (new Pager(this, User.class, itemsPerPage, formData.asMap(), + "groups", getGroupIdOrPath(groupIdOrPath), "search")); + + default: + throw new GitLabApiException("Invalid GroupSearchScope [" + scope + "]"); + } + } + + /** + * Search within the specified project. If a user is not a member of a project and the project is private, + * a request on that project will result to a 404 status code. + * + *
GitLab Endpoint: POST /projects/:projectId/search?scope=:scope&search=:search-query
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users + * @param search the search query + * @return a List containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public List projectSearch(Object projectIdOrPath, ProjectSearchScope scope, String search) throws GitLabApiException { + return (projectSearch(projectIdOrPath, scope, search, this.getDefaultPerPage()).all()); + } + + /** + * Search within the specified project. If a user is not a member of a project and the project is private, + * a request on that project will result to a 404 status code. + * + *
GitLab Endpoint: POST /projects/:projectId/search?scope=:scope&search=:search-query
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users + * @param search the search query + * @return a Stream containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public Stream projectSearchStream(Object projectIdOrPath, ProjectSearchScope scope, String search) throws GitLabApiException { + return (projectSearch(projectIdOrPath, scope, search, getDefaultPerPage()).stream()); + } + + /** + * Search within the specified project. If a user is not a member of a project and the project is private, + * a request on that project will result to a 404 status code. + * + *
GitLab Endpoint: POST /project/:projectId/search?scope=:scope&search=:search-query
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required + * @param scope search the expression within the specified scope. Currently these scopes are supported: + * issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users + * @param search the search query + * @param itemsPerPage the number of items that will be fetched per page + * @return a Pager containing the object type specified by the scope + * @throws GitLabApiException if any exception occurs + * @since GitLab 10.5 + */ + public Pager projectSearch(Object projectIdOrPath, ProjectSearchScope scope, String search, int itemsPerPage) throws GitLabApiException { + + GitLabApiForm formData = new GitLabApiForm() + .withParam("scope", scope, true) + .withParam("search", search, true); + + switch (scope) { + case BLOBS: + return (new Pager(this, SearchBlob.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case COMMITS: + return (new Pager(this, Commit.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case ISSUES: + return (new Pager(this, Issue.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case MERGE_REQUESTS: + return (new Pager(this, MergeRequest.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case MILESTONES: + return (new Pager(this, Milestone.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case NOTES: + return (new Pager(this, Note.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case WIKI_BLOBS: + return (new Pager(this, SearchBlob.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + case USERS: + return (new Pager(this, User.class, itemsPerPage, formData.asMap(), + "projects", getProjectIdOrPath(projectIdOrPath), "search")); + + default: + throw new GitLabApiException("Invalid ProjectSearchScope [" + scope + "]"); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/gitlab4j/api/models/SearchBlob.java b/src/main/java/org/gitlab4j/api/models/SearchBlob.java new file mode 100644 index 00000000..8b283662 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/SearchBlob.java @@ -0,0 +1,75 @@ +package org.gitlab4j.api.models; + +import org.gitlab4j.api.utils.JacksonJson; + +public class SearchBlob { + + private String basename; + private String data; + private String filename; + private Integer id; + private String ref; + private Integer startline; + private Integer projectId; + + public String getBasename() { + return basename; + } + + public void setBasename(String basename) { + this.basename = basename; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getRef() { + return ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + + public Integer getStartline() { + return startline; + } + + public void setStartline(Integer startline) { + this.startline = startline; + } + + public Integer getProjectId() { + return projectId; + } + + public void setProjectId(Integer projectId) { + this.projectId = projectId; + } + + @Override + public String toString() { + return (JacksonJson.toJsonString(this)); + } +} -- GitLab