diff --git a/lib/api/files.rb b/lib/api/files.rb index 047d7c028e4a7015153c799351ba18d7f5d68c90..f90a1a6ae46e25e2a43e0c54bade5007cb2df289 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -230,6 +230,7 @@ def blob_data requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false, documentation: { example: 'main' } end + route_setting :mcp, tool_name: :get_repository_file, params: [:id, :file_path, :ref] get ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do # Loads metadata for @blob as a side effect, but not not the actual data # diff --git a/spec/requests/api/files_spec.rb b/spec/requests/api/files_spec.rb index b2e5750fcc3798b16e16922e9bca9b6ee67b08f7..3bf0b74f5b2b3d0387657bd0e23e210a85cd4bb8 100644 --- a/spec/requests/api/files_spec.rb +++ b/spec/requests/api/files_spec.rb @@ -154,6 +154,21 @@ def expect_to_send_git_blob(url, params) end describe 'HEAD /projects/:id/repository/files/:file_path' do + shared_examples 'an endpoint with mcp route setting' do |expected_tool, expected_params| + it "has the correct mcp route setting configured" do + get api(route(file_path), current_user, **options), params: params + + expect(response).to have_gitlab_http_status(:ok) + + endpoint = request.env['api.endpoint'] + actual_value = endpoint.route_setting(:mcp) + + expect(actual_value).to be_present + expect(actual_value[:tool_name]).to eq(expected_tool.to_sym) + expect(actual_value[:params]).to eq(expected_params) if expected_params + end + end + shared_examples_for 'repository files' do let(:options) { {} } @@ -167,6 +182,8 @@ def expect_to_send_git_blob(url, params) subject { head api(route(absolute_path), current_user, **options), params: params } end + include_examples 'an endpoint with mcp route setting', :get_repository_file + it 'returns file attributes in headers' do head api(route(file_path), current_user, **options), params: params @@ -203,6 +220,7 @@ def expect_to_send_git_blob(url, params) expect(response).to have_gitlab_http_status(:ok) expect(response.headers['X-Gitlab-File-Name']).to eq('commit.js.coffee') + expect(response.headers['X-Gitlab-Ref']).to eq(params[:ref]) expect(response.headers['X-Gitlab-Content-Sha256']).to eq('08785f04375b47f81f46e68cc125d5ef368aa20576ddb53f91f4d83f1d04b929') end