diff --git a/app/graphql/types/repository/blob_type.rb b/app/graphql/types/repository/blob_type.rb index a5c9d6940ceb6a9e1831882ea0cf0d6049b51992..300dab726d4962c2360beee42741cf71cac1b2f5 100644 --- a/app/graphql/types/repository/blob_type.rb +++ b/app/graphql/types/repository/blob_type.rb @@ -127,6 +127,9 @@ class BlobType < BaseObject calls_gitaly: true, description: 'Whether the current user can modify the blob.' + field :can_modify_blob_with_web_ide, GraphQL::Types::Boolean, null: false, method: :can_modify_blob_with_web_ide?, + description: 'Whether the current user can modify the blob with Web IDE.' + field :can_current_user_push_to_branch, GraphQL::Types::Boolean, null: true, method: :can_current_user_push_to_branch?, description: 'Whether the current user can push to the branch.' diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 117b2ea6f80d56c4bf868f649ced2bdb99a035ab..d4c3cb5b5de79a52c5f809b01d1853aed1f703c5 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -76,10 +76,17 @@ def edit_blob_button(project = @project, ref = @ref, path = @path, options = {}) ) end + # Used for single file Web Editor, Delete and Replace UI actions. + # can_edit_tree checks if ref is on top of the branch. def can_modify_blob?(blob, project = @project, ref = @ref) !blob.stored_externally? && can_edit_tree?(project, ref) end + # Used for WebIDE editor where editing is possible even if ref is not on top of the branch. + def can_modify_blob_with_web_ide?(blob, project = @project) + !blob.stored_externally? && can_collaborate_with_project?(project) + end + def leave_edit_message _("Leave edit mode? All unsaved changes will be lost.") end diff --git a/app/presenters/blob_presenter.rb b/app/presenters/blob_presenter.rb index 778043955198f2986c71a2414e24c6b661117a4f..dcaf9de19978eed8d4c4b104d316ee26372af15e 100644 --- a/app/presenters/blob_presenter.rb +++ b/app/presenters/blob_presenter.rb @@ -140,6 +140,10 @@ def can_modify_blob? super(blob, project, commit_id) end + def can_modify_blob_with_web_ide? + super(blob, project) + end + def can_current_user_push_to_branch? return false unless current_user && project.repository.branch_exists?(commit_id) diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md index cc0dd1aa272d5df30e1ede18483655f798fee914..0c9f2455d479cfb1dca87ee71633598b41f633e9 100644 --- a/doc/api/graphql/reference/index.md +++ b/doc/api/graphql/reference/index.md @@ -30863,6 +30863,7 @@ Returns [`RepositoryCodeownerValidation`](#repositorycodeownervalidation). | <a id="repositoryblobblamepath"></a>`blamePath` | [`String`](#string) | Web path to blob blame page. | | <a id="repositoryblobcancurrentuserpushtobranch"></a>`canCurrentUserPushToBranch` | [`Boolean`](#boolean) | Whether the current user can push to the branch. | | <a id="repositoryblobcanmodifyblob"></a>`canModifyBlob` | [`Boolean`](#boolean) | Whether the current user can modify the blob. | +| <a id="repositoryblobcanmodifyblobwithwebide"></a>`canModifyBlobWithWebIde` | [`Boolean!`](#boolean) | Whether the current user can modify the blob with Web IDE. | | <a id="repositoryblobcodenavigationpath"></a>`codeNavigationPath` | [`String`](#string) | Web path for code navigation. | | <a id="repositoryblobcodeowners"></a>`codeOwners` | [`[UserCore!]`](#usercore) | List of code owners for the blob. | | <a id="repositoryblobeditblobpath"></a>`editBlobPath` | [`String`](#string) | Web path to edit the blob in the old-style editor. | diff --git a/spec/graphql/types/repository/blob_type_spec.rb b/spec/graphql/types/repository/blob_type_spec.rb index 1c27b6fca503f00476764c5aec03fe7ab1c7db61..a76c51a92dcfdcddfbf34d56e75df05398c996af 100644 --- a/spec/graphql/types/repository/blob_type_spec.rb +++ b/spec/graphql/types/repository/blob_type_spec.rb @@ -42,6 +42,7 @@ :rich_viewer, :plain_data, :can_modify_blob, + :can_modify_blob_with_web_ide, :can_current_user_push_to_branch, :archived, :ide_edit_path, diff --git a/spec/presenters/blob_presenter_spec.rb b/spec/presenters/blob_presenter_spec.rb index e6c8b63f2346cb501dd7a7d23f55ca7866f2f5a6..128f3facdac6435140785bd8e3f389f217c0e0fe 100644 --- a/spec/presenters/blob_presenter_spec.rb +++ b/spec/presenters/blob_presenter_spec.rb @@ -119,6 +119,31 @@ end end + describe '#can_modify_blob_with_web_ide?' do + before do + allow(blob).to receive(:stored_externally?).and_return(false) + allow(presenter).to receive(:can_collaborate_with_project?).with(project).and_return(false) + end + + it { expect(presenter.can_modify_blob_with_web_ide?).to be_falsey } + + context 'when blob is stored externally' do + before do + allow(blob).to receive(:stored_externally?).and_return(true) + end + + it { expect(presenter.can_modify_blob_with_web_ide?).to be_falsey } + end + + context 'when user can collaborate with the project' do + before do + allow(presenter).to receive(:can_collaborate_with_project?).with(project).and_return(true) + end + + it { expect(presenter.can_modify_blob_with_web_ide?).to be_truthy } + end + end + describe '#can_current_user_push_to_branch?' do context 'when ref is a branch' do let(:ref) { 'feature' }