Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Table Bot Accounts: Remove PAT panel on destroy #860

Merged
merged 17 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/components/bots/table_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
t("bots.index.table.actions.destroy"),
destroy_path(row),
data: {
turbo_method: :delete,
turbo_confirm: t("bots.index.table.actions.destroy_confirmation"),
turbo_frame: "_top",
"turbo-stream": true,
},
aria: {
label: t("bots.index.table.actions.destroy_aria_label"),
Expand Down
4 changes: 2 additions & 2 deletions app/components/bots/table_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def new_token_path(bot)

def destroy_path(bot)
if @namespace.is_a?(Group)
group_bot_path(id: bot.id)
group_bot_destroy_confirmation_path(bot_id: bot.id)
elsif @namespace.is_a?(Namespaces::ProjectNamespace)
namespace_project_bot_path(id: bot.id)
namespace_project_bot_destroy_confirmation_path(bot_id: bot.id)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
<%= link_to(
t("personal_access_tokens.table.revoke"),
revoke_path(row),
data: {
turbo_method: :delete,
turbo_confirm: t("personal_access_tokens.table.revoke_confirmation"),
},
data: revoke_data_attributes,
class:
"font-medium text-blue-600 underline dark:text-blue-500 hover:no-underline cursor-pointer",
) %>
Expand Down
15 changes: 13 additions & 2 deletions app/components/personal_access_tokens/table_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ def initialize(

def revoke_path(token)
if @namespace.is_a?(Group)
revoke_group_bot_personal_access_token_path(
revoke_confirmation_group_bot_personal_access_token_path(
bot_id: @bot_account.id,
id: token.id
)
elsif @namespace.is_a?(Namespaces::ProjectNamespace)
revoke_namespace_project_bot_personal_access_token_path(
revoke_confirmation_namespace_project_bot_personal_access_token_path(
bot_id: @bot_account.id,
id: token.id
)
else
revoke_profile_personal_access_token_path(id: token.id)
end
end

def revoke_data_attributes
if @namespace
{ 'turbo-stream': true }
else
{
turbo_method: :delete,
turbo_confirm: t('personal_access_tokens.table.revoke_confirmation')
}
end
end
end
end
15 changes: 13 additions & 2 deletions app/controllers/concerns/bot_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module BotActions
included do
before_action proc { namespace }
before_action proc { access_levels }
before_action proc { bot_account }, only: %i[destroy]
before_action proc { bot_account }, only: %i[destroy destroy_confirmation]
before_action proc { bot_type }, only: %i[create]
before_action proc { bot_accounts }
end
Expand Down Expand Up @@ -53,6 +53,16 @@ def create # rubocop:disable Metrics/MethodLength
end
end

def destroy_confirmation
authorize! @namespace, to: :destroy_bot_accounts?
render turbo_stream: turbo_stream.update('bot_modal',
partial: 'destroy_confirmation_modal',
locals: {
open: true,
bot_account: @bot_account
}), status: :ok
end

def destroy
Bots::DestroyService.new(@bot_account, current_user).execute
respond_to do |format|
Expand All @@ -74,7 +84,8 @@ def destroy
private

def bot_account
@bot_account = @namespace.namespace_bots.find_by(id: params[:id]) || not_found
id = params[:bot_id] || params[:id]
@bot_account = @namespace.namespace_bots.find_by(id:) || not_found
end

def access_levels
Expand Down
13 changes: 11 additions & 2 deletions app/controllers/concerns/bot_personal_access_token_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module BotPersonalAccessTokenActions
before_action proc { namespace }
before_action proc { bot_account }
before_action proc { personal_access_tokens }, only: %i[index revoke]
before_action proc { personal_access_token }, only: %i[revoke]
before_action proc { personal_access_token }, only: %i[revoke revoke_confirmation]
before_action proc { bot_accounts }
end

Expand Down Expand Up @@ -54,9 +54,18 @@ def create # rubocop:disable Metrics/MethodLength
end
end

def revoke
def revoke_confirmation
authorize! @namespace, to: :revoke_bot_personal_access_token?
render turbo_stream: turbo_stream.update('token_dialog',
partial: 'revoke_confirmation_modal',
locals: {
open: true,
personal_access_token: @personal_access_token,
bot_account: @bot_account
}), status: :ok
end

def revoke
respond_to do |format|
format.turbo_stream do
if @personal_access_token.revoke!
Expand Down
9 changes: 8 additions & 1 deletion app/javascript/controllers/token_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class extends Controller {
this.viewTarget.classList.add("hidden");
this.inputTarget.value = Array.prototype.join.call(
{ length: this.itemValue.length },
"*"
"*",
);
} else {
this.hideTarget.classList.add("hidden");
Expand All @@ -35,4 +35,11 @@ export default class extends Controller {
}
this.visible = !this.visible;
}

removeTokenPanel() {
let panel = document.getElementById("access-token-section");
joshsadam marked this conversation as resolved.
Show resolved Hide resolved
if (panel) {
panel.remove();
}
}
}
31 changes: 31 additions & 0 deletions app/views/groups/bots/_destroy_confirmation_modal.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= viral_dialog(open: open) do |dialog| %>
<%= dialog.with_header(title: t("bots.destroy_confirmation.title")) %>
<%= dialog.with_section do %>

<%= turbo_frame_tag("deletion-alert") %>

<div
class="
mb-4 font-normal text-slate-500 dark:text-slate-400 overflow-x-visible
"
>
<p class="mb-4">
<%= t("bots.destroy_confirmation.description", bot_name: bot_account.user.email) %>
</p>
<%= form_for(:deletion, url: group_bot_path(id: bot_account.id), method: :delete,
data: {
turbo_frame: "_top",
controller: "token",
action:"turbo:submit-end->viral--dialog#close"
}
) do |form| %>
<%= form.submit t("bots.destroy_confirmation.submit_button"),
class:
"button text-sm px-5 py-2.5 text-white bg-red-700 border-red-800 focus:outline-none hover:bg-red-800 focus:ring-red-300 dark:focus:ring-red-700 dark:bg-red-600 dark:text-white dark:border-red-600 dark:hover:bg-red-700",
data: {
action: "click->token#removeTokenPanel",
} %>
<% end %>
</div>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions app/views/groups/bots/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<%= turbo_frame_tag "token_dialog" %>
<%= render Viral::PageHeaderComponent.new(title: t('.title'), subtitle: t('.subtitle')) do |component| %>
<%= component.with_icon(name: "users", classes: "h-14 w-14 text-primary-700") %>
<%= component.with_buttons do %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%= viral_dialog(open: open) do |dialog| %>
<%= dialog.with_header(title: t("personal_access_tokens.revoke_confirmation.title")) %>
<%= dialog.with_section do %>

<%= turbo_frame_tag("deletion-alert") %>

<div
class="
mb-4 font-normal text-slate-500 dark:text-slate-400 overflow-x-visible
"
>
<p class="mb-4">
<%= t(
"personal_access_tokens.revoke_confirmation.description",
token_name: personal_access_token.name,
bot_name: bot_account.user.email,
) %>
</p>
<%= form_for(:deletion, url: revoke_group_bot_personal_access_token_path(
bot_id: @bot_account.id,
id: personal_access_token.id
), method: :delete,
data: {
turbo_frame: "_top",
controller: "token",
action:"turbo:submit-end->viral--dialog#close"
}
) do |form| %>
<%= form.submit t("personal_access_tokens.revoke_confirmation.submit_button"),
class:
"button text-sm px-5 py-2.5 text-white bg-red-700 border-red-800 focus:outline-none hover:bg-red-800 focus:ring-red-300 dark:focus:ring-red-700 dark:bg-red-600 dark:text-white dark:border-red-600 dark:hover:bg-red-700",
data: {
action: "click->token#removeTokenPanel",
} %>
<% end %>
</div>
<% end %>
<% end %>
31 changes: 31 additions & 0 deletions app/views/projects/bots/_destroy_confirmation_modal.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= viral_dialog(open: open) do |dialog| %>
<%= dialog.with_header(title: t("bots.destroy_confirmation.title")) %>
<%= dialog.with_section do %>

<%= turbo_frame_tag("deletion-alert") %>

<div
class="
mb-4 font-normal text-slate-500 dark:text-slate-400 overflow-x-visible
"
>
<p class="mb-4">
<%= t("bots.destroy_confirmation.description", bot_name: bot_account.user.email) %>
</p>
<%= form_for(:deletion, url: namespace_project_bot_path(id: bot_account.id), method: :delete,
data: {
turbo_frame: "_top",
controller: "token",
action:"turbo:submit-end->viral--dialog#close"
}
) do |form| %>
<%= form.submit t("bots.destroy_confirmation.submit_button"),
class:
"button text-sm px-5 py-2.5 text-white bg-red-700 border-red-800 focus:outline-none hover:bg-red-800 focus:ring-red-300 dark:focus:ring-red-700 dark:bg-red-600 dark:text-white dark:border-red-600 dark:hover:bg-red-700",
data: {
action: "click->token#removeTokenPanel",
} %>
<% end %>
</div>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions app/views/projects/bots/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<%= turbo_frame_tag "token_dialog" %>
<%= render Viral::PageHeaderComponent.new(title: t('.title'), subtitle: t('.subtitle')) do |component| %>
<%= component.with_icon(name: "users", classes: "h-14 w-14 text-primary-700") %>
<% if allowed_to?(:create_bot_accounts?, @namespace) %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%= viral_dialog(open: open) do |dialog| %>
<%= dialog.with_header(title: t("personal_access_tokens.revoke_confirmation.title")) %>
<%= dialog.with_section do %>

<%= turbo_frame_tag("deletion-alert") %>

<div
class="
mb-4 font-normal text-slate-500 dark:text-slate-400 overflow-x-visible
"
>
<p class="mb-4">
<%= t(
"personal_access_tokens.revoke_confirmation.description",
token_name: personal_access_token.name,
bot_name: bot_account.user.email,
) %>
</p>
<%= form_for(:deletion, url: revoke_namespace_project_bot_personal_access_token_path(
bot_id: bot_account.id,
id: personal_access_token.id
), method: :delete,
data: {
turbo_frame: "_top",
controller: "token",
action:"turbo:submit-end->viral--dialog#close"
}
) do |form| %>
<%= form.submit t("personal_access_tokens.revoke_confirmation.submit_button"),
class:
"button text-sm px-5 py-2.5 text-white bg-red-700 border-red-800 focus:outline-none hover:bg-red-800 focus:ring-red-300 dark:focus:ring-red-700 dark:bg-red-600 dark:text-white dark:border-red-600 dark:hover:bg-red-700",
data: {
action: "click->token#removeTokenPanel",
} %>
<% end %>
</div>
<% end %>
<% end %>
9 changes: 8 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ en:
label: Scopes
read_api: Grants read access to the API.
bots:
destroy_confirmation:
description: This action will permanently delete the bot account and will remove any existing memberships for the bot account. Are you sure you want to permanently delete bot account %{bot_name}?
submit_button: Confirm
title: Delete Bot
index:
pagy_item: Bot Accounts
table:
Expand All @@ -325,7 +329,6 @@ en:
actions:
destroy: Delete
destroy_aria_label: Delete bot account
destroy_confirmation: This action will permanently delete the bot account and will remove any existing memberships for the bot account. Are you sure you want to permanently delete this bot account?
generate_new_token: Generate new token
generate_new_token_aria_label: Generate a new personal access token for bot account
empty_state:
Expand Down Expand Up @@ -944,6 +947,10 @@ en:
helper: A custom name will make it easier to search for this in the future.
label: Name (Optional)
personal_access_tokens:
revoke_confirmation:
description: Are you sure you'd like to remove access token '%{token_name}' from bot %{bot_name}?
submit_button: Confirm
title: Revoke personal access token
table:
header:
action: Action
Expand Down
9 changes: 8 additions & 1 deletion config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ fr:
label: Scopes
read_api: Grants read access to the API.
bots:
destroy_confirmation:
description: This action will permanently delete the bot account and will remove any existing memberships for the bot account. Are you sure you want to permanently delete bot account %{bot_name}?
submit_button: Confirm
title: Delete Bot
index:
pagy_item: Bot Accounts
table:
Expand All @@ -325,7 +329,6 @@ fr:
actions:
destroy: Delete
destroy_aria_label: Delete bot account
destroy_confirmation: This action will permanently delete the bot account and will remove any existing memberships for the bot account. Are you sure you want to permanently delete this bot account?
generate_new_token: Generate new token
generate_new_token_aria_label: Generate a new personal access token for bot account
empty_state:
Expand Down Expand Up @@ -944,6 +947,10 @@ fr:
helper: A custom name will make it easier to search for this in the future.
label: Name (Optional)
personal_access_tokens:
revoke_confirmation:
description: Are you sure you'd like to remove access token '%{token_name}' from bot %{bot_name}?
submit_button: Confirm
title: Revoke personal access token
table:
header:
action: Action
Expand Down
2 changes: 2 additions & 0 deletions config/routes/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
resources :members, only: %i[create destroy index new update]

resources :bots, only: %i[create destroy index new] do
get :destroy_confirmation
resources :personal_access_tokens, module: :bots, only: %i[index new create] do
member do
get :revoke_confirmation
delete :revoke
end
end
Expand Down
2 changes: 2 additions & 0 deletions config/routes/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
end

resources :bots, only: %i[create destroy index new] do
get :destroy_confirmation
resources :personal_access_tokens, module: :bots, only: %i[index new create] do
member do
delete :revoke
get :revoke_confirmation
end
end
end
Expand Down
Loading
Loading