From a4e9259d8a39c0f1777af025fd7255dafb5d0e44 Mon Sep 17 00:00:00 2001 From: Lee Tickett Date: Fri, 17 May 2019 06:10:08 +0000 Subject: #46469 - Added ref querystring parameter to project search to allow searching on branches other than master --- changelogs/unreleased/add-branch-to-project-search-api.yml | 5 +++++ doc/api/search.md | 5 +++-- lib/api/search.rb | 3 ++- spec/requests/api/search_spec.rb | 7 +++++++ 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/add-branch-to-project-search-api.yml diff --git a/changelogs/unreleased/add-branch-to-project-search-api.yml b/changelogs/unreleased/add-branch-to-project-search-api.yml new file mode 100644 index 00000000000..74cff94ab76 --- /dev/null +++ b/changelogs/unreleased/add-branch-to-project-search-api.yml @@ -0,0 +1,5 @@ +--- +title: Added ref querystring parameter to project search API to allow searching on branches/tags other than the default +merge_request: 28069 +author: Lee Tickett +type: added diff --git a/doc/api/search.md b/doc/api/search.md index 6ee3d32d8bc..c2dd72479c1 100644 --- a/doc/api/search.md +++ b/doc/api/search.md @@ -556,6 +556,7 @@ GET /projects/:id/search | `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user | | `scope` | string | yes | The scope to search in | | `search` | string | yes | The search query | +| `ref` | string | no | The name of a repository branch or tag to search on. The project's default branch is used by default. This is only applicable for scopes: commits, blobs, and wiki_blobs. | Search the expression within the specified scope. Currently these scopes are supported: issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users. @@ -850,7 +851,7 @@ Blobs searches are performed on both filenames and contents. Search results: times in the content. ```bash -curl --request GET --header "PRIVATE-TOKEN: " https://gitlab.example.com/api/v4/projects/6/search?scope=blobs&search=installation +curl --request GET --header "PRIVATE-TOKEN: " https://gitlab.example.com/api/v4/projects/6/search?scope=blobs&search=installation&ref=feature ``` Example response: @@ -863,7 +864,7 @@ Example response: "data": "```\n\n## Installation\n\nQuick start using the [pre-built", "filename": "README.md", "id": null, - "ref": "master", + "ref": "feature", "startline": 46, "project_id": 6 } diff --git a/lib/api/search.rb b/lib/api/search.rb index 60095300ea1..1cab1a97186 100644 --- a/lib/api/search.rb +++ b/lib/api/search.rb @@ -112,12 +112,13 @@ module API type: String, desc: 'The scope of the search', values: Helpers::SearchHelpers.project_search_scopes + optional :ref, type: String, desc: 'The name of a repository branch or tag. If not given, the default branch is used' use :pagination end get ':id/(-/)search' do check_users_search_allowed! - present search(project_id: user_project.id), with: entity + present search({ project_id: user_project.id, repository_ref: params[:ref] }), with: entity end end end diff --git a/spec/requests/api/search_spec.rb b/spec/requests/api/search_spec.rb index 49672591b3b..7d61ec9c4d8 100644 --- a/spec/requests/api/search_spec.rb +++ b/spec/requests/api/search_spec.rb @@ -414,6 +414,13 @@ describe API::Search do expect(response).to have_gitlab_http_status(200) expect(json_response.size).to eq(11) end + + it 'by ref' do + get api("/projects/#{repo_project.id}/search", user), params: { scope: 'blobs', search: 'This file is used in tests for ci_environments_status', ref: 'pages-deploy' } + + expect(response).to have_gitlab_http_status(200) + expect(json_response.size).to eq(1) + end end end end -- cgit v1.2.1