BigW Consortium Gitlab

Commit 767c6f13 by Douwe Maan Committed by Luke Bennett

Merge branch 'fj-41681-add-param-disable-commit-stats-api' into 'master'

Add option to disable commit stats to commit API Closes #41681 See merge request gitlab-org/gitlab-ce!16309 (cherry picked from commit 088de97c) baeceb8b Adding stats param to the commit's endpoint 114cad58 Updated documentation d5c5f2e6 Added specs 8f9e778a Added changelog
parent 818979e1
---
title: Added option to disable commits stats in the commit endpoint
merge_request: 16309
author:
type: added
...@@ -159,6 +159,7 @@ Parameters: ...@@ -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 | `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 | | `sha` | string | yes | The commit hash or name of a repository branch or tag |
| `stats` | boolean | no | Include commit stats. Default is true |
```bash ```bash
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/5/repository/commits/master curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/5/repository/commits/master
......
...@@ -82,13 +82,14 @@ module API ...@@ -82,13 +82,14 @@ module API
end end
params do params do
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag' 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 end
get ':id/repository/commits/:sha', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do get ':id/repository/commits/:sha', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
commit = user_project.commit(params[:sha]) commit = user_project.commit(params[:sha])
not_found! 'Commit' unless commit not_found! 'Commit' unless commit
present commit, with: Entities::CommitDetail present commit, with: Entities::CommitDetail, stats: params[:stats]
end end
desc 'Get the diff for a specific commit of a project' do desc 'Get the diff for a specific commit of a project' do
......
...@@ -278,7 +278,7 @@ module API ...@@ -278,7 +278,7 @@ module API
end end
class CommitDetail < Commit class CommitDetail < Commit
expose :stats, using: Entities::CommitStats expose :stats, using: Entities::CommitStats, if: :stats
expose :status expose :status
expose :last_pipeline, using: 'API::Entities::PipelineBasic' expose :last_pipeline, using: 'API::Entities::PipelineBasic'
end end
......
...@@ -71,13 +71,14 @@ module API ...@@ -71,13 +71,14 @@ module API
end end
params do params do
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag' 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 end
get ":id/repository/commits/:sha", requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do get ":id/repository/commits/:sha", requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
commit = user_project.commit(params[:sha]) commit = user_project.commit(params[:sha])
not_found! "Commit" unless commit not_found! "Commit" unless commit
present commit, with: ::API::Entities::CommitDetail present commit, with: ::API::Entities::CommitDetail, stats: params[:stats]
end end
desc 'Get the diff for a specific commit of a project' do desc 'Get the diff for a specific commit of a project' do
......
...@@ -512,6 +512,31 @@ describe API::Commits do ...@@ -512,6 +512,31 @@ describe API::Commits do
end end
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 context 'when unauthenticated', 'and project is public' do
let(:project) { create(:project, :public, :repository) } let(:project) { create(:project, :public, :repository) }
......
...@@ -403,6 +403,33 @@ describe API::V3::Commits do ...@@ -403,6 +403,33 @@ describe API::V3::Commits do
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
expect(json_response['status']).to eq("created") expect(json_response['status']).to eq("created")
end 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 end
context "unauthorized user" do 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