Commit df58da33 authored by Illya Klymov's avatar Illya Klymov

Expose import url and status in projects API

* add import_url field on project instance
* add import_status field on project instance

Changelog: added
parent 95c6c218
...@@ -656,7 +656,9 @@ class Project < ApplicationRecord ...@@ -656,7 +656,9 @@ class Project < ApplicationRecord
preload(:project_feature, :route, namespace: [:route, :owner]) preload(:project_feature, :route, namespace: [:route, :owner])
} }
scope :created_by, -> (user) { where(creator: user) }
scope :imported_from, -> (type) { where(import_type: type) } scope :imported_from, -> (type) { where(import_type: type) }
scope :imported, -> { where.not(import_type: nil) }
scope :with_tracing_enabled, -> { joins(:tracing_setting) } scope :with_tracing_enabled, -> { joins(:tracing_setting) }
scope :with_enabled_error_tracking, -> { joins(:error_tracking_setting).where(project_error_tracking_settings: { enabled: true }) } scope :with_enabled_error_tracking, -> { joins(:error_tracking_setting).where(project_error_tracking_settings: { enabled: true }) }
......
...@@ -49,6 +49,7 @@ GET /projects ...@@ -49,6 +49,7 @@ GET /projects
| `archived` | boolean | **{dotted-circle}** No | Limit by archived status. | | `archived` | boolean | **{dotted-circle}** No | Limit by archived status. |
| `id_after` | integer | **{dotted-circle}** No | Limit results to projects with IDs greater than the specified ID. | | `id_after` | integer | **{dotted-circle}** No | Limit results to projects with IDs greater than the specified ID. |
| `id_before` | integer | **{dotted-circle}** No | Limit results to projects with IDs less than the specified ID. | | `id_before` | integer | **{dotted-circle}** No | Limit results to projects with IDs less than the specified ID. |
| `imported` | boolean | **{dotted-circle}** No | Limit results to projects which were imported from external systems by current user. |
| `last_activity_after` | datetime | **{dotted-circle}** No | Limit results to projects with last_activity after specified time. Format: ISO 8601 (`YYYY-MM-DDTHH:MM:SSZ`) | | `last_activity_after` | datetime | **{dotted-circle}** No | Limit results to projects with last_activity after specified time. Format: ISO 8601 (`YYYY-MM-DDTHH:MM:SSZ`) |
| `last_activity_before` | datetime | **{dotted-circle}** No | Limit results to projects with last_activity before specified time. Format: ISO 8601 (`YYYY-MM-DDTHH:MM:SSZ`) | | `last_activity_before` | datetime | **{dotted-circle}** No | Limit results to projects with last_activity before specified time. Format: ISO 8601 (`YYYY-MM-DDTHH:MM:SSZ`) |
| `membership` | boolean | **{dotted-circle}** No | Limit by projects that the current user is a member of. | | `membership` | boolean | **{dotted-circle}** No | Limit by projects that the current user is a member of. |
......
...@@ -85,8 +85,11 @@ module API ...@@ -85,8 +85,11 @@ module API
end end
expose :mr_default_target_self, if: -> (project) { project.forked? } expose :mr_default_target_self, if: -> (project) { project.forked? }
expose :import_url, if: -> (project, options) { Ability.allowed?(options[:current_user], :admin_project, project) } do |project|
project[:import_url]
end
expose :import_type, if: -> (project, options) { Ability.allowed?(options[:current_user], :admin_project, project) }
expose :import_status expose :import_status
expose :import_error, if: lambda { |_project, options| options[:user_can_admin_project] } do |project| expose :import_error, if: lambda { |_project, options| options[:user_can_admin_project] } do |project|
project.import_state&.last_error project.import_state&.last_error
end end
......
...@@ -20,6 +20,7 @@ module API ...@@ -20,6 +20,7 @@ module API
projects = projects.with_merge_requests_enabled if params[:with_merge_requests_enabled] projects = projects.with_merge_requests_enabled if params[:with_merge_requests_enabled]
projects = projects.with_statistics if params[:statistics] projects = projects.with_statistics if params[:statistics]
projects = projects.joins(:statistics) if params[:order_by].include?('project_statistics') # rubocop: disable CodeReuse/ActiveRecord projects = projects.joins(:statistics) if params[:order_by].include?('project_statistics') # rubocop: disable CodeReuse/ActiveRecord
projects = projects.created_by(current_user).imported.with_import_state if params[:imported]
lang = params[:with_programming_language] lang = params[:with_programming_language]
projects = projects.with_programming_language(lang) if lang projects = projects.with_programming_language(lang) if lang
...@@ -125,6 +126,7 @@ module API ...@@ -125,6 +126,7 @@ module API
optional :search_namespaces, type: Boolean, desc: "Include ancestor namespaces when matching search criteria" optional :search_namespaces, type: Boolean, desc: "Include ancestor namespaces when matching search criteria"
optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user' optional :owned, type: Boolean, default: false, desc: 'Limit by owned by authenticated user'
optional :starred, type: Boolean, default: false, desc: 'Limit by starred status' optional :starred, type: Boolean, default: false, desc: 'Limit by starred status'
optional :imported, type: Boolean, default: false, desc: 'Limit by imported by authenticated user'
optional :membership, type: Boolean, default: false, desc: 'Limit by projects that the current user is a member of' optional :membership, type: Boolean, default: false, desc: 'Limit by projects that the current user is a member of'
optional :with_issues_enabled, type: Boolean, default: false, desc: 'Limit by enabled issues feature' optional :with_issues_enabled, type: Boolean, default: false, desc: 'Limit by enabled issues feature'
optional :with_merge_requests_enabled, type: Boolean, default: false, desc: 'Limit by enabled merge requests feature' optional :with_merge_requests_enabled, type: Boolean, default: false, desc: 'Limit by enabled merge requests feature'
......
...@@ -11,8 +11,6 @@ itself: # project ...@@ -11,8 +11,6 @@ itself: # project
- has_external_wiki - has_external_wiki
- hidden - hidden
- import_source - import_source
- import_type
- import_url
- jobs_cache_index - jobs_cache_index
- last_repository_check_at - last_repository_check_at
- last_repository_check_failed - last_repository_check_failed
...@@ -63,6 +61,8 @@ itself: # project ...@@ -63,6 +61,8 @@ itself: # project
- empty_repo - empty_repo
- forks_count - forks_count
- http_url_to_repo - http_url_to_repo
- import_status
- import_url
- name_with_namespace - name_with_namespace
- open_issues_count - open_issues_count
- owner - owner
......
...@@ -683,6 +683,33 @@ RSpec.describe API::Projects do ...@@ -683,6 +683,33 @@ RSpec.describe API::Projects do
end end
end end
context 'and imported=true' do
before do
other_user = create(:user)
# imported project by other user
create(:project, creator: other_user, import_type: 'github', import_url: 'http://foo.com')
# project created by current user directly instead of importing
create(:project)
project.update_attribute(:import_url, 'http://user:password@host/path')
project.update_attribute(:import_type, 'github')
end
it 'returns only imported projects owned by current user' do
get api('/projects?imported=true', user)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
expect(json_response.map { |p| p['id'] }).to eq [project.id]
end
it 'does not expose import credentials' do
get api('/projects?imported=true', user)
expect(json_response.first['import_url']).to eq 'http://host/path'
end
end
context 'when authenticated as a different user' do context 'when authenticated as a different user' do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { {} } let(:filter) { {} }
......
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