Commit e467a11e authored by Roger Rüttimann's avatar Roger Rüttimann Committed by Douwe Maan

Feature/add language in repository to api

parent 70083ebf
---
title: 'API: add languages of project GET /projects/:id/languages'
merge_request: 17770
author: Roger Rüttimann
type: added
...@@ -915,6 +915,29 @@ Example response: ...@@ -915,6 +915,29 @@ Example response:
} }
``` ```
## Languages
Get languages used in a project with percentage value.
```
GET /projects/:id/languages
```
```bash
curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/5/languages"
```
Example response:
```json
{
"Ruby": 66.69,
"JavaScript": 22.98,
"HTML": 7.91,
"CoffeeScript": 2.42
}
```
## Archive a project ## Archive a project
Archives the project if the user is either admin or the project owner of this project. This action is Archives the project if the user is either admin or the project owner of this project. This action is
......
...@@ -338,6 +338,11 @@ module API ...@@ -338,6 +338,11 @@ module API
end end
end end
desc 'Get languages in project repository'
get ':id/languages' do
user_project.repository.languages.map { |language| language.values_at(:label, :value) }.to_h
end
desc 'Remove a project' desc 'Remove a project'
delete ":id" do delete ":id" do
authorize! :remove_project, user_project authorize! :remove_project, user_project
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
require 'spec_helper' require 'spec_helper'
shared_examples 'languages and percentages JSON response' do
let(:expected_languages) { project.repository.languages.map { |language| language.values_at(:label, :value)}.to_h }
it 'returns expected language values' do
get api("/projects/#{project.id}/languages", user)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq(expected_languages)
expect(json_response.count).to be > 1
end
end
describe API::Projects do describe API::Projects do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:user2) { create(:user) } let(:user2) { create(:user) }
...@@ -1694,6 +1706,42 @@ describe API::Projects do ...@@ -1694,6 +1706,42 @@ describe API::Projects do
end end
end end
describe 'GET /projects/:id/languages' do
context 'with an authorized user' do
it_behaves_like 'languages and percentages JSON response' do
let(:project) { project3 }
end
it 'returns not_found(404) for not existing project' do
get api("/projects/9999999999/languages", user)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with not authorized user' do
it 'returns not_found for existing but unauthorized project' do
get api("/projects/#{project3.id}/languages", user3)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'without user' do
let(:project_public) { create(:project, :public, :repository) }
it_behaves_like 'languages and percentages JSON response' do
let(:project) { project_public }
end
it 'returns not_found for existing but unauthorized project' do
get api("/projects/#{project3.id}/languages", nil)
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
describe 'DELETE /projects/:id' do describe 'DELETE /projects/:id' do
context 'when authenticated as user' do context 'when authenticated as user' do
it 'removes project' do it 'removes project' 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