BigW Consortium Gitlab

Commit f6c1d382 by Francisco Javier López Committed by Douwe Maan

Add option to disable commit stats to commit API

parent 4eae806a
---
title: Added option to disable commits stats in the commit endpoint
merge_request: 16309
author:
type: added
......@@ -159,6 +159,7 @@ Parameters:
| --------- | ---- | -------- | ----------- |
| `id` | integer/string | yes | The ID or [URL-encoded path of the project](README.md#namespaced-path-encoding) owned by the authenticated user
| `sha` | string | yes | The commit hash or name of a repository branch or tag |
| `stats` | boolean | no | Include commit stats. Default is true |
```bash
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/5/repository/commits/master
......
......@@ -82,13 +82,14 @@ module API
end
params do
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
optional :stats, type: Boolean, default: true, desc: 'Include commit stats'
end
get ':id/repository/commits/:sha', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
commit = user_project.commit(params[:sha])
not_found! 'Commit' unless commit
present commit, with: Entities::CommitDetail
present commit, with: Entities::CommitDetail, stats: params[:stats]
end
desc 'Get the diff for a specific commit of a project' do
......
......@@ -278,7 +278,7 @@ module API
end
class CommitDetail < Commit
expose :stats, using: Entities::CommitStats
expose :stats, using: Entities::CommitStats, if: :stats
expose :status
expose :last_pipeline, using: 'API::Entities::PipelineBasic'
end
......
......@@ -71,13 +71,14 @@ module API
end
params do
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag'
optional :stats, type: Boolean, default: true, desc: 'Include commit stats'
end
get ":id/repository/commits/:sha", requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
commit = user_project.commit(params[:sha])
not_found! "Commit" unless commit
present commit, with: ::API::Entities::CommitDetail
present commit, with: ::API::Entities::CommitDetail, stats: params[:stats]
end
desc 'Get the diff for a specific commit of a project' do
......
......@@ -512,6 +512,31 @@ describe API::Commits do
end
end
context 'when stat param' do
let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}" }
it 'is not present return stats by default' do
get api(route, user)
expect(response).to have_gitlab_http_status(200)
expect(json_response).to include 'stats'
end
it "is false it does not include stats" do
get api(route, user), stats: false
expect(response).to have_gitlab_http_status(200)
expect(json_response).not_to include 'stats'
end
it "is true it includes stats" do
get api(route, user), stats: true
expect(response).to have_gitlab_http_status(200)
expect(json_response).to include 'stats'
end
end
context 'when unauthenticated', 'and project is public' do
let(:project) { create(:project, :public, :repository) }
......
......@@ -403,6 +403,33 @@ describe API::V3::Commits do
expect(response).to have_gitlab_http_status(200)
expect(json_response['status']).to eq("created")
end
context 'when stat param' do
let(:project_id) { project.id }
let(:commit_id) { project.repository.commit.id }
let(:route) { "/projects/#{project_id}/repository/commits/#{commit_id}" }
it 'is not present return stats by default' do
get v3_api(route, user)
expect(response).to have_gitlab_http_status(200)
expect(json_response).to include 'stats'
end
it "is false it does not include stats" do
get v3_api(route, user), stats: false
expect(response).to have_gitlab_http_status(200)
expect(json_response).not_to include 'stats'
end
it "is true it includes stats" do
get v3_api(route, user), stats: true
expect(response).to have_gitlab_http_status(200)
expect(json_response).to include 'stats'
end
end
end
context "unauthorized user" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment