Commit 6c8db7fa authored by Nathan Friend's avatar Nathan Friend Committed by Jan Provaznik

Add GraphQL mutation to delete release asset link

This commit adds a new mutation - `releaseAssetLinkDelete` - that
deletes a release asset link.
parent 2b1aa4d6
# frozen_string_literal: true
module Mutations
module ReleaseAssetLinks
class Delete < BaseMutation
graphql_name 'ReleaseAssetLinkDelete'
authorize :destroy_release
ReleaseAssetLinkID = ::Types::GlobalIDType[::Releases::Link]
argument :id, ReleaseAssetLinkID,
required: true,
description: 'ID of the release asset link to delete.'
field :link,
Types::ReleaseAssetLinkType,
null: true,
description: 'The deleted release asset link.'
def resolve(id:)
link = authorized_find!(id)
unless link.destroy
return { link: nil, errors: link.errors.full_messages }
end
{ link: link, errors: [] }
end
def find_object(id)
# TODO: remove this line when the compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
id = ReleaseAssetLinkID.coerce_isolated_input(id)
GitlabSchema.find_by_gid(id)
end
end
end
end
......@@ -68,6 +68,7 @@ module Types
mount_mutation Mutations::Releases::Delete
mount_mutation Mutations::ReleaseAssetLinks::Create
mount_mutation Mutations::ReleaseAssetLinks::Update
mount_mutation Mutations::ReleaseAssetLinks::Delete
mount_mutation Mutations::Terraform::State::Delete
mount_mutation Mutations::Terraform::State::Lock
mount_mutation Mutations::Terraform::State::Unlock
......
---
title: Add GraphQL mutation to delete an existing release asset link
merge_request: 56417
author:
type: added
......@@ -5032,6 +5032,16 @@ Autogenerated return type of ReleaseAssetLinkCreate.
| `errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| `link` | [`ReleaseAssetLink`](#releaseassetlink) | The asset link after mutation. |
### `ReleaseAssetLinkDeletePayload`
Autogenerated return type of ReleaseAssetLinkDelete.
| Field | Type | Description |
| ----- | ---- | ----------- |
| `clientMutationId` | [`String`](#string) | A unique identifier for the client performing the mutation. |
| `errors` | [`[String!]!`](#string) | Errors encountered during execution of the mutation. |
| `link` | [`ReleaseAssetLink`](#releaseassetlink) | The deleted release asset link. |
### `ReleaseAssetLinkEdge`
An edge in a connection.
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Mutations::ReleaseAssetLinks::Delete do
include GraphqlHelpers
let_it_be(:project) { create(:project, :private, :repository) }
let_it_be_with_reload(:release) { create(:release, project: project) }
let_it_be(:developer) { create(:user).tap { |u| project.add_developer(u) } }
let_it_be(:maintainer) { create(:user).tap { |u| project.add_maintainer(u) } }
let_it_be_with_reload(:release_link) { create(:release_link, release: release) }
let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }
let(:mutation_arguments) { { id: release_link.to_global_id } }
describe '#resolve' do
subject(:resolve) do
mutation.resolve(**mutation_arguments)
end
let(:deleted_link) { subject[:link] }
context 'when the current user has access to delete the link' do
let(:current_user) { maintainer }
it 'deletes the link and returns it', :aggregate_failures do
expect(deleted_link).to eq(release_link)
expect(release.links).to be_empty
end
context "when the link doesn't exist" do
let(:mutation_arguments) { super().merge(id: "gid://gitlab/Releases::Link/#{non_existing_record_id}") }
it 'raises an error' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
context "when the provided ID is invalid" do
let(:mutation_arguments) { super().merge(id: 'not-a-valid-gid') }
it 'raises an error' do
expect { subject }.to raise_error(::GraphQL::CoercionError)
end
end
end
context 'when the current user does not have access to delete the link' do
let(:current_user) { developer }
it 'raises an error' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Deletes a release asset link' do
include GraphqlHelpers
let_it_be(:project) { create(:project, :private, :repository) }
let_it_be(:release) { create(:release, project: project) }
let_it_be(:maintainer) { create(:user).tap { |u| project.add_maintainer(u) } }
let_it_be(:release_link) { create(:release_link, release: release) }
let(:current_user) { maintainer }
let(:mutation_name) { :release_asset_link_delete }
let(:mutation_arguments) { { id: release_link.to_global_id.to_s } }
let(:mutation) do
graphql_mutation(mutation_name, mutation_arguments, <<~FIELDS)
link {
id
name
url
linkType
directAssetUrl
external
}
errors
FIELDS
end
let(:delete_link) { post_graphql_mutation(mutation, current_user: current_user) }
let(:mutation_response) { graphql_mutation_response(mutation_name)&.with_indifferent_access }
it 'deletes the release asset link and returns the deleted link', :aggregate_failures do
delete_link
expected_response = {
id: release_link.to_global_id.to_s,
name: release_link.name,
url: release_link.url,
linkType: release_link.link_type.upcase,
directAssetUrl: end_with(release_link.filepath),
external: true
}.with_indifferent_access
expect(mutation_response[:link]).to match(expected_response)
expect(mutation_response[:errors]).to eq([])
end
end
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