From 784c9a742b9c520510da98a5d29bbede74dd6edd Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Fri, 23 Aug 2024 17:47:30 -0400 Subject: [PATCH] Add scm webhook delete mutation (#1331) --- assets/src/generated/graphql.ts | 6 ++++ lib/console/deployments/git.ex | 6 ++-- lib/console/graphql/deployments/git.ex | 7 +++++ .../graphql/resolvers/deployments/git.ex | 3 ++ schema/schema.graphql | 2 ++ .../deployments/git_mutations_test.exs | 30 +++++++++++++++++++ 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/assets/src/generated/graphql.ts b/assets/src/generated/graphql.ts index 4564534d82..7f0dfd8bfa 100644 --- a/assets/src/generated/graphql.ts +++ b/assets/src/generated/graphql.ts @@ -4370,6 +4370,7 @@ export type RootMutationType = { deletePullRequest?: Maybe; deleteRole?: Maybe; deleteScmConnection?: Maybe; + deleteScmWebhook?: Maybe; deleteServiceContext?: Maybe; deleteServiceDeployment?: Maybe; deleteStack?: Maybe; @@ -4849,6 +4850,11 @@ export type RootMutationTypeDeleteScmConnectionArgs = { }; +export type RootMutationTypeDeleteScmWebhookArgs = { + id: Scalars['ID']['input']; +}; + + export type RootMutationTypeDeleteServiceContextArgs = { id: Scalars['ID']['input']; }; diff --git a/lib/console/deployments/git.ex b/lib/console/deployments/git.ex index e4c8cd323c..083bd2d10f 100644 --- a/lib/console/deployments/git.ex +++ b/lib/console/deployments/git.ex @@ -207,7 +207,7 @@ defmodule Console.Deployments.Git do def create_scm_webhook(attrs, %User{} = user) do %ScmWebhook{} |> ScmWebhook.changeset(attrs) - |> allow(user, :edit) + |> allow(user, :write) |> when_ok(:insert) end @@ -218,7 +218,7 @@ defmodule Console.Deployments.Git do def update_scm_webhook(attrs, id, %User{} = user) do get_scm_webhook!(id) |> ScmWebhook.changeset(attrs) - |> allow(user, :edit) + |> allow(user, :write) |> when_ok(:update) end @@ -228,7 +228,7 @@ defmodule Console.Deployments.Git do @spec delete_scm_webhook(binary, User.t) :: webhook_resp def delete_scm_webhook(id, %User{} = user) do get_scm_webhook!(id) - |> allow(user, :edit) + |> allow(user, :write) |> when_ok(:delete) end diff --git a/lib/console/graphql/deployments/git.ex b/lib/console/graphql/deployments/git.ex index aff5f58a1b..f7786f544d 100644 --- a/lib/console/graphql/deployments/git.ex +++ b/lib/console/graphql/deployments/git.ex @@ -582,6 +582,13 @@ defmodule Console.GraphQl.Deployments.Git do safe_resolve &Deployments.create_webhook_for_connection/2 end + field :delete_scm_webhook, :scm_webhook do + middleware Authenticated + arg :id, non_null(:id) + + safe_resolve &Deployments.delete_scm_webhook/2 + end + @desc "creates a webhook reference in our system but doesn't attempt to create it in your upstream provider" field :create_scm_webhook_pointer, :scm_webhook do middleware Authenticated diff --git a/lib/console/graphql/resolvers/deployments/git.ex b/lib/console/graphql/resolvers/deployments/git.ex index 6bdf586c41..d659ae8c5c 100644 --- a/lib/console/graphql/resolvers/deployments/git.ex +++ b/lib/console/graphql/resolvers/deployments/git.ex @@ -115,6 +115,9 @@ defmodule Console.GraphQl.Resolvers.Deployments.Git do def create_webhook(%{attributes: attrs}, %{context: %{current_user: user}}), do: Git.create_webhook(attrs, user) + def delete_scm_webhook(%{id: id}, %{context: %{current_user: user}}), + do: Git.delete_scm_webhook(id, user) + def setup_renovate(%{connection_id: id, repos: repos} = args, %{context: %{current_user: user}}), do: Git.setup_renovate(args, id, repos, user) diff --git a/schema/schema.graphql b/schema/schema.graphql index 3f99cd9f7b..84180882df 100644 --- a/schema/schema.graphql +++ b/schema/schema.graphql @@ -523,6 +523,8 @@ type RootMutationType { createScmWebhook(connectionId: ID!, owner: String!): ScmWebhook + deleteScmWebhook(id: ID!): ScmWebhook + "creates a webhook reference in our system but doesn't attempt to create it in your upstream provider" createScmWebhookPointer(attributes: ScmWebhookAttributes!): ScmWebhook diff --git a/test/console/graphql/mutations/deployments/git_mutations_test.exs b/test/console/graphql/mutations/deployments/git_mutations_test.exs index 9ff2f30b17..666a4ec415 100644 --- a/test/console/graphql/mutations/deployments/git_mutations_test.exs +++ b/test/console/graphql/mutations/deployments/git_mutations_test.exs @@ -313,4 +313,34 @@ defmodule Console.GraphQl.Deployments.GitMutationsTest do }, %{current_user: insert(:user)}) end end + + describe "deleteScmWebhook" do + test "admins can delete scm webhooks" do + admin = admin_user() + hook = insert(:scm_webhook) + + {:ok, %{data: %{"deleteScmWebhook" => deleted}}} = run_query(""" + mutation Delete($id: ID!) { + deleteScmWebhook(id: $id) { + id + } + } + """, %{"id" => hook.id}, %{current_user: admin}) + + assert deleted["id"] == hook.id + refute refetch(hook) + end + + test "non-admins cannot delete" do + hook = insert(:scm_webhook) + + {:ok, %{errors: [_ | _]}} = run_query(""" + mutation Delete($id: ID!) { + deleteScmWebhook(id: $id) { + id + } + } + """, %{"id" => hook.id}, %{current_user: insert(:user)}) + end + end end