Skip to content

Commit

Permalink
Add scm webhook delete mutation (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Aug 23, 2024
1 parent 5c9bb9d commit 784c9a7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
6 changes: 6 additions & 0 deletions assets/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4370,6 +4370,7 @@ export type RootMutationType = {
deletePullRequest?: Maybe<PullRequest>;
deleteRole?: Maybe<Role>;
deleteScmConnection?: Maybe<ScmConnection>;
deleteScmWebhook?: Maybe<ScmWebhook>;
deleteServiceContext?: Maybe<ServiceContext>;
deleteServiceDeployment?: Maybe<ServiceDeployment>;
deleteStack?: Maybe<InfrastructureStack>;
Expand Down Expand Up @@ -4849,6 +4850,11 @@ export type RootMutationTypeDeleteScmConnectionArgs = {
};


export type RootMutationTypeDeleteScmWebhookArgs = {
id: Scalars['ID']['input'];
};


export type RootMutationTypeDeleteServiceContextArgs = {
id: Scalars['ID']['input'];
};
Expand Down
6 changes: 3 additions & 3 deletions lib/console/deployments/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions lib/console/graphql/deployments/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/console/graphql/resolvers/deployments/git.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions test/console/graphql/mutations/deployments/git_mutations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 784c9a7

Please sign in to comment.