Commit 676fd0cb authored by Illya Klymov's avatar Illya Klymov

Merge branch '331567-view-history-of-all-project-imports-backend-v2' into 'master'

Expose import url in projects API

See merge request gitlab-org/gitlab!82498
parents 1c94f434 df58da33
......@@ -656,7 +656,9 @@ class Project < ApplicationRecord
preload(:project_feature, :route, namespace: [:route, :owner])
}
scope :created_by, -> (user) { where(creator: user) }
scope :imported_from, -> (type) { where(import_type: type) }
scope :imported, -> { where.not(import_type: nil) }
scope :with_tracing_enabled, -> { joins(:tracing_setting) }
scope :with_enabled_error_tracking, -> { joins(:error_tracking_setting).where(project_error_tracking_settings: { enabled: true }) }
......
......@@ -49,6 +49,7 @@ GET /projects
| `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_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_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. |
......
......@@ -85,8 +85,11 @@ module API
end
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_error, if: lambda { |_project, options| options[:user_can_admin_project] } do |project|
project.import_state&.last_error
end
......
......@@ -20,6 +20,7 @@ module API
projects = projects.with_merge_requests_enabled if params[:with_merge_requests_enabled]
projects = projects.with_statistics if params[:statistics]
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]
projects = projects.with_programming_language(lang) if lang
......@@ -125,6 +126,7 @@ module API
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 :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 :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'
......
......@@ -11,8 +11,6 @@ itself: # project
- has_external_wiki
- hidden
- import_source
- import_type
- import_url
- jobs_cache_index
- last_repository_check_at
- last_repository_check_failed
......@@ -63,6 +61,8 @@ itself: # project
- empty_repo
- forks_count
- http_url_to_repo
- import_status
- import_url
- name_with_namespace
- open_issues_count
- owner
......
......@@ -683,6 +683,33 @@ RSpec.describe API::Projects do
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
it_behaves_like 'projects response' do
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